account.Change( / UpdateStopOrder(

mail@pastecode.io avatar
unknown
csharp
2 years ago
2.5 kB
1
Indexable
Never
        private void UpdateStopOrder(string signalName, Instrument instrument, OrderAction orderAction, OrderEntry orderEntry, double price)
        {
            if (orderAction != OrderAction.BuyToCover && orderAction != OrderAction.SellShort)
            {
                RealLogger.PrintOutput("ERROR: Order action of " + orderAction.ToString() + " not supported in UpdateStopOrder.");
                return;
            }

            double lastPrice = RealInstrumentService.GetLastPrice(instrument);


            if (orderAction == OrderAction.BuyToCover && price <= lastPrice)
            {
                RealLogger.PrintOutput("ERROR: Stop order price must be greater than last price.");
                return;
            }
            else if (orderAction == OrderAction.SellShort && price >= lastPrice)
            {
                RealLogger.PrintOutput("ERROR: Stop order price must be less than last price.");
                return;
            }

            bool orderChanged = false;
            int orderCount = RealOrderService.OrderCount;

            for (int index = 0; index < orderCount; index++)
            {
                RealOrder order = null;

                if (RealOrderService.TryGetByIndex(index, out order))
                {
                    if (RealOrderService.IsValidBuyStopOrder(order, instrument, OrderAction.BuyToCover) || RealOrderService.IsValidSellStopOrder(order, instrument, OrderAction.SellShort))
                    {
                        orderChanged = false;
                        Order foundNTOrder = GetNinjaTraderOrder(order);

                        if (foundNTOrder.StopPrice != price)
                        {
                            foundNTOrder.StopPriceChanged = price;
                            orderChanged = true;
                        }

                        if (orderChanged)
                        {
                            try
                            {
                                account.Change(new[] { foundNTOrder });
                            }
                            catch (Exception ex)
                            {
                                RealLogger.PrintOutput("Exception in UpdateStopOrder:" + ex.Message + " " + ex.StackTrace);  //log and ignore exception
                            }
                        }
                    }
                }
            }

        }