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

disabling strategy after 1 trade

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

    disabling strategy after 1 trade

    Hello,

    I have multiple execution signals in a strategy (if condition a = true then buy. if condition b = true then buy). Which ever condition execute the buy and after the trade is closed, I would like the strategy to not execution any more trades until the following trading session. Is this possible?

    Thanks.

    #2
    Hello AdeptistJune,

    Yes that is possible. You would need to make a bool variable and add that as part of both conditions.
    1. Make a bool variable with a default value of true.
    2. if either condition becomes true set the variable to false.
    3. In both conditions check if the variable is true as part of the condition to trade.
    You can then use IsFirstBarOfSession to reset the variable to true to allow trading again.



    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello,

      Are there some examples in the forum. I'm a bit confused how to syntax the code.

      Comment


        #4
        Hello AdeptistJune,

        A simple example of what I was referring to would be:
        Code:
        private bool allowEntry = true; 
        
        protected override void OnBarUpdate()
        {
            if (Bars.IsFirstBarOfSession)
            { 
                allowEntry = true;
            }
            if(conditionA && allowEntry)
            {  
        
                allowEntry = false; 
            }
        }
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello Jesse,

          Thanks. I think I understand how to do it. If I want to have control of the number of trades, do I use a counter?
          Last edited by AdeptistJune; 02-08-2022, 03:27 PM.

          Comment


            #6
            Hello, Jesse,

            Here is the print statement. The prints are showing multiple fist bar processed of the session at multiple time throughout the session.
            Attached Files

            Comment


              #7
              Hello AdeptistJune,

              I am not sure what this output represents.
              I would need to see what print you are using, where that was used and what the expectation was around that print to better understand what the output means.


              JesseNinjaTrader Customer Service

              Comment


                #8
                Good morning Jesse,

                Here is what the code looks like:

                private bool allowEntry = true;
                protected override void OnBarUpdate()
                {
                if (CurrentBars[0] < BarsRequiredToTrade || CurrentBars[1] <= BarsRequiredToTrade || CurrentBars[2] <= BarsRequiredToTrade)
                return;
                if (Bars.IsFirstBarOfSession)
                {
                allowEntry = true;
                }
                // Print the current bar number of the first bar processed for each session on a chart
                if (Bars.IsFirstBarOfSession)
                Print(string.Format("Bar number {0} was the first bar processed of the session at {1}.", CurrentBar, Time[0]));
                // All trades within this context execute on a Primary chart
                //================================================== ========
                if (BarsInProgress == 0)
                {
                if ((Position.MarketPosition == MarketPosition.Flat)
                && (Times[0][0].TimeOfDay >= new TimeSpan(06, 00, 00) && Times[0][0].TimeOfDay < new TimeSpan(15, 00, 00))
                && (EMA1[0] > VWAP81[0])
                && (High[0] < EMA1[0])
                && allowEntry == true
                )
                {
                Print(string.Format("{0} | BarsInProgress: {1}", Time[0], BarsInProgress));
                Print(Time[0] + " | Set1" );
                EnterShort(1, @"Set1");
                allowEntry = false;
                }
                }
                }

                Comment


                  #9
                  Helllo AdeptistJune,

                  If you are using multiple series then you ill see that property become true for each series. If you are trying to print the IsFirstBarOfSession for a specific series that print would need to go inside oe of your BarsInProgress conditions.

                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Hello Jesse,

                    Is it possible to control the number of trades allowed before the strategy is switched off until the next session?

                    Thanks.

                    Comment


                      #11
                      Hello AdeptistJune,

                      Yes you could use logic to do that.

                      You can use IsFirstBarOfSession to make a condition which happens on the first bar of each session. That can be used to reset a variable.


                      You could use a simple int variable to keep count and increment it each time you submit an order.

                      Code:
                      private int tradeCount = 0; 
                      
                      protected override void OnBarUpdate()
                      {
                          if(someCondition && tradeCount < 3)
                          { 
                              EnterLong(); 
                              tradeCount++;
                          }
                      }
                      Once the trade count surpasses the condition of 3 then the entry condition stops working. In a seperate condition you would reset the variable using IsFirstBarOfSession.



                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        Hello, Jesse,

                        I have a question: Why is the tradecount++ in the scope of the if (someCondition && tradeCount < 3)? The code works perfectly. I'm just curious of what the code is saying.

                        Thanks.

                        Comment


                          #13
                          Hello AdeptistJune,

                          The tradecount++ means to increment the variable by 1 so that is within the scope of if (someCondition && tradeCount < 3) to limit the entry and to avoid endless counting up when its not relevant.

                          For each time the entry condition is true the trade count is incremented if (someCondition && tradeCount < 3) tradeCount++;

                          As long as the trade count is less than the number in the condition if (someCondition && tradeCount < 3) then it can continue to be incremented along with whatever other actions are present within the entry condition.


                          JesseNinjaTrader Customer Service

                          Comment


                            #14
                            so from what I understand it should be like this..
                            if All conditions are met; Bool=True
                            Do the following: Bool=False

                            1. Should I add the bools on exits as well or only on the entries tabs?
                            2. the actual number of trades should be entered on Script only?

                            Comment


                              #15
                              Hello rafael_delima86,

                              The bool sample I previously provided is just a base sample that shows how to use a class level variable and how to reset it. That controls the entry order, you very likely don't want to involve that variable with exit conditions so that they sure to work. Exit conditions usually use the Market Position to determine if they work or not. For example, if you are Short then use the short exit methods, If you are flat don't call the exit method, etc.

                              The number of trades sample has hard coded values to make a simple example. If you wanted to make the number of trades variable you could add a user input and use that in place of the 3 in the example.
                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Jonafare, 12-06-2012, 03:48 PM
                              5 responses
                              3,986 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
                              8 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