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.
Last edited by maxmorrow; November 18th, 2014 at 10:54 PM.
You can check the collection Performance.AllTrades. For example, last trade would be:
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.
The following user says Thank You to pintope for this post:
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.
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:
And this is the code to read:
(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) 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.
The following 4 users say Thank You to pintope for this post: