static public string TradeSideToString(ProtoOATradeSide tradeSide)
        {
            switch (tradeSide)
            {
            case ProtoOATradeSide.Buy:
                return("BUY");

            case ProtoOATradeSide.Sell:
                return("SELL");

            default:
                return("unknown");
            }
        }
        public void SendTrade(UserConfig config, string symbolName, string direction, long size, string clientMsgId, string orderComment, long stopPoints = 0, long takeProfitPoints = 0)
        {
            ProtoOATradeSide tradeSide = ProtoOATradeSide.BUY;

            if (direction == "SHORT")
            {
                tradeSide = ProtoOATradeSide.SELL;
            }

            int symbolId = config.Symbols.SymbolId(symbolName);

            //create the trade request message and queue it
            var _msg = ProtoOANewOrderReq.CreateBuilder();

            _msg.SetCtidTraderAccountId(config.AccountId);
            _msg.SetSymbolId(symbolId);
            _msg.SetOrderType(ProtoOAOrderType.MARKET);
            _msg.SetTradeSide(tradeSide);
            _msg.SetVolume(size);
            _msg.SetLabel("LABEL");

            if (stopPoints > 0)
            {
                _msg.SetRelativeStopLoss(stopPoints);
            }
            if (takeProfitPoints > 0)
            {
                _msg.SetRelativeTakeProfit(takeProfitPoints);
            }
            _msg.SetComment(orderComment);

            var protoMsg = ProtoMessage.CreateBuilder();

            protoMsg.PayloadType = (uint)_msg.PayloadType;
            protoMsg.SetPayload(_msg.Build().ToByteString());
            protoMsg.SetClientMsgId(clientMsgId);

            _trasmitQueue.Enqueue(protoMsg.Build());
        }
        public ProtoMessage CreateStopLimitOrderRequest(long accountId, string accessToken, int symbolId, ProtoOATradeSide tradeSide, long volume, double stopPrice, int slippageInPoints, string clientMsgId = null)
        {
            var _msg = ProtoOANewOrderReq.CreateBuilder();;

            _msg.SetCtidTraderAccountId(accountId);
            _msg.SetSymbolId(symbolId);
            _msg.SetOrderType(ProtoOAOrderType.STOP_LIMIT);
            _msg.SetTradeSide(tradeSide);
            _msg.SetVolume(volume);
            _msg.SetSlippageInPoints(slippageInPoints);
            _msg.SetStopPrice(stopPrice);
            _msg.SetComment("TradingApiTest.CreateStopLimitOrderRequest");
            return(CreateMessage((uint)_msg.PayloadType, _msg.Build().ToByteString(), clientMsgId));
        }
        public ProtoMessage CreateLimitOrderRequest(long accountId, string accessToken, int symbolId, ProtoOATradeSide tradeSide, long volume, double limitPrice, string clientMsgId = null)
        {
            var _msg = ProtoOANewOrderReq.CreateBuilder();;

            _msg.SetCtidTraderAccountId(accountId);
            _msg.SetSymbolId(symbolId);
            _msg.SetOrderType(ProtoOAOrderType.LIMIT);
            _msg.SetTradeSide(tradeSide);
            _msg.SetVolume(volume);
            _msg.SetLimitPrice(limitPrice);
            _msg.SetComment("TradingApiTest.CreateLimitOrderRequest");
            DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

            _msg.SetExpirationTimestamp((long)(DateTime.UtcNow.AddHours(1) - epoch).TotalMilliseconds);
            return(CreateMessage((uint)_msg.PayloadType, _msg.Build().ToByteString(), clientMsgId));
        }
        public ProtoMessage CreateMarketOrderRequest(long accountId, string accessToken, int symbolId, ProtoOATradeSide tradeSide, long volume, string clientMsgId = null)
        {
            var _msg = ProtoOANewOrderReq.CreateBuilder();

            _msg.SetCtidTraderAccountId(accountId);
            _msg.SetSymbolId(symbolId);
            _msg.SetOrderType(ProtoOAOrderType.MARKET);
            _msg.SetTradeSide(tradeSide);
            _msg.SetVolume(volume);
            _msg.SetComment("TradingApiTest.CreateMarketOrderRequest");
            return(CreateMessage((uint)_msg.PayloadType, _msg.Build().ToByteString(), clientMsgId));
        }
        public static ProtoMessage New_Order_Req(long ctidTraderAccountId,
                                                 long symbolId,
                                                 ProtoOAOrderType orderType,
                                                 ProtoOATradeSide tradeSide,
                                                 long volume,
                                                 double limitPrice = 0,
                                                 double stopPrice  = 0,
                                                 ProtoOATimeInForce timeInForce = ProtoOATimeInForce.GoodTillCancel,
                                                 long expirationTimestamp       = 0,
                                                 double stopLoss          = 0,
                                                 double takeProfit        = 0,
                                                 string comment           = "",
                                                 double baseSlippagePrice = 0,
                                                 int slippageInPoints     = 0,
                                                 string label             = "",
                                                 long positionId          = 0,
                                                 string clientOrderId     = "",
                                                 long relativeStopLoss    = 0,
                                                 long relativeTakeProfit  = 0,
                                                 bool guaranteedStopLoss  = false,
                                                 bool trailingStopLoss    = false,
                                                 ProtoOAOrderTriggerMethod stopTriggerMethod = ProtoOAOrderTriggerMethod.Trade)
        {
            ProtoOANewOrderReq message = new ProtoOANewOrderReq
            {
                payloadType         = ProtoOAPayloadType.ProtoOaNewOrderReq,
                ctidTraderAccountId = ctidTraderAccountId,
                symbolId            = symbolId,
                orderType           = orderType,
                tradeSide           = tradeSide,
                Volume = volume
            };

            if (limitPrice > 0)
            {
                message.limitPrice = limitPrice;
            }
            if (stopPrice > 0)
            {
                message.stopPrice = stopPrice;
            }
            if (timeInForce > 0)
            {
                message.timeInForce = timeInForce;
            }
            if (expirationTimestamp > 0)
            {
                message.expirationTimestamp = expirationTimestamp;
            }
            if (stopLoss > 0)
            {
                message.stopLoss = stopLoss;
            }
            if (takeProfit > 0)
            {
                message.takeProfit = takeProfit;
            }
            if (comment != "")
            {
                message.Comment = comment;
            }
            if (baseSlippagePrice > 0)
            {
                message.baseSlippagePrice = baseSlippagePrice;
            }
            if (slippageInPoints > 0)
            {
                message.slippageInPoints = slippageInPoints;
            }
            if (label != "")
            {
                message.Label = label;
            }
            if (positionId > 0)
            {
                message.positionId = positionId;
            }
            if (clientOrderId != "")
            {
                message.clientOrderId = clientOrderId;
            }
            if (relativeStopLoss != 0)
            {
                message.relativeStopLoss = relativeStopLoss;
            }
            if (relativeTakeProfit != 0)
            {
                message.relativeTakeProfit = relativeTakeProfit;
            }
            if (guaranteedStopLoss)
            {
                message.guaranteedStopLoss = true;
            }
            if (trailingStopLoss)
            {
                message.trailingStopLoss = true;
            }
            if (stopTriggerMethod != ProtoOAOrderTriggerMethod.Trade)
            {
                message.stopTriggerMethod = stopTriggerMethod;
            }

            Log.Info("ProtoOANewOrderReq:: " +
                     $"ctidTraderAccountId: {ctidTraderAccountId}; " +
                     $"symbolId: {symbolId}; " +
                     $"orderType: {orderType}; " +
                     $"tradeSide: {tradeSide}; " +
                     $"volume: {volume}; " +
                     $"limitPrice: {limitPrice}; " +
                     $"stopPrice: {stopPrice}; " +
                     $"timeInForce: {timeInForce}; " +
                     $"expirationTimestamp: {expirationTimestamp} ({EpochToString(expirationTimestamp)}; " +
                     $"stopLoss: {stopLoss}; " +
                     $"takeProfit: {takeProfit}; " +
                     $"comment: {comment}; " +
                     $"baseSlippagePrice: {baseSlippagePrice}; " +
                     $"slippageInPoints: {slippageInPoints}; " +
                     $"label: {label}; " +
                     $"positionId: {positionId}; " +
                     $"clientOrderId: {clientOrderId}; " +
                     $"relativeStopLoss: {relativeStopLoss}; " +
                     $"relativeTakeProfit: {relativeTakeProfit}; " +
                     $"guaranteedStopLoss: {guaranteedStopLoss}; " +
                     $"trailingStopLoss: {trailingStopLoss}; " +
                     $"stopTriggerMethod: {stopTriggerMethod}");

            InnerMemoryStream.SetLength(0);
            Serializer.Serialize(InnerMemoryStream, message);

            return(Encode((uint)message.payloadType, InnerMemoryStream.ToArray()));
        }
Esempio n. 7
0
        public ProtoMessage CreateStopOrderRequest(long accountId, string accessToken, int symbolId, ProtoOATradeSide tradeSide, long volume, double stopPrice, double takeProfitPrice, double stopLossPrice, string label = null, string clientMsgId = null)
        {
            var _msg = ProtoOANewOrderReq.CreateBuilder();

            _msg.SetCtidTraderAccountId(accountId);
            _msg.SetSymbolId(symbolId);
            _msg.SetOrderType(ProtoOAOrderType.STOP);
            _msg.SetTradeSide(tradeSide);
            _msg.SetVolume(volume);
            _msg.SetStopPrice(stopPrice);
            if (takeProfitPrice > 0)
            {
                _msg.SetTakeProfit(takeProfitPrice);
            }
            if (stopLossPrice > 0)
            {
                _msg.SetStopLoss(stopLossPrice);
            }
            _msg.SetComment("TradingApiTest.CreateStopOrderRequest");
            if (!string.IsNullOrEmpty(label))
            {
                _msg.SetLabel(label);
            }
            return(CreateMessage((uint)_msg.PayloadType, _msg.Build().ToByteString(), clientMsgId));
        }
Esempio n. 8
0
        public ProtoMessage CreateMarketRangeOrderRequest(long accountId, string accessToken, int symbolId, ProtoOATradeSide tradeSide, long volume, double baseSlippagePrice, int slippageInPoints, string label = null, string clientMsgId = null)
        {
            var _msg = ProtoOANewOrderReq.CreateBuilder();;

            _msg.SetCtidTraderAccountId(accountId);
            _msg.SetSymbolId(symbolId);
            _msg.SetOrderType(ProtoOAOrderType.MARKET_RANGE);
            _msg.SetTradeSide(tradeSide);
            _msg.SetVolume(volume);
            _msg.SetBaseSlippagePrice(baseSlippagePrice);
            _msg.SetSlippageInPoints(slippageInPoints);
            _msg.SetComment("TradingApiTest.CreateMarketRangeOrderRequest");
            if (!string.IsNullOrEmpty(label))
            {
                _msg.SetLabel(label);
            }
            return(CreateMessage((uint)_msg.PayloadType, _msg.Build().ToByteString(), clientMsgId));
        }
Esempio n. 9
0
        public ProtoOAPosition.Builder CreatePositionBuilder(long positionId, ProtoOAPositionStatus positionStatus, long accountId, ProtoOATradeSide tradeSide, int symbolId, long volume, double entryPrice, long swap,
                                                             long commission, long openTimestamp, string channel = null, string comment = null)
        {
            var _obj          = ProtoOAPosition.CreateBuilder();
            var _objTradeData = ProtoOATradeData.CreateBuilder();

            _obj.SetPositionId(positionId);
            _obj.SetPositionStatus(positionStatus);
            _objTradeData.SetTradeSide(tradeSide);
            _objTradeData.SetSymbolId(symbolId);
            _objTradeData.SetVolume(volume);
            _obj.SetSwap(swap);
            _obj.SetCommission(commission);
            _obj.SetTradeData(_objTradeData);
            // _obj.SetOpenTimestamp(openTimestamp);
            //if (channel != null)
            //    _obj.SetChannel(channel);
            //if (comment != null)
            //    _obj.SetComment(comment);
            return(_obj);
        }
Esempio n. 10
0
        //public ProtoOASpotSubscription GetSpotSubscription(byte[] obj = null)
        //{
        //    return ProtoOASpotSubscription.CreateBuilder().MergeFrom(obj).Build();
        //}
        #endregion

        #region Creating new Proto Model objects with parameters specified
        public ProtoOAOrder.Builder CreateOrderBuilder(long orderId, long accountId, ProtoOAOrderType orderType, ProtoOATradeSide tradeSide, int symbolId, long requestedVolume, long executedVolume, bool closingOrder,
                                                       string channel = null, string comment = null)
        {
            var _obj          = ProtoOAOrder.CreateBuilder();
            var _objTradeData = ProtoOATradeData.CreateBuilder();

            _objTradeData.SetTradeSide(tradeSide);
            _objTradeData.SetSymbolId(symbolId);
            _objTradeData.SetVolume(requestedVolume);
            _obj.SetOrderId(orderId);
            //   _obj.SetAccountId(accountId);
            _obj.SetOrderType(orderType);
            _obj.SetTradeData(_objTradeData);
            _obj.SetExecutedVolume(executedVolume);
            _obj.SetClosingOrder(closingOrder);
            //if (channel != null)
            //    _obj.SetChannel(channel);
            //if (comment != null)
            //    _obj.SetComment(comment);
            return(_obj);
        }