private void UpdateDictUserOpenedOrders(FUTTRADE.orders_log ol) { try { string isin = m_plaza2Connector.Instruments.GetInstrumentByIsinId(ol.isin_id); CRawOrdersLogStruct rawOls = new CRawOrdersLogStruct((FUTTRADE.orders_log)ol.Copy()); if (ol.action == (sbyte)EnmOrderAction.Added) { DictUsersOpenedOrders[ol.id_ord] = rawOls; // m_plaza2Connector.GUIBox.UpdateOrders(isin, new CRawOrdersLogStruct(ol)); Log("Update " + ol.id_ord); string dbgString = "Update " + ol.id_ord; m_plaza2Connector.GUIBox.ExecuteWindowsUpdate(new Action(() => m_plaza2Connector.GUIBox.UpdateOrders(isin, rawOls))); Log("End update " + ol.id_ord); } else if (ol.action == (sbyte)EnmOrderAction.Deleted || ol.action == (sbyte)EnmOrderAction.Deal) { DictUsersOpenedOrders.Remove(ol.id_ord); //m_plaza2Connector.GUIBox.RemoveOrder(ol.id_ord); Log("Remove " + ol.id_ord); string dbgString = "Remove " + ol.id_ord; m_plaza2Connector.GUIBox.ExecuteWindowsUpdate(new Action(() => m_plaza2Connector.GUIBox.RemoveOrder(rawOls.Id_ord))); Log("End remove " + ol.id_ord); } } catch (Exception e) { Error("UpdateDictUserOpenedOrders", e); } }
public OrderInst(string isin, CRawOrdersLogStruct ols) { Id = ols.Id_ord; Isin = isin; Dir = (ols.Dir == (int)OrderDirection.Buy) ? "Buy" : "Sell"; Price = ols.Price; Moment = ols.Moment; Amount = ols.Amount; Ext_id = ols.Ext_id; }
public void DBGDeleted(CRawOrdersLogStruct userOrdLogStruct) { string msg = "[ORDBOX_DELETED] "; msg += String.Format("Ext_id={0} Instrument={1} Id_ord={2} ", userOrdLogStruct.Ext_id, userOrdLogStruct.Instrument, userOrdLogStruct.Id_ord); Log(msg); }
public void Process(CTableRecrd record) { //TODO normal // if (!_client.IsOnlineUserOrderLog) // _client.IsOnlineUserOrderLog = true; _onlineDetector.Update(); //TODO check partial acception! CRawOrdersLogStruct userOrdLogStruct = new CRawOrdersLogStruct() { Id_ord = Convert.ToInt64(record.values["ORDERNO"]), Ext_id = Convert.ToInt16(record.values["EXTREF"]), Dir = Convert.ToChar(record.values["BUYSELL"]) == 'B' ? (sbyte)EnmOrderDir.Buy : (sbyte)EnmOrderDir.Sell, Price = Convert.ToDecimal(record.values["PRICE"]), Amount = Convert.ToInt16(record.values["QUANTITY"]), Instrument = Convert.ToString(record.values["SECCODE"]), Action = (sbyte)CASTSConv.ASTSActionToEnmOrderAction(Convert.ToChar(record.values["STATUS"])), Moment = CASTSConv.ASTSTimeToDateTime(record.values["ORDERTIME"].ToString()) }; EnmBotEventCode evnt = EnmBotEventCode.OnEmptyEvent; if (userOrdLogStruct.Action == (sbyte)EnmOrderAction.Added) { evnt = EnmBotEventCode.OnOrderAccepted; } else if (userOrdLogStruct.Action == (sbyte)EnmOrderAction.Deleted) { evnt = EnmBotEventCode.OnOrderCancel; } else if (userOrdLogStruct.Action == (sbyte)EnmOrderAction.Deal) { evnt = EnmBotEventCode.OnOrderDeal; } _client.TriggerRecalculateBot(userOrdLogStruct.Ext_id, userOrdLogStruct.Instrument, evnt, userOrdLogStruct); //foreach(CBotBase bb in }
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * * INHERITED METHODS +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ /// <summary> /// Extends CBotBase Method. /// - Sends data to traders /// - Updates subbot's datra /// </summary> protected override void ProcessRawOrdLogStruct(CRawOrdersLogStruct rols) { base.ProcessRawOrdLogStruct(rols); BindTradersDispatcher(); if (_tradersDispatcher != null) //if (m_plaza2Connector.IsOnlineUserOrderLog) { //TODO enqueue _tradersDispatcher.EnqueueUserOrdersUpdate(BotId, this); } UpdateSubBotOrdersMonitorInstr(rols); }
public void UpdateOrders(string isin, CRawOrdersLogStruct rols) { if (m_plaza2Connector.IsOnlineUserOrderLog && !m_plaza2Connector.GUIBox.IsOnlineUserOrderLogDelayed) { m_plaza2Connector.GUIBox.ExecuteWindowsUpdate(new Action(() => { m_plaza2Connector.GUIBox.IsOnlineUserOrderLogDelayed = true; } )); } m_plaza2Connector.GUIBox.ExecuteWindowsUpdate(new Action(() => Orders.UpdateWithId(new OrderInst(isin, rols)) )); }
/// <summary> /// Call from ProcessRawOrdLogStruct /// </summary> /// <param name="rols"></param> private void UpdateSubBotOrdersMonitorInstr(CRawOrdersLogStruct rols) { if (rols.Ext_id != BotId) { return; } string instrument = "unknown"; //update 2017-08-10 for ASTS if (rols.Instrument != null && rols.Instrument != "") //for ASTS { instrument = rols.Instrument; } else //for Plaza 2 { if (_dealingServer.Instruments.IsContainsIsinId(rols.Isin_Id)) { instrument = _dealingServer.Instruments.DictIsinId_Instrument[rols.Isin_Id]; } else { Log("Error ! Unknown isin"); } } if (!_subBots.ContainsKey(instrument)) { return; } //2017-07-18 check _monitorOrdersAllTotal if (_monitorOrdersAllTotal.ContainsKey(instrument)) { _subBots[instrument].UpdateOrdersMonitorInstr(_monitorOrdersAllTotal[instrument]); } //_monitorOrdersAllTotal[instrument] }
public void Recalc(CRawOrdersLogStruct ol, CLogger logger) { decimal oldAmount = Amount; Amount = (ol.Dir == (sbyte)OrderDirection.Buy) ? Amount + ol.Amount : Amount - ol.Amount; if (Amount != 0) { //new del has same dir as pos if ((oldAmount >= 0 && ol.Dir == (sbyte)OrderDirection.Buy) || (oldAmount <= 0 && ol.Dir == (sbyte)OrderDirection.Sell)) { AvPos = (AvPos * Math.Abs(oldAmount) + ol.Deal_Price * ol.Amount) / (Math.Abs(oldAmount) + ol.Amount); } else //new dir has another direction and more than pos if ((Math.Abs(ol.Amount) > Math.Abs(oldAmount))) { AvPos = ol.Deal_Price; } } else { AvPos = 0; } logger.Log("Amount=" + Amount + " AvPos=" + AvPos.ToString("0.0") + " | oldAmount=" + oldAmount + " | ol.Price=" + ol.Deal_Price + " ol.Amount=" + ol.Amount + " ol.Dir=" + ol.Dir + " ol.Id_ord=" + ol.Id_ord); }
public void UpdateOrders(string isin, CRawOrdersLogStruct rols) { ExecuteWindowsUpdate(new Action(() => Orders.UpdateWithId(new OrderInst(isin, rols)) )); }
public void RemoveOrders(string isin, CRawOrdersLogStruct rols) { m_plaza2Connector.GUIBox.ExecuteWindowsUpdate(new Action(() => Orders.RemoveWithId(rols.Id_ord) )); }
public void Update(FUTTRADE.orders_log ol) { int i; UpdateDictUserOpenedOrders((FUTTRADE.orders_log)ol.Copy()); mxListRawOrders.WaitOne(); { try { //Here we fill "Raw Orders struct list based on replRev/replId for (i = 0; i < m_listRawOrdersStruct.Count; i++) { if (m_listRawOrdersStruct[i].ReplId == ol.replID) { //Seems like this will never happened m_listRawOrdersStruct[i] = new CRawOrdersLogStruct(ol); } } if (i == m_listRawOrdersStruct.Count) { m_listRawOrdersStruct.Add(new CRawOrdersLogStruct(ol)); if (ol.action == (sbyte)EnmOrderAction.Deal) { } //TO DO put trigger new log here ? { foreach (CBotBase bt in m_plaza2Connector.ListBots) { if (bt.BotId == ol.ext_id) { EnmBotEventCode evCode = EnmBotEventCode.OnEmptyEvent; if (ol.action == (sbyte)EnmOrderAction.Added) { evCode = EnmBotEventCode.OnOrderAccepted; //TEMPO ! comment it /* CRawOrdersLogStruct rlstr = new CRawOrdersLogStruct(ol); * string instr = m_plaza2Connector.Instruments.GetInstrumentByIsinId(ol.isin_id); * bt.Recalc(instr, EnmBotEventCode.OnOrderAccepted, rlstr); */ } else if (ol.action == (sbyte)EnmOrderAction.Deleted) { /* CRawOrdersLogStruct rls = new CRawOrdersLogStruct(ol); * string isin = m_plaza2Connector.Instruments.GetInstrumentByIsinId(ol.isin_id); * bt.Recalc(isin, EnmBotEventCode.OnOrderCancel, rls);*/ evCode = EnmBotEventCode.OnOrderCancel; } else if (ol.action == (sbyte)EnmOrderAction.Deal) { /* CRawOrdersLogStruct rls = new CRawOrdersLogStruct(ol); * string isin = m_plaza2Connector.Instruments.GetInstrumentByIsinId(ol.isin_id); * bt.Recalc(isin, EnmBotEventCode.OnOrderDeal, rls); */ evCode = EnmBotEventCode.OnOrderDeal; } CRawOrdersLogStruct rls = new CRawOrdersLogStruct(ol); string isin = m_plaza2Connector.Instruments.GetInstrumentByIsinId(ol.isin_id); bt.Recalc(isin, evCode, rls); } } } } mxListRawOrders.ReleaseMutex(); } catch (Exception e) { Error("CUserOrderBox.Update", e); } } // // TODO gen here orderslist for each obot // }
/// <summary> /// /// /// Algorithm is described here: /// https://github.com/bitfinexcom/bitfinex-api-node/issues/153 /// </summary> /// <param name="respOrders"></param> /// <param name="orderAction"></param> public void ProcessOrder(ResponseOrders respOrders, EnmOrderAction orderAction) { //TODO normal string instrument = respOrders.Symbol.Remove(0, 1); int decimalsOfVolume = _client.GetDecimalVolume(instrument); //int iAmount = (int)Math.Abs(CUtilConv.GetIntVolume((decimal)respOrders.Amount, decimalsOfVolume)); //int iAmountOrig = (int)Math.Abs(CUtilConv.GetIntVolume((decimal)respOrders.AmountOrig, decimalsOfVolume)); decimal amount = Convert.ToDecimal(Math.Abs((double)respOrders.Amount)); decimal amountOrig = Convert.ToDecimal(Math.Abs((double)respOrders.AmountOrig)); //int iAmountRest = (int) CBfxUtils.GetIntVolume( Math.Abs((decimal)respOrders.AmountOrig) - Math.Abs((decimal)respOrders.Amount),decimalsOfVolume); DateTime momentOrderCreate = CUtilTime.DateTimeFromUnixTimestampMillis((long)respOrders.MtsCreate); DateTime momentOrderUpdate = CUtilTime.DateTimeFromUnixTimestampMillis((long)respOrders.MtsUpdate); //check advance data in status field //Could by like "Cancelled" or more complex "EXECUTED @ 10908.0(-0.0) EnmBfxOrderStatus orderStatus = CBfxUtils.GetOrderStatus(respOrders.OrderStatus); //common parametters same for all order update type CRawOrdersLogStruct userOrdLogStruct = new CRawOrdersLogStruct { Id_ord = respOrders.Id, Ext_id = (int)respOrders.Gid, Price = Math.Abs((decimal)respOrders.Price), // Amount = (int)iAmount, // Amount_rest = iAmountRest, Instrument = instrument, Action = (sbyte)BfxOrdStatusToEnmOrderAction(orderStatus), // Moment = momentOrderCreate }; //case of add order if (orderAction == EnmOrderAction.Added) { //2018-04-04 NOTE. Added order could be two cases: //1) Jast Added limit order - we do use amount //2) Put limit order by market and it is partialy filled - we alse do // use amount as we initial order in bot with rest of order. // One important thing - we do override Action from "Partialy filled" to "OrderAccepted" userOrdLogStruct.Amount = amount; userOrdLogStruct.Dir = respOrders.Amount > 0 ? (sbyte)EnmOrderDir.Buy : (sbyte)EnmOrderDir.Sell; //Override to "OrderAccepted" for "partially filled" case if (orderStatus == EnmBfxOrderStatus.PartiallyFilled) { userOrdLogStruct.Action = (sbyte)EnmOrderAction.Added; } _client.TriggerRecalculateBot(userOrdLogStruct.Ext_id, userOrdLogStruct.Instrument, EnmBotEventCode.OnOrderAccepted, userOrdLogStruct); //added 2018-06-06 //first process deal if exist //old place of CheckForDealsWithNoBotId before 2018-08-03 CUserOrder userOrder = new CUserOrder { Amount = amount, BotId = userOrdLogStruct.Ext_id, Instrument = userOrdLogStruct.Instrument, OrderActionLast = EnmOrderAction.Added, OrderId = userOrdLogStruct.Id_ord, Status = respOrders.OrderStatus }; _userOrderLog.Update(userOrder); //2018-06-03 move AFTER adding order (as is _userOrderLog later) _client.CheckForDealsWithNoBotId(userOrdLogStruct.Id_ord, userOrdLogStruct.Ext_id); _dbg.DBGProcessOrderAdded(userOrder); } //case of fully deleted order else if (orderAction == EnmOrderAction.Deleted && orderStatus == EnmBfxOrderStatus.Canceled) { //just tell bot to cancell order _client.TriggerRecalculateBot(userOrdLogStruct.Ext_id, userOrdLogStruct.Instrument, EnmBotEventCode.OnOrderCancel, userOrdLogStruct); //added 2018-05-13 //first process deal if exist //old place of CheckForDealsWithNoBotId before 2018-08-03 //no order is not need even for "very late" update. //2018-05-31 removed (user cancel before "te" and "tu"- having a problem) //_userOrderLog.Delete(userOrdLogStruct.Ext_id, // userOrdLogStruct.Instrument, // userOrdLogStruct.Id_ord); //2018-05-31 set order deleted CUserOrder userOrder = new CUserOrder { Amount = amount, BotId = userOrdLogStruct.Ext_id, Instrument = userOrdLogStruct.Instrument, OrderActionLast = EnmOrderAction.Deleted, OrderId = userOrdLogStruct.Id_ord, Status = respOrders.OrderStatus }; _userOrderLog.Update(userOrder); //2018-06-03 move AFTER adding order (as is _userOrderLog later) _client.CheckForDealsWithNoBotId(userOrdLogStruct.Id_ord, userOrdLogStruct.Ext_id); _dbg.DBGDeleted(userOrdLogStruct); //TODO check not processed deals } //case of full or partial executed order else if ((orderAction == EnmOrderAction.Deleted && orderStatus == EnmBfxOrderStatus.Executed) || //case of partial filled (orderAction == EnmOrderAction.Update && orderStatus == EnmBfxOrderStatus.PartiallyFilled)) { decimal amountUse = amountOrig - amount; //check for partially filled userOrdLogStruct.Dir = respOrders.AmountOrig > 0 ? (sbyte)EnmOrderDir.Buy : (sbyte)EnmOrderDir.Sell; //2018-04-04 possible cases: //1) Deleted and executed order by market when limit order was not set yet, on that case amount=0 // but as no order exist in BotBase.MonitorOrders - nothing happens //2) Deal for order that exists, we do encrease amounts of orders on that case -NOT TESTED- userOrdLogStruct.Amount = amount; //first tell bot order deal _client.TriggerRecalculateBot(userOrdLogStruct.Ext_id, userOrdLogStruct.Instrument, EnmBotEventCode.OnOrderDeal, userOrdLogStruct); //_userOrderTracker.Update(userOrdLogStruct.Ext_id, userOrdLogStruct.Id_ord, respOrders.OrderStatus); //2018-06-13 the same as in all two other conditions //old place of CheckForDealsWithNoBotId before 2018-08-03 EnmOrderAction ordActUse = EnmOrderAction.Unknown; if (orderStatus == EnmBfxOrderStatus.Executed) { ordActUse = EnmOrderAction.Deal; } else if (orderStatus == EnmBfxOrderStatus.PartiallyFilled) { ordActUse = EnmOrderAction.PartialFilled; } CUserOrder userOrder = new CUserOrder { Amount = amount, BotId = userOrdLogStruct.Ext_id, Instrument = userOrdLogStruct.Instrument, OrderActionLast = ordActUse, OrderId = userOrdLogStruct.Id_ord, Status = respOrders.OrderStatus }; _userOrderLog.Update(userOrder); _client.CheckForDealsWithNoBotId(userOrdLogStruct.Id_ord, userOrdLogStruct.Ext_id); _dbg.DBGExecPartFilled(ordActUse, userOrder); } }