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

Highest high and lowest low for time period

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

    Highest high and lowest low for time period

    The NinjaTrader Reference Samples (https://ninjatrader.com/support/foru...splay.php?f=30) are a great resource. Thanks!

    I have been looking at the reference sample entitled Indicator: Calculating the highest high or lowest low for a specified time range (https://ninjatrader.com/support/foru...ad.php?p=44911). I need this functionality for both NT7 and NT8. I am running it within a strategy (no plotting required).

    It seems the reference sample works, but it only covers a very small time period. I have tried to adapt it (fot NT7 initially) to cover much longer time periods (e.g. the past 12 months). I can easily calculate the required start/end DateTimes for the required periods. But I have encountered some strange interactions between the script and the Days2Load and BarsRequired settings and cannot get it to produce any meaningful results.

    For example, even if I set Days to load in the Strategy Grid UI to 120 and Min. bars required there also to 5000 (say), and also Maximum bars look back being Infinite, I find that GetBar does not seem to find any bars (returns 0).

    So, if my date range is start on 01-Jan-2018 and end on 14-Apr-2018 (a 103 day period), with 120 days loaded, BarsArray[0].Count is 25000 but GetBar returns zero.

    The code is:
    Code:
    		private int RangeOverTime(DateTime StartDT,
    								DateTime EndDT, string hdr)
    		{
    			if (EndDT <= StartDT)			// Start must be before end
    				return -1 ;
    			
    			int startBarsAgo = GetBar(StartDT) ;	// Number of bars ago for start time
    			int endBarsAgo   = GetBar(EndDT) ;		// Number of bars ago for end   time
    			
    			double highestHigh = MAX(High,startBarsAgo-endBarsAgo+1)[endBarsAgo] ;
    			double lowestLow = MIN(Low,startBarsAgo-endBarsAgo+1)[endBarsAgo] ;
    			
    			int rot = (int)Math.Round((highestHigh-lowestLow)*(Instrument.FullName.Contains("JPY") ? 100.0 : 10000.0)) ;
    			
    Print(hdr+": "+endBarsAgo.ToString().PadLeft(4)+" to "+startBarsAgo.ToString().PadLeft(4)+
    			"  Range from "+StartDT.ToString("ddd dd-MMM-yy")+" to "+EndDT.ToString("ddd dd-MMM-yy")+" is "+rot) ;
    
    			return rot ;
    		}
    Running this for a variety of date ranges using the settings mentioned above gives:
    Code:
    [FONT="Courier New"]Mon :    0 to    0  Range from Mon 09-Apr-18 to Tue 10-Apr-18 is 3
    Tue :    0 to    0  Range from Tue 10-Apr-18 to Wed 11-Apr-18 is 3
    Wed :    0 to    0  Range from Wed 11-Apr-18 to Thu 12-Apr-18 is 3
    Thu :    0 to    0  Range from Thu 12-Apr-18 to Fri 13-Apr-18 is 3
    Fri :    0 to    0  Range from Fri 13-Apr-18 to Sat 14-Apr-18 is 3
    WTD :    0 to    0  Range from Mon 09-Apr-18 to Sat 14-Apr-18 is 3
    LsWk:    0 to    0  Range from Mon 02-Apr-18 to Fri 06-Apr-18 is 3
    MTD :    0 to    0  Range from Sun 01-Apr-18 to Sat 14-Apr-18 is 3
    LsMn:    0 to    0  Range from Thu 01-Mar-18 to Sat 31-Mar-18 is 3
    YTD :    0 to 3872  Range from Mon 01-Jan-18 to Sat 14-Apr-18 is 392[/FONT]
    With Min. bars required set to 500, this:
    Code:
    [FONT="Courier New"]Mon :    0 to    0  Range from Mon 09-Apr-18 to Tue 10-Apr-18 is 4
    Tue :    0 to    0  Range from Tue 10-Apr-18 to Wed 11-Apr-18 is 4
    Wed :    0 to    0  Range from Wed 11-Apr-18 to Thu 12-Apr-18 is 4
    Thu :    0 to    0  Range from Thu 12-Apr-18 to Fri 13-Apr-18 is 4
    Fri :    0 to    0  Range from Fri 13-Apr-18 to Sat 14-Apr-18 is 4
    WTD :    0 to    0  Range from Mon 09-Apr-18 to Sat 14-Apr-18 is 4
    LsWk:    0 to    0  Range from Mon 02-Apr-18 to Fri 06-Apr-18 is 4
    MTD :    0 to    0  Range from Sun 01-Apr-18 to Sat 14-Apr-18 is 4
    LsMn:    0 to    0  Range from Thu 01-Mar-18 to Sat 31-Mar-18 is 4
    YTD :    0 to    0  Range from Mon 01-Jan-18 to Sat 14-Apr-18 is 4[/FONT]
    And with Min. bars required set to 10000 and Days to load is 365, this:
    Code:
    [FONT="Courier New"]Mon :    0 to    0  Range from Mon 09-Apr-18 to Tue 10-Apr-18 is 7
    Tue :    0 to    0  Range from Tue 10-Apr-18 to Wed 11-Apr-18 is 7
    Wed :    0 to    0  Range from Wed 11-Apr-18 to Thu 12-Apr-18 is 7
    Thu :    0 to    0  Range from Thu 12-Apr-18 to Fri 13-Apr-18 is 7
    Fri :    0 to    0  Range from Fri 13-Apr-18 to Sat 14-Apr-18 is 7
    WTD :    0 to    0  Range from Mon 09-Apr-18 to Sat 14-Apr-18 is 7
    LsWk:    0 to    0  Range from Mon 02-Apr-18 to Fri 06-Apr-18 is 7
    MTD :    0 to    0  Range from Sun 01-Apr-18 to Sat 14-Apr-18 is 7
    LsMn:    0 to    0  Range from Thu 01-Mar-18 to Sat 31-Mar-18 is 7
    YTD :    0 to    0  Range from Mon 01-Jan-18 to Sat 14-Apr-18 is 7[/FONT]
    Clearly, something is not as it should be, and I am not sure what. Advice most appreciated.

    Thanks.
    Last edited by jeronymite; 04-13-2018, 03:29 AM.
    Multi-Dimensional Managed Trading
    jeronymite
    NinjaTrader Ecosystem Vendor - Mizpah Software

    #2
    Update:

    If I run it as an Indicator (150 Days loaded), it seems to produce more interesting results, although I'm not sure if the results are correct:

    Code:
    [FONT="Courier New"]Mon : 5068 to 6482  Range from Mon 09-Apr-18 to Tue 10-Apr-18 is 113
    Tue : 3628 to 5068  Range from Tue 10-Apr-18 to Wed 11-Apr-18 is 136
    Wed : 2189 to 3628  Range from Wed 11-Apr-18 to Thu 12-Apr-18 is 96
    Thu :  754 to 2189  Range from Thu 12-Apr-18 to Fri 13-Apr-18 is 145
    Fri :    0 to  754  Range from Fri 13-Apr-18 to Sat 14-Apr-18 is 90
    WTD :    0 to 6482  Range from Mon 09-Apr-18 to Sat 14-Apr-18 is 237
    LsWk: 7919 to 13631  Range from Mon 02-Apr-18 to Fri 06-Apr-18 is 342
    MTD :    0 to 13631  Range from Sun 01-Apr-18 to Sat 14-Apr-18 is 352
    LsMn: 13632 to 45125  Range from Thu 01-Mar-18 to Sat 31-Mar-18 is 728
    YTD :    0 to 105133  Range from Mon 01-Jan-18 to Sat 14-Apr-18 is 1028
    [/FONT]
    ... and if they are, what needs to be done to get the correct results in a strategy?

    Thanks.
    Last edited by jeronymite; 04-13-2018, 03:38 AM.
    Multi-Dimensional Managed Trading
    jeronymite
    NinjaTrader Ecosystem Vendor - Mizpah Software

    Comment


      #3
      Hello jeronymite,

      Thank you for the post.

      Based on the description and details provided, I couldn't really say what may be happening but I can provide some direction on moving forward.

      GetBar specifically will return 0 If the time stamp passed in is older than the 1st bar, a bar index of 0 is returned. So this is possibly related, as I cannot see the whole scope of your test I would not rule this out as 0 is a default return value.

      The BarsRequired sets the minimum bars to wait before the script starts. By setting a high value, you would essentially give the script fewer bars to work with, a high value may push the start point past your begin/end times you have set.

      I cant see if you are using other Prints, but if you are not currently comparing the Time object, you may just double check that the loaded bars/times are not greater than your GetBar times.

      Outside of this observation, I would very likely need to see a more complete/simplified example of the problem along with a testing scenario to better understand why this may be happening. If you can provide a reduced sample that I can import and test, I could review the same situation as well to see if we get alike results.

      I look forward to being of further assistance.
      JesseNinjaTrader Customer Service

      Comment


        #4
        Highest high and lowest low for time period

        Thanks, Jesse.

        I have been able to update the code and confirm it works when used in an indicator.

        Since I can do this in an indicator (based on the NT sample code), what changes (generally speaking) are required to make similar capability available from a strategy?

        In other words, how would one adapt the SampleGetHighLowByTimeRange indicators (https://ninjatrader.com/support/foru...ad.php?p=44911) to work in a strategy? Can updated samples for the equivalent in a strategy in NT7 and NT8 be created, please?

        Thanks.
        Multi-Dimensional Managed Trading
        jeronymite
        NinjaTrader Ecosystem Vendor - Mizpah Software

        Comment


          #5
          Hello jeronymite,

          Thank you for the reply.

          In general, you can run most code that works in an indicator in a strategy, it would really depend on the scope of what you are making. This would not make a good sample to post as a strategy because technically for this to all be in a strategy that would require NT8 to also include the plotting. NT7 strategies rely on adding indicators if a visual plot is needed, so a question may be: do you need to see the plot or not?

          In case of the sample, for NT7 the plotting specifically would be a problem for a strategy so that would be something that would need to be removed. Outside of the plots, the remainder of the C# NinjaScript code could be copied over to a strategy and reused. If you need plots, it would be better to leave the code in an indicator and just call the indicator to access a value.

          If an indicator uses plots for data storage/calculations that can be converted to a DataSeries when used in a strategy. A DataSeries is basically a plot without a visual.


          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Highest high and lowest low for time period

            Thanks, Jesse.

            The essence of the code is to calculate the HH/LL data over the available dataseries. Plotting is really just one use of the data.

            So, I want to be able to generate that same data in a strategy or indicator, then use it as required in either case.

            Thanks.
            Multi-Dimensional Managed Trading
            jeronymite
            NinjaTrader Ecosystem Vendor - Mizpah Software

            Comment


              #7
              Hello jeronymite,

              Yes if the goal is to make reusable code that can be used in either case, likely an indicator is the best choice. This would allow you to have 1 code base and it could be used in either a strategy or visually on a chart. Calling indicators from a strategy should yield the same result as applying it to a chart. Because the strategy hosts the indicator you would also need to be certain the settings used are identical to the chart if you are comparing a manually applied indicator against a strategy value.

              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by ZenCortexAuCost, Today, 04:24 AM
              0 responses
              3 views
              0 likes
              Last Post ZenCortexAuCost  
              Started by ZenCortexAuCost, Today, 04:22 AM
              0 responses
              0 views
              0 likes
              Last Post ZenCortexAuCost  
              Started by SantoshXX, Today, 03:09 AM
              0 responses
              13 views
              0 likes
              Last Post SantoshXX  
              Started by DanielTynera, Today, 01:14 AM
              0 responses
              2 views
              0 likes
              Last Post DanielTynera  
              Started by yertle, 04-18-2024, 08:38 AM
              9 responses
              42 views
              0 likes
              Last Post yertle
              by yertle
               
              Working...
              X