private void StrategyProcess(MessageQuote message)
        {
            if (historicalData.Count <= maxPeriod)
            {
                return;
            }

            // Calculation of trend
            Trend trend = (Trend)threeMaIndicator.GetValue();

            switch (trend)
            {
            case Trend.Up:
                // Up trend detected. If we were in short position, first closing it
                if (tradingState == TradingState.EnteredSell)
                {
                    // If request for closing has been sent, setting the current state -
                    // we have already exit the market
                    Position position = Core.Instance.GetPositionById(currentOrderResult.OrderId, symbol.ConnectionId);
                    if (position == null)
                    {
                        return;
                    }

                    var result = position.Close();
                    if (result.Status == TradingOperationResultStatus.Success)
                    {
                        tradingState = TradingState.ExitMarket;
                        Log($"{currentOrderResult.Status}. Position was closed.", StrategyLoggingLevel.Trading);
                    }

                    // exitting the program to give some time to
                    // the system for processing the order. Entrance will
                    // be performed on the next quote
                    return;
                }

                // If we haven't aleady entered the market, do it
                if (tradingState != TradingState.EnteredBuy)
                {
                    var orderPrice = symbol.Bid;
                    if (message is Last last)
                    {
                        orderPrice = last.Price;
                    }

                    if (message is Quote quote)
                    {
                        orderPrice = quote.Ask;
                    }

                    // Sending request for opening long position, and
                    // setting the state - "Entered long position"
                    currentOrderResult = Core.PlaceOrder(new PlaceOrderRequestParameters
                    {
                        Account     = account,
                        Symbol      = this.symbol,
                        Price       = orderPrice,
                        OrderTypeId = OrderType.Market,
                        Quantity    = quantity,
                        Side        = Side.Buy,
                    });
                    longOrdersCount++;
                    tradingState = TradingState.EnteredBuy;

                    if (currentOrderResult.Status == TradingOperationResultStatus.Success)
                    {
                        Log($"{currentOrderResult.Status}. Long position was placed.", StrategyLoggingLevel.Trading);
                    }
                    else
                    {
                        Log($"{currentOrderResult.Status}. {currentOrderResult.Message}", StrategyLoggingLevel.Trading);
                    }
                }
                break;

            case Trend.Down:
                //Down trend detected. If we were in long position, firstly closing it
                if (tradingState == TradingState.EnteredBuy)
                {
                    // If request for closing has been sent, setting the current state -
                    // we have already exit the market
                    Position position = Core.Instance.GetPositionById(currentOrderResult.OrderId, symbol.ConnectionId);
                    if (position == null)
                    {
                        return;
                    }

                    var result = position.Close();
                    if (result.Status == TradingOperationResultStatus.Success)
                    {
                        tradingState = TradingState.ExitMarket;
                        Log($"{currentOrderResult.Status}. Position was closed.", StrategyLoggingLevel.Trading);
                    }
                    // exitting the program to give some time to
                    // the system for processing the order. Entrance will
                    // be performed on the next quote
                    return;
                }

                // If we haven't aleady entered the market, do it
                if (tradingState != TradingState.EnteredSell)
                {
                    var orderPrice = symbol.Bid;
                    if (message is Last last)
                    {
                        orderPrice = last.Price;
                    }

                    if (message is Quote quote)
                    {
                        orderPrice = quote.Bid;
                    }

                    // Sending request for opening long position, and
                    // if request is sent, then setting the state - "Entered short position"
                    currentOrderResult = Core.PlaceOrder(new PlaceOrderRequestParameters
                    {
                        Account     = account,
                        Symbol      = this.symbol,
                        Price       = orderPrice,
                        OrderTypeId = OrderType.Market,
                        Quantity    = quantity,
                        Side        = Side.Sell,
                    });
                    shortOrdersCount++;
                    tradingState = TradingState.EnteredSell;

                    if (currentOrderResult.Status == TradingOperationResultStatus.Success)
                    {
                        Log($"{currentOrderResult.Status}. Short position was placed.", StrategyLoggingLevel.Trading);
                    }
                    else
                    {
                        Log($"{currentOrderResult.Status}. {currentOrderResult.Message}", StrategyLoggingLevel.Trading);
                    }
                }
                break;
            }
        }
Esempio n. 2
0
 private void Strike_NewMessage(Symbol symbol, MessageQuote quote)
 {
 }