• 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.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Swing length

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

    Swing length

    Hello,

    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,
    Fib
    Attached Files

    #2
    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 :

    Code:
    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.

    Code:
            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

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by ballboy11, Today, 05:22 PM
    0 responses
    3 views
    0 likes
    Last Post ballboy11  
    Started by ballboy11, Today, 04:33 PM
    2 responses
    4 views
    0 likes
    Last Post ballboy11  
    Started by dedouglas, Today, 03:16 PM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by TradeMyth, Today, 02:45 PM
    3 responses
    26 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by Ticnician, Today, 01:46 PM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_Jim  
    Working...
    X