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

Using Calculate.OnEachTick on Longer Term charts

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

    Using Calculate.OnEachTick on Longer Term charts

    How do I set up a strategy to enter long or short or send an alert or whatever, on an indicator crossover say, when calculate.OnEachTick is on?
    For instance, if (CrossAbove(EMA(5),EMA(8),1)
    {
    EnterLong();
    }
    How do I implement such a strategy on say a 5-minute chart or 10 minute chart without having multiple entries as price breathes back and forth, tick by tick?
    How do I get it to execute only once?
    I don't want to use IsFirstTickOfBar either, cos I don't want to wait for a new bar to print before entry. I want it to strictly follow the indicators and enter only once, the first time it is true.
    Thanks.

    #2
    Hello aaadetos,

    Thanks for your post.

    You would need to add a condition to your entry conditions to prevent entering additional orders on each tick. As you have observed when using calculate.OnEachTick it is possible that the crossover entry can occur repeatedly as price oscillates within the bar.

    Once the cross condition has become true, the first time, save the CurrentBar number into a variable and add the check of the CurrentBar to the variable as part of your entry condition. This would limit orders to 1 per bar while the crossover conditions is true.

    For example:

    if (CrossAbove(EMA(5),EMA(8),1) && CurrentBar != savedBar)
    {
    EnterLong();
    savedBar = CurrentBar; // on the first entry, saves the bar number to prevent subsequent re-entry until next bar at the earliest.
    }

    CurrentBar is a system int variable that holds the "count" of the current bar being formed (in this case of Calculate.OnEachTick). savedBar is an int type variable that you would need to create/declare.



    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks Paul. I attached the conditions to both the entry and exit rules, but on trying to test it live (rather than on the backtest which calculates on bar close), it opens and closes at the same price level. What am I doing wrong? Thanks again.

      Comment


        #4
        Hello aaadetos,

        Thanks for your reply.

        Based on the information provided I could not advise why your exit is occurring on the same bar. As we have talked about using an int variable to save the current bar number in an entry condition and if you used that same variable in other conditions that might be something to look at as you would want to use different variables for each use (so if you have both a long and short entry that would be two variables, if you have multiple exit conditions than use a new variable for each).

        In general though if a script (strategy or indicator) is not working as expected and there are no errors in the Log tab of the control center then the next step would be to debug the script typically done by using Print statements to help understand the value of variables and the determined state of the conditions. We have assembled some debugging tips here: https://ninjatrader.com/support/help...script_cod.htm
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Thanks Paul. I apologize for not updating this sooner. It actually works in live testing.
          For some reason, when one first enables it, the historical data seems to open and close at the same price level; but it didn't do this as data came in...
          Thanks again!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by judysamnt7, 03-13-2023, 09:11 AM
          4 responses
          59 views
          0 likes
          Last Post DynamicTest  
          Started by ScottWalsh, Today, 06:52 PM
          4 responses
          36 views
          0 likes
          Last Post ScottWalsh  
          Started by olisav57, Today, 07:39 PM
          0 responses
          7 views
          0 likes
          Last Post olisav57  
          Started by trilliantrader, Today, 03:01 PM
          2 responses
          21 views
          0 likes
          Last Post helpwanted  
          Started by cre8able, Today, 07:24 PM
          0 responses
          10 views
          0 likes
          Last Post cre8able  
          Working...
          X