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

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:
    [FONT=Courier New]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[/FONT]
    This code should get you started. It will show a tick delta between SwingHighBar s.

    Code:
    [FONT=Courier New]        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);
                }
            }[/FONT]
    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


      #3
      How would I get the average over the past 5/10/50/etc.?? Thanks

      Comment


        #4
        Hi elcowen, thanks for your question.

        In the example, it looks like there is a hard coded 5 in the for loop, so if you wanted the loop to be of variable size, you would need to use a user input or variable of some kind. e.g.

        Code:
                    for (int i = 1; i < VarX; i++)
                    {
                        int        curHighInt    = Swing(5).SwingHighBar(0, i, CurrentBar-1);
                        int        priHighInt    = Swing(5).SwingHighBar(0, i+1, CurrentBar-1);
        Kind regards.
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Jon17, Today, 04:33 PM
        0 responses
        1 view
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        4 views
        0 likes
        Last Post Javierw.ok  
        Started by timmbbo, Today, 08:59 AM
        2 responses
        10 views
        0 likes
        Last Post bltdavid  
        Started by alifarahani, Today, 09:40 AM
        6 responses
        41 views
        0 likes
        Last Post alifarahani  
        Started by Waxavi, Today, 02:10 AM
        1 response
        19 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Working...
        X