The problem:
When I attach the strategy to a chart, after it calculates all the historical trades, it closes the current position and takes the market position flat before going live. This causes my strategy to fail as it “can never be flat.”
Why is the position being taken flat before going live? Is there another way to write this so it works for live trading?
Thanks,
CS
PS. I am attaching the strategy code to give you an idea of what I’m trying to do.
// User defined variables (add any user defined variables below) private IOrder buyOrder = null; // buy order private IOrder sellOrder = null; // sell order privatedouble buyStop = 0; // buy stop price privatedouble sellStop = 0; // sell stop price privateint startBar = 0; // bar to start strategy protectedoverridevoid Initialize() { CalculateOnBarClose = true; TimeInForce = Cbi.TimeInForce.Gtc; BarsRequired = 0; ExitOnClose = false; } protectedoverridevoid OnBarUpdate() { // check for minumum bars if (!Signal().BStop.ContainsValue(0)) { startBar = CurrentBar + 20; return; } if (CurrentBar < startBar) return; // start strategy buyStop = Signal().BStop[1]; sellStop = Signal().SStop[1]; if (Position.MarketPosition == MarketPosition.Flat) // enter initial buy/sell order { if (High[0] >= buyStop) buyOrder = EnterLong(1, "Buy"); elseif (Low[0] <= sellStop) sellOrder = EnterShort(1, "Sell"); } elseif (Position.MarketPosition == MarketPosition.Short) // enter buy stop order { buyOrder = EnterLongStop(1, buyStop, "BuyStop"); } elseif (Position.MarketPosition == MarketPosition.Long) // enter sell stop order { sellOrder = EnterShortStop(1, sellStop, "SellStop"); } }
Comment