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

Get High of x sessions ago

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

    Get High of x sessions ago

    Hi,

    I want to get the High of x sessions (not days) ago on an 60min chart. Is this possible? I basically just need to store the CurrentBar of each session and then reference to it via PriorDayOHLC().PriorHigh


    So I tried to create an int-series and access the High via the PriorDayOHLC() statement:

    private Series<int> Session;

    if (State == State.Configure)
    {
    Session= new Series<int>(this);
    }

    protected override void OnBarUpdate()
    {
    if (Bars.IsFirstBarOfSession)
    Session[0] = CurrentBar;

    if (PriorDayOHLC().PriorHigh[CurrentBar - Session[1])
    do something
    }

    I thought I could access the first bar of x sessions ago with Session[x]. However, this doesn't work. How can I store every first bar of session and access it afterwards on intrady data? Is there a way to solve this problem?

    I also tried PriorDayOHLC().PriorHigh[CurrentBar - Bars.GetBar(Time[1].AddDays(-1))]. But I dont like this because of weekends and holidays. I want to use this strategy for various of instruments and trading hour templates, so I really want to access the High of each session.

    Would very much appreciate your help!
    Last edited by KirkHammett; 12-16-2019, 03:07 PM.

    #2
    Hello KirkHammett,

    Thanks for your question.

    Series objects will be synchronized to the bars object they are created with, or the primary data series for that script. I may suggest to keep 5 class level variables or to use an array to track the last 5 session highs.

    For example, when your strategy sees Bars.IsFirstBarOfSession, you can do the following. Please consider that PriorHigh1, PriorHigh2, PriorHigh3, PriorHigh4, PriorHigh5 are all class level doubles.

    Assign PriorHigh5 to PriorHigh4 value.
    Assign PriorHigh4 to PriorHigh3 value.
    Assign PriorHigh3 to PriorHigh2 value.
    Assign PriorHigh2 to PriorHigh1 value.
    Assign PriorHigh1 to PriorDayOHLC().PriorHigh[1] value.

    You could then use the variables you have created to reference the session high for up to 5 sessions ago because these variables update only when a new session is seen.

    You could also increment a counter when Bars.IsFirstBarOfSession is seen so you can check if the strategy has processed 5 sessions and your variables will have expected values.

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

    Comment


      #3
      Hi Jim!
      Thanks a lot for the quick answer! I know the purpose of this support is not to provide us with full code and debugging wrong strategies. Hower, could you maybe give me just a simple example of code on how to access the highs of 4 and 5 sessions ago? I don't really understand how to create this class level double PriorHigh4 and 5 and then reference to it or how to create an array of these highs and reference to it when calling IsFirstBarOfSession.

      I would very much appreciate your help. Thanks in advance!!

      Comment


        #4
        Hello KirkHammett,

        NinjaScript is based on C#. Educational resources on C# can give you further direction on class level variables.

        Publicly available resource - https://www.geeksforgeeks.org/scope-...es-in-c-sharp/

        The code below can be tested.

        Code:
        private PriorDayOHLC PriorDayOHLC1;
        
        private double PriorHigh1, PriorHigh2, PriorHigh3, PriorHigh4, PriorHigh5;
        private int sessionCount;
        
        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Name                                        = "TestStrategy";
                Calculate                                    = Calculate.OnBarClose;
            }
            else if (State == State.DataLoaded)
            {                
                PriorDayOHLC1                = PriorDayOHLC(Close);
            }
        }
        
        protected override void OnBarUpdate()
        {
            if (Bars.IsFirstBarOfSession)
            {
                sessionCount++;
                PriorHigh5 = PriorHigh4;
                PriorHigh4 = PriorHigh3;
                PriorHigh3 = PriorHigh2;
                PriorHigh2 = PriorHigh1;
        
                if (sessionCount < 2)
                    return;
        
                PriorHigh1 = PriorDayOHLC1.PriorHigh[1];
                Print(String.Format("PriorHigh5: {0} PriorHigh4: {1} PriorHigh3: {2} PriorHigh2: {3} PriorHigh1: {4}  PriorDayOHLC1.PriorHigh[1]: {5}", PriorHigh5, PriorHigh4, PriorHigh3, PriorHigh2, PriorHigh1, PriorDayOHLC1.PriorHigh[1]));
            }
        
        }
        I look forward to being of further assistance.
        JimNinjaTrader Customer Service

        Comment


          #5
          That's awesome, thank you so much for your help! I really appreciate it!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Balage0922, Today, 07:38 AM
          0 responses
          3 views
          0 likes
          Last Post Balage0922  
          Started by JoMoon2024, Today, 06:56 AM
          0 responses
          6 views
          0 likes
          Last Post JoMoon2024  
          Started by Haiasi, 04-25-2024, 06:53 PM
          2 responses
          19 views
          0 likes
          Last Post Massinisa  
          Started by Creamers, Today, 05:32 AM
          0 responses
          6 views
          0 likes
          Last Post Creamers  
          Started by Segwin, 05-07-2018, 02:15 PM
          12 responses
          1,786 views
          0 likes
          Last Post Leafcutter  
          Working...
          X