public CProduct(CExchange exchange, String symbol, int precisionSize, int precisionPrice) { Symbol = symbol; this.Exchange = exchange; this.PrecisionSize = precisionSize; this.PrecisionPrice = precisionPrice; colStrategy = new MTObservableCollection <CStrategy>(); TradingPnL = 0; if (Symbol.Equals("USDT")) { SetLast(1); } if (Symbol.Contains("LTC") && Symbol.Contains("BTC")) { MinSize = 0.01; } }
public override void cycleStrategy() { if (processStrategy) { return; } else { processStrategy = true; } try { Double profitUSD = server.TradeUSD.GetValueOrDefault() * server.MinProfit.GetValueOrDefault(); switch (State) { case StrategyState.Inactive: break; case StrategyState.Active: case StrategyState.Continuous: if (Profit >= profitUSD) { State = StrategyState.MakerSend; DctLegToOrder.Clear(); goto case StrategyState.MakerSend; } break; case StrategyState.MakerSend: CurrentLeg = MakerLeg; double dUSD = server.TradeUSD.GetValueOrDefault(); OrderSide sideMaker = dctLegs[CurrentLeg].Item1; CProduct productMaker = dctLegs[CurrentLeg].Item2; Double sizeMaker = GetSize(sideMaker, productMaker); Double priceMaker = (double)(sideMaker == OrderSide.Buy ? productMaker.Bid : productMaker.Ask); dctLegs[MakerLeg].Item2.Exchange.trade(this, MakerLeg, sideMaker, productMaker, Math.Round(sizeMaker, productMaker.PrecisionSize), Math.Round(priceMaker, productMaker.PrecisionPrice)); State = StrategyState.MakerProcess; break; case StrategyState.MakerProcess: COrder order = DctLegToOrder[MakerLeg]; if (order.Status.Equals(COrder.OrderState.Filled)) { State = StrategyState.TakerSend; goto case StrategyState.TakerSend; } else if (Profit < profitUSD && order.canCancel()) { order.cancel(); State = Continuous ? StrategyState.Continuous : StrategyState.Active; DctLegToOrder.Clear(); } break; case StrategyState.TakerSend: for (int currentLeg = 1; currentLeg <= dctLegs.Count; currentLeg++) { if (!DctLegToOrder.ContainsKey(currentLeg)) { OrderSide sideTaker = dctLegs[currentLeg].Item1; CProduct productTaker = dctLegs[currentLeg].Item2; Double sizeTaker = GetSize(sideTaker, productTaker); Double priceTaker = ((Double)productTaker.Bid + (Double)productTaker.Ask) / 2.0; CurrentLeg = currentLeg; dctLegs[currentLeg].Item2.Exchange.trade(this, currentLeg, sideTaker, productTaker, Math.Round(sizeTaker, productTaker.PrecisionSize), Math.Round(priceTaker, productTaker.PrecisionPrice)); } } if (DctLegToOrder.Count >= 3) { State = StrategyState.TakerProcess; } break; case StrategyState.TakerProcess: Boolean allFilled = true; for (int currentLeg = 1; currentLeg <= dctLegs.Count; currentLeg++) { if (DctLegToOrder.ContainsKey(currentLeg)) { COrder orderTaker = DctLegToOrder[currentLeg]; if (orderTaker.Status.Equals(COrder.OrderState.Sent) || orderTaker.Status.Equals(COrder.OrderState.Cancelled)) { allFilled = false; } else if (orderTaker.canCancel()) { allFilled = false; CExchange exchange = orderTaker.Exchange; OrderSide sideTaker = orderTaker.Side; CProduct productTaker = orderTaker.Product; Double sizeTaker = orderTaker.Size; Double priceTaker = ((Double)productTaker.Bid + (Double)productTaker.Ask) / 2.0; CurrentLeg = currentLeg; orderTaker.cancel(); COrder orderCancel; DctLegToOrder.TryRemove(currentLeg, out orderCancel); exchange.trade(this, currentLeg, sideTaker, productTaker, Math.Round(sizeTaker, productTaker.PrecisionSize), Math.Round(priceTaker, productTaker.PrecisionPrice)); } } else { allFilled = false; } } if (allFilled) { if (Continuous) { State = StrategyState.Continuous; } else { State = StrategyState.Inactive; } DctLegToOrder.Clear(); } break; } } catch (Exception ex) { server.AddLog(ex.Message); } processStrategy = false; }