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

ATR-Multitimeframe Plot Problem

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

    ATR-Multitimeframe Plot Problem

    hi everybody, i have a strategy in progress with atr calculations and plot to chart. following scenario:

    picture left NQ in 2 minutes timeframe and right NQ in 4 tick unirenko, the script runs on the unirenko 4 tick chart. after a while in the unirenko 4 tick chart the lines are not displayed correctly. everything should always look the same as on the left side of the 2 minute chart. of course the lines are not exactly the same in both charts, because on the left is the 2 minute timeframe and on the right the 4 tick unirenko chart. it seems that the line is calculated correctly on 2 minutes, but is drawn too fast in the 4 tick chart. i've marked it in yellow on the image where it's out of sync.
    When I reload the script with F12, the line is displayed correctly and synchronously on both charts again, for some time. the goal is to have a line on the 4 tick unirenko chart that is calculated from the 2 minute timeframe and also drawn synchronously as in the 2 minute chart. please look at the pictures, the first image is before reloading the script with F12 and the second image after.

    Can anyone help me with this, please?

    public class RenkoTrend : Strategy
    {
    private double atrtrailHTF;


    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Calculate = Calculate.OnEachTick;
    ATRPeriodHTF = 7;
    MultiplikatorHTF = 2.3;
    }

    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 2)
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < BarsRequiredToTrade)
    return;

    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1 || CurrentBars[1] < 1 || CurrentBars[2] < 1 || CurrentBars[3] < 1)
    return;



    double atrlossHTF = ATR(Closes[3], ATRPeriodHTF)[0] * MultiplikatorHTF;

    if (Closes[3][0] > ATRTrendHTF[1] && Closes[3][1] > ATRTrendHTF[1])
    atrtrailHTF = Math.Max(ATRTrendHTF[1], Closes[3][0] - atrlossHTF);

    else if (Closes[3][0] < ATRTrendHTF[1] && Closes[3][1] < ATRTrendHTF[1])
    atrtrailHTF = Math.Min(ATRTrendHTF[1], Closes[3][0] + atrlossHTF);

    else if (Closes[3][0] > ATRTrendHTF[1])
    {
    atrtrailHTF = Closes[3][0] - atrlossHTF;
    }

    else
    {
    atrtrailHTF = Closes[3][0] + atrlossHTF;
    }

    if (showATRTrendHTF)
    {
    ATRTrendHTF[0] = atrtrailHTF;
    }
    }
    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="ATRPeriod", Description="ATR Period", Order=19, GroupName="2. Indikatoren")]
    public int ATRPeriod
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, double.MaxValue)]
    [Display(Name="Multiplikator", Description="ATR multiplication", Order=20, GroupName="2. Indikatoren")]
    public double Multiplikator
    { get; set; }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> ATRTrendHTF
    {
    get { return Values[4]; }
    }
    #endregion

    }
    }
    Last edited by sidlercom80; 01-22-2020, 01:46 PM.
    sidlercom80
    NinjaTrader Ecosystem Vendor - Sidi Trading

    #2
    Hello sidlercom80,

    Thanks for your post.

    Reloading NinjaScripts will effectively cause the script to reload, process the historical data, and then continue processing the new realtime data. Historical data is always processed following Calculate.OnBarClose while realtime data will use the configured Calculate mode, which you have set to OnEachTick.

    It sounds like your script is placing drawings which you do not expect when it is processing realtime data, but the results are what you expect when that data is processed historically following OnBarClose. If you would like to have the drawings follow the OnBarClose behavior when using Calculate.OnEachTick or Calculate.OnPriceChange, you can place your logic in a check for IsFirstTickOfBar. Note that you will want to reference the previous bar (BarsAgo 1, I.E. Close[1]) within IsFirstTickOfBar to make the same reference as Close[0] with Calculate.OnBarClose.

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

    You can use the Playback Connection to simulate realtime data when testing and debugging.

    Playback - https://ninjatrader.com/support/help...connection.htm

    Please let us know if we can be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi, sidlercom80.
      The reason is that your indicator works OnEachTick. And 2 Mins line constantly repaints. On left chart you see line in the very last point, while smaller timeframe shows you all historical intrabar fluctuations.

      There are at least 3 ways out:
      1. [simplest] Switch to OnBarClose.
      2. [will plot wit delay of 1 bar] Set values of completed 2 Min bar only ( Closes[1][1] ).
      3. [most tricky] You need to remember where 2 Min bar starts and repaint plot on all small timeframe bars, that corresponds to non-completed 2 Min bar.
      fx.practic
      NinjaTrader Ecosystem Vendor - fx.practic

      Comment


        #4
        Thank you very much for your answers NinjaTrader_Jim and fx.practic!
        i have loaded this part of my strategy into an indicator for testing. as a higher timeframe i use here 5 minutes. on the left side is the 5 minute chart on which the calculations for the indicator should run. on the right side is the 1 minute chart and the 4 tick unirenko chart. as you can see very well on the pictures, the 1 minute chart is displayed correctly and on the 4 tick unirenko chart the lines are not correct anymore. the value of the line is shown on the 5min chart on the bottom right and on the 1min and unirenko on the bottom left.
        i am aware that on a smaller timeframe more lines are drawn. the goal should be to draw the line in the smaller timeframe only when the price and therefore the line in the larger timeframe changes. I have also added the whole indicator script as an image. someone can give me the decisive tip, that would be great.

        very probably I will not see the forest with so many trees anymore ;-)
        sidlercom80
        NinjaTrader Ecosystem Vendor - Sidi Trading

        Comment


          #5
          Hello sidlercom80,

          The indicator logic is being processed for the primary data series, so if we change the chart that the indicator is applied to, we will likely come across differences in behavior.

          I may then suggest either of the following approaches.

          1. Check for these conditions within a BarsInProgress check for the data series you would like these drawing based on. Call your Draw methods within this BarsInProgress check so they are drawn consistently between different time frames. (This would be the most straight forward approach but you may need to consider the below if this does not give you the behavior you are looking for.)

          2. Check for these conditions within a BarsInProgress check for the data series you would like them based on. Instead of calling your Draw methods here, set private variables describing how the drawing should be made. Then within your BarsInProgress == 0 logic, call your drawing methods using the information from your private variables.

          More information on differentiating logic for different processing data series can be found below. Keep in mind, if you have if (BarsInProgress != 0) return; at the beginning of OnBarUpdate, any later checks for if (BarsInProgress == 1){ } will not be reached.

          https://ninjatrader.com/support/help...arupdateMethod

          We look forward to being of further assistance.
          JimNinjaTrader Customer Service

          Comment


            #6
            Hi _Jim many thanks for the answer! I've adjusted the code now so that it works great. if the calculations run on minutes timeframe, i can change the chart to 1/2/3/4/5/10 or arbitrarily and the line is displayed correctly, but as soon as i change from minutes to a non-timeframe chart, there are deviations. this is probably because the renko bars themselves still have to be calculated and therefore there can be slight deviations., or is there also a solution?
            sidlercom80
            NinjaTrader Ecosystem Vendor - Sidi Trading

            Comment


              #7
              Hello sidlercom80,

              The logic is now processing for all added data series. As one data series is tick based and the other is time based, you will see differences in behavior for the reason you had mentioned.

              My suggestions from post 5 would be to check for these conditions on a specific BarsInProgress for a specific data series that is added to the script and then to either:

              1. Draw based on these calculations within this BarsInProgress check alone. (Don't draw on other data series)
              2. Assign values to private variables and then to call your Draw methods within a check for BarsInProgress == 0. (This would result in behavior that the script will calculate what should be drawn in one data series, and then draw them on the primary data series.)

              I look forward to assisting.
              JimNinjaTrader Customer Service

              Comment


                #8
                thank you very much for the answer, I will try it!
                sidlercom80
                NinjaTrader Ecosystem Vendor - Sidi Trading

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by ScottWalsh, Today, 04:29 PM
                0 responses
                4 views
                0 likes
                Last Post ScottWalsh  
                Started by rtwave, 04-12-2024, 09:30 AM
                2 responses
                21 views
                0 likes
                Last Post rtwave
                by rtwave
                 
                Started by tsantospinto, 04-12-2024, 07:04 PM
                5 responses
                69 views
                0 likes
                Last Post tsantospinto  
                Started by cre8able, Today, 03:20 PM
                0 responses
                7 views
                0 likes
                Last Post cre8able  
                Started by Fran888, 02-16-2024, 10:48 AM
                3 responses
                49 views
                0 likes
                Last Post Sam2515
                by Sam2515
                 
                Working...
                X