I created a strategy that goes against the trend. Works well until the market is trending. When trending I will be fill numerous times for loses until the trend is over. I'm attempting to fix that problem by not allowing another trade after my stoploss is hit and the market 'resets' and I'm attempting to use the IOrder() to accomplish this. Here is the logic:
private bool m_stopLos****Long = false;
private IOrder m_longOrder = null;
private bool m_longOrderFilled = false;
protected override void Initialize()
SetStopLoss("Long", CalculationMode.Ticks, 25, false);
SetStopLoss("Short", CalculationMode.Ticks, 25, false);
protected override void OnBarUpdate()
if (Position.MarketPosition == MarketPosition.Flat && m_longOrderFilled)
{
m_stopLos****Long = true;
m_longOrderFilled = false;
}
if (m_stopLos****Long && !CrossBelow(DM(10).DiMinus, DM(10).DiPlus, 1))//Stoploss hit but normal exit condition has not been called yet
{
return;
}
else if (m_stopLos****Long && CrossBelow(DM(10).DiMinus, DM(10).DiPlus, 1))//Stoploss hit and exit condition signaled
m_stopLos****Long = false;
// VCR Long Entry
if (Position.MarketPosition == MarketPosition.Flat)
{
if (RValueCharts(Color.Blue, 3, 3).VClose[0] <= -4.4
&& Position.MarketPosition == MarketPosition.Flat
&& TrendStrengthA(VC.NinjaScript.Utility.MovingAverag eType.VWMA, 200, 20, 10).TrendStrength[0] < 0)
{
m_longOrder = EnterLong(DefaultQuantity, "Long");
m_longOrderFilled = false;
}
}
else
{
// VCR Long Exit
if (CrossBelow(DM(10).DiMinus, DM(10).DiPlus, 1))
{
ExitLong("Exit VCR Long", "Long");
m_longOrderFilled = false;
}
}
protected override void OnOrderUpdate(IOrder order)
{
if (m_longOrder.OrderId == order.OrderId)
{
if (order.OrderState== OrderState.PartFilled || order.OrderState == OrderState.Filled)
{
m_longOrderFilled = true;
}
}
This works on the long side.
The problem is when I try the same template on the short side. It compiles, but no trades fire and I get an error in the log stating:
"Object reference not set to an instance of an object".
When I take one side alone it works. When I do both sides I get that error. Any suggestions?
Comment