// ********************************************************************** public string KillOrder(long oid) { if (connected) { transId++; Trans2Quik.Result r = Trans2Quik.SEND_ASYNC_TRANSACTION( "TRANS_ID=" + transId + "; SECCODE=" + secCode + "; CLASSCODE=" + classCode + "; ACTION=KILL_ORDER; ORDER_KEY=" + oid + ";", out error, msg, msg.Capacity); if (r == Trans2Quik.Result.SUCCESS) { return(null); } else { return(msg.Length > 0 ? Trim(msg.ToString()) : r + ", " + error); } } else { return(NoConnectionText); } }
// ********************************************************************** // * Сделки * // ********************************************************************** void TradeStatusCallback( int nMode, double trade_id, double order_id, string classCode, string secCode, double price, int quantity, double msum, int isSell, int tradeDescriptor) { if (nMode == 0) { if (isSell != 0) { quantity = -quantity; } string comment = Marshal.PtrToStringAnsi(Trans2Quik.TRADE_BROKERREF(tradeDescriptor)); if ((comment.EndsWith(cfg.FullProgName) || cfg.u.AcceptAllTrades)) { mgr.PutOwnTrade(new OwnTrade(Price.GetInt(price), quantity)); } } }
// ********************************************************************** // * Управление заявками * // ********************************************************************** string SendOrder(char op, int price, int quantity, out int tid) { if (connected) { tid = ++transId; Trans2Quik.Result r = Trans2Quik.SEND_ASYNC_TRANSACTION( "TRANS_ID=" + tid + "; ACCOUNT=" + cfg.u.QuikAccount + "; CLIENT_CODE=" + cfg.u.QuikClientCode + "//" + cfg.FullProgName + "; SECCODE=" + secCode + "; CLASSCODE=" + classCode + "; ACTION=NEW_ORDER; OPERATION=" + op + "; PRICE=" + Price.GetRaw(price) + "; QUANTITY=" + quantity + ";", out error, msg, msg.Capacity); if (r == Trans2Quik.Result.SUCCESS) { return(null); } else { tid = 0; return(msg.Length > 0 ? Trim(msg.ToString()) : r + ", " + error); } } else { tid = 0; return(NoConnectionText); } }
// ********************************************************************** public void Deactivate() { working = false; if (connected) { Trans2Quik.DISCONNECT(out error, msg, msg.Capacity); } }
// ********************************************************************** public void Disconnect() { working = false; connecting.Change(Timeout.Infinite, Timeout.Infinite); if (connected) { Trans2Quik.DISCONNECT(out error, msg, msg.Capacity); } }
// ********************************************************************** // * Соединение * // ********************************************************************** void TryConnect(Object state) { if (working) { try { if ( Trans2Quik.SET_CONNECTION_STATUS_CALLBACK(StatusCallback, out error, msg, msg.Capacity) != Trans2Quik.Result.SUCCESS || Trans2Quik.SET_TRANSACTIONS_REPLY_CALLBACK(TransactionReplyCallback, out error, msg, msg.Capacity) != Trans2Quik.Result.SUCCESS ) { mgr.ConnectionUpdate(TermConnection.None, msg.ToString()); return; } Trans2Quik.Result result = Trans2Quik.CONNECT(cfg.u.QuikFolder, out error, msg, msg.Capacity); if (result != Trans2Quik.Result.SUCCESS && result != Trans2Quik.Result.ALREADY_CONNECTED_TO_QUIK) { mgr.ConnectionUpdate(TermConnection.None, msg.ToString()); return; } } catch (Exception e) { mgr.ConnectionUpdate(TermConnection.None, e.Message); return; } connected = true; if (Trans2Quik.UNSUBSCRIBE_ORDERS() != Trans2Quik.Result.SUCCESS || Trans2Quik.UNSUBSCRIBE_TRADES() != Trans2Quik.Result.SUCCESS || Trans2Quik.SUBSCRIBE_ORDERS(cfg.u.ClassCode, cfg.u.SecCode) != Trans2Quik.Result.SUCCESS || Trans2Quik.START_ORDERS(OrderStatusCallback) != Trans2Quik.Result.SUCCESS || Trans2Quik.SUBSCRIBE_TRADES(cfg.u.ClassCode, cfg.u.SecCode) != Trans2Quik.Result.SUCCESS || Trans2Quik.START_TRADES(TradeStatusCallback) != Trans2Quik.Result.SUCCESS) { mgr.ConnectionUpdate(TermConnection.Partial, "Соединение установлено неполностью"); return; } mgr.ConnectionUpdate(TermConnection.Full, "Соединение с сервером QUIK установлено"); } connecting.Change(Timeout.Infinite, Timeout.Infinite); }
// ********************************************************************** void OrderStatusCallback( int nMode, int tid, double order_id, string classCode, string secCode, double price, int balance, double msum, int isSell, int status, int orderDescriptor) { if (nMode == 0 && OrderUpdateHandler != null) { int filled; if (isSell == 0) { filled = Trans2Quik.ORDER_QTY(orderDescriptor) - balance; } else { filled = balance - Trans2Quik.ORDER_QTY(orderDescriptor); balance = -balance; } if (status == 1) { OrderUpdateHandler((long)order_id, balance, filled); } else { OrderUpdateHandler((long)order_id, 0, filled); } } }
// ********************************************************************** // * Сделки * // ********************************************************************** void TradeStatusCallback( int nMode, double trade_id, double order_id, string classCode, string secCode, double price, int quantity, double msum, int isSell, int tradeDescriptor) { string comment = Marshal.PtrToStringAnsi(Trans2Quik.TRADE_BROKERREF(tradeDescriptor)); if ((comment != null && comment.EndsWith(cfg.FullProgName)) || cfg.u.AcceptAllTrades) { if (isSell == 0) { if (lastBuyId >= trade_id) { return; } else { lastBuyId = trade_id; } } else { if (lastSellId >= trade_id) { return; } else { lastSellId = trade_id; } quantity = -quantity; } if (nMode == 0 && OwnTradeHandler != null) { int date = Trans2Quik.TRADE_DATE(tradeDescriptor); int time = Trans2Quik.TRADE_TIME(tradeDescriptor); int year, month, day; int hour, min, sec; year = date / 10000; month = (day = date - year * 10000) / 100; day -= month * 100; hour = time / 10000; min = (sec = time - hour * 10000) / 100; sec -= min * 100; OwnTradeHandler(new OwnTrade((long)order_id, new DateTime(year, month, day, hour, min, sec), Price.GetInt(price), quantity)); } } }