Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
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
Can you help answer these questions from other members on NexusFi?
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.
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:
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.