Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy placing multiple orders

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

    Strategy placing multiple orders

    Hello,

    I created a strategy using ATM order creation and placement. The orders are being placed exactly where I want them to be based on the "entryResult" but my problem is that the strategy places multiple orders. The orders are being placed correctly but I would like it to stop placing orders if there is already a position open. I have tried to use entry handling, I've tried a private bool that is only true when market position is flat, etc. None of those attempts worked. How can I accomplish this?

    The portion of my code that controls order placement is below:

    // Calculate the trade, create, and place orders.
    private void TakeTheTrade()
    {
    if (State != State.Realtime)
    {
    return;
    }

    if (Open[0] == Close[0])
    {
    if (entryResult < Close[0])
    {
    // Long ATM Order.
    atmStrategyId = GetAtmStrategyUniqueId();
    atmStrategyOrderId = GetAtmStrategyUniqueId();
    AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, atmStrategyOrderId, "DojiLongStrategy", atmStrategyId, (atmCallbackErrorCode, atmCallbackId) =>
    {
    // Checks that the call back is returned for the current atmStrategyId stored.
    if (atmCallbackId == atmStrategyId)
    {
    // Check the atm call back for any error codes.
    if (atmCallbackErrorCode == Cbi.ErrorCode.NoError)
    {
    // If no error, set private bool to true to indicate the atm strategy is created.
    isAtmStrategyCreated = true;
    }
    }
    });

    if(isAtmStrategyCreated)
    {
    // ATM logic.
    EnterLong("Long");
    }

    else if(!isAtmStrategyCreated)
    {
    // Custom handling for a failed atm Strategy.
    Print("Long ATM Doji Strategy failed.");
    }
    }

    else if (entryResult > Close[0])
    {
    // Short ATM Order.
    atmStrategyId = GetAtmStrategyUniqueId();
    atmStrategyOrderId = GetAtmStrategyUniqueId();
    AtmStrategyCreate(OrderAction.Sell, OrderType.Market, 0, 0, TimeInForce.Day, atmStrategyOrderId, "DojiShortStrategy", atmStrategyId, (atmCallbackErrorCode, atmCallbackId) =>
    {
    // Checks that the call back is returned for the current atmStrategyId stored.
    if (atmCallbackId == atmStrategyId)
    {
    // Check the atm call back for any error codes.
    if (atmCallbackErrorCode == Cbi.ErrorCode.NoError)
    {
    // If no error, set private bool to true to indicate the atm strategy is created.
    isAtmStrategyCreated = true;
    }
    }
    });

    if(isAtmStrategyCreated)
    {
    // ATM logic.
    EnterShort("Short");
    }

    else if(!isAtmStrategyCreated)
    {
    // Custom handling for a failed atm Strategy.
    Print("Short ATM Doji Strategy failed.");
    }
    }
    }
    }

    #2
    You ever figure this out ? I think you need to check if (Position.MarketPosition == MarketPosition.Long) or shot before placing a new trade.

    Comment


      #3
      Originally posted by jeffom View Post
      You ever figure this out ? I think you need to check if (Position.MarketPosition == MarketPosition.Long) or shot before placing a new trade.
      I've tried using a number of methods to prevent this including a private bool that checks if (Position.MarketPosition == MarketPosition.Flat) and only allows it to progress to the order creation code if that bool is true as this was the solution someone from NT suggested to another user many years ago. For some reason it completely ignores it and proceeds to place orders anyway.

      I've also tried setting the entry handling for both all entries as well as unique entries to 1 per direction and it ignores those rules as well.

      There must be something happening behind the scenes that I am not aware of.

      Comment


        #4
        Hello benjamin$ and jeffom,

        The problem here is that a general entry condition is used to start a new order, every time the entry condition is true a new order is formed. ATM's do not populate strategy properties like Position so you need to follow the code in the SampleATMStrategy script that comes with NinjaTrader when using ATM's. You have to form your own variables and conditions to know when the strategy was submitted and when it is filled to reset the logic.

        I also see you have EnterShort being used, ATM and Managed approach orders cant be mixed. The strategy is not going to be aware of the ATM position/statistics so those should not be combined. You can use all managed approach orders or just the ATM orders like shown in the SampleATMStrategy script. That uses the orderid string logically to know the ATM is active and also uses that to request information about the ATM while its active. That helps to delegate the flow of submitting one order at a time.

        Last edited by NinjaTrader_Jesse; 08-01-2022, 08:23 AM.

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello benjamin$ and jeffom,

          The problem here is that a general entry condition is used to start a new order, every time the entry condition is true a new order is formed. ATM's do not populate strategy properties like Position so you need to follow the code in the SampleATMStrategy script that comes with NinjaTrader when using ATM's. You have to form your own variables and conditions to know when the strategy was submitted and when it is filled to reset the logic.

          I also see you have EnterShort being used, ATM and Managed approach orders cant be mixed. The strategy is not going to be aware of the ATM position/statistics so those should not be combined. You can use all managed approach orders or just the ATM orders like shown in the SampleATMStrategy script. That uses the orderid string logically to know the ATM is active and also uses that to request information about the ATM while its active. That helps to delegate the flow of submitting one order at a time.
          Thank you! I appreciate the help. I should be able to take it from here, especially with that example. You guys are so helpful !!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          54 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          130 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          72 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          44 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          49 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X