Bars.Last() significantly slower than Bars.Last(1)!!!
Bars.Last() significantly slower than Bars.Last(1)!!!
06 Jul 2023, 22:30
Dear all,
I was wondering why some of my cBots run significantly sloer than others and today I found the reason. When backtesting my bots I can see a huge difference between bost using the function call Bars.Last() vs. Bars.Last(1).
To proof my observation I developed the following Test bot. The lower the timeframe you choose and irrespectivly whether you are using Tick data or Bar open data for backtest you should see a big difference in execution time.
In my case when backtesting for a month on 1M calling Last() it takes 42 seconds to finish the test. The same test just using Last(1) will finish in 0.2 seconds!!!
You can test yourself.
PS: I am using cTrader 4.7.13
Test Bot source code
I am using MarketData.GetBars in my script, but I can observe the same slow performance with just simple calling Bars.Last()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class Test : Robot
{
[Parameter("Time Frame")]
public TimeFrame InpTimeFrame { get; set; }
[Parameter("Use Bars.Last()", DefaultValue = false)]
public bool InpUseLast { get; set; }
private Bars myBars;
private DateTime startTime;
protected override void OnStart()
{
myBars = MarketData.GetBars(InpTimeFrame);
startTime = DateTime.Now;
}
protected override void OnBar()
{
var bar = InpUseLast ? myBars.Last() : myBars.Last(1);
}
protected override void OnStop()
{
Print("Duration: ", DateTime.Now - startTime);
}
}
}
Replies
Best.Algo.Trader
07 Jul 2023, 10:21
Indeed, Last(0) is as fast as Last(1) but since Last() does compile and also seems to deliver the same result (only much slower) I wonder why this has not been handled in code.
Anyway, I hope my post here will help others facing the same issue.
@Best.Algo.Trader
Best.Algo.Trader
07 Jul 2023, 10:27
( Updated at: 07 Jul 2023, 10:28 )
For the sake of completion. that would be the correct link to the Bars.Last() documentation.
@Best.Algo.Trader
PanagiotisChar
08 Jul 2023, 08:07
Hi Kaspricci,
Last() is not a part of cTrader Automate API. It's part of Linq. So talk to Microsoft :)
Need help? Join us on Telegram
@PanagiotisChar
Best.Algo.Trader
08 Jul 2023, 13:16
( Updated at: 08 Jul 2023, 13:18 )
RE:
PanagiotisChar said:
Hi Kaspricci,
Last() is not a part of cTrader Automate API. It's part of Linq. So talk to Microsoft :)
Need help? Join us on Telegram
I understand that this is a function of a parent class of Bars. But when I was still a professional developer we ensured that all methods of our classes, even those we simply inherit are working properly. So why not providing a method in Bars class which overwrites it by returning Last(0).
Just a thought...
@Best.Algo.Trader
PanagiotisChar
10 Jul 2023, 08:29
Hi Kaspricci,
I understand that this is a function of a parent class of Bars.
No it is not. As I explained above is a part of LINQ.
Need help? Join us on Telegram
@PanagiotisChar
Best.Algo.Trader
10 Jul 2023, 10:56
( Updated at: 10 Jul 2023, 20:02 )
Ok, first of all I want to make clear that I made this post to support others avoid making the same mistake as I did suffering from a low performance of Last(). I don't have the impression that this intention is appreciated here in this forum.
@Best.Algo.Trader
firemyst
07 Jul 2023, 03:12
You're using the .Last incorrectly.
.Last should have an index indicator how many bars back you want.
If you want the last (most recent) value, then you need to use either:
.Last(0)
.LastValue
@firemyst