NexusFi: Find Your Edge


Home Menu

 





highest bar close occuring within the last X number of bars . . . .


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Loukas with 16 posts (0 thanks)
    2. looks_two forrestang with 12 posts (8 thanks)
    3. looks_3 jmejedi with 6 posts (0 thanks)
    4. looks_4 Fat Tails with 2 posts (1 thanks)
    1. trending_up 23,168 views
    2. thumb_up 10 thanks given
    3. group 4 followers
    1. forum 37 posts
    2. attach_file 5 attachments




 
Search this Thread

highest bar close occuring within the last X number of bars . . . .

  #11 (permalink)
Loukas
London
 
Posts: 27 since Jun 2011
Thanks Given: 4
Thanks Received: 2

Thanks forrestang,

I am trying to create a condition to enter "Long" when the current "Close" price "CrossAbove" the highest price from the previous 5 highs. I done this with two different ways, by creating a New Dataseries and also as a Double value but it seems that it is not working. Do you have any ideas why is not working? Thanks

1. double HighW=Math.Max(High[0],5);
if (CrossAbove(Close,HighWk1))
{...

2. HighWeekly.Set(Math.Max(High[0],5));
if (CrossAbove(Close,HighWeekly,1))
{...

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
Deepmoney LLM
Elite Quantitative GenAI/LLM
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Better Renko Gaps
The Elite Circle
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
38 thanks
NexusFi site changelog and issues/problem reporting
27 thanks
GFIs1 1 DAX trade per day journal
19 thanks
The Program
18 thanks
  #12 (permalink)
 
forrestang's Avatar
 forrestang 
Chicago IL
 
Experience: None
Platform: Ninja, MT4, Matlab
Broker: CQG, AMP, MB, DTN
Trading: E/U, G/U
Posts: 1,329 since Jun 2010
Thanks Given: 354
Thanks Received: 1,047


Loukas View Post
Thanks forrestang,

I am trying to create a condition to enter "Long" when the current "Close" price "CrossAbove" the highest price from the previous 5 highs. I done this with two different ways, by creating a New Dataseries and also as a Double value but it seems that it is not working. Do you have any ideas why is not working? Thanks

1. double HighW=Math.Max(High[0],5);
if (CrossAbove(Close,HighWk1))
{...

2. HighWeekly.Set(Math.Max(High[0],5));
if (CrossAbove(Close,HighWeekly,1))
{...

Did you not like the solution I posted? I think it does exactly what you where asking for. Just wondering if there was some reason you didn't go with that? There are dozens of ways to do what you want, what I posted was just one possible way of doing it.

The two examples you posted above will not accomplish what you want. You are referencing a C# function when you are using "Math.Max," and that function returns the HIGHEST of two values you pass it.

So for example when you pass it the value of "High[0]" and "5", it will simply return the highest value of those two numbers. In this case, you are passing it the HIGH of the current bar, and the number 5. So if you are using an instrument like EurUsd which is commonly 1.xxxx, the statement......... Math.Max(High[0],5) will ALWAYS return 5, cause the EurUsd doesn't trade that high. If you were applying that to say the TF which is currently trading above 600s, it will always return whatever 'High[0]' is. Does that make sense?

So if say you pass "Math.Max" two numbers, say 3 and 5, it will return 5. More examples.......

Math.Max( 3, 5) = 5
Math.Max( 5, 3) = 5
Math.Max( 20,2) = 20
Math.Max( 2, 20) = 20

Math.Min( 20,2) = 2
Math.Min( 3, 5) = 3


If you want to reference the highest value of a few bars ago, you will want to use the Ninjatrader Function "MAX()" as opposed to "Math.Max." I made this mistake a few weeks ago myself

Reply With Quote
  #13 (permalink)
Loukas
London
 
Posts: 27 since Jun 2011
Thanks Given: 4
Thanks Received: 2


Hi forrestang,

thanks for your input. I applied your suggestion but it is not complited.
HighWeekly.Set(Max(High,5)[0]);
if (CrossAbove(Close,HighWeek,1))

or as double variable
double HighWeek=MAX(High,5)[0];
if (CrossAbove(Close,HighWeek,1))

Do you have any ideas how can I solve his issue?

Reply With Quote
  #14 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102


Loukas View Post
Hi forrestang,

thanks for your input. I applied your suggestion but it is not complited.
HighWeekly.Set(Max(High,5)[0]);
if (CrossAbove(Close,HighWeek,1))

or as double variable
double HighWeek=MAX(High,5)[0];
if (CrossAbove(Close,HighWeek,1))

Do you have any ideas how can I solve his issue?

If you save your .cs file as it is and post it here, somebody will review and modify it. A common mistake is accessing bars with a negative index, that is looking back 5 bars from the first bar of the bar series. To avoid this problem, you would need to insert

 
Code
if(CurrentBar < 5)
return;
at the beginning of OnBarUpdate. But as long as I cannot see what you have coded, this is mere speculation.

Reply With Quote
Thanked by:
  #15 (permalink)
Loukas
London
 
Posts: 27 since Jun 2011
Thanks Given: 4
Thanks Received: 2

Here is the file

Attached Files
Elite Membership required to download: AtrendtoTrendSample.cs
Reply With Quote
  #16 (permalink)
 
forrestang's Avatar
 forrestang 
Chicago IL
 
Experience: None
Platform: Ninja, MT4, Matlab
Broker: CQG, AMP, MB, DTN
Trading: E/U, G/U
Posts: 1,329 since Jun 2010
Thanks Given: 354
Thanks Received: 1,047


Loukas View Post
Here is the file

Ok, looks like you are creating an indicator, but trying to use things you would put into a strategy. This is why you couldn't compile.

What I usually do if I am wanting to do a strategy, start with an indicator first, then modify it to perform as a strategy. As most of the logic you create for your indie will work on a strategy depending on how you write it. In fact I usually just copy the entire script and paste it in my strategy and make minor changes.

When you want to create a strategy, you will want to go to Tools -> NewNinjaScript -> Strategy. Strategies are used to create an ATS(auto-trade strategy).

If you just want visual aids on your chart, you will want to go Tools -> NewNinjaScript -> Indicator. Indicators just are used to plot various visuals on your chart. So there is some difference between the two.

In either case, go through the editor, get to the last step then hit generate/unlock, and we'll start from there with the new .cs file you post in here.

Reply With Quote
  #17 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker: Interactive Brokers
Trading: Keyboard
Posts: 9,888 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,102


Loukas View Post
Here is the file

In NinjaScript there is no such thing as Plot0, you just invented it. As it is a variable which is not declared, the compiler cannot read it. Try to replace Plot0[0] with Values[0][0].

Reply With Quote
  #18 (permalink)
Loukas
London
 
Posts: 27 since Jun 2011
Thanks Given: 4
Thanks Received: 2

""Ok, looks like you are creating an indicator, but trying to use things you would put into a strategy. This is why you couldn't compile.""

The problem is not there because I have done the same think from the beging with a simple condition EMA10 > EMA 20 and it is not make a trade because of the CrossAbove.

I change the condition with the statment below and it is working. The problem is this the CrossAbove and I cannot understad why.

if (Close[0]>=MAX(High,Weekly)[1])
{if ( EMA(5)[0]>EMA(24)[0]
enter long

Reply With Quote
  #19 (permalink)
Loukas
London
 
Posts: 27 since Jun 2011
Thanks Given: 4
Thanks Received: 2

I am trying something simple just to improve my skills, I have tried 3 ways but no one look to working eventhough they are compiled.
1. if (CurrentBar < Weekly )
return;
double HighWeek=MAX(High,Weekly)[0];
if (Close[0]>HighWeek)
{
EnterLong(DefaultQuantity, "");
}
2. HighWeek.Set(MAX(High,Weekly)[0]); if (CrossAbove(Close,HighWeek,1))
{
EnterLong(DefaultQuantity, "");
}
3.
if (Close[0]>MAX(High,Weekly)[0])
{
EnterLong(DefaultQuantity, "");
}
I dont thing so this is something sophisticated, I just cannot understand why is not working. Can someone guide me how to solve this .

Reply With Quote
  #20 (permalink)
 
forrestang's Avatar
 forrestang 
Chicago IL
 
Experience: None
Platform: Ninja, MT4, Matlab
Broker: CQG, AMP, MB, DTN
Trading: E/U, G/U
Posts: 1,329 since Jun 2010
Thanks Given: 354
Thanks Received: 1,047


@Loukas

Start with this. Simple strategy that buys the breakout if CLOSE above the highest high of the past 5 bars. Conversely, it SELLS the breakout if CLOSE below lowest low of prior 5 bars.

It has set in it an arbitrary 8 tic profit target, with a 16 tic stoploss.

--EDIT---
Initially put the wrong file up there. It is correct now though if you downloaded before I typed this.

Attached Thumbnails
Click image for larger version

Name:	Prime2011-09-10_192358.jpg
Views:	444
Size:	287.3 KB
ID:	48986  
Attached Files
Elite Membership required to download: LoukasBarBreakout.zip
Reply With Quote
Thanked by:




Last Updated on October 6, 2014


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts