1. Check if there is an open position (It can be of any instrument on that particular account).
2. If there is an open position, it should immediately open 1 MNQ DEC23 contract in the opposite direction to that of the open position. So, if the open position is LONG, the MNQ DEC23 contract should be SHORT, and vice versa.
3. If there is no position, the strategy should close the existing MNQ DEC23 position
Here is the code that I am using, but it doesn't so anything. What could be the problem.
namespace NinjaTrader.NinjaScript.Strategies
{
public class MultiInstrumentTest : Strategy
{
private Order entryOrder;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "A simple strategy that opens a MNQ DEC23 contract in the opposite direction of the open position.";
Name = "MultiInstrumentTest";
}
else if (State == State.Configure)
{
AddDataSeries("MNQ DEC23", Data.BarsPeriodType.Minute, 5, Data.MarketDataType.Last);
}
}
protected override void OnBarUpdate()
{
if (Position.MarketPosition != MarketPosition.Flat)
{
if (Position.MarketPosition == MarketPosition.Long)
{
entryOrder = EnterShort(1, 1, "MNQENTRY");
}
else if (Position.MarketPosition == MarketPosition.Short)
{
entryOrder = EnterLong(1, 1, "MNQENTRY");
}
}
else
{
if (entryOrder != null && entryOrder.OrderState == OrderState.Filled)
{
ExitLong(1, 1, "MNQEXIT", "MNQENTRY");
ExitShort(1, 1, "MNQEXIT", "MNQENTRY");
entryOrder = null;
}
}
}
}
}

Comment