Thanks.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Count number of bars back for swing high
Collapse
X
-
Hello brucelevy,
You could use Max to quickly find the maximum number of bars back the last Swing High took place. Here is the Swing Help Guide and the Maximum (MAX) Help Guide to assist you further.
Here is an example that prints the number of bars back the last Swing High to the output window.
[/LEFT]Code:[COLOR=#000000][FONT=Tahoma][LEFT] [FONT=Courier New][SIZE=2][COLOR=#0000FF]protected[/SIZE] [FONT=Courier New][SIZE=2][COLOR=#0000FF]override[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#0000FF]void[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]OnBarUpdate()[/SIZE][/FONT][/FONT][/COLOR][COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2] { [/SIZE][/FONT][/LEFT] [/FONT][/COLOR][COLOR=#000000][FONT=Tahoma][LEFT] [FONT=Courier New][SIZE=2][COLOR=#0000FF]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2](Swing([/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]5[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]).SwingHigh[[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]] >=[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]5[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2])[/SIZE][/FONT][/LEFT] [/FONT][/COLOR][COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2] {[/SIZE][/FONT][/LEFT] [/FONT][/COLOR][COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2] Print([/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800000]"It has been "[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]+ MAX(Swing([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]5[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]).SwingHigh,[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]14[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2])[[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]] +[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800000]" bars since the last Swing High"[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]);[/SIZE][/FONT][/LEFT] [/FONT][/COLOR][COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2] } [/SIZE][/FONT][/LEFT] [/FONT][/COLOR][COLOR=#000000][FONT=Tahoma][LEFT][FONT=Courier New][SIZE=2] }[/SIZE][/FONT][/LEFT] [/FONT][/COLOR]
[/FONT][/COLOR]
-
Originally posted by brucelevy View PostHow can I print the number of bars back a prior swing high took place?
Thanks.
That is a documented available output directly of the Swing() indicator.
ref: http://ninjatrader.com/support/helpGuides/nt7/swing.htm
In fact, it is the very first method signature listed.
Comment
-
Thanks, it is now showing how many bars back the swing occurred but I am now trying to find out how many bars ago price crossed and closed above that swing?
if(Swing( 5).SwingHigh[ 0] >= 5)
{
Draw.TextFixed(this, "Prior Swing High", "Bars since swing high: "+Swing(5 ).SwingHighBar(0,1, 200), TextPosition.TopRight);
}
Comment
-
Hello brucelevy,
You can use GetBar() to return the number of bars ago the price crossed and closed above the swing.
Here is the GetBar() Help Guide to assist you further.
Here is an example using GetBar().
[/LEFT][/FONT][/COLOR]Code:[LEFT] [FONT=Times New Roman][SIZE=2][COLOR=#ADD8E6]protected[/SIZE] [FONT=Times New Roman][SIZE=2][COLOR=#ADD8E6]override[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#ADD8E6]void[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]OnBarUpdate[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2]()[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2] {[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2] [/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#ADD8E6]if[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2]([/SIZE][/FONT][FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]CrossAbove[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2]([/SIZE][/FONT][FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]Close[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2],[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]Swing[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2]([/SIZE][/FONT][FONT=Times New Roman][SIZE=2][COLOR=#22CDFF]5[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2]).[/SIZE][/FONT][FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]SwingHighBar[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2]([/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#22CDFF]0[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2],[/SIZE][/FONT][FONT=Times New Roman][SIZE=2][COLOR=#22CDFF]1[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2],[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#22CDFF]200[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2]),[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#22CDFF]5[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2]) &&[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]Close[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][[/SIZE][/FONT][FONT=Times New Roman][SIZE=2][COLOR=#22CDFF]0[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2]] >=[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]Swing[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2]([/SIZE][/FONT][FONT=Times New Roman][SIZE=2][COLOR=#22CDFF]5[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2]).[/SIZE][/FONT][FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]SwingHigh[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2][[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#22CDFF]0[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2]])[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2] {[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2] [/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#ADD8E6]int[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]startTime[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2]=[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]ToTime[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2]([/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]Time[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2][[/SIZE][/FONT][FONT=Times New Roman][SIZE=2][COLOR=#22CDFF]0[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2]]);[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2] [/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#ADD8E6]int[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]barsAgo[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2]=[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]CurrentBar[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2]-[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]Bars[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2].[/SIZE][/FONT][FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]GetBar[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2]([/SIZE][/FONT][FONT=Times New Roman][SIZE=2][COLOR=#ADD8E6]new[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]DateTime[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2]([/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]startTime[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2]));[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2] [/SIZE][/FONT] [FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]Print[/COLOR][/SIZE][/FONT][FONT=Times New Roman][SIZE=2]([/SIZE][/FONT][FONT=Times New Roman][SIZE=2][COLOR=#F0F0F0]barsAgo[/COLOR][/SIZE][/FONT] [FONT=Times New Roman][SIZE=2]);[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2] }[/SIZE][/FONT] [FONT=Times New Roman][SIZE=2] }[/SIZE][/FONT]
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
657 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
373 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
109 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
574 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
579 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment