How to Access OHLC Prices in the Backtester?

Created at 21 Aug 2024, 15:26
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!
SC

scotpip

Joined 24.07.2018

How to Access OHLC Prices in the Backtester?
21 Aug 2024, 15:26


Attempting to write my first cTrader Algo.

Bemused to realise that in the backtester, Bars.LastBar.Open/High/Low/Close seem to contain the same (Open) price.

Then I noticed that in the backtester Data settings, it only offers Open prices for the M1 bars. So I tried the Tick Data setting, but I'm still only seeing the Open price.

Is it really not possible to access the HLC prices in the backtester?? This doesn't seem to be covered explicitly in the docs.

Hopefully I'm being dim and missing something, otherwise the backtester is useless to anyone who trades price action.


@scotpip
Replies

scotpip
21 Aug 2024, 16:29

Another issue - the dropdown Data selector is showing a duplicate entry for the "m1 bars from Server" option. Is my client corrupted in some way? This clearly isn't right.
 



 


@scotpip

PanagiotisCharalampous
22 Aug 2024, 05:05

Hi there,

If you are calling LastBar in OnBar() then you will get the same prices, since the method is called on bar opening. Try OnBarClosed() instead and you should get the OHLC values.

Best regards,

Panagiotis


@PanagiotisCharalampous

scotpip
22 Aug 2024, 18:12 ( Updated at: 23 Aug 2024, 06:31 )

RE: How to Access OHLC Prices in the Backtester?

Hi Panagiotis

Many thanks! That seems to be working now, both with m1 data and tick data.

I assume that all 4 prices are used when evaluating exit stops, and calculating indicators like the ATR which need OHLC values?

If you're open to feedback, I feel that the bar event methods and docs are confusing. This is something you might care to review.

In the tutorial, OnBarClosed() isn't even mentioned:

https://help.ctrader.com/ctrader-algo/articles/for-developers/create-a-cbot-in-5-mins/#add-a-new-cbot

The wording doesn't make it explicit that you will be accessing an incomplete new bar, rather than the newly completed old bar with all the OHLC data. In every other platform I've used (quite a few now) OnBar() passes or signals the interesting old bar rather than the uninteresting new bar.

The next point of confusion is the dropdown from the cog menu item, which clearly states that you are only getting open prices. While in fact as you have just explained you can access all 4 prices. (And there's also the bug where it shows the same option twice). Surely this is a UX glitch?

So unless the user takes a deep dig through the API docs, the procedure for accessing this vital data is both unintuitive (in the sense that other platforms do it differently) and well hidden (in the sense that the tutorial doesn't explain it and the cog dropdown text is misleading).

Maybe I'm unusually dim, but I'm surprised that more people haven't run into this roadblock. It would be helpful if the tutorial and the dropdown were more user-friendly…

Geoff


@scotpip

PanagiotisCharalampous
23 Aug 2024, 06:46

RE: RE: How to Access OHLC Prices in the Backtester?

scotpip said: 

Hi Panagiotis

Many thanks! That seems to be working now, both with m1 data and tick data.

I assume that all 4 prices are used when evaluating exit stops, and calculating indicators like the ATR which need OHLC values?

If you're open to feedback, I feel that the bar event methods and docs are confusing. This is something you might care to review.

In the tutorial, OnBarClosed() isn't even mentioned:

https://help.ctrader.com/ctrader-algo/articles/for-developers/create-a-cbot-in-5-mins/#add-a-new-cbot

The wording doesn't make it explicit that you will be accessing an incomplete new bar, rather than the newly completed old bar with all the OHLC data. In every other platform I've used (quite a few now) OnBar() passes or signals the interesting old bar rather than the uninteresting new bar.

The next point of confusion is the dropdown from the cog menu item, which clearly states that you are only getting open prices. While in fact as you have just explained you can access all 4 prices. (And there's also the bug where it shows the same option twice). Surely this is a UX glitch?

So unless the user takes a deep dig through the API docs, the procedure for accessing this vital data is both unintuitive (in the sense that other platforms do it differently) and well hidden (in the sense that the tutorial doesn't explain it and the cog dropdown text is misleading).

Maybe I'm unusually dim, but I'm surprised that more people haven't run into this roadblock. It would be helpful if the tutorial and the dropdown were more user-friendly…

Geoff

Hi Geoff,

Thanks for your suggestions. Here are my responses to some of the points raised

The next point of confusion is the dropdown from the cog menu item, which clearly states that you are only getting open prices.

You get all historical OHLC prices but the execution is based on the open prices only

And there's also the bug where it shows the same option twice

It's not really a bug. In general, these are two different options, the one is to use the selected timeframe's bars, the other is to use m1 bars (even if you are on h1). When you choose m1 these timeframes coincide. It looks weird in this specific case but I not think it's a real problem.

Best regards,

Panagiotis


@PanagiotisCharalampous