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

Establishing high and low values from a specific time frame. How do i avoid 0 values?

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

    Establishing high and low values from a specific time frame. How do i avoid 0 values?

    Is there a function i am unaware of that takes in two ninjatrader times, and returns the high and low from that time frame? I would like to make one, but i think i have something logically wrong here anyways. Any pointers would help.

    I need to find a way to not set the values im testing in my if statements to zero every time OnBarUpdate() is called. The orH, and orL variables are the ones im referring to specifically, the others work fine since the functions used to assign them returns the correct value on any bar.

    Im thinking i will have to reference the bars that occurred during this time frame each time using some sort of indexing each time the OnBarUpdate() function is called.

    Code:
    protected override void OnBarUpdate()
            {
    			double orH = 0, orL = 0, openRH, openRL, yH = 0, yL = 0;
    			int index;
    			
    			if(ToTime(Time[0]) >= 160000 && ToTime(Time[0]) < 180000)
    			{	
    				if(High[0] > orH)
    				{
    					orH = High[0];
    					openRH = orH;
    				}
    				if(Low[0] < orL)
    				{
    					orL = Low[0];
    					openRL = orL;
    				}
    			}
    			
    			if((PriorDayOHLC().PriorLow[0]) != 0)
    			{
    				yL = PriorDayOHLC().PriorLow[0];
    				yH = PriorDayOHLC().PriorHigh[0];
    			}
    			
                ORhigh.Set(orH);
                ORlow.Set(orL);
                Yh.Set(yH);
                Yl.Set(yL);
            }

    #2
    Hi Riley, for the task I would suggest you take a look at this reference sample here - http://www.ninjatrader.com/support/f...ead.php?t=8600
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thanks! I finished it up and it works just like i wanted here.

      Code:
      #region Variables
      		
      			private int startHour = 16; // Default setting for StartHour
                  private int startMinute = 0; // Default setting for StartMinute
                  private int endHour = 18; // Default setting for EndHour
                  private int endMinute = 0; // Default setting for EndMinute
              
              #endregion
      
              /// <summary>
              /// This method is used to configure the indicator and is called once before any bar data is loaded.
              /// </summary>
              protected override void Initialize()
              {
                  Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "ORhigh"));
      			CalculateOnBarClose = true;
                  Add(new Plot(Color.FromKnownColor(KnownColor.Blue), PlotStyle.Line, "ORlow"));
      			CalculateOnBarClose = true;
                  Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Yh"));
      			CalculateOnBarClose = true;
                  Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Yl"));
      			CalculateOnBarClose = true;
                  Overlay				= true;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
      		
              protected override void OnBarUpdate()
              {
                  // Use this method for calculating your indicator values. Assign a value to each
                  // plot below by replacing 'Close[0]' with your own formula.
      			
      			double high, low, yH, yL; // declare variables for plotted values
      			int year, month, day, Today; // declare variables for getting the current days
      			int indexHigh = 0, indexLow = 0, range = 0; // declare index values for bar positions during time period
      			
      			Today = ToDay(Time[0]); // stores date in YYYYMMDD format as an int in "Today"
      
      			year = Today / 10000; // get the year
      			Today = Today - year * 10000; // reformat today to show only MMDD
      			month = Today / 100; // get the month (MM)		
      			day = Today - month * 100; // get the day
      			
      			// If passed midnight, but same forex session, go back a day
      			if(ToTime(Time[0]) >= ToTime(00, 00, 00) && ToTime(Time[0]) <= ToTime(startHour, startMinute, 00))
      			{
      				day -= 1;
      			}
      			
      			// if the time is not between 4pm and 6pm, find the bar index for bar at 4PM and 6PM
      			if(ToTime(Time[0]) <= ToTime(startHour, startMinute, 00) || ToTime(Time[0]) > ToTime(endHour, endMinute, 00))
      			{
      				indexHigh = GetBar(new DateTime(year, month, day, startHour, startMinute, 0));
      				indexLow = GetBar(new DateTime(year, month, day, endHour, endMinute, 0));
      				++indexLow;
      				range = indexHigh - indexLow; // store the number of bars in the time range
      			}
      			else
      			{ 
      				int hh, mm, TT = ToTime(Time[0]);
      				
      				hh = TT / 10000;
      				TT = TT - hh * 10000;
      				mm = TT / 100;
      				
      				indexHigh = GetBar(new DateTime(year, month, day, startHour, startMinute, 0));
      				indexLow = GetBar(new DateTime(year, month, day, hh, mm, 0 ));
      				range = indexHigh - indexLow;
      			}
      			
      			// find Low of the TimeFrame by using the indexed 6PM bar, and looking back the number of periods in "Range"
      			high = MAX(High, range)[indexLow]; 
      			// find Low of the TF by using the indexed 6PM bar, and looking back the number of periods in "Range"
      			low = MIN(Low, range)[indexLow]; 
      			
      			yL = PriorDayOHLC().PriorLow[0]; // this get the previous days low
      			yH = PriorDayOHLC().PriorHigh[0]; // this gets the previous days high
      				
      			// plot values
                  ORhigh.Set(high);
                  ORlow.Set(low);
                  Yh.Set(yH);
                  Yl.Set(yL);
              }

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by bortz, 11-06-2023, 08:04 AM
      47 responses
      1,606 views
      0 likes
      Last Post aligator  
      Started by jaybedreamin, Today, 05:56 PM
      0 responses
      9 views
      0 likes
      Last Post jaybedreamin  
      Started by DJ888, 04-16-2024, 06:09 PM
      6 responses
      19 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by Jon17, Today, 04:33 PM
      0 responses
      6 views
      0 likes
      Last Post Jon17
      by Jon17
       
      Started by Javierw.ok, Today, 04:12 PM
      0 responses
      15 views
      0 likes
      Last Post Javierw.ok  
      Working...
      X