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

Retrieving barsAgo when Value of the underlying is known

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

    Retrieving barsAgo when Value of the underlying is known

    Hi Ninjas
    I wonder how I can track down the barsAgo when I know the MACD Value. I need both of them.
    I’m using SWING to return both of them, happy with Value, but not with barsAgo:

    Code:
     
    [COLOR=blue][FONT=Courier New]double[COLOR=black][FONT=Courier New] prevMacdHigh = Swing(MACD([/FONT][/COLOR][COLOR=purple][FONT=Courier New]12[/FONT][/COLOR][COLOR=black][FONT=Courier New], [/FONT][/COLOR][COLOR=purple][FONT=Courier New]25[/FONT][/COLOR][COLOR=black][FONT=Courier New], [/FONT][/COLOR][COLOR=purple][FONT=Courier New]9[/FONT][/COLOR][COLOR=black][FONT=Courier New]), [/FONT][/COLOR][COLOR=purple][FONT=Courier New]5[/FONT][/COLOR][COLOR=black][FONT=Courier New]).SwingHigh[[/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=black][FONT=Courier New]];[/FONT][/COLOR]
    [COLOR=blue][FONT=Courier New]int[/FONT][/COLOR][COLOR=black][FONT=Courier New] prevMacdHighBar = Swing(MACD([/FONT][/COLOR][COLOR=purple][FONT=Courier New]12[/FONT][/COLOR][COLOR=black][FONT=Courier New], [/FONT][/COLOR][COLOR=purple][FONT=Courier New]25[/FONT][/COLOR][COLOR=black][FONT=Courier New], [/FONT][/COLOR][COLOR=purple][FONT=Courier New]9[/FONT][/COLOR][COLOR=black][FONT=Courier New]), [/FONT][/COLOR][COLOR=purple][FONT=Courier New]5[/FONT][/COLOR][COLOR=black][FONT=Courier New]).SwingHighBar([/FONT][/COLOR][COLOR=purple][FONT=Courier New]0[/FONT][/COLOR][COLOR=black][FONT=Courier New],[/FONT][/COLOR][COLOR=purple][FONT=Courier New]1[/FONT][/COLOR][COLOR=black][FONT=Courier New],[/FONT][/COLOR][COLOR=purple][FONT=Courier New]10[/FONT][/COLOR][COLOR=black][FONT=Courier New]); [/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]                  [/FONT][/COLOR]
    [COLOR=black][FONT=Courier New]Print([/FONT][/COLOR][COLOR=maroon][FONT=Courier New]"Value:"[/FONT][/COLOR][COLOR=black][FONT=Courier New] + prevMacdHigh.ToString([/FONT][/COLOR][COLOR=maroon][FONT=Courier New]"0.0000"[/FONT][/COLOR][COLOR=black][FONT=Courier New]) + [/FONT][/COLOR][COLOR=maroon][FONT=Courier New]"   BarsAgo:"[/FONT][/COLOR][COLOR=black][FONT=Courier New] + prevMacdHighBar);[/FONT][/COLOR]
    [/FONT][/COLOR]Here’s what Output window looks like [attached]

    So say I’m interested in Value 0.9923, how do I figure out the barsAgo for this value?

    My purpose is to draw a dot on the MACD line, so I believe I need the correct combination of Value & barsAgo to define the location of the dot on the chart.
    Attached Files

    #2
    Hello ionaz,

    Thank you for your post.

    The int value of -1 is letting you know that no Swing High was found within your lookback period of 5 bars. I have tested your code on my end and the prevMachHighBar can be used as the barsAgo int in the DrawDot() method.

    For additional information on Swing please visit the following link: http://www.ninjatrader.com/support/h.../nt7/swing.htm

    Please let me know if I may be of further assistance.

    Comment


      #3
      Hi Patrick

      Thanks for your reply.

      You can see in my picture though that value 0.9923 pairs with a variety of "non-1" values. How does DrawDot know which one of them refers to the 0.9923 ?

      In other words I appreciate that as per your test prevMachHighBar can be used in the DrawDot. My problem is I don't know which value (paired with 0.9923) I have to feed to the DrawDot. You can see from the picture that prevMachHighBar varies between 5 and 15 as the bars update.

      Or I got you wrong; do you actually suggest that prevMachHighBar finds the right value. Right for me=the one which would allow to draw a dot where in my example prevMacdHigh=0.9923

      please clarify

      Comment


        #4
        Hello ionaz,

        Thank you for your response.

        I will need to test an instrument that has a -1 value for the int now. What instrument are you testing this on?

        Comment


          #5
          Hi Patrick

          The MACD code below works with EOD stocks data. I backtested the default DOW30.
          I've added MACD indicator panel under the price data and now attemt to draw 2 dots on the MACD line.

          I haven't tested prevMacdHighBar and prevMacdHigh with DrawDot yet. If you say it works - great! Looking at the Output window I assumed it won't work == I don't understand how prevMacdHigh and prevMacdHigh would find each other so that the dot is in the right place. Again, if you say it worked, great!

          If it didn't work, then this problem is still outstanding for me.
          If it worked for you without errors, can you pls share the DrawDot code you compiled using my prevMacdHighBar and prevMacdHigh.

          Thanks!

          Comment


            #6
            Hello ionaz,

            Thank you for your response.

            The current value is 0.9923 in your original example, so the bars ago value of that is 0. Are you trying to pull the barsAgo int for when the swing high started? If are you just checking to see if there was a swing high within the last 10 bars?

            Comment


              #7
              Hi Patrick

              Thanks for the post.

              Are you trying to pull the barsAgo int for when the swing high started?
              EXACTLY! that's my ultimate goal. If you could show me how to do that, I'd be very happy.


              Now I've added these lines of code to a MACD indicator's OnBarUpdate():

              Code:
              double prevMacdHigh = Swing(MACD(12, 25, 9), 5).SwingHigh[0]; 
                          int prevMacdHighBar = Swing(MACD(12, 25, 9), 5).SwingHighBar(0,1,10);     
                          
                          DrawOnPricePanel = false;
                          DrawDot("tag1", true, prevMacdHighBar, prevMacdHigh, Color.Red);
              Result: as expected Red dot appears correct in terms of Y, but in terms of X - it sits at the last bar. Don't know why.

              My prevMacdHighBar says .SwingHighBar(0,1,10). I checked the NT manual and 0 here means "The number of bars ago that serves as the starting
              bar and works backwards", so if I understand this correctly I think it makes sense to set it to 0, because I want to start looking for a swing from the end of the chart and go backwards. So I don't believe this is BarsAgo I need for the DrawDot (pls correct me if I'm wrong), because it wouldn't make sense: I need to find one, but I'm asked to input one instead.

              Anyway, my ultimate goal is laid in your words now, and is quoted at the top of this post.

              Comment


                #8
                Perhaps I'm missing something I'm not seeing any issues with your original image.

                This is printing the correct results for number of bars ago when the most recent swing occurred.

                Notice that the minimum value when a swing is Identified is 5. This is because you are using a Swing strength of 5. This will mean 5 bars have to pass before confirming the swing. From this point forward it will keep check for the next next 10 bars (your lookbackperiod). After these 10 bars have passed count will reach 15 bars and reset to -1 if a new swing has not been formed.

                Attached image shows how you can place the swing indicator on the MACD

                Let me know if I can be of further assistance.
                Attached Files
                LanceNinjaTrader Customer Service

                Comment


                  #9
                  Hi Lance

                  Thanks for your post.

                  My problem is not the output screen, I appreciate you explained why the values look so in the output. I'm also aware of the way how to put Swing on MACD as per your illustration.

                  However, I want to be able to do that programatically. And from what I can see I am very very close - just need to sort out the prevMacdHighBar which=barsAgo for when the swing high started. HOW CAN I DO THIS???

                  As you may have noticed I need the correct combo of prevMacdHigh and prevMacdHighBar to feed them into DrawDot so that the dot is in the right place on the MACD line.

                  Comment


                    #10
                    Lance, Patrick

                    Thanks a lot, my mistake. All I needed is to increase the lookback period.
                    Dot sits in the right place!

                    BR

                    Jonas

                    Comment


                      #11
                      Glad to hear it. Let us know if we can further assist
                      LanceNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Tim-c, Today, 03:54 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post Tim-c
                      by Tim-c
                       
                      Started by FrancisMorro, Today, 03:24 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post FrancisMorro  
                      Started by Segwin, 05-07-2018, 02:15 PM
                      10 responses
                      1,772 views
                      0 likes
                      Last Post Leafcutter  
                      Started by Rapine Heihei, 04-23-2024, 07:51 PM
                      2 responses
                      31 views
                      0 likes
                      Last Post Max238
                      by Max238
                       
                      Started by Shansen, 08-30-2019, 10:18 PM
                      24 responses
                      945 views
                      0 likes
                      Last Post spwizard  
                      Working...
                      X