MarketData.GetBarsAsync()

Created at 22 Apr 2020, 16:44
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!
KE

kenneyy_eur

Joined 22.04.2020

MarketData.GetBarsAsync()
22 Apr 2020, 16:44


Hello,

I have been struggling to get the code below to populate my collection straight-away.  It returns nothing,

                MarketData.GetBarsAsync(tf, asset, bars =>
                {
                    lbrs.Add(bars);
                });

i need to await the result - but how?  Please help!


@kenneyy_eur
Replies

PanagiotisCharalampous
22 Apr 2020, 17:11

Hi Kenneth,

I am not sure what is the problem. The below seems to be working fine for me

            var lbrs = new List<Bars>();
            MarketData.GetBarsAsync(TimeFrame, Symbol.Name, bars =>
            {
                lbrs.Add(bars);
                Print(lbrs.Count + " Bar collection added");
            });

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

kenneyy_eur
22 Apr 2020, 17:31 ( Updated at: 21 Dec 2023, 09:22 )

RE:

Hi Panagiotis and thanks for your quick response.

You are able to see something because you added the print line below the collection population line.

If you remove the print line and try accessing the collection below the MarketData.GetBarAsync block. The collection will be empty trust me.

I can add the print line to my code, but i know i need to add the bars to a Task<IEnumerable>  that's where i'm stuck . Once i achieve that, then i'm home and dry.

Deep appreciation for the work you do!

Regards

Kenneth

 

 

 

PanagiotisCharalampous said:

Hi Kenneth,

I am not sure what is the problem. The below seems to be working fine for me

            var lbrs = new List<Bars>();
            MarketData.GetBarsAsync(TimeFrame, Symbol.Name, bars =>
            {
                lbrs.Add(bars);
                Print(lbrs.Count + " Bar collection added");
            });

Best Regards,

Panagiotis 

Join us on Telegram

 


@kenneyy_eur

PanagiotisCharalampous
22 Apr 2020, 17:40

Hi Kenneth,

It will be empty because that line of code will be executed before the bars are retrieved. That's the whole point of asynchronous execution, to proceed with the execution of the main thread while another task is executed in parallel on the side. Why do you need to use GetBarsAsync() and not just GetBars()?

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

kenneyy_eur
22 Apr 2020, 17:53

RE:

PanagiotisCharalampous said:

Hi  again,

I have learnt something new thanks to you. 

My market scanner has to scan over 70 symbols and for each symbol it has to load the bars for 5 timeframes i.e 70 X 5  = 350 iterations = 52 seconds (synchronously)

A lot of time.

Ok, never mind and thanks for your help. I will go digging and find the right way to implement this without the Print line.

Regards

Kenneth

 

 

Hi Kenneth,

It will be empty because that line of code will be executed before the bars are retrieved. That's the whole point of asynchronous execution, to proceed with the execution of the main thread while another task is executed in parallel on the side. Why do you need to use GetBarsAsync() and not just GetBars()?

Best Regards,

Panagiotis 

Join us on Telegram

 


@kenneyy_eur