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

Prior days high/low

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

    Prior days high/low

    What's the best way to get the highs and lows of the last "x" days within a strategy? For instance:

    High5 = Highest price 5 days ago
    Low5 = Lowest price 5 days ago
    High 4 = ...
    Low 4 = ...

    (or more likely I'd load into an array, but you get the idea).

    The strategy will be an intraday strategy - perhaps a 2000 tick or 5 minute - but that doesn't really matter. I was thinking that perhaps I also need to use AddDataSeries and add a separate daily series, but I'm not sure how to get these historical values within the strategy. Any help is appreciated.

    Thanks.


    #2
    Hi, thanks for posting.

    Use AddDataSeries to add a daily series and you can calculate highest high/ lowest low using MIN and MAX:

    Code:
    //in OnStateChanged:
    else if (State == State.Configure)
    {
        AddDataSeries(BarsPeriodType.Day, 1); //adds a daily series of the primary instrument
    }
    
    //inOnBarUpdate:
    if(BarsInProgress == 0) //OnBarUpdate is called for the primary (chart) bar context
    {
        //primary series
    }
    
    if(BarsInProgress == 1 && CurrentBars[1] > 5) //OnBarUpdate is called for the Daily bar context
    {
        var highestDailyHigh = MAX(Highs[1], 5)[0]; //Maximum number in the Highs[1] array
        Print(highestDailyHigh);
    
        var lowestDailyLow = MIN(Lows[1], 5)[0]; //Minimum number in the Lows[1] array
    }
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks Chris. I tested this out, but it's not quite what I need. And I have some more questions/concerns below. I added some print statements as a test:

      Code:
      if (BarsInProgress == 1){
           Print ("CurrentBars: " + CurrentBars[1]);
           if (CurrentBars[1] < 5){
                Print (Time[0]);
                Print ("High Days Ago " + CurrentBars[1] + ": " + MAX(Highs[1],5)[0]);
           }
      }
      else if (BarsInProgress == 0){
           Print ("Time of primary series: " + Time[0]);
      }
      I ran this for 1/3/2021 - 10/21/2021. What I noticed is that I get "Time of primary series" for every 2000 ticks (my primary series), and then at 5:00 PM I get the "High Days Ago" print statement. I also didn't get the "High Days Ago" statement on 1/3/2021. It started on 1/4.

      What I really want is to get the highs and lows of the prior 5 days PRIOR to processing the current day on the primary series. What I'm trying to do is get the average of the (High minus Low) for the previous 5 days, and then use that average during my daily processing. I also don't want to cut it off at 5:00 PM. I want the high and low for the entire day.

      So, on 1/3/2021, I would get the Highs and Lows of the previous 5 trading days, calculate my average and use it during the 1/3/2021 processing. Then, at 12:01 AM on 1/4/2021 (or the first bar processed on 1/4), recalculate the average based on the previous 5 days (so 1/3 would now be included). Then use that average for 1/4/2021 processing.

      I also noticed that when I add the second data series, I can no longer use the High Fill Order Resolution in the Strategy Analyzer. That's going to be a problem too, since I use that for better testing.

      It's one thing to run this routine in the Analyzer too, versus running it in real time against either a live account or sim account. In the Analyzer I'm entering a starting date in the past. I could always start the Analyzer 5 days earlier and calculate my highs and lows by rolling-forward (remove the 5th day prior and add yesterday). I can do this without adding a second data series too. I could just keep track of the highest high and lowest low as I loop through the bars. However, when I'm running this in real time, I'm assuming I can't start 5 days in the past, so I'm not sure about the "proper" way to do this that solves all my issues.

      Thanks for the help.






      Comment


        #4
        Hi sjeffblack , thanks for your reply.

        If you need custom code a developer may be hired from the NinjaTrader Ecosystem website. Just search for "NinjaTrader Ecosystem" through Google and on the sight there is a "Programmers" section that will be able to work in further detail than the support team will be able to do. Modifying to suit your needs is something you or a developer will need to do as the support team will not be able to get into further customizations. Please also see this guide on how multi time frame scripts work in the platform:
        https://ninjatrader.com/support/help...nstruments.htm

        Since you are now adding a 1 day series the high-resolution series can not be used through the analyzer, the strategy now needs to add a 1 tick series (or the series you were using in the high-resolution series) with AddDataSeries as well. We have a full example here:
        https://ninjatrader.com/support/help...ipt_strate.htm

        Best regards,
        -ChrisL
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thanks Chris. I don't need a custom developer. I am a software developer with about 25 years experience. It's not a matter of not understanding the coding. It's a matter of not fully understanding how certain Ninjascript objects are used/executed (e.g. how to get the daily series to execute prior to the intraday series).

          I'm relatively new to Ninjatrader though, so I know there's a lot to learn. I'll review your links.

          Thanks.

          Comment


            #6
            Hi sjeffblack, thanks for your reply.

            The secondary bar is always processed after the primary series. The alternative of this approach is to use only the primary series and capture the highest price as the historical data run is performed.

            Code:
            private double highestprice = -1;
            //in OnBarUpdate:
            if(Bars.IsFirstBarOfSession)
            {
                highestprice = -1;
            }
            
            if(High[0] > highestprice)
            {
                highestprice = High[0];
            }
            Some additional logic will be needed to capture the prior 5 days. Use a List<double> to make this easier. Also see the SessionIterator:

            https://ninjatrader.com/support/help...oniterator.htm

            Best regards,
            -ChrisL
            Chris L.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cmv727, Today, 05:38 AM
            0 responses
            13 views
            0 likes
            Last Post cmv727
            by cmv727
             
            Started by AlessioCianini12, Today, 01:38 AM
            0 responses
            34 views
            0 likes
            Last Post AlessioCianini12  
            Started by nandhumca, Yesterday, 11:54 PM
            0 responses
            31 views
            0 likes
            Last Post nandhumca  
            Started by nandhumca, Yesterday, 11:51 PM
            0 responses
            28 views
            0 likes
            Last Post nandhumca  
            Started by ezrollin, Yesterday, 11:48 PM
            2 responses
            31 views
            0 likes
            Last Post ezrollin  
            Working...
            X