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

ZigZag Indicator (how to get the number of Peaks and Troughs)

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

    ZigZag Indicator (how to get the number of Peaks and Troughs)

    I'd be grateful if someone can show me how I can get the number of peaks and troughs from the zigzag indicator for a given deviation value?

    #2
    Hello boreland,

    Thanks for your post.

    The help guide for the ZigAag indicator shows how to specify the ZigZag indicator parameters in Ninjascript and also how to obtain the High Values and the bars ago of those as well as the low values and the low bars ago of those. reference: https://ninjatrader.com/support/help...t8/?zigzag.htm

    When you specify the look back period, you can iterate through the number of high or low bar parameter "instances" counting them until you reach a -1 value indicating no more Highs or Lows.

    Here is a short example to get your started:

    At the class level: private ZigZag myZZ; // create a private instance to use.

    In OnStateChange():
    else if (State == State.DataLoaded)
    {
    myZZ = ZigZag(DeviationType.Points, 0.5, false); // initialize the private instance of the indicator
    }


    In OnBarUpdate():
    protected override void OnBarUpdate()
    {
    if (CurrentBar < 100) // wait for 100 bars before checking 100 bar look back
    return;

    int highCount = 0; // reset the counter

    if (CurrentBar % 100 == 0) // check every 100 bars
    {
    if (myZZ.HighBar(0, 1, 100) != -1) // if we have Highs (this is a sanity check)
    {
    for (int i = 1; i < 100; i++) // process the instances, will always be less than 100
    {
    if (myZZ.HighBar(0, i, 100) == -1) // if this instance is -1 we are done
    break;
    highCount++; // otherwise increment the counter
    }
    }
    Print (Time[0] +" Last 100 bars Peaks = "+highCount); // report the number of highs found in 100 bar sections
    }


    Example of output (ES 06-20, 1 minute bars):

    4/9/2020 5:56:00 AM Last 100 bars Peaks = 18
    4/9/2020 7:36:00 AM Last 100 bars Peaks = 21
    4/9/2020 9:16:00 AM Last 100 bars Peaks = 21
    4/9/2020 10:56:00 AM Last 100 bars Peaks = 21
    4/9/2020 12:36:00 PM Last 100 bars Peaks = 20
    4/9/2020 2:31:00 PM Last 100 bars Peaks = 22
    4/12/2020 5:11:00 PM Last 100 bars Peaks = 14
    4/12/2020 6:51:00 PM Last 100 bars Peaks = 19
    4/12/2020 8:31:00 PM Last 100 bars Peaks = 14
    4/12/2020 10:11:00 PM Last 100 bars Peaks = 14
    4/12/2020 11:51:00 PM Last 100 bars Peaks = 16
    4/13/2020 1:31:00 AM Last 100 bars Peaks = 10
    4/13/2020 3:11:00 AM Last 100 bars Peaks = 14
    4/13/2020 4:51:00 AM Last 100 bars Peaks = 14
    4/13/2020 6:31:00 AM Last 100 bars Peaks = 17
    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by franciscog21, 05-10-2024, 05:27 PM
    1 response
    30 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by llanqui, Today, 11:10 AM
    0 responses
    8 views
    0 likes
    Last Post llanqui
    by llanqui
     
    Started by llanqui, Today, 10:29 AM
    0 responses
    9 views
    0 likes
    Last Post llanqui
    by llanqui
     
    Started by llanqui, Today, 08:32 AM
    1 response
    16 views
    0 likes
    Last Post llanqui
    by llanqui
     
    Started by lollers, Yesterday, 03:26 AM
    1 response
    54 views
    0 likes
    Last Post lollers
    by lollers
     
    Working...
    X