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

A custom close time for Prior Day OHLC

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

    A custom close time for Prior Day OHLC

    I've made a copy of the indicator, but is it possible to change the close time using a code like:

    Code:
    Bars.GetBar(new DateTime(today.Year, today.Month, today.Day, 9, 30, 0));
    Where would you make the change?

    If anyone wants to reply, I'll paste the source code below to save you time.

    Thanks!

    Code:
    public class PriorDayOHLC : Indicator
    {
    private DateTime currentDate = Core.Globals.MinDate;
    private double currentClose = 0;
    private double currentHigh = 0;
    private double currentLow = 0;
    private double currentOpen = 0;
    private double priorDayClose = 0;
    private double priorDayHigh = 0;
    private double priorDayLow = 0;
    private double priorDayOpen = 0;
    private Data.SessionIterator sessionIterator;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionPriorDayOHLC;
    Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa mePriorDayOHLC;
    IsAutoScale = false;
    IsOverlay = true;
    IsSuspendedWhileInactive = true;
    DrawOnPricePanel = false;
    ShowClose = true;
    ShowLow = true;
    ShowHigh = true;
    ShowOpen = true;
    
    AddPlot(new Stroke(Brushes.SteelBlue, DashStyleHelper.Dash, 2), PlotStyle.Hash, NinjaTrader.Custom.Resource.PriorDayOHLCOpen);
    AddPlot(new Stroke(Brushes.DarkCyan, 2), PlotStyle.Hash, NinjaTrader.Custom.Resource.PriorDayOHLCHigh);
    AddPlot(new Stroke(Brushes.Crimson, 2), PlotStyle.Hash, NinjaTrader.Custom.Resource.PriorDayOHLCLow);
    AddPlot(new Stroke(Brushes.SlateBlue, DashStyleHelper.Dash, 2), PlotStyle.Hash, NinjaTrader.Custom.Resource.PriorDayOHLCClose);
    }
    else if (State == State.Configure)
    {
    currentDate = Core.Globals.MinDate;
    currentClose = 0;
    currentHigh = 0;
    currentLow = 0;
    currentOpen = 0;
    priorDayClose = 0;
    priorDayHigh = 0;
    priorDayLow = 0;
    priorDayOpen = 0;
    sessionIterator = null;
    }
    else if (State == State.DataLoaded)
    {
    sessionIterator = new Data.SessionIterator(Bars);
    }
    else if (State == State.Historical)
    {
    if (!Bars.BarsType.IsIntraday)
    {
    Draw.TextFixed(this, "NinjaScriptInfo", NinjaTrader.Custom.Resource.PriorDayOHLCIntradayEr ror, TextPosition.BottomRight);
    Log(NinjaTrader.Custom.Resource.PriorDayOHLCIntrad ayError, LogLevel.Error);
    }
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (!Bars.BarsType.IsIntraday)
    return;
    
    // If the current data is not the same date as the current bar then its a new session
    if (currentDate != sessionIterator.GetTradingDay(Time[0]) || currentOpen == 0)
    {
    // The current day OHLC values are now the prior days value so set
    // them to their respect indicator series for plotting
    priorDayOpen = currentOpen;
    priorDayHigh = currentHigh;
    priorDayLow = currentLow;
    priorDayClose = currentClose;
    
    if (ShowOpen) PriorOpen[0] = priorDayOpen;
    if (ShowHigh) PriorHigh[0] = priorDayHigh;
    if (ShowLow) PriorLow[0] = priorDayLow;
    if (ShowClose) PriorClose[0] = priorDayClose;
    
    // Initilize the current day settings to the new days data
    currentOpen = Open[0];
    currentHigh = High[0];
    currentLow = Low[0];
    currentClose = Close[0];
    
    currentDate = sessionIterator.GetTradingDay(Time[0]);
    }
    else // The current day is the same day
    {
    // Set the current day OHLC values
    currentHigh = Math.Max(currentHigh, High[0]);
    currentLow = Math.Min(currentLow, Low[0]);
    currentClose = Close[0];
    
    if (ShowOpen) PriorOpen[0] = priorDayOpen;
    if (ShowHigh) PriorHigh[0] = priorDayHigh;
    if (ShowLow) PriorLow[0] = priorDayLow;
    if (ShowClose) PriorClose[0] = priorDayClose;
    }
    }
    
    [HASHTAG="t3322"]region[/HASHTAG] Properties
    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public Series<double> PriorOpen
    {
    get { return Values[0]; }
    }
    
    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> PriorHigh
    {
    get { return Values[1]; }
    }
    
    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> PriorLow
    {
    get { return Values[2]; }
    }
    
    [Browsable(false)]
    [XmlIgnore()]
    public Series<double> PriorClose
    {
    get { return Values[3]; }
    }
    
    [Display(ResourceType = typeof(Custom.Resource), Name = "ShowClose", GroupName = "NinjaScriptParameters", Order = 0)]
    public bool ShowClose
    { get; set; }
    
    [Display(ResourceType = typeof(Custom.Resource), Name = "ShowHigh", GroupName = "NinjaScriptParameters", Order = 1)]
    public bool ShowHigh
    { get; set; }
    
    [Display(ResourceType = typeof(Custom.Resource), Name = "ShowLow", GroupName = "NinjaScriptParameters", Order = 2)]
    public bool ShowLow
    { get; set; }
    
    [Display(ResourceType = typeof(Custom.Resource), Name = "ShowOpen", GroupName = "NinjaScriptParameters", Order = 3)]
    public bool ShowOpen
    { get; set; }
    #endregion
    }
    }

    #2
    Hello

    This indicator currently uses the session iterator to determine which data it is to break the plots. You could add a custom close, you would need to modify the time logic such as the following line:

    Code:
    if (currentDate != sessionIterator.GetTradingDay(Time[0]) || currentOpen == 0)


    You can see the following example for making time based conditions, the above is checking the Date rather than a time.

    https://ninjatrader.com/support/help...ightsub=totime
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks Jesse,

      I should clarify that I need the high and lows to use the session iterator, but the close to use the price from the bar 45 minutes before the session iterator's close.

      Where should I paste your above line into the indicator's code?

      Much appreciated!

      Comment


        #4
        Hello Bob-Habanai,

        You would need to make your own custom logic to do that, the original logic breaks all of the values based on the session. To do only 1 of the values at a custom time would require writing a second set of logic which is similar to the first but uses its own variables to know when the time to reset happens. You could keep the original logic as is and then add another set of conditions in OnBarUpdate which are outside the original logic.

        Code:
        if (currentDate != sessionIterator.GetTradingDay(Time[0]) || currentOpen == 0)
        {
        
        }
        else // The current day is the same day
        {
        
        }
        // your new conditions/logic outside the original conditions
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi Jesse,

          You mean to make it like this, yes?

          Code:
          // If the current data is not the same date as the current bar then its a new session
          if (currentDate != sessionIterator.GetTradingDay(Time[0]) || currentOpen == 0)
          {
          // The current day OHLC values are now the prior days value so set
          // them to their respect indicator series for plotting
          priorDayOpen = currentOpen;
          priorDayHigh = currentHigh;
          priorDayLow = currentLow;
          
          if (currentDate != sessionIterator.GetTradingDay(Time[0]) || currentOpen == 0)
          {
          }
          else // The current day is the same day
          {
          }
          // your new conditions/logic outside the original conditions
          priorDayClose    = currentClose;
          Apologies, I don't know how to write the conditions.
          How would you write 45 minutes before the session Iterator close?

          Thanks
          Last edited by Bob-Habanai; 09-26-2022, 06:33 PM.

          Comment


            #6
            Hello Bob-Habanai

            How would you write 45 minutes before the session Iterator close?
            You can use the sessioniterators method to get the end datetime and then use C#'s datetime math to subtract 45 minutes:

            Code:
            sessionIterator.GetNextSession(Time[0], true);
            DateTime endTime = sessionIterator.ActualSessionEnd;
            DateTime fourtyFiveMinutesBeforeEnd = endTime.AddMinutes(-45);
            Print("The current session end time is " + endTime + " And 45 minutes before: " + fourtyFiveMinutesBeforeEnd);
            
            if(Time[0] < fourtyFiveMinutesBeforeEnd)
            {
            
            }
            ​​
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thank you for all your help and patience Jesse.

              I tried this, but something went wrong, could you tell me where I got the order wrong in where the code is to be placed?

              Much appreciated!

              Code:
                      protected override void OnBarUpdate()
                      {
                          if (!Bars.BarsType.IsIntraday)
                              return;
              
                          // If the current data is not the same date as the current bar then its a new session
                          if (currentDate != sessionIterator.GetTradingDay(Time[0]) || currentOpen == 0)
                          {
                              // The current day OHLC values are now the prior days value so set
                              // them to their respect indicator series for plotting
                              priorDayOpen    = currentOpen;
                              priorDayHigh    = currentHigh;
                              priorDayLow        = currentLow;
              
                          if (currentDate != sessionIterator.GetTradingDay(Time[0]) || currentOpen == 0)
                          {
              
                          }
                          else // The current day is the same day
                          {
              
                          }
                          // your new conditions/logic outside the original conditions
                          sessionIterator.GetNextSession(Time[0], true);
                          DateTime endTime = sessionIterator.ActualSessionEnd;
                          DateTime fourtyFiveMinutesBeforeEnd = endTime.AddMinutes(-45);
                          Print("The current session end time is " + endTime + " And 45 minutes before: " + fourtyFiveMinutesBeforeEnd);
              
                          if(Time[0] < fourtyFiveMinutesBeforeEnd)
                          {
                          }    
                          priorDayClose    = currentClose;
              
                              if (ShowOpen)    PriorOpen[0]    = priorDayOpen;
                              if (ShowHigh)    PriorHigh[0]    = priorDayHigh;
                              if (ShowLow)    PriorLow[0]        = priorDayLow;
                              if (ShowClose)    PriorClose[0]    = priorDayClose;
              
                              // Initilize the current day settings to the new days data
                              currentOpen     =    Open[0];
                              currentHigh     =    High[0];
                              currentLow        =    Low[0];
                              currentClose    =    Close[0];
              
                              currentDate     =    sessionIterator.GetTradingDay(Time[0]);
                          }
                          else // The current day is the same day
                          {
                              // Set the current day OHLC values
                              currentHigh     =    Math.Max(currentHigh, High[0]);
                              currentLow        =    Math.Min(currentLow, Low[0]);
                              currentClose    =    Close[0];
              
                              if (ShowOpen)    PriorOpen[0] = priorDayOpen;
                              if (ShowHigh)    PriorHigh[0] = priorDayHigh;
                              if (ShowLow)    PriorLow[0] = priorDayLow;
                              if (ShowClose)    PriorClose[0] = priorDayClose;
                          }
                      }
              ​

              Comment


                #8
                Hello Bob-Habanai,

                In this script you have the curly braces mismatched which looks like its due to a duplication of the condition. You have the first day related condition twice, you can remove the second copy that looks like this:

                Code:
                if (currentDate != sessionIterator.GetTradingDay(Time[0]) || currentOpen == 0)
                {
                It would also be easiest to not change the existing code and just make a duplicate of the original by doing RIght click -> Save As. To do what you are asking you just need to add another condition which comes after the original conditions.

                The structure you would want looks like the following. I removed the plot code to make it easier to see the curly brace structure:


                Code:
                if (currentDate != sessionIterator.GetTradingDay(Time[0]) || currentOpen == 0)
                {
                    // The current day OHLC values are now the prior days value so set
                    // them to their respect indicator series for plotting
                
                    // original code here
                
                    currentDate = sessionIterator.GetTradingDay(Time[0]);
                }
                else // The current day is the same day
                {
                    // Set the current day OHLC values
                    //original code here
                }​
                
                sessionIterator.GetNextSession(Time[0], true);
                DateTime endTime = sessionIterator.ActualSessionEnd;
                DateTime fourtyFiveMinutesBeforeEnd = endTime.AddMinutes(-45);​
                if(Time[0] == fourtyFiveMinutesBeforeEnd)
                {
                   // set whichever plots you need here
                }
                ​
                You would also need to use a chart that has intervals of 1 minute or 15 minutes so that a time with 45 minutes after can happen.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Thanks for all your help Jesse!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by kainermy, Today, 05:22 AM
                  0 responses
                  7 views
                  0 likes
                  Last Post kainermy  
                  Started by KlausTheMause, Yesterday, 02:57 PM
                  1 response
                  29 views
                  0 likes
                  Last Post NinjaTrader_Jason  
                  Started by Jaggy, 02-04-2023, 07:24 PM
                  1 response
                  61 views
                  0 likes
                  Last Post NinjaTrader_Jason  
                  Started by sjmuk1, Yesterday, 09:35 AM
                  2 responses
                  69 views
                  0 likes
                  Last Post sjmuk1
                  by sjmuk1
                   
                  Started by NinjaCustomer, 05-24-2018, 10:29 PM
                  3 responses
                  946 views
                  0 likes
                  Last Post elliot5
                  by elliot5
                   
                  Working...
                  X