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

Higher Time Frame detail in Lower time Frame indicator

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

    Higher Time Frame detail in Lower time Frame indicator

    Hi

    I am looking for coding guidance on using measurements on higher time frame charts in an indicator on a trading time frame.

    Example. I want to test if Fibonacci retracement and extension measurements (that are not actually drawn) made of swings on a 1 day chart are at specific zones on my trading time frame, and make the results (Bool true or false test) available to a variable for a indicator on a 1 hour chart.

    Another example would be the Y value of a support and resistance zone identified on a day chart available to a variable of the indicator on the 1 hour chart.

    What I am trying to do is write an indicator that takes a number of criteria into account and then presents a score back to the User. Eg is HTF support is present at your target zone =1, if HTF trend is identified that supports entry direction = 2, if HTF Fibonacci at trading time frame entry zone = 1, total score is 4 etc...

    While I can write all of the tests for criteria I want to score on the trading time frame, I am stuck as to how to test a higher time frame and report the results back to the trading time frame.

    Thoughts?

    #2
    Hello EastLondonKiwi, and thank you for your question.

    The best resource online for these types of strategies will be the multi-time frame section of the help guide, available here.



    The first consideration you need to take when running a multi-time frame strategy, is to ensure that every member of CurrentBars has enough in the way of bars to examine. This is up to the individual coder, but the "lazy" way I usually accomplish this is

    • examine my code for my greatest period, number of bars looked back, etc
    • store this number in BarsRequired (NT7), BarsRequiredToPlot (NT8 chart), or BarsRequiredToTrade (NT8 strategy)
    • use code like this to ensure every one of my bars series has enough bars

    Code:
    [FONT=Courier New]foreach(int CurrentBarI in CurrentBars)[/FONT]
    [FONT=Courier New]{[/FONT]
    [FONT=Courier New]    if (CurrentBarI < BarsRequired)[/FONT]
    [FONT=Courier New]    {[/FONT]
    [FONT=Courier New]        return;
    [/FONT]
    [FONT=Courier New]    }
    [/FONT]
    [FONT=Courier New]}[/FONT]

    While this code is not optimized, as it could be the case that some bars series could be processed sooner if I chose a number appropriate to that bars series rather than a global BarsRequired variable, it is more reliable and easier to maintain than optimal code.


    Once you have ensured you have enough in the way of bars, your next order of business will be to ensure that you are on your 1-hour series. You can review the documentation for BarsInProgress here to ensure this.





    Now that we know you have populated daily bars, and you are processing your 1-hour bars, we can finally retrieve your Y values. Y values are the same no matter what time series we are trading on. So you can simply retrieve any stored Y values.


    This means that we need to actually store these Y values when your BarsInProgress value matches your one hour bars. When this is the case is when we should be drawing your Fibos. If we look at the signature for the Fibo drawing methods, we see this :


    Code:
    [FONT=Courier New]Draw.FibonacciTimeExtensions(NinjaScriptBase owner, string tag, bool isAutoScale, [I]int startBarsAgo[/I], [B]double startY[/B], [I]int endBarsAgo[/I], [B]double endY[/B])[/FONT]

    This means that in order to draw a Fibo retracement, you have to have a price you know that you are drawing with. We'll call these prices startY and endY.


    So tying all these together, let's say your 1 hour series was on BarsInProgress == 0, and your 1-day series was on BarsInProgress == 1. Let's say that startBarsAgo (italicized) is your largest period or BarsAgo value in your script, and is equal to 3. If all this is true, then the attached script contains an example where what you are asking is accomplished.


    Please let us know if there are any other ways we can help.
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thanks Jessica. I am a little confused by your explanation, but your example script looks fairly simple.

      I'll digest and get back with questions when I undoubtedly get stuck.

      Kind regards

      Duncan

      Comment


        #4
        Hi Jessica, I have now read the help manual on this subject, and your reply is starting to make some sense.

        I have a question about hard coding in the higher time frame to the BarsPeriodType but that can wait.

        For now can I email you my attempt at a basic script that looks to draw a Fib Retracement on a higher time frame and test of any of the Fib lines are within the price area of a rectangle drawn on the lower time frame.

        Kind regards

        Ducnan

        Comment


          #5
          Hi I am getting stuck harvesting a User drawn rectangle StartY and EndY values.

          The part of the indicator code that refers to the drawn rectangle I have written looks like this:

          // Loads the Price values of the two Y values from the User drawn Rectangle with a specific tag name held in the string variable ctsZone.
          foreach (DrawingTool draw in DrawObjects)
          {
          if (draw.Tag == ctsZone && draw is DrawingTools.Rectangle)
          {
          if(draw.IsLocked)
          draw.IsLocked = false;
          foreach (Properties d in draw.Anchors)
          {
          ctsSY = d.startY;
          ctsEY = d.endY;
          }
          if(ctsSY > ctsEY)
          {
          ctsStartY = ctsSY;
          ctsEndY = ctsEY;
          }
          else
          {
          ctsStartY = ctsEY;
          ctsEndY = ctsSY;
          }

          HTF_Fibs();
          Print(htfFib);
          }
          }
          Ignore the function I am calling HTF_Fibs() and the print statement which is used to QC the result.

          Where I am stumped is the correct statement to harvest the StartY and EndY values from the drawn rectangle properties.

          Help please.

          Comment


            #6
            I am happy to help. I noticed you were looping through each member of Anchors. As a result, you were grabbing each ChartAnchor's start and end Y values, not those of the Rectangle itself. You can think of a ChartAnchor as an individual vertex. In the case of a Rectangle we find

            Originally posted by http://ninjatrader.com/support/helpGuides/nt8/en-us/rectangle.htm
            Methods and Properties

            StartAnchor | An IDrawingTool's ChartAnchor representing the starting point of the drawing object

            EndAnchor | An IDrawingTool's ChartAnchor representing the starting point of the drawing object
            This implies that Rectangles have 2 ChartAnchors : one for the lower left corner, and one for the upper right corner. You can verify these with

            Code:
            Print(((Rectangle) draw).StartAnchor.ToString());
            Print(((Rectangle) draw).EndAnchor.ToString());
            If this is the case, we can modify your loop as follows :

            Code:
            [FONT=Courier New]// Loads the Price values of the two Y values from the User drawn  Rectangle with a specific tag name held in the string variable ctsZone.
            foreach (DrawingTool draw in DrawObjects)
            {
              if (draw.Tag == ctsZone && draw is DrawingTools.Rectangle)
              {
                if(draw.IsLocked)
                  draw.IsLocked = false;
                ctsSY = ((Rectangle) draw).StartAnchor.Price;
                ctsEY = ((Rectangle) draw).EndAnchor.Price;
                HTF_Fibs();
                Print(htfFib);
              }
            }[/FONT]
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              Thanks Jessica that is exactly what I was struggling to find..

              Next issue I have is the Fibs Method I have written. I get an error that states not all code paths return a value. Can you give me some guidance as what I am doing wrong.


              private bool HTF_Fibs()
              {

              // Day Bars

              if (BarsInProgress == 1)
              {
              startY = ZigZagUTG(true,10, 3).pv2;
              endY = ZigZagUTG(true,10, 3).pv3;
              startBar = ZigZagUTG(true,10, 3).pb2;
              endBar = ZigZagUTG(true,10, 3).pb3;

              Print("StartY " + startY + " EndY " + endY);

              // Draw the Fib

              myRetracements = Draw.FibonacciRetracements(this, "htfFib" + CurrentBar, true, startBar, startY, 0, endY);
              // Print each price level in the PriceLevels collection contain in myRetracements
              * foreach (PriceLevel p in myRetracements.PriceLevels)
              * {
              * * double fibValue = p.Value;
              string fibName = p.Name;
              Print(fibValue);
              Print(fibName);

              if (fibValue <= ctsStartY && fibValue >= ctsEndY)
              {
              htfFib = true;
              }
              else
              {
              htfFib = false;
              }

              return htfFib;
              * }


              }

              }

              The cstStartY and cstEndY defined the rectangle's area. From my last question

              Cheers Duncan

              Comment


                #8
                I am glad I was able to help. While the message you are getting currently is beyond the scope of the support we may provide directly, I was able to find a relevant publicly available Stack Overflow article on the subject.

                I'm trying to write code that returns whether or not a given integer is divisible evenly by 1 to 20, but I keep receiving the following error: error CS0161: 'ProblemFive.isTwenty(int)': not all...


                After reviewing the discussion on that page, I would like to recommend returning true or false before the final curly brace.
                Jessica P.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks Jessica that thread did the trick.

                  Ok the next issue I have is my indicator right now should identify when a User draws a rectangle on a 1 hour time frame and edits the tag to ctsZone.

                  Then on the 1 Day time frame draw a Fib between the latest swing high and low. and establish if any of the Fib lines pass within the rectangles price boundary.

                  I have two print statements in my OnBar update that should state the Rectangle Y values, and then the bool return that of the Fibs location (at a minimum false).

                  I have no errors on compiling but I get nothing at all in the NinjaScript Output window.

                  Any guidance on how to determine what is happening?

                  Cheers Duncan

                  protected override void OnBarUpdate()
                  {
                  //Add your custom indicator logic here.

                  foreach (int CurrentBarI in CurrentBars)
                  {
                  if (CurrentBarI < BarsRequiredToPlot)
                  {
                  return;
                  }
                  }

                  // Loads the Price values of the two Y values from the User drawn Rectangle with a specific tag name held in the string variable ctsZone.
                  foreach (DrawingTool draw in DrawObjects)
                  {
                  if (draw.Tag == ctsZone && draw is DrawingTools.Rectangle)
                  {
                  if(draw.IsLocked)
                  draw.IsLocked = false;
                  ctsSY = ((Rectangle) draw).StartAnchor.Price;
                  ctsEY = ((Rectangle) draw).EndAnchor.Price;

                  Print("ctsSY " + ctsSY + " ctsEY " + ctsEY);



                  if(ctsSY > ctsEY)
                  {
                  ctsStartY = ctsSY;
                  ctsEndY = ctsEY;
                  }
                  else
                  {
                  ctsStartY = ctsEY;
                  ctsEndY = ctsSY;
                  }

                  HTF_Fibs();
                  Print(htfFib);
                  }
                  }

                  }

                  Comment


                    #10
                    Ooppps I gues you already got a response

                    ~J

                    Comment


                      #11
                      I recommend the following re-write to part of your code. This should make it immediately clear why this is not occurring.

                      Code:
                      [FONT=Courier New]// ...
                      [B]Print("ctsZone is '" + ctsZone + "'");[/B]
                      foreach (DrawingTool draw in DrawObjects)
                      {
                      [B]  Print("Now checking '" + draw.Tag + "'");[/B]
                        if (draw.Tag == ctsZone && draw is DrawingTools.Rectangle)
                        {
                          if(draw.IsLocked)
                            draw.IsLocked = false;
                      // ...[/FONT]
                      Jessica P.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks Jessica

                        With your print statement in the code, it was obvious.

                        Now I am trying to work out some weird behaviour. if I move between the 1 hour time frame and the 4 hour time frame ( was doing this to refresh the chart and force the output to start from fresh), my indicator seems to be resizing the rectangle to a time period which is the entire data period of the 4 hour chart, meaning when I return to the 1 hour chart the rectangle is the entire length of the 1 hour data series.

                        Also print statements I have in the Fib method do not output, so I need to work out why. as the final return statement is sending back the expected false.

                        Cheers Duncan

                        Comment


                          #13
                          With respect to the unusual behavior, could you send an up-to-date copy of your code we can test against, to see if Ninja is behaving properly? You can send this to platformsupport[at]ninjatrader[dot]com, referencing 1625935 and Attn:NinjaTrader_JessicaP in the subject line. I will review your code to see if I can provide any hints or debugging assistance with respect to your print statements as well.
                          Jessica P.NinjaTrader Customer Service

                          Comment


                            #14
                            Ok to update this thread. I'll add in part of a conversation Jesse and I have had via email.

                            To work out what was going wrong I needed to send Jesse a copy of my workspace and the two scripts I was using.

                            Jesse has taken the time to examine both indicators and explain in detail to me what is occurring. I really appreciate this effort, because it goes beyond the scope of NT8 support. And is helping me to learn and written better code.

                            The trouble display issue that was occurring turned out to be badly written code within the ZigZAgUTG indicator, or a misunderstood concept of passing data between two classes (something as a novice coder I have very little understanding of my self).

                            ok so what was happening and how did Jesse fix it.

                            Within ZigZagUTG the author was presenting 7 variables for public use:
                            // public int dir, pb1, pb2, pb3;
                            // public double pv1, pv2, pv3;
                            The trouble with presenting these variables in this manner is that an external class does not have the correct access to them, and so weird stuff happens (which is what was causing the errors in my indicator).

                            What the author should have done is use a second layer of variables that uses Public Properties. properties give you access to the Get and Set methods, which in turn provide a level of control to the underlying variable content. For example you can dictate if a variable is read only or also writable.

                            So to fix my errors. I had to modify ZigZagUTG like this:

                            // This code is not correct and needed to be replaced. in its current form it causes an error of any script calling these variables from outside this class
                            // public int dir, pb1, pb2, pb3;
                            // public double pv1, pv2, pv3;

                            // The new code renames the variables as private
                            private int dir, pb1, pb2, pb3;
                            private double pv1, pv2, pv3;
                            // Then adds in Properties that can be safely called by external classes.
                            public int Dir
                            {
                            //The properties need to invoke the Update in the get before the variable will send intended content.
                            get{Update(); return dir;}
                            set{dir = value;}
                            }
                            public int Pb1
                            {
                            get{Update(); return pb1;}
                            set{pb1 = value;}
                            }
                            public int Pb2
                            {
                            get{Update(); return pb2;}
                            set{pb2 = value;}
                            }
                            public int Pb3
                            {
                            get{Update(); return pb3;}
                            set{pb3 = value;}
                            }

                            public double Pv1
                            {
                            get{Update(); return pv1;}
                            set{pv1 = value;}
                            }
                            public double Pv2
                            {
                            get{Update(); return pv2;}
                            set{pv2 = value;}
                            }
                            public double Pv3
                            {
                            get{Update(); return pv3;}
                            set{pv3 = value;}
                            }
                            Then within my own indicator I call the Property Variables that in turn pass the ZigZAgUTG variable content correctly, like this:

                            startY = ZigZagUTG(true,10, 3).Pv2;
                            Ok so my indicator is now working and I am making good progress, with a long way to go until I get to the end of this particular project.

                            What this error also highlights is the use of third-party code and external classes.

                            I tend to like the modular nature of C sharp code. I really want my OnBarUpdate method to have very readable statements and be very slim in nature. Therefore I tend to develop and write a lot of methods that sit outside the OnBarUpdate method.

                            It means I can write code like

                            if (DoubleTop)
                            {
                            do something
                            }
                            And that means I can write very intuitive if then statements and not have to relay on comments. It also should mean I can easily find and reuse useful methods in other projects.

                            I see that Classes for me would be very similar, in that I should be able to build up a library of useful code and simply call it from any new project I want, without the need to rewrite a load of code. But clearly the structure of how code is past between classes needs to be very clear fully thought about and the delivery mechanism written into it from the beginning before any methods or variables are presented as useful outside of the class itself.

                            How people use external code or cross class code would be useful to understand so I look forward to others comments on this subject.

                            I'll update this thread and my project progresses. I may not share everything as the intended project is massive, but I'll share those useful bits where like this recent error there are general solutions others may benefit from.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by adeelshahzad, Today, 03:54 AM
                            4 responses
                            25 views
                            0 likes
                            Last Post adeelshahzad  
                            Started by merzo, 06-25-2023, 02:19 AM
                            10 responses
                            823 views
                            1 like
                            Last Post NinjaTrader_ChristopherJ  
                            Started by frankthearm, Today, 09:08 AM
                            5 responses
                            17 views
                            0 likes
                            Last Post NinjaTrader_Clayton  
                            Started by jeronymite, 04-12-2024, 04:26 PM
                            3 responses
                            43 views
                            0 likes
                            Last Post jeronymite  
                            Started by yertle, Today, 08:38 AM
                            5 responses
                            16 views
                            0 likes
                            Last Post NinjaTrader_BrandonH  
                            Working...
                            X