Backtesting Returns Different Results
Backtesting Returns Different Results
22 Feb 2017, 14:56
Hi All,
I wrote a silly bot and did some testing. I see to be getting resutls all over the place. I cant for the life of my figure out what would cause it to behave so weird!
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class HedgeBot : Robot { [Parameter(DefaultValue = 1000)] public double Parameter { get; set; } [Parameter(DefaultValue = 5)] public double PT { get; set; } [Parameter(DefaultValue = 2)] public double Scope { get; set; } private Random random = new Random(); private double negsellscope = 0; private double negbuyscope = 0; private double grouptrades = 0; protected override void OnStart() { // Put your initialization logic here ExecuteOrder(Parameter, GetRandomTradeType()); } protected override void OnTick() { // Put your core logic here if (negsellscope == 0) { negsellscope = -Scope; } if (negbuyscope == 0) { negbuyscope = -Scope; } double negsellside = 0; double negbuyside = 0; long negsellvolume = 0; long negbuyvolume = 0; double negsellpips = 0; double negbuypips = 0; double possellside = 0; double posbuyside = 0; long possellvolume = 0; long posbuyvolume = 0; double possellpips = 0; double posbuypips = 0; double target = 0; foreach (var trade in Positions) { if (trade.SymbolCode == Symbol.Code && trade.TradeType == TradeType.Sell) { if (trade.NetProfit < 0) { negsellside += trade.GrossProfit; negsellvolume += trade.Volume; negsellpips += trade.Pips; } else { possellside += trade.GrossProfit; possellvolume += trade.Volume; possellpips += trade.Pips; } } else if (trade.SymbolCode == Symbol.Code && trade.TradeType == TradeType.Buy) { if (trade.NetProfit < 0) { negbuyside += trade.GrossProfit; negbuyvolume += trade.Volume; negbuypips += trade.Pips; } else { posbuyside += trade.GrossProfit; posbuyvolume += trade.Volume; posbuypips += trade.Pips; } } } if (negsellside < negsellscope) { ExecuteOrder(Parameter, TradeType.Buy); negsellscope = negsellscope - Scope; } if (negbuyside < negbuyscope) { ExecuteOrder(Parameter, TradeType.Sell); negbuyscope = negbuyscope - Scope; } if (posbuyside > negsellside) { target = posbuyside + negsellside; } else if (possellside > negbuyside) { target = possellside + negbuyside; } if (target > PT) { CloseAll(); } DrawInfo(negbuyside, negbuyvolume, negbuypips, negsellside, negsellvolume, negsellpips, posbuyside, posbuyvolume, posbuypips, possellside, possellvolume, possellpips, target); } private void CloseAll() { foreach (var position in Positions) { ClosePosition(position); } negsellscope = 0; negbuyscope = 0; grouptrades++; ExecuteOrder(Parameter, GetRandomTradeType()); } private void DrawInfo(double negb, long negbv, double negbp, double negs, long negsv, double negsp, double posb, long posbv, double posbp, double poss, long possv, double possp, double target) { ChartObjects.DrawText("negb", "Negative Buy: " + Convert.ToString(negb), StaticPosition.TopLeft, Colors.Orange); ChartObjects.DrawText("negbv", "\nNegative Buy Vol: " + Convert.ToString(negbv), StaticPosition.TopLeft, Colors.Orange); ChartObjects.DrawText("negbp", "\n\nNegative Buy Pips: " + Convert.ToString(negbp), StaticPosition.TopLeft, Colors.Orange); ChartObjects.DrawText("negs", "\n\n\nNegative Sell: " + Convert.ToString(negs), StaticPosition.TopLeft, Colors.Orange); ChartObjects.DrawText("negsv", "\n\n\n\nNegative Sell Vol: " + Convert.ToString(negsv), StaticPosition.TopLeft, Colors.Orange); ChartObjects.DrawText("negsp", "\n\n\n\n\nNegative Sell Pips: " + Convert.ToString(negsv), StaticPosition.TopLeft, Colors.Orange); ChartObjects.DrawText("split", "\n\n\n\n\n\n---------------------------------", StaticPosition.TopLeft, Colors.Yellow); ChartObjects.DrawText("posb", "\n\n\n\n\n\n\nPositive Buy: " + Convert.ToString(posb), StaticPosition.TopLeft, Colors.Green); ChartObjects.DrawText("posbv", "\n\n\n\n\n\n\n\nPositive Buy Vol: " + Convert.ToString(posbv), StaticPosition.TopLeft, Colors.Green); ChartObjects.DrawText("posbp", "\n\n\n\n\n\n\n\n\nPositive Buy Pips: " + Convert.ToString(posbp), StaticPosition.TopLeft, Colors.Green); ChartObjects.DrawText("poss", "\n\n\n\n\n\n\n\n\n\nPositive Sell: " + Convert.ToString(poss), StaticPosition.TopLeft, Colors.Green); ChartObjects.DrawText("possv", "\n\n\n\n\n\n\n\n\n\n\nPositive Sell Vol: " + Convert.ToString(possv), StaticPosition.TopLeft, Colors.Green); ChartObjects.DrawText("possp", "\n\n\n\n\n\n\n\n\n\n\n\nPositive Sell Pips: " + Convert.ToString(possp), StaticPosition.TopLeft, Colors.Green); ChartObjects.DrawText("split", "\n\n\n\n\n\n\n\n\n\n\n\n---------------------------------", StaticPosition.TopLeft, Colors.Yellow); ChartObjects.DrawText("possp", "\n\n\n\n\n\n\n\n\n\n\n\n\nTarget: " + Convert.ToString(target), StaticPosition.TopLeft, Colors.Blue); } protected override void OnStop() { // Put your deinitialization logic here } private TradeType GetRandomTradeType() { return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell; } private void ExecuteOrder(double quantity, TradeType tradeType) { var result = ExecuteMarketOrder(tradeType, Symbol, Convert.ToInt64(Parameter), "Martingale" + grouptrades.ToString(), null, null); if (result.Error == ErrorCode.NoMoney) Stop(); } } }
If I do backtesting on any currency same time frame it will get different resutls each run.... Sometimes it seems to not execute more then the first trade. Othertimes it does, sometimes it just abruptly looses everything no matter what the account size it.....If anyone can help me figure this out? In my testing I used EURJPY and got $5000 net once, $0 net, $32000 net....Its very strange. Im using 20,000 position size with PT 100 and Scope 25.
Replies
petra41@bigpond.com
22 Feb 2017, 15:26
Here is an example, one just doesn't work I run it a few times in a row and get the first one. Then eventually it works fine and I get a normal looking chart. And it randomly abruptly looses everything at the end?
@petra41@bigpond.com
tradermatrix
01 Mar 2017, 10:40
Hello
This is normal because you have coded "randomly"
After each closing it starts at random buy or sell
private Random random = new Random();
protected override void OnStart()
{
// Put your initialization logic here
ExecuteOrder(Parameter, GetRandomTradeType());
}
private void CloseAll()
{
foreach (var position in Positions)
{
ClosePosition(position);
}
negsellscope = 0;
negbuyscope = 0;
grouptrades++;
var volumeInUnits = Symbol.QuantityToVolume(Quantity);
ExecuteOrder(volumeInUnits, GetTradeCommand());
}
You can do a hundred times the backtesting it will always be different ..
I have replaced "random" by an indicator and it works well..the backtesting is regular.
good trades
@+
@tradermatrix
petra41@bigpond.com
22 Feb 2017, 15:25 ( Updated at: 21 Dec 2023, 09:20 )
@petra41@bigpond.com