/// <summary>Called when new data is received</summary> public override void Step() { base.Step(); if (Instrument.NewCandle) { Debugging.LogInstrument(); Debugging.BreakOnPointOfInterest(); } // Run the position manager if (PosMgr != null) { PosMgr.Step(); } // If there is a pending order, wait for it to trigger or expire var pending = PendingOrders.FirstOrDefault(); if (pending != null) { // If the price is more than the SL/TP distance from the EP then give up on the order var dist = Math.Abs(Instrument.LatestPrice.Mid - pending.TargetPrice); if (dist > Math.Abs(pending.TakeProfitRel()) || dist > Math.Abs(pending.StopLossRel())) { Broker.CancelPendingOrder(pending); } else { return; } } // Look for an existing trade for this strategy. var position = Positions.FirstOrDefault(); if (position == null) { // Look for indicators to say "enter" and which direction. QuoteCurrency?ep, tp; var tt = Instrument.FindTradeEntry(out ep, out tp); if (tt == null) { return; } // Create a trade in the suggested direction var trade = new Trade(Instrument, tt.Value, Label, ep: ep, tp: tp); trade.Expiration = (Bot.UtcNow + Instrument.TimeFrame.ToTimeSpan(10)).DateTime; if (trade.RtR < 0.2) { return; } // Create a pending order Broker.CreatePendingOrder(trade); //Broker.CreateOrder(trade); } }
public void Handle(OrderCommited t) { var pendingOrder = PendingOrders.FirstOrDefault(o => o.OrderId == t.Order.OrderId); if (pendingOrder == null) { return; } PendingOrders.Remove(pendingOrder); Console.WriteLine($"Order Complete: {pendingOrder.OrderId}"); }
/// <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>Called when new data is received</summary> public override void Step() { base.Step(); if (Instrument.NewCandle || Positions.Any() || PendingOrders.Any()) { Dump(); } var sign0 = Math.Sign(EMA0[0].CompareTo(EMA1[0])); var sign1 = Math.Sign(EMA0[-1].CompareTo(EMA1[-1])); var order = PendingOrders.FirstOrDefault(); var position = Positions.FirstOrDefault(); // Cancel or close positions/orders that are against the trend if (order != null && order.Sign() != sign0) { Broker.CancelPendingOrder(order); order = null; } if (position != null && position.Sign() != sign0) { Broker.CloseAt(Instrument, position, EMA0[0]); } // Look for EMA crosses if (sign0 != sign1) { var mcs = Instrument.MCS; var price = EMA0[0] - sign0 * Instrument.Spread; if (order == null || Math.Abs(order.TargetPrice - price) > 2) { // Cancel any previous pending orders Broker.CancelAllPendingOrders(Label); // Create a pending order at the cross price var sl = price - sign0 * mcs * 5; var vol = Broker.ChooseVolume(Instrument, Math.Abs(price - sl), risk: Risk); var trade = new Trade(Instrument, CAlgo.SignToTradeType(sign0), Label, price, sl, null, vol); Broker.CreatePendingOrder(trade); } } }