/// <summary>Called when new data is received</summary> public override void Step() { base.Step(); Dump(); EMA0.Add(Instrument.LatestPrice.Mid); var position = Positions.FirstOrDefault(); var buy = PendingOrders.FirstOrDefault(x => x.TradeType == TradeType.Buy); var sel = PendingOrders.FirstOrDefault(x => x.TradeType == TradeType.Sell); // If a pending order is triggered cancel the other one if (position != null) { if (buy != null) { Broker.CancelPendingOrder(buy); } if (sel != null) { Broker.CancelPendingOrder(sel); } buy = null; sel = null; if (position.NetProfit > Broker.Balance * 0.001) { Broker.ModifyOrder(Instrument, position, sl: position.EntryPrice + position.Sign() * 2.0 * Instrument.Spread); } } // Update the pending orders with each tick else if (buy != null && sel != null) { var buy_ = new Trade(Instrument, buy); var sel_ = new Trade(Instrument, sel); MakeSpanningTrades(buy_, sel_); Broker.ModifyPendingOrder(buy, buy_); Broker.ModifyPendingOrder(sel, sel_); } // If there are no positions or pending orders, create them else if (position == null && buy == null && sel == null) { var buy_ = new Trade(Instrument, TradeType.Buy, Label, 0, null, null, 0); var sel_ = new Trade(Instrument, TradeType.Sell, Label, 0, null, null, 0); MakeSpanningTrades(buy_, sel_); Broker.CreatePendingOrder(buy_); Broker.CreatePendingOrder(sel_); } }
/// <summary>Watch for position closed</summary> protected override void OnPositionClosed(Position position) { EMA0.Reset(EMA0.WindowSize); EMA0.Add(Instrument.LatestPrice.Mid); Dump(); }