Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Change Frequency of execution of Strategy or Indicator on the run

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

    Change Frequency of execution of Strategy or Indicator on the run

    Hi,

    Is it possible to switch the frequency of execution of a Strategy or Indicator programmatically during the OnBarUpdate(). For example, A strategy starts running with Calculate.OnBarClose and when some condition is met make the strategy execute on a tick by tick basis (Calculate.OnEachTick) and later on when some other condition is met make it execute again on eacho bar close (Calculate.OnBarClose) ?
    What would be the outcome ? Is this viable ?

    Thank you.

    Anthony

    #2
    Hello Anthony_0709,

    Thank you for your post.

    Yes, logic can be separated between Calculate.OnEachTick and Calculate.OnBarClose using IsFirstTickOfBar. Please note that a hosted script will inherit the Calculate mode of the script that hosts it. You can take the following approach to differentiate logic between OnBarClose and OnEachTick processing.

    Please see this reference sample which demonstrates a technique used for those who need to separate their logic to calculate some values on each tick and others only on the close of a bar. You will set your host script to Calculate.OnEachTick and use if(IsFirstTickOfBar) and place all code that needs to calculate once every bar within that condition check. Then place all code that you want to calculate OnEachTick outside of the IsFirstTickOfBar condition check.

    SampleEnterOnceExitEveryTick -https://ninjatrader.com/support/helpGuides/nt8/?separating_logic_to_either_cal.htm

    Please let us know if we may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandon H.,

      Thank you for your prompt response, but you didn't answer my question(s). Please read carefully as what I'm trying to work on is a DIFFERENT approach to "SampleEnterOnceExitEveryTick".
      I've read and downloaded "SampleEnterOnceExitEveryTick" and can see that IT IS TICK INTENSIVE. What I would like to do is make the same strategy be fired sometimes "OnBarClose" and other times "onEachTick". This approach isn't 100% TICK INTENSIVE. So please read again. I know that you guys are very busy...

      Thank you.
      Anthony

      Comment


        #4
        Hello Anthony_0709,

        Thank you for that information.

        It is not possible to change the Calculate Property on the fly, so if you need to filter code between OnBarClose and OnEachTick, you must use the IsFirstTickOfBar property while running the script with Calculate.OnEackTick. This way your custom code can have the option to run on either calculate setting. Although the OnBarUpdate method will run on every tick, the actual code that will run is being limited by this logic separation.

        IsFirstTickOfBar - https://ninjatrader.com/support/help...ttickofbar.htm

        Please let us know if we may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Hi Brandon,

          I have a relevant question. Is there a way to switch between Calculate.OnBarClose and Calculate.OnPriceChange within a strategy? I have a strategy that's working with Calculate.OnBarClose and the way it works is the strategy looks for a crossing up/crossing down on an indicator I use which is used as a trigger and it sets a private variable to true. Once triggered the strategy waits until the price is below 8ema and variable (for the trigger as true) in an if statement to enter a position. After a position is entered it resets the variable for a trigger.

          What I want to have is have a trigger for crossing up/down on an indicator with Calculate.OnBarClose and once its triggered I want to enter a position with Close[0] < EMA8[0] using Calculate.OnPriceChange. Once the position is entered it'll go back to OnBarClose. Is that possible to do?

          Here is the code snippet for long entry that I have using OnBarClose

          private ATRTrailingStop ATRTrailingStop4;
          private EMA EMA8;

          State.DataLoaded -
          ATRTrailingStop4 = ATRTrailingStop(Close, 10, 3);
          EMA8 = EMA(Close,8);

          long trigger -
          if (CrossAbove(Close, ATRTrailingStop4, 1)
          && Times[0][0].TimeOfDay > new TimeSpan(6, 30, 0)
          && Times[0][0].TimeOfDay < new TimeSpan(12, 53, 0))
          isReadyForLong = true;


          long entry -
          if (Close[0] < EMA8[0] && Close[0] > ATRTrailingStop4[0] && isReadyForLong
          && Times[0][0].TimeOfDay > new TimeSpan(6, 30, 0)
          && Times[0][0].TimeOfDay < new TimeSpan(12, 57, 0))
          EnterLong("Lot1");


          Right now both trigger and entry are done using Calculate.OnBarClose specified in State == State.SetDefaults but I wanted to check if its possible to trigger using OnBarClose, change OnBarClose to OnPriceChange after the trigger until entry and once position is entered switch back to OnBarClose for future triggers

          Thanks

          Comment


            #6
            Originally posted by vpatanka View Post
            I have a relevant question. Is there a way to switch between Calculate.OnBarClose and Calculate.OnPriceChange within a strategy? I have a strategy that's working with Calculate.OnBarClose and the way it works is the strategy looks for a crossing up/crossing down on an indicator I use which is used as a trigger and it sets a private variable to true. Once triggered the strategy waits until the price is below 8ema and variable (for the trigger as true) in an if statement to enter a position. After a position is entered it resets the variable for a trigger.

            What I want to have is have a trigger for crossing up/down on an indicator with Calculate.OnBarClose and once its triggered I want to enter a position with Close[0] < EMA8[0] using Calculate.OnPriceChange. Once the position is entered it'll go back to OnBarClose. Is that possible to do?
            Yes, it is possible, but not by using Calculate.

            At least not directly. Leave Calculate set to
            OnPriceChange, and then don't touch it.

            You will need to design your own logic, and when you
            want your logic to work in the OnBarClose mode, you
            design a special variable to know that -- and when you
            want OnPriceChange, your special variable tells you
            that, too.

            Code:
            private bool MyOnBarClose = true;
            Then check for IsFirstTickOfBar, something like this,

            Code:
            protected override void OnBarUpdate()
            {
                if (MyOnBarClose)
                {
                    if (IsFirstTickOfBar)
                    {
                        ... a bar has just closed ...
                        ... when the logic is right ...
                        MyOnBarClose = false;
                    }
                }
                else
                {
                    ... price has changed ...
                    ... when the logic is right ...
                    MyOnBarClose = true;
                }
            }
            You'll have to fill in the rest, but to alternate between
            both modes, like you want, something like the above
            skeleton code is the correct way to do it.

            Make sense?

            Comment


              #7
              Originally posted by bltdavid View Post

              Yes, it is possible, but not by using Calculate.

              At least not directly. Leave Calculate set to
              OnPriceChange, and then don't touch it.

              You will need to design your own logic, and when you
              want your logic to work in the OnBarClose mode, you
              design a special variable to know that -- and when you
              want OnPriceChange, your special variable tells you
              that, too.

              Code:
              private bool MyOnBarClose = true;
              Then check for IsFirstTickOfBar, something like this,

              Code:
              protected override void OnBarUpdate()
              {
              if (MyOnBarClose)
              {
              if (IsFirstTickOfBar)
              {
              ... a bar has just closed ...
              ... when the logic is right ...
              MyOnBarClose = false;
              }
              }
              else
              {
              ... price has changed ...
              ... when the logic is right ...
              MyOnBarClose = true;
              }
              }
              You'll have to fill in the rest, but to alternate between
              both modes, like you want, something like the above
              skeleton code is the correct way to do it.

              Make sense?
              That does make sense. I am going to give it a try and will let you know how that goes. Thank you so much !

              Comment


                #8
                Hello vpatanka,

                Thanks for your note.

                bltdavid is correct. You would not be able to switch between Calculate.OnBarClose and Calculate.OnPriceChange within a strategy by using Calculate.

                If you need to filter code between OnBarClose and OnPriceChange, you must use the IsFirstTickOfBar property while running the script with Calculate.OnPriceChange.

                Although the OnBarUpdate method will run on every price change, the actual code that will run is being limited by this logic separation.

                Let us know if we may assist further.
                Brandon H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by gravdigaz6, Today, 11:40 PM
                0 responses
                0 views
                0 likes
                Last Post gravdigaz6  
                Started by MarianApalaghiei, Today, 10:49 PM
                3 responses
                9 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by XXtrader, Today, 11:30 PM
                0 responses
                3 views
                0 likes
                Last Post XXtrader  
                Started by love2code2trade, Yesterday, 01:45 PM
                4 responses
                28 views
                0 likes
                Last Post love2code2trade  
                Started by funk10101, Today, 09:43 PM
                0 responses
                9 views
                0 likes
                Last Post funk10101  
                Working...
                X