Calculating previous candle size

Created at 26 Apr 2023, 13:16
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!
MR

Mr4x

Joined 12.04.2019

Calculating previous candle size
26 Apr 2023, 13:16


Hi,

I'd like to know what code is required to calculate the previous candle (preferably the body) in pip size.

Secondly, could you please give me an example of code to then place a stop/limit order the same size as the previous candle?

for example: code will calculate the pip size of the previous candle. it will then create a pending buy / sell order of the same number of pips as the size of the previous candle.

Thank you


@Mr4x
Replies

firemyst
27 Apr 2023, 04:49

Sample code snippets to get the size of the previous candle:

private Bars bars;

// In your OnStart or Initialize method depending on whether cBot or indicator
bars = MarketData.GetBars(Chart.TimeFrame);


//In the Calculate, OnTick, or OnBar method depending on whether indicator or cBot and when you want to know the difference
//If you want the body size
double differenceInPips = Math.Abs(bars.OpenPrices.Last(1) - bars.ClosePrices.Last(1)) / Symbol.PipSize;

//If you want the entire candle. No Math.Abs function needed
double differenceInPips = (bars.HighPrices.Last(1) - bars.LowPrices.Last(1)) / Symbol.PipSize;

 

 

Check out code samples to see how to place the order you want:
https://help.ctrader.com/ctrader-automate/cbot-code-samples/

 


@firemyst