If current bar crosses previous bars high

Created at 12 May 2023, 12:07
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
BG

BGBG

Joined 12.05.2023

If current bar crosses previous bars high
12 May 2023, 12:07


Executing a market order when the current bar closes higher than the previous bars high is simple...

if(Bars.ClosePrices.Last(1) > Bars.HighPrices.Last(2)) {ExecuteMarketOrder........

How do I execute a market order as soon as the current bar is greater than the previous bars high?

- - -

Please note: I've been trying to implement this for hours and searched the forums for potential code examples, to no avail. It's not a question born out of laziness. 


@BGBG
Replies

PanagiotisChar
12 May 2023, 13:51

Hi there,

Try

if(Bars.ClosePrices.Last(0) > Bars.HighPrices.Last(1)) 

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar

pick
12 May 2023, 16:39

All you'd need to do is check whether the bid is higher than the previous candle high (but only once per bar I'm assuming):

bool prevHighViolated = true;

protected override void OnTick()
{
	if (!prevHighViolated && Bid > Bars.HighPrices.Last(1))
	{
		Print($"Last high: {Bars.HighPrices.Last(1)} violated");
		prevHighViolated = true;
		
		//Order logic here
	}
}

protected override void OnBar()
{
	prevHighViolated = false;
}

 


@pick

BGBG
13 May 2023, 02:22 ( Updated at: 13 May 2023, 02:58 )

Thank you both!

Pick, you answered the next burning question I had with PanagiotisChar's suggestion... "how do I get it to fire once per bar". Your solution works exactly as I'd hoped, thanks very much for taking the time, it's appreciated.

Thanks guys,

W.


@BGBG