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

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.
        JesseNinjaTrader Customer Service

        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 jxs_xrj, 01-12-2020, 09:49 AM
          6 responses
          3,290 views
          1 like
          Last Post jgualdronc  
          Started by Touch-Ups, Today, 10:36 AM
          0 responses
          9 views
          0 likes
          Last Post Touch-Ups  
          Started by geddyisodin, 04-25-2024, 05:20 AM
          11 responses
          62 views
          0 likes
          Last Post halgo_boulder  
          Started by Option Whisperer, Today, 09:55 AM
          0 responses
          8 views
          0 likes
          Last Post Option Whisperer  
          Started by halgo_boulder, 04-20-2024, 08:44 AM
          2 responses
          25 views
          0 likes
          Last Post halgo_boulder  
          Working...
          X