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 am looking for a way to keep a limit order valid for more than one bar after the conditions of my strategy are met.
Here is an example of what I want to do: The strategy will enter long with a limit order -1 tick of the actual price when the conditions of the strategy are met. One of the condition is the crossover of the MACDs, so typically that condition will not remain satisfied on the next bar update. The limit order get cancelled on the next bar update (my order is only valid for 5 minutes..., the time of one bar).
I would like to keep the order good till 'x' amount of bar updates. Seems to be a simple concept, but I didn't find anyone doing it on ninjatrader, neither on other Platform. I am not a coder, so I play carefully in the code.
On ninjatrader support, I found the following way, but it is not keeping the limit order for a period of time, but until the reversal of the initial condition (SMA crossover/above):
----------------------------------------------------------- protected override void OnBarUpdate()
{
entryPrice = Low[0] - 2 * TickSize;
if (CrossAbove(SMA(Fast), SMA(Slow), 1))
{
EnterLongLimit(0, true, DefaultQuantity, entryPrice, "live until cancelled long");
}
if (Close[0] > Open[0])
{
EnterLongLimit(0, false, DefaultQuantity, entryPrice, "long order to be resubmitted");
}
if (CrossBelow(SMA(Fast), SMA(Slow), 1))
ExitLong();
----------------------------------------------------------------------
Can I add a bar counter after each bar update? like:
barcount = barcount +1
And the condition to terminate the limit order would be:
if (barcount == 3)
ExitLong();
-----------------------------------------------------------------------
Is that reliable? I am looking for something as much reliable as possible.
Is the comment "live until cancelled long" and "long order to be resubmitted" are usefull? are they supposed to be called and use somewhere else in the code?
Thanks a lot for your help, martin
Can you help answer these questions from other members on NexusFi?
I am learning myself ninjascript at the time, so take my advice with care.
In order to cancel your order after a given period of time, you need a counter as you defined, but the correct way to cancel it is with the command CancelOrder().
Click on any of the limit orders to see the full code syntax for example click on EnterLongLimit() to see the examples.
There are 5 or so ways to code the order up. But you want the last one that has the keep alive until canceled = true property.
But you will run into some issues if you are not careful. This is kind of like taking the training wheels off a bit. So by keeping orders alive, you will need to create your own logic to cancel them. And this needs to cover every permutation of everything that possibly could happen. So take care to think about all the many possibilities once you introduce an order that will not automatically cancel into your code.
If you ever get stumped I might be able to help. I have gone a few dozen steps beyond this and actually taken NT completely apart and used to use the unmanaged approach (No built in safety, training wheels completely off) which is not advisable for anyone less than a professional developer.
Best of luck
Ian
In the analytical world there is no such thing as art, there is only the science you know and the science you don't know. Characterizing the science you don't know as "art" is a fools game.