18000% backtesting bot doesn't perform good on live trading
18000% backtesting bot doesn't perform good on live trading
23 Sep 2022, 00:38
Hi, i have written the following bot based on a ma envelope strategy. It opens a position whenever price gets below or the upper or lower band, with a tp of 2pips. When backtesting on eurusd, with comimission set to 20 and spread to 0.2, it gives pretty nice results. I dont use ontick data, as my strategy is based on a onbar method. However during forward testing it looses money very quickly. Any idea why, and tips so it might work?
You should use the envelope indicator: https://ctrader.com/algos/indicators/show/281
Source code:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
public class NewcBot : Robot
{
[Parameter(DefaultValue = 52)]
public int EnvelopePeriod { get; set; }
[Parameter(DefaultValue = 0.101)]
public double BandDistance { get; set; }
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Periodpricema", DefaultValue = 2)]
public int Periodsprice { get; set; }
[Parameter("TP", DefaultValue = 2)]
public int TP { get; set; }
[Parameter("SL", DefaultValue = 10000)]
public int SL { get; set; }
private ExponentialMovingAverage emaprice;
private EnvelopeChannels envelopeChannels;
protected override void OnStart()
{
emaprice = Indicators.ExponentialMovingAverage(Source, Periodsprice);
envelopeChannels = Indicators.GetIndicator<EnvelopeChannels>(EnvelopePeriod, BandDistance, MovingAverageType.Simple);
}
protected override void OnBar()
{
var currentPriceMa = emaprice.Result.Last(1);
var previousPriceMa = emaprice.Result.Last(2);
var channelup = envelopeChannels.ChannelUp.Last(1);
var prevchannelup = envelopeChannels.ChannelUp.Last(2);
var channeldown = envelopeChannels.ChannelLow.Last(1);
var prevchanneldown = envelopeChannels.ChannelLow.Last(1);
var longPosition = Positions.Find("Buy", SymbolName, TradeType.Buy);
var shortPosition = Positions.Find("Sell", SymbolName, TradeType.Sell);
{
//Buy Entry
if (prevchannelup > previousPriceMa && channelup < currentPriceMa)
{
if (shortPosition != null)
ClosePosition(shortPosition);
{
if(Positions.Count == 0)
ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits,"Buy", SL, TP);
}
}
if(prevchannelup < previousPriceMa && channelup > currentPriceMa && longPosition != null)
ClosePosition(longPosition);
//Sell Entry
else if (prevchanneldown < previousPriceMa && channeldown > currentPriceMa)
{
if (longPosition != null)
ClosePosition(longPosition);
{
if(Positions.Count == 0)
ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell",SL,TP);
}
}
if (prevchanneldown > previousPriceMa && channeldown < currentPriceMa && shortPosition != null)
ClosePosition(shortPosition);
}
}
private double VolumeInUnits
{
get { return Symbol.QuantityToVolumeInUnits(Quantity); }
}
}
}
Replies
PanagiotisCharalampous
26 Sep 2022, 10:06
Hi isabakas,
Make sure that you are backtesting using tick data.
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous
acrigney
04 Oct 2023, 03:12
( Updated at: 04 Oct 2023, 05:38 )
RE: 18000% backtesting bot doesn't perform good on live trading
Mate but if we want our code to run on minutes (i.e. not on ticks) then is it legitimate to backtest on minutes. Or should we backtest on ticks but convert the ticks to minute bars? When I capture the data on ticks live its a lot more than what you get on backtesting and I think on minute trading but I need to check.
PanagiotisCharalampous said:
Hi isabakas,
Make sure that you are backtesting using tick data.
Best Regards,
Panagiotis
@acrigney
PanagiotisChar
04 Oct 2023, 05:59
( Updated at: 04 Oct 2023, 06:09 )
RE: RE: 18000% backtesting bot doesn't perform good on live trading
acrigney said:
Mate but if we want our code to run on minutes (i.e. not on ticks) then is it legitimate to backtest on minutes. Or should we backtest on ticks but convert the ticks to minute bars? When I capture the data on ticks live its a lot more than what you get on backtesting and I think on minute trading but I need to check.
PanagiotisCharalampous said:
Hi isabakas,
Make sure that you are backtesting using tick data.
Best Regards,
Panagiotis
If your strategy contains tick related execution like TP or SL, you need to use tick data. Anything else will be wrong
@PanagiotisChar
waym77
23 Sep 2022, 08:48
Real-world market conditions are always different from backtests. I once created a bot based on the stochastic oscillator with a similar style of TP (1-5 pips) that gave me a 150,000% increase over a 10-year backtest. It then proceeded to lose money instantly.
In reality, you should only backtest a bot to see if it works as intended (creating positions when conditions are met). I would suggest forward testing via demo as a more viable way to test this, as most brokers' demo accounts simulate current liquidity scenarios.
@waym77