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

using Time comparison in strategy

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

    using Time comparison in strategy

    HI i have an indicator that returns Time[1] in one of the functions.

    Imbalance = new Imbalance(tag, resistance, High[1]], Lows[1], Time[1]);

    I want strategy to access that Time[1] value and compare it to current time to find out how long this has been active.

    What would be the course for this?
    I was thinking ot use plots but i cant use Time format in plots that require double.

    #2
    Hello tkaboris,

    You are showing Time[1] being supplied as a parameter to an indicator call, not as a return value..

    From the indicator you can make a public DateTime property, and in the getter call Update() to trigger OnBarUpdate() to run before returning a value.
    https://ninjatrader.com/support/help...nt8/update.htm

    [XmlIgnore]
    [Browsable(false)]
    public DateTime MyDateTimeValue
    {
    get { Update(); return value; }
    }

    Then from the strategy you can access this public property.

    Print(Imbalance().MyDateTimeValue);

    Note, calling an indicator method is done without using the 'new' keyword.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you it worked.

      What if there are multiple imbalances created with each one has its own start time that i want to track.
      is it possible to have series with DateTime?
      for me its compalingn that it cant impoicitly convert datetime to series datetime

      private Series<DateTime> lastSTime;
      lastSTime = new Series<DateTime>(this);
      lastSTime = gapStartTime;

      Comment


        #4
        Hello tkaboris,

        Yes, a Series can hold DateTime objects or any other object.

        This will use indexes.

        lastSTime[0] = gapStartTime;
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you. I was wondering if you would guess what the issue is when i try to print this line ? i get error liek its
          Error on calling 'OnBarUpdate' method on bar 13937: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

          protected override void OnBarUpdate()
          {

          Print("IsBullishFVGInLastBars(7)"+(IsBullishFVGInL astBars(7) ));​
          }


          Code:
          protected override void OnBarUpdate()
          {
          if (CurrentBar < 10)
                      return;​
          Print("IsBullishFVGInLastBars(7)"+(IsBullishFVGInL astBars(7) ));
          ​
          if (IsBullishFVGInLastBars(10))
          EnterLong(1,PositionSize, "Long Entry");​
          }
          
          bool IsBullishFVGInLastBars(int lookbackBars)
          {
          
          for (int i = 1; i <= lookbackBars; i++)
          {
          
          if (IsBullishFVG(i))
          {
          return true;
          }
          
          }
          
          return false;
          }​
          
          private bool IsBullishFVG(int index)
          {
          rreturn (Close[index - 3] > Open[index - 3] && Close[index - 2] > Open[index - 2] && Close[index - 1] > Open[index - 1]) &&
                             Low[index - 1] > High[index - 3] && (Math.Abs(Low[index - 1] - High[index - 3]) >= minFvgSizeStrategy);​
          
          }​​​

          Comment


            #6
            Hello tkaboris,

            You have an invalid bars ago index and you should be adding prints and debugging to see which index it is.
            Hello, I want to create an indicator that show data in a chart but calculate in other charttime different to the time of the chart where is showed. For example:


            It looks like a loop that starts with 1 then you subtract 3 and try and use a negative value for the barsAgo index.

            Negative indexes are not allowed.

            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I start at 3 now and strategy runs.... I tried to print index but in only prints returning false even though there is a condition met.
              i just want to go long if this condition occured in the last 7 bars. IsBullishFVGInLastBars(7) == true;

              onbarupdate
              Print("IsBullishFVGInLastBars(7):"+(IsBullishFVGIn LastBars(7) ) + CurrentBar);


              bool IsBullishFVGInLastBars(int lookbackBars)
              {
              // Print("beforeIsBullish");
              for (int i = 3; i <= lookbackBars; i++)
              {

              if (IsBullishFVG(i))
              {
              Print("IsBullishFVG at index: " + i + ". returning true.");
              return true;
              }

              }
              Print("returning false" + CurrentBar);
              return false;
              }​


              private bool IsBullishFVG(int index)
              {
              // return (Close[3] > Open[3] && Close[2] > Open[2] && Close[1] > Open[1]) &&
              // Low[1] > High[3] && (Math.Abs(Low[1] - High[3]) >= minFvgSizeStrategy);
              return (Close[index - 3] > Open[index - 3] && Close[index - 2] > Open[index - 2] && Close[index - 1] > Open[index - 1]) &&
              Low[index - 1] > High[index - 3] && (Math.Abs(Low[index - 1] - High[index - 3]) >= minFvgSizeStrategy);

              }​

              Comment


                #8
                Hello tkaboris,

                Add prints print the indexes you are using in the condition. It may help you to see the problem.

                Print(string.Format("{0} | index: {1}, index - 3: {2}, index + 3: {3}", Time[0], index, (index - 3), (index + 3)));

                In this condition are you trying measure from 1 bar ago to -2 bars ago? or are you trying to measure from 1 bar ago to 3 bars ago?

                Do you mean to be adding adding to the bars ago and not subtracting?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  hi the pattern is based on 3 candlestick formation, so i need to calcualte 3 bar formations and see if this pattern occured in the last 3 bars.
                  Currently my loop evaluates to true only once on a bar and i need the bool to evaluate to true on 3 bars because it evalutes "if condition occured in teh last 3 bars"

                  I added prints and here is what i get. the pattern occured and it printed yes bullish fvg(i added simple condition outside of bool and loop) and below are indexes what you suggested... So condition is there but loop and bool doesnt see it yet.

                  if ((Close[3] > Open[3] && Close[2] > Open[2] && Close[1] > Open[1]) &&
                  Low[1] > High[3] && (Math.Abs(Low[1] - High[3]) >= 2))
                  {
                  Print("yes bullish fvg" + Time[1]);
                  }​

                  yes bullish fvg11/10/2023 4:45:00 AM
                  11/10/2023 4:45:10 AM | index: 3, index - 3: 0, index + 3: 6
                  11/10/2023 4:45:10 AM | index: 4, index - 3: 1, index + 3: 7
                  11/10/2023 4:45:10 AM | index: 5, index - 3: 2, index + 3: 8
                  11/10/2023 4:45:10 AM | index: 6, index - 3: 3, index + 3: 9
                  11/10/2023 4:45:10 AM | index: 7, index - 3: 4, index + 3: 10​
                  Last edited by tkaboris; 11-19-2023, 05:30 AM.

                  Comment


                    #10
                    Here are my prints. On 2;15 we evaluated to true within last 3 bars and continue to evaluate to true for next 2 bars, on next bar we evaluate to false, but I want it to still to evaluate to true, since its still within the last 3 bars.

                    bool htfBullPinBarLast3bars = false;
                    for (int i = 0; i <= 3; i++)
                    {
                    Print(string.Format("{0} | {1}", i, Time[i]));
                    if ((Open[i + 1] > Close[i+1]) && (bodyPrev > body) && (downshadow > 0.5 * body) && (downshadow > 2 * upshadow))
                    {
                    htfBullPinBarLast3bars = true;
                    // break; // Exit the loop once the condition is met
                    }
                    }
                    Print("htfBullPinBarLast3bars:"+htfBullPinBarLast3 bars + Time[0]);​

                    0 | 11/17/2023 2:15:00 AM
                    1 | 11/17/2023 2:14:00 AM
                    2 | 11/17/2023 2:13:00 AM
                    3 | 11/17/2023 2:12:00 AM
                    4 | 11/17/2023 2:11:00 AM
                    htfBullPinBarLast3bars:True11/17/2023 2:15:00 AM​

                    0 | 11/17/2023 2:16:00 AM
                    1 | 11/17/2023 2:15:00 AM
                    2 | 11/17/2023 2:14:00 AM
                    3 | 11/17/2023 2:13:00 AM
                    4 | 11/17/2023 2:12:00 AM
                    htfBullPinBarLast3bars:False11/17/2023 2:16:00 AM​

                    Comment


                      #11
                      i think i need to uncomment //break

                      How do we index though bodyPrev if indexing cannot be applied to double?
                      bodyPrev[i]


                      double body = Math.Abs(Close[1] - Open[1]);
                      double bodyPrev = Math.Abs(Close[2] - Open[2]);
                      double upshadow = Open[2] > Close[2] ? High[1] - Open[1] : High[1] - Close[1];
                      double downshadow = Open[2] > Close[2] ? Close[1] - Low[1] : Open[1] - Low[1];

                      // pinbar_l
                      htfBullPinBarLast3bars = false;
                      for (int i = 0; i <= 3; i++)
                      {
                      // Print(string.Format("{0} | {1}", i, Time[i]));
                      if ((Open[i + 1] > Close[i+1]) && (bodyPrev > body) && (downshadow > 0.5 * body) && (downshadow > 2 * upshadow))
                      {
                      htfBullPinBarLast3bars = true;
                      // break; // Exit the loop once the condition is met
                      }
                      }​
                      Last edited by tkaboris; 11-19-2023, 07:03 AM.

                      Comment


                        #12
                        Hello tkaboris,

                        CountIf() and MRO() can look for a condition being true in the last n bars.



                        A double is a single value. A Series<double> is a series that has a double value for each bar.

                        Chelsea B.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by tsantospinto, 04-12-2024, 07:04 PM
                        7 responses
                        126 views
                        0 likes
                        Last Post aligator  
                        Started by futtrader, 04-21-2024, 01:50 AM
                        5 responses
                        56 views
                        0 likes
                        Last Post NinjaTrader_Eduardo  
                        Started by PeakTry, Today, 10:49 AM
                        0 responses
                        2 views
                        0 likes
                        Last Post PeakTry
                        by PeakTry
                         
                        Started by llanqui, Today, 10:32 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post llanqui
                        by llanqui
                         
                        Started by StockTrader88, 03-06-2021, 08:58 AM
                        45 responses
                        3,994 views
                        3 likes
                        Last Post johntraderuser2  
                        Working...
                        X