NexusFi: Find Your Edge


Home Menu

 





Is NinjaScript only good for day trading?


Discussion in NinjaTrader

Updated
    1. trending_up 1,372 views
    2. thumb_up 5 thanks given
    3. group 2 followers
    1. forum 4 posts
    2. attach_file 1 attachments




 
Search this Thread

Is NinjaScript only good for day trading?

  #1 (permalink)
 
maxmorrow's Avatar
 maxmorrow 
Chicago, IL.
 
Experience: Intermediate
Platform: Ninjascript, ThinkOrSwim
Trading: Stocks
Posts: 5 since Nov 2012
Thanks Given: 8
Thanks Received: 5

I have coded a couple different strategies that are basically position trades that would hold a stock for a couple days and possibly a couple months. The strategies only trade during the regular 9:30-4 market hours. I am fairly happy with how everything is looking. They won't win any awards but are well thought out and fairly simple. Everything is automated from position size, multiple entries, and multiple exits/stops

Now that I am paper trading them I see that a Ninjascript can’t see what positions I hold in Interactive Brokers that were entered the previous day or days. It would make coding something like “if Position.MarketPosition == MarketPosition.Long do XYZ “ worthless since the script can not see if there was a position.

Can anyone point me in the direction of solving this? Is Ninjascript only good for day trading?

I have been looking around for awhile but haven’t really seen anyone discussing this except one post in the Ninjatrader support forum. Any suggestions, links to forum posts, any leads to chase down would be super appreciated.

Thanks

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
ZombieSqueeze
Platforms and Indicators
How to apply profiles
Traders Hideout
What broker to use for trading palladium futures
Commodities
REcommedations for programming help
Sierra Chart
 
  #3 (permalink)
 
pintope's Avatar
 pintope 
Madrid, Spain
 
Experience: Beginner
Platform: NinjaTrader + Dendric
Broker: IB/Kinetick
Trading: Futures
Posts: 17 since May 2014
Thanks Given: 5
Thanks Received: 9


Hello @maxmorrow,

You can check the collection Performance.AllTrades. For example, last trade would be:
 
Code
Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];

The Trade object ( have a look here) has properties Entry and Exit, of type IExecution, where you can check the time (property Time) the execution occurred.

To know the current position size, check Position.Quantity.

But all this from the same strategy instance. If you have separate strategy instances running and want to know all the open positions, I would use a file. Every strategy would read/write in this unique file when opening/closing a position. I have been using files in my strategies and it works nicely.

Follow me on Twitter Reply With Quote
Thanked by:
  #4 (permalink)
 
maxmorrow's Avatar
 maxmorrow 
Chicago, IL.
 
Experience: Intermediate
Platform: Ninjascript, ThinkOrSwim
Trading: Stocks
Posts: 5 since Nov 2012
Thanks Given: 8
Thanks Received: 5

Ahh, a new thing to learn. Do you write to a text, excel, or some other kind of file? I have been reading as much as I can about C# and writing to file. Gonna take some trial and error but it makes sense. I am surprised about how little I have found about writing to file in regard to Ninjascript. I will post some more about my trials in hopes that it will help someone else in their coding. Cheers.

Started this thread Reply With Quote
  #5 (permalink)
 
pintope's Avatar
 pintope 
Madrid, Spain
 
Experience: Beginner
Platform: NinjaTrader + Dendric
Broker: IB/Kinetick
Trading: Futures
Posts: 17 since May 2014
Thanks Given: 5
Thanks Received: 9

Hello @maxmorrow,

The good thing of NinjaScript is that it’s pure C#, not another platform-specific language. There are tons of books, sample code and an immense community around C#. Three cheers for that.

I write to a text file in CSV format (comma-separated values). To make it easier, I will give you some code. Check the file CSVFileManager.cs attached to this post. It’s all you need to read/write a CSV text file. I took this code from here.

Copy it to folder “C:\Users\Myself\Documents\NinjaTrader 7\bin\Custom\Strategy” (1). Take into account all the classes in this file belongs to namespace futures.io (formerly BMT), so you need to add to your strategies a “using futures.io (formerly BMT)” directive.

To write I use this code in one indicator of mine:
 
Code
using (CsvFileWriter writer = new CsvFileWriter(fileFullPath)) // (2)
{
	CsvRow row = new CsvRow();
	
	// Header.
	row.Add("Week");
	row.Add("Average");
	row.Add("StandardDeviation");
	writer.WriteRow(row);
	
	// Data.
	for (int i = 0; i < periodData["Avg"].Count; i++)
	{
		row = new CsvRow();
		row.Add(periodData["YW"][i]);
		row.Add(periodData["Avg"][i]);
		row.Add(periodData["Std"][i]);

		writer.WriteRow(row);
	}
}

And this is the code to read:
 
Code
using (CsvFileReader reader = new CsvFileReader(fileFullPath))
{
	CsvRow row = new CsvRow();
	reader.ReadRow(row); // Header (3).
	while (reader.ReadRow(row))
	{
		avg = row[1]; // (4)
		std = row[2]; // (4)
		int rowYear = int.Parse(row[0].Split(DELIMITER_CHARS)[0]);
		int rowWeek = int.Parse(row[0].Split(DELIMITER_CHARS)[1]);
		if (rowYear < currentYear || rowWeek < currentWeek) // (5)
		{
			continue;
		}
		else
		{
			break;
		}
	}
}

Notes:
(1) You could also copy it to folder “C:\Users\Myself\Documents\NinjaTrader 7\bin\Custom\Type” and your strategies will compile, but when you try to export a strategy using these classes an error occurs. You have to export the strategy along with CSVFileManager.cs.
(2) A candidate for “fileFullPath” could look like “C:\Users\Myself\Documents\Trading\Year\Month\tradesLog1.csv”. These files would be your trades logs for you to consult.
(3) Don’t forget the first line is a header and must be discarded.
(4) This is the info I’m looking for, in your case the trades info.
(5) The first column (row[0]) is my “key” field, I use a string in year/week format, like “2014/30”. You should use the instrument and something else.

I hope this will help you.

Attached Files
Elite Membership required to download: CSVFileManager.cs
Follow me on Twitter Reply With Quote




Last Updated on November 25, 2014


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts