No result found by backtesting my code
No result found by backtesting my code
05 Apr 2024, 04:26
No result found by backtesting my code
04 Apr 2024, 09:21
Dear CTrader Developers and Programmers,
I've written a small code that should reverse position (scalping) whenever 2 indicators ( SMA and EMA ) cross each other. I built the code without the system giving me any error message, however when I run my cbot using backtesting I don't get any result, I mean no capital gain or loss. It finishes the test and stays 0.00$ (0%).
What am I doing wrong? Can you help me please?
Thank you for everyone who gives me any idea!
Here is my code.
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
// Scalping when 2 indicators (SMA and EMA) cross each other.
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SMAEMACross : Robot
{
private SimpleMovingAverage MySMA;
private ExponentialMovingAverage MyEMA;
[Parameter("Source", Group = "Simple MA")]
public DataSeries SimpleMaPrice { get; set; }
[Parameter("Period", DefaultValue = 14, Group = "Simple MA")]
public int SimpleMaPeriod { get; set; }
[Parameter("Source", Group = "Exponential MA")]
public DataSeries ExponentialMaPrice { get; set; }
[Parameter("Period", DefaultValue = 14, Group = "Exponential MA")]
public int ExponentialMaPeriod { get; set; }
private double MyUnitVolume;
[Parameter("Volume (Lots)", DefaultValue = 0.01)]
public double MyVolumeInLots { get; set; }
public Position[] BotPositions
{
get
{
return Positions.FindAll(“”,SymbolName);
}
}
protected override void OnStart()
{
MyUnitVolume = Symbol.QuantityToVolumeInUnits(MyVolumeInLots);
MySMA = Indicators.SimpleMovingAverage(SimpleMaPrice, SimpleMaPeriod);
MyEMA = Indicators.ExponentialMovingAverage(ExponentialMaPrice, ExponentialMaPeriod);
}
protected override void OnBar()
{
if (SimpleMaPrice.Last(1) > ExponentialMaPrice.Last(1) && SimpleMaPrice.Last(2) <= ExponentialMaPrice.Last(2))
{
ClosePositions(TradeType.Buy);
ExecuteMarketOrder(TradeType.Sell, SymbolName, MyUnitVolume);
}
else if (SimpleMaPrice.Last(1) < ExponentialMaPrice.Last(1) && SimpleMaPrice.Last(2) >= ExponentialMaPrice.Last(2))
{
ClosePositions(TradeType.Sell);
ExecuteMarketOrder(TradeType.Buy, SymbolName, MyUnitVolume);
}
}
private void ClosePositions(TradeType tradeType)
{
foreach (var position in BotPositions)
{
if (position.TradeType != tradeType) continue;
ClosePosition(position);
}
}
}
}
Replies
gyoy
07 Apr 2024, 23:24
( Updated at: 08 Apr 2024, 05:23 )
RE: No result found by backtesting my code
gbg561 said:
Hi
I had a similar problem during testing on US500 and in the log for back testing it indicated “Bad Volume” Error. So I increased the volume to 1, that got it working then it was just a case of calculating the correct amount of lots required.
Hope that helps.
Gary
Hi Gary,
Thanks, yes I've figured it out that the volume needs to be adjusted properly. I have made another code which does the same thing but a bit easier to understand and works fine.
GyO
@gyoy
gbg561
05 Apr 2024, 14:31 ( Updated at: 07 Apr 2024, 05:23 )
Hi
I had a similar problem during testing on US500 and in the log for back testing it indicated “Bad Volume” Error. So I increased the volume to 1, that got it working then it was just a case of calculating the correct amount of lots required.
Hope that helps.
Gary
@gbg561