• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


Swing length

  • Filter
  • Time
  • Show
Clear All
new posts

    Swing length


    I focus a lot on the swing lengths and I am seeking some programming help with calculating lengths a little differently.

    Normally, the length of a swing is drawn and calculated from say, LL to LH, and the amount of ticks from the lowest value to highest value is displayed.

    I want to calculate the amount of ticks that is between the previous swing levels, or to put it a little differently, I want to calculate the amount in ticks the instrument moves beyond a previous level.

    I have attached a snippit of a basic NT8 chart with the Swing indicator set to default 5, and have used the ruler to draw the area's im trying to calculate. So instead of having to manually calculate how far the market moves from its previous swing break, Ideally I would love an indicator that will display those numbers for me automatically.

    Thank you for your help,
    Attached Files

    Hello fibbee,

    The built-in indicator Swing contains the methods SwingHighBar and SwingLowBar, which return the number of bars ago a swing occurred, or -1 if a swing of the appropriate type is not found within the look back period. We can take advantage of this method to accomplish this goal.

    In pseudo code, this is what our indicator will do :

    Ask the user whether they want deltas between High or Low swings
    Grab the 2 most recent SwingBars of the appropriate type
    Return the tick delta between the most recent and second most recent SwingBar of the appropriate type
    This code should get you started. It will show a tick delta between SwingHighBar s.

            protected override void OnBarUpdate()
                if (CurrentBar <= 10) return;
                // let's say we just want to do this 5 times
                for (int i = 1; i < 5; i++)
                    int        curHighInt    = Swing(5).SwingHighBar(0, i, CurrentBar-1);
                    int        priHighInt    = Swing(5).SwingHighBar(0, i+1, CurrentBar-1);
                    // return if no swing found for either instance
                    if (curHighInt == -1 || priHighInt == -1) return;
                    double     curHigh        = Swing(5).SwingHigh[curHighInt];
                    double     priHigh        = Swing(5).SwingHigh[priHighInt];
                    // draw line connecting points
                    Draw.Line(this, "hiLine"+i.ToString(), priHighInt, priHigh, curHighInt, curHigh, Brushes.Green);
                    // draw text for number of ticks in distance
                    int        distance    = Convert.ToInt32(Math.Abs(curHigh - priHigh) / TickSize);
                    Draw.Text(this, "hiText"+i.ToString(), distance.ToString(), curHighInt, curHigh + 2 * TickSize);
    A few improvements as an exercise to you will need to be made before this is production ready

    • Use a while loop, or an input parameter, rather than hard-coding a 5 iteration loop
    • Set up a user parameter to allow users to choose between SwingHighBar and SwingLowBar
      • The code will need to be modified to reflect this change
    • Allow output in prices and percentages as well as ticks
      • This will require another input parameter
      • The ROC indicator will help you with percentages

    Happy trading and happy coding!
    Jessica P.NinjaTrader Customer Service


    Latest Posts


    Topics Statistics Last Post
    Started by yeshujbp, Today, 08:10 PM
    0 responses
    1 view
    Last Post yeshujbp  
    Started by backtester831, Today, 06:54 PM
    0 responses
    1 view
    Last Post backtester831  
    Started by frayrengytrader, Today, 06:03 PM
    0 responses
    Last Post frayrengytrader  
    Started by JustAFish, Today, 05:29 PM
    0 responses
    Last Post JustAFish  
    Started by TraderCAD, Today, 04:27 PM
    1 response
    Last Post TraderCAD