Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Updating ATM Strategy Orders

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Updating ATM Strategy Orders

    I have an AddOn that extends Chart Trader with custom order entry buttons. The entry buttons use StartAtmStrategy and uses the currently selected ATM Strategy. This is working as expected.

    The AddOn also has custom buttons to partially exit a position. Using the Account CreateOrder method, the exit buttons will partially exit a position, but the orders associated with the ATM Strategy do not update to reflect the new position. Any suggestions on how I can update the ATM Strategy orders from the AddOn?

    I've noticed I can use the standard Chart Trader buttons to partially exit a position and the ATM Strategy orders are updated.

    Thanks,
    Greg
    The Trading Mantis
    NinjaTrader Ecosystem Vendor - The Trading Mantis

    #2
    Hello gregschr,

    I will look into this. I appreciate your patience.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello gregschr,

      As far as I am able to see, there is no method to scale in to or out of an existing AtmStrategy. Instead, an order can be placed to the account and the stop and target quantities be adjusted.

      There is no documentation for the code below.

      The approach is to save the atm instance to a variable.
      Code:
       // entry starts atm with quantity of 3. atm template has stop 1 with a quantity of 2 at 10 ticks, and stop 2 with a quantity of 1 at 15 ticks
      Order buyMar****rder = accountSelector.SelectedAccount.CreateOrder(atmStr ategySelector.Instrument, OrderAction.Buy, OrderType.Market, TimeInForce.Day, 3, 0, 0, string.Empty, "Entry", null);
      atmStrategy = NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrate gy(atmStrategySelector.SelectedAtmStrategy, buyMar****rder);
      The AtmStrategy will have a collection of the stop orders with <atmStrategy>.GetStopOrders(stop index).

      The atmStrategy.Account can be used to submit an order.
      The stops can be cancelled or the quantity adjusted.

      Code:
      if (atmStrategy == null)
      return;
      // check to see if stop 2 exists
      if (atmStrategy.GetStopOrders(1).Count == 1)
      {
      Order sellMar****rder = atmStrategy.Account.CreateOrder(atmStrategy.Instrument, OrderAction.Sell, OrderType.Market, TimeInForce.Day, 2, 0, 0, string.Empty, "exit market", null);
      // submit order to exit 2 quantity
      atmStrategy.Account.Submit(new Order[] { sellMar****rder });
      // cancel stop 2
      atmStrategy.Account.Cancel(new Order[] { atmStrategy.GetStopOrders(1)[0] });
      // set quantity of stop 1 from 2 to 1
      atmStrategy.GetStopOrders(0)[0].QuantityChanged = 1;
      // set price of stop 1 from 10 ticks below to 20 ticks below.
      atmStrategy.GetStopOrders(0)[0].StopPriceChanged = atmStrategy.GetStopOrders(0)[0].StopPrice - atmStrategy.GetStopOrders(0)[0].Instrument.MasterInstrument.TickSize * 20;
      atmStrategy.Account.Change(atmStrategy.GetStopOrde rs(0));
      }
      Or you may try monitoring all orders on the <account>.OrderUpdate event (or looping through all of the <account>.Orders) and change working stop orders.
      Last edited by NinjaTrader_ChelseaB; 02-15-2021, 12:31 PM.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Thanks for the suggestions Chelsea.

        I ending up using the <account>.OrderUpdate event approach. The atmStrategy and the <account>.Order approaches also work, but using the OrderUpdate event seemed like the best approach in my case.

        With any of the approaches, I still had to build out logic to handle multiple orders. For future reference, here is a snippet of the logic I ended up using:
        Code:
         int stopExitQuantity = exitQuantity;
        Print("");
        while (stopExitQuantity > 0)
        {
        Order stopOrder = account.Orders.Where(o => o.IsStopMarket && o.OrderState == OrderState.Accepted)
        .OrderByDescending(o => o.Quantity)
        .FirstOrDefault();
        stopOrder.QuantityChanged = stopOrder.Quantity > stopExitQuantity ? stopOrder.Quantity - stopExitQuantity : 0;
        stopExitQuantity = stopOrder.QuantityChanged == 0 ? stopExitQuantity - stopOrder.Quantity : 0;
        Print(string.Format("Exit Stop Order Qty:{0}, Initial Qty:{1}, Revised Qty:{2}, Remaining Qty to Exit:{3}",
        exitQuantity,
        stopOrder.Quantity,
        stopOrder.QuantityChanged,
        stopExitQuantity));
        if (stopOrder.QuantityChanged > 0)
        {
        account.Change(new[] { stopOrder });
        }
        else if (stopOrder.QuantityChanged == 0)
        {
        account.Cancel(new[] { stopOrder });
        }
        }
        
        int limitExitQuantity = exitQuantity;
        while (limitExitQuantity > 0)
        {
        Order limitOrder = null;
        if (orderAction == OrderAction.BuyToCover)
        {
        limitOrder = account.Orders.Where(o => o.IsLimit && o.OrderState == OrderState.Working)
        .OrderByDescending(o => o.LimitPrice)
        .FirstOrDefault();
        }
        else if (orderAction == OrderAction.Sell)
        {
        limitOrder = account.Orders.Where(o => o.IsLimit && o.OrderState == OrderState.Working)
        .OrderBy(o => o.LimitPrice)
        .FirstOrDefault();
        }
        else
        {
        break;
        }
        
        limitOrder.QuantityChanged = limitOrder.Quantity > limitExitQuantity ? limitOrder.Quantity - limitExitQuantity : 0;
        limitExitQuantity = limitOrder.QuantityChanged == 0 ? limitExitQuantity - limitOrder.Quantity : 0;
        Print(string.Format("Exit Limit Order Qty:{0}, Initial Qty:{1}, Revised Qty:{2}, Remaining Qty to Exit:{3}",
        exitQuantity,
        limitOrder.Quantity,
        limitOrder.QuantityChanged,
        limitExitQuantity));
        if (limitOrder.QuantityChanged > 0)
        {
        account.Change(new[] { limitOrder });
        }
        else if (limitOrder.QuantityChanged == 0)
        {
        // since stop and limit are linked, don't need to cancel the limit order, i.e. it is already cancelled
        // calling cancel again here will cancel a second limit order (and linked stopped order)
        //account.Cancel(new[] { limitOrder });
        }
        }
        On a side note, while testing the atmStrategy approach, I found some useful undocumented methods:
        Code:
        atmStrategy.CloseStrategy("Entry"); // closes open ATM position and cancels ATM orders.
        atmStrategy.RemoveTarget(); // removes last target and combines with the next target
        atmStrategy.AddTarget(); // adds a target one point below last target
        atmStrategy.ProtectPosition(atmStrategySelector.SelectedAtmStrategy, position);// protects the current open position.
        The Trading Mantis
        NinjaTrader Ecosystem Vendor - The Trading Mantis

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by arvidvanstaey, Today, 02:19 PM
        4 responses
        11 views
        0 likes
        Last Post arvidvanstaey  
        Started by samish18, 04-17-2024, 08:57 AM
        16 responses
        60 views
        0 likes
        Last Post samish18  
        Started by jordanq2, Today, 03:10 PM
        2 responses
        9 views
        0 likes
        Last Post jordanq2  
        Started by traderqz, Today, 12:06 AM
        10 responses
        18 views
        0 likes
        Last Post traderqz  
        Started by algospoke, 04-17-2024, 06:40 PM
        5 responses
        47 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X