I'm still learning scripting in NT, so please be patient.
I am testing a strategy where I would like to make the following (I make the example with an initial LONG order: it's symmetrical for short trades)
* when I have a crossover in one direction I place a EnterLongStop order with a Stop Loss
* I keep the order open GTC
* If a crossover in the opposite direction takes place, I would like to cancel the stop loss and reverse the position with a EnterShortStop order.
Reading the Managed Orders rules, I discovered that this will not work so easily, because, if a Stop Loss order (let's say, for exiting a Long trade) is active, no EnterShortStop is allowed (conflict between the Long Stop and the Short Entry). I.e., I think I cannot simply use the SetStopLoss() method.
So: I tried to make the following:
In the Variables section I have defined my four brave IOrder objects:
private IOrder myLongOrder = null; private IOrder myShortOrder = null; private IOrder existsLSL = null; private IOrder existsSSL = null;
protected override void OnExecution(IExecution execution)
{
if(myLongOrder != null && myLongOrder == execution.Order && myStopLoss > 0)
{
Print(execution.ToString());
double stopDistance = (myLongOrder.AvgFillPrice * myStopLoss/100);
double stopLevel = (myLongOrder.AvgFillPrice - stopDistance);
existsLSL = ExitLongStop(0, true, 10000, stopLevel, "Long Stop Loss", "MA bull crossover");
}
if(myShortOrder != null && myShortOrder == execution.Order && myStopLoss > 0)
{
Print(execution.ToString());
double stopDistance = (myShortOrder.AvgFillPrice * myStopLoss/100);
double stopLevel = (myShortOrder.AvgFillPrice + stopDistance);
existsSSL = ExitShortStop(0, true, 10000, stopLevel, "Short Stop Loss", "MA bear crossover");
}
}
// Condition set 1
if (CrossAbove(SMA(Close, FastMAperiod), SMA(Close, SlowMAperiod), 1))
{
if(myShortOrder != null)
{
CancelOrder(myShortOrder);
//debug
//Print("SellStop cancelled");
myShortOrder = null;
}
if(existsSSL != null)
{
//Cancelling stop loss in the other direction otherwise, due to Ninja Managed Order methods
//it won't be possible to place an opposite-direction order
CancelOrder(existsSSL);
existsSSL = null;
}
**NT** Error on calling 'OnBarUpdate' method for strategy 'Luxor1v3SL/b6754a89931b48c3bf9e888b7582d47c': Object reference not set to an instance of an object.
Can some mighty guy around give me some hints? Is there a simpler way of making what I need? Why cannot I access and IOrder instance created by the OnExecution method from within the OnBarUpdate()?
Thanks a lot in advance!
fsprea

Comment