public override async void cancel(string orderID) { try { if (server.dctIdToOrder.ContainsKey(orderID)) { COrder order = server.dctIdToOrder[orderID]; if (order.canCancel()) { await api.CancelOrderAsync(orderID); System.Threading.Thread.Sleep(100); order.Status = COrder.OrderState.Cancelled; order.TimeStampLastUpdate = DateTime.Now; order.updateGUI(); } } } catch (Exception ex) { server.AddLog(ex.Message); } }
public override async void trade(CStrategy strategy, int?leg, OrderSide?side, CProduct product, Double size, Double price) { try { String endpoint = "/v1/order"; Dictionary <string, string> parameters = new Dictionary <string, string> { { "amount", size.ToString() }, { "price", price.ToString() }, { "symbol", product.Symbol }, { "type", side.GetValueOrDefault().ToString().ToUpper() } }; HttpRequestMessage requestMessage = KuCoinPrivate(endpoint, parameters, HttpMethod.Post); // Create a client HttpClient httpClient = new HttpClient(); // Send the request to the server HttpResponseMessage response = await httpClient.SendAsync(requestMessage); // get json back string json = await response.Content.ReadAsStringAsync(); // parse order String dynamic orderData = JsonConvert.DeserializeObject(json); String orderID; try { orderID = orderData.data.orderOid; } catch (Exception ex) { server.AddLog(ex.Message); orderID = ""; } if (!orderID.Equals("")) { COrder order = new COrder(orderID); order.Product = product; order.Side = side.GetValueOrDefault(); order.Size = size; order.Price = price; String orderStatus = orderData.msg.ToString(); if (orderStatus.Equals("OK") || orderStatus.Equals("Sent")) { order.Status = COrder.OrderState.Sent; } else { order.Status = COrder.OrderState.Unknown; } order.Strategy = strategy; order.Exchange = this; Double timeStamp = orderData.timestamp; order.TimeStampSent = CHelper.ConvertFromUnixTimestamp(timeStamp); server.AddLog("Created Order " + this.Name + " " + orderID + " " + product + " " + side + " " + size + " " + price); // add order to global Orders server.colServerOrders.Add(order); server.dctIdToOrder[orderID] = order; // add order to strategy orders strategy.DctStrategyOrders[orderID] = order; if (leg != null) { strategy.DctLegToOrder[(int)leg] = order; } // cleanup order.updateGUI(); server.AddLog(json); } } catch (Exception ex) { server.AddLog(ex.Message); } }
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; }
public override async void pollOrders(object source, ElapsedEventArgs e) { if (pollingOrders) { return; } else { pollingOrders = true; } try { HttpClient httpClient = new HttpClient(); foreach (CProduct product in dctExchangeProducts.Values) { for (int active_dealt = 0; active_dealt < 2; active_dealt++) { String endpoint; Dictionary <string, string> parameters; if (active_dealt == 1) { endpoint = "/v1/order/dealt"; parameters = new Dictionary <string, string> { { "symbol", product.Symbol } }; } else { endpoint = "/v1/order/active"; parameters = new Dictionary <string, string> { { "symbol", product.Symbol } }; } HttpRequestMessage requestMessage = KuCoinPrivate(endpoint, parameters, HttpMethod.Get); // Send the request to the server HttpResponseMessage response = await httpClient.SendAsync(requestMessage); // Just as an example I'm turning the response into a string here string json = await response.Content.ReadAsStringAsync(); dynamic orderData = JsonConvert.DeserializeObject(json); var orders = orderData.data; if (orders != null) { if (active_dealt == 1) { var orderDealtAll = orders.datas; foreach (var orderDealt in orderDealtAll) { String oid = orderDealt.oid; String orderOid = orderDealt.orderOid; Double dealPrice = orderDealt.dealPrice; Double fee = orderDealt.fee; Double feeRate = orderDealt.feeRate; Double amount = orderDealt.amount; Double dealValue = orderDealt.dealValue; COrder order = null; if (server.dctIdToOrder.ContainsKey(oid)) { order = server.dctIdToOrder[oid]; } else if (server.dctIdToOrder.ContainsKey(orderOid)) { order = server.dctIdToOrder[orderOid]; } /* * if (order == null) * { * order = new COrder(orderOid); * //Server.colOrders.Add(order); * Server.dctIdToOrder.Add(orderOid, order); * } */ if (order != null) { order.OID = oid; order.DealPrice = dealPrice; order.Fee = fee; order.FeeRate = feeRate; order.DealValue = dealValue; order.Filled = amount; order.Status = COrder.OrderState.Filled; order.TimeStampFilled = DateTime.Now; order.updateGUI(); } } } else { foreach (var orderSideSet in orders) { String name = orderSideSet.Name; foreach (var orderSideSetOrders in orderSideSet) { foreach (var orderSideSetOrder in orderSideSetOrders) { int iAttrCount = 0; Double filled = 0; Double timeStamp = 0; String orderID = null; foreach (var orderSideSetOrderAttr in orderSideSetOrder) { // timestamp, side, price, size, executed, orderID switch (++iAttrCount) { case 1: timeStamp = orderSideSetOrderAttr; break; case 5: filled = orderSideSetOrderAttr; break; case 6: orderID = orderSideSetOrderAttr; break; } } if (orderID != null && server.dctIdToOrder.ContainsKey(orderID)) { COrder order = server.dctIdToOrder[orderID]; if (filled > 0) { order.Filled = filled; order.TimeStampFilled = DateTime.Now; if (order.Filled < order.Size) { order.Status = COrder.OrderState.Partial; } } order.Status = COrder.OrderState.Queued; order.TimeStampSent = CHelper.ConvertFromUnixTimestamp(timeStamp); order.TimeStampLastUpdate = DateTime.Now; order.updateGUI(); } } } } } } } } } catch (Exception ex) { server.AddLog(ex.Message); } pollingOrders = false; }