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

How Do I Limit a Strategy Trade Entry to Only ONE Trade?

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

    #16
    Hello Assalt,

    My guess would be that there is actually an entry being submitted and that BarsSinceEntryExecution() is now greater than -1 which returns and prevents further entries.

    Provide the output from prints and TraceOrders saved to a text file and I would be happy to analyze the output and confirm.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #17
      Hi Chelsea!

      Below is the fist and the final part of the output I've got from Print comand. I excluded middle part cause it's all the same "| Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False".

      The strategy enters position one time, acts as it has to (only one order per candle) and then doesn't perform any action. As I understand the reason is in the final part but it is extremely hard for me to understand what is the reason in the code. Please advise.

      Thank you!

      12/21/2023 11:49:52 AM Strategy 'AADelta3OneDirection/314488185': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='' Mode=Ticks Value=3 IsSimulatedStop=False IsMarketIfTouched=False
      12/21/2023 11:49:52 AM Strategy 'AADelta3OneDirection/314488185': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Ticks Value=10 IsSimulatedStop=False IsMarketIfTouched=False
      12/12/2023 1:04:00 AM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False
      12/12/2023 1:04:00 AM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False
      12/12/2023 1:04:00 AM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False
      12/12/2023 1:04:00 AM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False
      12/12/2023 1:04:00 AM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False
      12/12/2023 1:04:00 AM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False
      12/12/2023 1:04:00 AM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False​
      ...
      12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False
      12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False
      12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False
      12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False
      12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False
      12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False
      12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False
      12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: True < Delta11.Sells[0] < Zero-1000: False
      12/12/2023 3:32:00 PM Strategy 'AADelta3OneDirection/314488185': Entered internal SubmitOrderManaged() method at 12/12/2023 3:32:00 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='1' FromEntrySignal=''
      12/12/2023 3:32:00 PM Strategy 'AADelta3OneDirection/314488185: Cancelled pending exit order, since associated position is closed, orderId='NT-00001-86' account='Playback101' name='Stop loss' orderState=Working instrument='ES 03-24' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=4686.5 quantity=1 tif=Gtc oco='NT-00000-86' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-12-12 15:32:00' gtd='2099-12-01' statementDate='2023-12-21'
      12/12/2023 3:32:00 PM Strategy 'AADelta3OneDirection/314488185': Cancelled OCO paired order: BarsInProgress=0, orderId='NT-00001-86' account='Playback101' name='Stop loss' orderState=Cancelled instrument='ES 03-24' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=4686.5 quantity=1 tif=Gtc oco='NT-00000-86' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2023-12-12 15:32:00' gtd='2099-12-01' statementDate='2023-12-21'
      Enabling NinjaScript strategy 'AADelta3OneDirection/314488185' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On price change IsUnmanaged=False MaxRestarts=4 in 5 minutes​

      Comment


        #18
        Forgot to show you how the part of the code looks like now

        Code:
            protected override void OnBarUpdate()
                {
        
                    if (BarsInProgress != 0)
                        return;
        
                    if (CurrentBars[0] < 1)
                        return;
        
                    if (BarsSinceEntryExecution() > -1)
                        return;
        
        
        
                     // Set 1
                    Print(string.Format("{0} | Delta11.Buys[0] > Zero+1000: {1} < Delta11.Sells[0] < Zero-1000: {2}", Time[0], Delta11.Buys[0] > Zero+1000, Delta11.Sells[0] < Zero-1000));
                    if (Delta11.Buys[0] > Zero+1000)
                    {
                        EnterLong(Convert.ToInt32(DefaultQuantity), "1");
                    }
        
                     // Set 2
        
                    if (Delta11.Sells[0] < Zero-1000)
        
                    {
                        EnterShort(Convert.ToInt32(DefaultQuantity), "1");
                    }
        
                }​

        Comment


          #19
          Hello Assalt,

          To save the output from the NinjaScript Output window to a text file, right-click the NinjaScript Output window, select Save As, click Save.
          Then you can attach the output text file to your post.


          It's good to see you are adding prints and seeing these in the output
          As an assist on using prints to understand behavior, you want to print each value compared in the condition, so that you can see WHY it is not evaluating as true.
          Within the action block of the condition, you can confirm with another print if the condition evaluated as true.
          Code:
          Print(string.Format("{0} " Delta11.Buys[0]: {1} > Zero: {2} + 1000: {3}", Time[0], Delta11.Buys[0], Zero, Zero+1000));
          if (Delta11.Buys[0] > Zero+1000)
          {
          Print(Time[0].ToString() + " Delta11.Buys greater than Zero+1000 condition true");
          EnterLong(Convert.ToInt32(DefaultQuantity), "1");
          }
          
          Print(string.Format("{0} " Delta11.Sells[0]: {1} < Zero: {2} - 1000: {3}", Time[0], Delta11.Sells[0], Zero, Zero-1000));
          if (Delta11.Sells[0] < Zero-1000)
          {
          EnterShort(Convert.ToInt32(DefaultQuantity), "1");
          Print(Time[0].ToString() + " Delta11.Buys greater than Zero+1000 condition true");
          }
          However, you stated the strategy is not placing any orders at all. I'm seeing in the output this is not the case, a buy a market order with the name "1" was submitted.
          12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: True < Delta11.Sells[0] < Zero-1000: False
          12/12/2023 3:32:00 PM Strategy 'AADelta3OneDirection/314488185': Entered internal SubmitOrderManaged() method at 12/12/2023 3:32:00 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='1' FromEntrySignal=''
          After the order named "1" was submitted, BarsSinceEntryExecution() is now greater than -1 and the script will return on all future OnBarUpdates and not process any further.​​
          Chelsea B.NinjaTrader Customer Service

          Comment


            #20
            I moved Print part of the code here:

            Code:
            protected override void OnBarUpdate()
                    {
            
                        if (BarsInProgress != 0)
                            return;
            
                        if (CurrentBars[0] < 1)
                            return;
            
                        Print(string.Format("{0} | Delta11.Buys[0] > Zero+1000: {1} < Delta11.Sells[0] < Zero-1000: {2}", Time[0], Delta11.Buys[0] > Zero+1000, Delta11.Sells[0] < Zero-1000));
                        if (BarsSinceEntryExecution() > -1)
                            return;
            
                        // Set 1
                        if (Delta11.Buys[0] > Zero+1000)
                        {
                            EnterLong(Convert.ToInt32(DefaultQuantity), "1");
                        }
            
                         // Set 2
            
                        if (Delta11.Sells[0] < Zero-1000)
            
                        {
                            EnterShort(Convert.ToInt32(DefaultQuantity), "1");
                        }​
            The strategy made the same thing - entered one time and then stopped executing the orders. Below I will provide the Print log to show where an order was executed and the part where all the entries were correct but an order wasn't executed.

            12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False & BarsSinceEntryExecution() > -1: False
            12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False & BarsSinceEntryExecution() > -1: False
            12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False & BarsSinceEntryExecution() > -1: False
            12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: True < Delta11.Sells[0] < Zero-1000: False & BarsSinceEntryExecution() > -1: False
            12/12/2023 3:31:12 PM Strategy 'AADelta3OneDirection/314488193': Entered internal SubmitOrderManaged() method at 12/12/2023 3:31:12 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='1' FromEntrySignal=''
            12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: True < Delta11.Sells[0] < Zero-1000: False & BarsSinceEntryExecution() > -1: True
            12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: True < Delta11.Sells[0] < Zero-1000: False & BarsSinceEntryExecution() > -1: True​
            ...
            12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: True < Delta11.Sells[0] < Zero-1000: False & BarsSinceEntryExecution() > -1: True
            12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: True < Delta11.Sells[0] < Zero-1000: False & BarsSinceEntryExecution() > -1: True
            12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: True < Delta11.Sells[0] < Zero-1000: False & BarsSinceEntryExecution() > -1: True
            12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False & BarsSinceEntryExecution() > -1: True
            12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False & BarsSinceEntryExecution() > -1: True
            12/12/2023 3:32:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False & BarsSinceEntryExecution() > -1: True
            ​...
            12/12/2023 3:40:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False & BarsSinceEntryExecution() > -1: True
            12/12/2023 3:40:00 PM | Delta11.Buys[0] > Zero+1000: False < Delta11.Sells[0] < Zero-1000: False & BarsSinceEntryExecution() > -1: True​

            As I understand - the value "BarsSinceEntryExecution() > -1: True" doesn't change to ​False. And this prevents a strategy to execute next orders. Please advice.

            Thank you.

            Comment


              #21
              Originally posted by NinjaTrader_ChelseaB View Post
              After the order named "1" was submitted, BarsSinceEntryExecution() is now greater than -1 and the script will return on all future OnBarUpdates and not process any further.​​
              Thank you Chelsea for the reply. Is there any solution for the strategy to process future orders?

              Thank you!
              Last edited by Assalt; 12-21-2023, 08:32 AM.

              Comment


                #22
                Hello Assalt,

                I'm not certain that I am understanding.

                Your inquiry is "How Do I Limit a Strategy Trade Entry to Only ONE Trade?".

                You want the strategy to place a single entry and then stop trading correct?

                This is why you've chosen to use the line:
                Code:
                if (BarsSinceEntryExecution() > -1)
                return;
                This condition means if an entry has been submitted and filled, causing ​BarsSinceEntryExecution() to become 0 which is greater than -1, then return and stop processing.

                Can you clarify your meaning on limiting a strategy trade entry to only one trade if you want more than one trade and for the strategy to continue processing future orders?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #23
                  Hello Chelsea,

                  Looks like you are right and I didn't understand the initial core of the topic. I was struggling to find a way to get my trading strategy to go in one trade on one candle. That is: if it entered a trade at Zero+1000, then exited at a profit on the same candle, and the Zero+1000 condition is still true - so that the strategy does not send another order for execution, but waits for the next candle. Given that this topic is not about that, then I have an initial question - is this even possible?

                  Separately, I would like to apologize for not understanding the essence of the discussion.​

                  Comment


                    #24
                    Hello Assalt,

                    Use a bool in the entry condition.

                    When IsFirstTickOfBar is true, reset the bool. When an order is placed set the bool.

                    Code:
                    private bool tradePlacedThisBar;
                    
                    if (IsFirstTickOfBar)
                    tradePlacedThisBar = false;
                    
                    if (tradePlacedThisBar == true)
                    return;
                    
                    if (Delta11.Buys[0] > Zero+1000)
                    {
                    tradePlacedThisBar = true;
                    EnterLong();
                    }
                    
                    if (Delta11.Sells[0] < Zero-1000)
                    {
                    tradePlacedThisBar = true;
                    EnterShort();
                    }
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #25
                      Hello Chelsea,

                      I thank you so much. Everything works perfect now. Thank you for your time and patience.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Jonafare, 12-06-2012, 03:48 PM
                      5 responses
                      3,985 views
                      0 likes
                      Last Post rene69851  
                      Started by Fitspressorest, Today, 01:38 PM
                      0 responses
                      2 views
                      0 likes
                      Last Post Fitspressorest  
                      Started by Jonker, Today, 01:19 PM
                      0 responses
                      2 views
                      0 likes
                      Last Post Jonker
                      by Jonker
                       
                      Started by futtrader, Today, 01:16 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post futtrader  
                      Started by Segwin, 05-07-2018, 02:15 PM
                      14 responses
                      1,792 views
                      0 likes
                      Last Post aligator  
                      Working...
                      X