Backtest for a multisymbol strategy crashes after upgrade to ctrader 5.0.x
Backtest for a multisymbol strategy crashes after upgrade to ctrader 5.0.x
10 Nov 2024, 09:12
I’m in the process of building a multisymbol strategy, to trade a universe of US stocks on a daily timeframe.
Backtesting this strategy framework was working fine on cTrader version 4.8.30, but since the upgrade to version 5.0.40 and later 5.0.44, the bot crashes with the following error.
09/07/2024 13:31:00.229 | Trade | Executing Market Order to Buy 157,6 INTC.US
09/07/2024 13:31:00.229 | Trade | → Executing Market Order to Buy 157,6 INTC.US SUCCEEDED, Position PID2
10/07/2024 13:31:00.340 | Error | CBot instance [MultiSymbolTest, AAPL.US, D1] crashed with error #98D77193.
To reproduce the issue, the following bot can be used. For the instance, choose AAPL.US and the daily timeframe.
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.FullAccess)]
public class MultiSymbolTest : Robot
{
// Multi symbol bot test
string[] symbols = { "AAXJ.US",
"MMM.US",
"AXP.US",
"AMGN.US",
"AAPL.US",
"BA.US",
"CAT.US",
"CVX.US",
"CSCO.US",
"KO.US",
"DIS.US",
"DOW.US",
"GS.US",
"HD.US",
"HON.US",
"IBM.US",
"INTC.US",
"JNJ.US",
"JPM.US",
"MCD.US",
"MRK.US",
"NKE.US",
"PG.US",
"CRM.US",
"VZ.US",
"V.US",
"WMT.US" };
string Label = "TestMultistrat";
List<RSI_Strategy> strategies = new List<RSI_Strategy>();
protected override void OnStart()
{
foreach ( var sym in symbols )
{
var rsiStrategy = new RSI_Strategy();
rsiStrategy.init( this, sym, Label );
strategies.Add( rsiStrategy );
}
}
protected override void OnBar()
{
List<Signal> signals = new List<Signal>();
foreach (var strat in strategies)
{
foreach (var pos in Positions.Where(x => x.SymbolName == strat.symbolName && x.Label == Label))
{
Print("Exit");
Print(strat.symbolName);
if (strat.ExitLong())
{
pos.Close();
}
}
if (strat.EntryLong())
{
Print("Entry");
Print(strat.symbolName);
Print(strat.PositionScore());
Print(String.Format("Entry for {0} typre {1} score {2}", strat.symbolName, TradeType.Buy, strat.PositionScore()));
signals.Add(new Signal(strat.symbolName, TradeType.Buy, strat.PositionScore()));
}
}
var noPositions = Positions.Count(x => x.Label == Label);
if (signals.Any() && noPositions < 10)
{
Print("Signal processing");
foreach (var signal in signals.Where(x => Positions.All(p => p.SymbolName != x.SymbolName && p.Label == Label)).OrderByDescending(x => x.SignalScore).Take(10 - noPositions))
{
Print(signal.Side + " " + signal.SymbolName);
ExecuteMarketOrder( signal.Side, signal.SymbolName, 10, Label, 0, 0 );
}
}
}
}
internal class Signal
{
public string SymbolName { get; set; }
public TradeType Side { get; set; }
public double SignalScore { get; set; }
public Signal(string name, TradeType TrdTpe, double posScore)
{
SymbolName = name;
Side = TrdTpe;
SignalScore = posScore;
}
}
// Sell when RSI crosses above 70
public class RSI_Strategy
{
RelativeStrengthIndex rsi;
Robot bot;
string symbol;
string label;
Bars bars;
public string symbolName;
public void init( Robot bot, string symbol, string lab )
{
this.symbol = symbol;
this.symbolName = symbol;
this.bot = bot;
this.label = lab;
Bars bars = bot.MarketData.GetBars( bot.TimeFrame, symbol );
rsi = bot.Indicators.RelativeStrengthIndex( bars.ClosePrices, 14 );
}
public double PositionScore()
{
return 1;
}
public bool EntryLong()
{
return rsi.Result.Last(1) > 70 && rsi.Result.Last(2) <= 70;
}
public bool ExitLong()
{
return rsi.Result.Last(1) > 80;
}
}
}
If the ‘ExecuteMarketOrder' is commented out, the backtesting is running fine and the signals are logged in the console. It also looks like the error occurs when a position is opened for a symbol other than the symbol selected in the instance.
I found this post with kind of the same error, but no fix https://ctrader.com/forum/ctrader-algo/41525/. I do use the same broker (Pepperstone) and have this issue on both the Live and Demo account.
Also the example of a multibot as described in this post https://ctrader.com/forum/ctrader-algo/22633/ won’t work, when changing the symbols list to the list of my example.
It seems to me that a change in the newer version has introduced this bug. Can this be investigated? If there is anymore troubleshooting info needed, please let me know.
Best regards,
Richard
Replies
richardsmits007
11 Nov 2024, 07:12
RE: Backtest for a multisymbol strategy crashes after upgrade to ctrader 5.0.x
PanagiotisCharalampous said:
Hi Richard,
Can you please let us know your broker as well?
Best regards,
Panagiotis
Hi Panagiotis,
I'm using Pepperstone (Europe) as broker.
Best regards,
Richard
@richardsmits007
PanagiotisCharalampous
11 Nov 2024, 08:16
RE: RE: Backtest for a multisymbol strategy crashes after upgrade to ctrader 5.0.x
richardsmits007 said:
PanagiotisCharalampous said:
Hi Richard,
Can you please let us know your broker as well?
Best regards,
Panagiotis
Hi Panagiotis,
I'm using Pepperstone (Europe) as broker.
Best regards,
Richard
Thank you. The issue will be investigated and resolved in an upcoming update
@PanagiotisCharalampous
richardsmits007
11 Nov 2024, 11:22
RE: RE: RE: Backtest for a multisymbol strategy crashes after upgrade to ctrader 5.0.x
PanagiotisCharalampous said:
Thank you. The issue will be investigated and resolved in an upcoming update
Thank you Panagiotis. I'm looking forward to the upcoming update.
@richardsmits007
PanagiotisCharalampous
11 Nov 2024, 06:34
Hi Richard,
Can you please let us know your broker as well?
Best regards,
Panagiotis
@PanagiotisCharalampous