Exemplo n.º 1
0
        /// <summary>Handle a request to place a market order</summary>
        private void HandleMsg(OutMsg.PlaceMarketOrder req)
        {
            // Ignore invalid requests
            if (req.Order.TradeType == ETradeType.None)
            {
                return;
            }

            // Get the symbol to be traded
            var sym = GetSymbol(req.Order.SymbolCode);

            // Place the immediate order
            var result = (TradeResult)null;

            if (req.Position != null)
            {
                // Note: CAlgo does not take an entry price for immediate orders. It must
                // assume the price of the received message and only reject it if the order cannot
                // be filled within 'MarketRangePips' of the price at the time the order is received.
                var position = req.Position;
                result = CAlgo.ExecuteMarketOrder(
                    position.TradeType.ToCAlgoTradeType(),
                    sym,
                    position.Volume,
                    position.Label,
                    position.StopLossRel > 0 ? sym.PriceToPips(position.StopLossRel)   : (double?)null,
                    position.TakeProfitRel > 0 ? sym.PriceToPips(position.TakeProfitRel) : (double?)null,
                    req.MarketRangePips,
                    position.Comment);
            }
            // Place the limit order
            else if (req.PendingLimit != null)
            {
                var pending = req.PendingLimit;
                result = CAlgo.PlaceLimitOrder(
                    pending.TradeType.ToCAlgoTradeType(),
                    sym,
                    pending.Volume,
                    pending.EntryPrice,
                    pending.Label,
                    pending.StopLossRel > 0 ? sym.PriceToPips(pending.StopLossRel) : (double?)null,
                    pending.TakeProfitRel > 0 ? sym.PriceToPips(pending.TakeProfitRel) : (double?)null,
                    pending.ExpirationTime != 0 ? new DateTime(pending.ExpirationTime, DateTimeKind.Utc) : (DateTime?)null,
                    pending.Comment);
            }
            // Place the stop order
            else if (req.PendingStop != null)
            {
                var pending = req.PendingStop;
                result = CAlgo.PlaceStopOrder(
                    pending.TradeType.ToCAlgoTradeType(),
                    sym,
                    pending.Volume,
                    pending.EntryPrice,
                    pending.Label,
                    pending.StopLossRel > 0 ? sym.PriceToPips(pending.StopLossRel) : (double?)null,
                    pending.TakeProfitRel > 0 ? sym.PriceToPips(pending.TakeProfitRel) : (double?)null,
                    pending.ExpirationTime != 0 ? new DateTime(pending.ExpirationTime, DateTimeKind.Utc) : (DateTime?)null,
                    pending.Comment);
            }

            // Send the result
            Post(new InMsg.MarketOrderChangeResult(result.IsSuccessful, result.Error != null ? result.Error.Value.ToTradeeErrorCode() : EErrorCode.NoError));

            // Send an update of the current/pending positions
            SendCurrentPositions();
            SendPendingPositions();
        }
Exemplo n.º 2
0
        /// <summary>Handle a request to place a market order</summary>
        private void HandleMsg(OutMsg.PlaceMarketOrder msg)
        {
            var order = msg.Order;

            // Find the transmitter of the instrument that the order is for
            Transmitter trans;

            if (!Transmitters.TryGetValue(order.SymbolCode, out trans))
            {
                Model.DispatchMsg(new InMsg.MarketOrderChangeResult(false, EErrorCode.Failed));
                return;
            }

            // Check the volume is valid
            if (order.Volume < trans.PriceData.VolumeMin ||
                order.Volume > trans.PriceData.VolumeMax ||
                order.Volume % trans.PriceData.VolumeStep != 0)
            {
                Model.DispatchMsg(new InMsg.MarketOrderChangeResult(false, EErrorCode.InvalidVolume));
                return;
            }

            // Place an immediate order
            if (msg.Position != null)
            {
                var res = ActionTrade(msg.Position);
                if (res != EErrorCode.NoError)
                {
                    Model.DispatchMsg(new InMsg.MarketOrderChangeResult(false, res));
                    return;
                }
            }

            // Place a limit order
            else if (msg.PendingLimit != null)
            {
                // Limit orders are rejected if the price is not on the profit side of the order
                if (msg.PendingLimit.TradeType == ETradeType.Long && trans.PriceData.AskPrice < msg.PendingLimit.EntryPrice)
                {
                    Model.DispatchMsg(new InMsg.MarketOrderChangeResult(false, EErrorCode.Failed));
                    return;
                }
                if (msg.PendingLimit.TradeType == ETradeType.Short && trans.PriceData.BidPrice > msg.PendingLimit.EntryPrice)
                {
                    Model.DispatchMsg(new InMsg.MarketOrderChangeResult(false, EErrorCode.Failed));
                    return;
                }

                // Add the pending order
                Pending.Add(msg.PendingLimit);
            }

            // Place a stop order
            else if (msg.PendingStop != null)
            {
                // Stop orders are rejected if the price is not on the loss side of the order
                if (msg.PendingStop.TradeType == ETradeType.Long && trans.PriceData.AskPrice > msg.PendingStop.EntryPrice)
                {
                    Model.DispatchMsg(new InMsg.MarketOrderChangeResult(false, EErrorCode.Failed));
                    return;
                }
                if (msg.PendingStop.TradeType == ETradeType.Short && trans.PriceData.BidPrice < msg.PendingStop.EntryPrice)
                {
                    Model.DispatchMsg(new InMsg.MarketOrderChangeResult(false, EErrorCode.Failed));
                    return;
                }

                // Add the pending order
                Pending.Add(msg.PendingStop);
            }

            // Send an update of the current/pending positions
            SendCurrentPositions();
            SendPendingPositions();
        }