Simulated trading
Forums › ProRealTime English forum › ProOrder support › Simulated trading
- This topic has 54 replies, 7 voices, and was last updated 3 years ago by Mattias.
-
-
02/28/2021 at 1:23 PM #162767
The EquityDD_OwnVariables_L.itf – Robo is working. I have used it on the DAX 1m and 5m-chart over several different candle periods.
Feel free to use this robo as good start for further improvements or your own strategies and please tell if you fixed any errors or limitations.
It would be nice to see successful trading strategies using this algo to pause trading and restart trading in a proper manner, avoiding trading in periods where the core strategy should not be trading.
We stop our robots, once the max DD as of backtesting has been exceeded. Now it is possible to track simulated equity curve and see when the robot is back on track! Then we can restart it.
Have fun and Good Trading!
🙂 Peter
02/28/2021 at 1:47 PM #16277002/28/2021 at 3:47 PM #162777it is possible to track simulated equity curve and see when the robot is back on track! Then we can restart it.
…having just missed a nice set of winning trades and just in time for the next set of losing trades. Simulated trading can keep you off the market during winning periods as easily as it can keep you on the market during losing periods! Yes simulated trading can make a back test look better by missing a few losing trades less than the winning trades it also misses but we can never know the distribution of winners and losers in the future which may be very different to the distribution of winners and losers in our historical data.
02/28/2021 at 3:58 PM #162779And could the bot be paused and started automatically with these indicators?
Yes… but you may miss a whole load of winners and get back on the market just in time for a whole bunch of losers so it is not the holy grail that will make a bad or even average strategy any better. It could just as easily make it worse in the future with just the right distribution of winners and losers.
A simple example. We have a strategy that stops trading if we have a loser and starts trading again if we have a simulated winner. The wins and losses come in the following order: LWLWLWLWLWLW. Our simulated trading just managed to hit every losing trade and miss every winning trade. Say we change it so that it stops trading after two losers in a row and you have to have two simulated winners in a row before it starts trading live again and then the winners and losers come like this: LLWWLLWWLLWWLLWW. Once again we get to trade every loser and miss every winner.
It can be the same for equity curve simulated trading. We start with one big loser that puts us in 5% drawdown from our starting capital so we switch to simulated trading and the next six trades make 1% each but we missed them all but as we are back in a simulated positive equity we turn trading back to live and the next trade is a loser so we turn trading back to simulated and the next trade is a winner…but once again we missed it…. and so on and so on until our simulated trading empties our bank account.
02/28/2021 at 4:42 PM #162783Yes, I understand and I know.
But so far my systems win 80-90% of trades.
They carry out between 100 and 250 operations in 3 years.
Losing trades are widely spread in those 3 years, with 2 consecutive trades with losses being the longest losing streak.
I think maybe they would earn a little less money, but the trade would be much safer … maybe it would be worth it.
02/28/2021 at 11:37 PM #162798So you have very rare losing trades very evenly spaced throughout history and never more than 2 losing trades in a row…. and how exactly will simulated trading avoid you trading those losing evenly spaced trades?
If you had said that you had 90% winners but sometimes a streak of 10 losers in a row then simulated trading might help avoid some of those losers… and also quite a few winners too.
Your strategy does not sound like it would benefit in any way from simulated trading at all. Better to just put a QUIT in the strategy if there are three losers in a row and then manually decide if it has failed or something major has changed the way the markets are behaving.
03/01/2021 at 10:33 AM #16281703/04/2021 at 10:45 PM #16315003/05/2021 at 9:11 PM #163245This code calculates
- equity (curve)
- max drawdown
- number of max Drawdown candles
Only Long trading so far. Room for improvement!
You can use this code for your own robots to pause trading and simulate trading during this time. The information gathered from simulated trading might be helpful to decide when to re-start trading.
EquityDD_OwnVariables_L_V3a123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181// --------------------------------------------------------------------------//// EquityDD_OwnVariables_L_V3a //// --------------------------------------------------------------------------//// [EquityDD_SystemVariables_L (Reference Algo!)]// 05.03.21// This robot simulates trading and calculates the equity curve, maxDD and max// drawdown candles. The algo is supposed to be used when trading shall be put// at hold for a certain time but information is required how a core trading// strategy would have performed during that trading hold period.// The information gathered from simulated trading might be helpful to decide// when trading should be considered to be restarted!// USE THE "GraphSWITCH" TO SEE THE RESULTS OF THE CALCULATIONS!!// KNOWN ISSUES / LIMITATIONS:// - no parameters included for currency trading// - or trading other contract sizes than "1"// - no short trading so far// for your own algo:// make sure you fill the variable "LongInMarket" at the precise moment you// enter or exit a position!// --------------------------------------------------------------------------//DEFPARAM CumulateOrders=falseDEFPARAM PreLoadBars = 8000 //PRT uses maximum of 10k candles!Once tradeflag=0 //tradeflag eliminates problems with DEFPARAM PreLoadBars!If tradeflag=0 and OnMarket THEN //once live ONMARKET, virtual trading starts!tradeflag=1ENDIF//VARIABLESOnce Capital = 10000 //chose your capital invested or set it to "0"Once Equity = CapitalOnce Spread = 2 //You can set a backtesting spread here!Halfspread = Spread/2Once Ordersize = 1//sample core trading strategy ------------------------------------------------daysOK = dayofweek<>6 and dayofweek<>0timeOK = time>080000 and time<180000GeneralCondOK = daysOK and timeOKindicator1 = Average[3](RSI[14](close))buyL1 = indicator1 crosses over 30exitL1 = indicator1 crosses over 40 or indicator1 crosses under 20//entry and exit signalsBuyLong = GeneralCondOK and buyL1ExitLong = exitL1// end of sample trading strategy --------------------------------------------/////////////////////////////////////////////////////////////////////////////////VIRTUAL TRADING! ///////////////////////////////////////////////////////////If tradeflag=1 THEN//CALCULATE EQUITY CURVE ++++++++++++++++++++++++++++++++++++++++++++++++++++++//after position exit reset variables at beginning of calculation cycleIF reset=1 THENreset=0TradePL=0LongEntryPrice=0LongInMarket=0ENDIF//Dont change steps!//step1: enter long trade on long signal, use open price next candle for calculation!IF NOT LongInMarket[1] AND BuyLong[1] THEN//LastTradeEquity = EquityLongEntryPrice = open + HalfspreadLongInMarket=1ENDIF//step2: exit long trade on exit signal, use open price next candle for calculation!IF (LongInMarket[1] and ExitLong[1]) THENLongExitPrice = open - HalfspreadLongInMarket=0ENDIF//step3: while LongInMarket use close prices for PL calculation!IF LongInMarket THENTradePL = (close-LongEntryPrice)Equity = Equity + TradePL - TradePL[1]ENDIF//step4: position closed, no matter what reason, use entry- and exit pricesIF LongInMarket[1]=1 and LongInMarket=0 THENTradePL = (LongExitPrice - LongEntryPrice)Equity = Equity + TradePL - TradePL[1]//Equity = LastTradeEquity + TradePLreset=1ENDIF//CALCULATE DRAWDOWNS +++++++++++++++++++++++++++++++++++++++++++++++++++++++++IF LongInMarket[1] or LongInMarket THEN//check on new equity high / runup highIF Equity>=RunupLevel THENRunupLevel=EquityDDLevel=EquityRunupIndex=BarIndexENDIF//New drawdown level low in equity curve?IF Equity<=DDLevel THENDDLevel=EquityENDIF//calculate DD (negativ sign) depending on algebraic level signsIF ((RunupLevel>0) AND (DDLevel>0)) THENDD = -(RunupLevel - DDLevel)ELSIF RunupLevel>=0 AND DDLevel<0 THENDD = (-RunupLevel + DDLevel)ENDIF//if new low in DD then save it to variable MaxDDIF DD<MaxDD THENMaxDD=DDENDIFENDIF//CALCULATE DRAWDOWN CANDLES +++++++++++++++++++++++++++++++++++++++++++++++++DDCandles = BarIndex-RunupIndex //For graph function only (testing purposes)IF MaxDD<MaxDD[1] THENMaxDDCandles = BarIndex-RunUpIndexENDIFENDIF//END OF VIRTUAL TRADING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Live Trading ////////////////////////////////////////////////////////////////entry longIF NOT LongOnMarket AND buyLong THENbuy Ordersize contract at marketENDIF//exitLongIF LongOnMarket and ExitLong THENsell at marketENDIF//END OF LIVE TRADING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////RESULTS////////////////////////////////////////////////////////////////////////GRAPH FUNCTIONS ///////////////////////////////////////////////////////////////select graph outputGraphSWITCH = 1 //1=Equity// //2=maxDD - set "ONCE equity=0" to improve graph layout// //3=MaxDDCandles+MostDDCandlesIF GraphSWITCH=1 THEN //EquityGraph Equity as "Equity"ELSIF GraphSWITCH=2 THEN //DrawdownGraph Equity as "Equity"Graph MaxDD coloured(200,0,0) as "MaxDD"Graph DD coloured(150,0,0) as "DD"Graph RunupLevel coloured(0,0,200) as "RunupLevel"graph DDLevel as "DDLevel"ELSIF GraphSWITCH=3 THEN //CandlesGraph DDCandles as "DDCandles"//Graph RunupIndex coloured(0,200,0) as "RunupIndex"Graph MaxDDCandles coloured(200,0,0) as "MaxDDCandles"ELSIF GraphSWITCH=4 THEN //temporary//Graph RunupLevel coloured(0,0,200) as "RunupLevel"Graph MaxDD coloured(200,0,0) as "MaxDD"Graph DDCandles as "DDCandles"//Graph RunupIndex coloured(0,200,0) as "RunupIndex"Graph MaxDDCandles coloured(200,0,0) as "MaxDDCandles"ENDIF//eof /////////////////////////////////////////////////////////////////////////03/07/2021 at 12:42 AM #163331Added Runup and number of Runup Candles!
Note: this code is only the part where virtual trading parameters are calculated!
Live trading (“buy” and “sell” orders) in this code is only used to check on results of simulated trading and make the robot run!
Without “buy” and “sell” orders the code would not start running!Using this code within an own robot means there is still some work to do!
There are no variables yet when to stop live trading or re-start live trading!
After re-starting live-trading I recommend to synchronise variable “equity” and PRT system variable “Strategyprofit” (equity=strategyprofit) and to reset relevant virtual trading variables respectively.🙂
EquityDD_OwnVariables_L_V3c123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227// ----------------------------------------------------------------------------//// EquityDD_OwnVariables_L_V3c //// ----------------------------------------------------------------------------//// [EquityDD_SystemVariables_L (Reference Algo!)]// 06.03.21// This robot simulates trading and calculates the equity curve, max DD,// max drawdown candles, max RU and max RU candles. The algo is supposed to// be used when trading shall be put on hold for a certain period of time// but information is required how a core trading strategy would have// performed during that trading hold period.// The information gathered from simulated trading might be helpful to decide// when trading should be considered to be restarted!// USE THE "GraphSWITCH" TO SEE THE RESULTS OF THE CALCULATIONS!!// KNOWN ISSUES / LIMITATIONS:// - DEFPARAM Preloadbars causes severe problems. Introduced "tradeflag"// variable mitigating this problem. However, there are still problems if// Preloadbars exceed no. of chart candles in backtesting! No clue why!// (1) PreLoadbars=0, tradeflag=1 -> hardly any errors// (2) Preloadbars>chart candles, tradeflag=1 -> severe errors// (3) Preloadbars>Chart candles, tradeflag=0 -> small errors// Any ideas about this problem? And what's gonna happen in livetrading?// - no parameters included for currency trading// - or trading other contract sizes than "1", $2-contracts// - no short trading so far// for your own algo:// make sure you fill the variable "LongInMarket" at the precise moment you// enter or exit a position!// ----------------------------------------------------------------------------//DEFPARAM CumulateOrders=falseDEFPARAM PreLoadBars = 50 //PRT uses maximum of 10k candles!Once tradeflag=0 //tradeflag=0 mitigates problems with DEFPARAM PreLoadBars!If tradeflag=0 and OnMarket THEN //once live ONMARKET, virtual trading starts!tradeflag=1ENDIF//VARIABLES TRADING ENVIRONMENTOnce Capital = 10000 //chose your capital invested or set it to "0"Once Equity = CapitalOnce Spread = 2 //You can set a backtesting spread here!Halfspread = Spread/2Once Ordersize = 1//sample core trading strategy --------------------------------------------------daysOK = dayofweek<>6 and dayofweek<>0timeOK = time>080000 and time<180000GeneralCondOK = daysOK and timeOKindicator1 = Average[3](RSI[14](close))buyL1 = indicator1 crosses over 30exitL1 = indicator1 crosses over 40 or indicator1 crosses under 20//entry and exit signalsBuyLong = GeneralCondOK and buyL1ExitLong = exitL1// end of sample trading strategy ----------------------------------------------///////////////////////////////////////////////////////////////////////////////////VIRTUAL TRADING! /////////////////////////////////////////////////////////////If tradeflag=1 THEN//CALCULATE EQUITY CURVE +++++++++++++++++++++++++++++++++++++++++++++++++++++//after position exit reset variables at beginning of calculation cycleIF reset=1 THENreset=0TradePL=0LongEntryPrice=0LongInMarket=0ENDIF//Don't change step order!//step1: enter long trade, use open price next candle for calculation!IF NOT LongInMarket[1] AND BuyLong[1] THENLongEntryPrice = open + HalfspreadLongInMarket=1ENDIF//step2: exit long trade, use open price next candle for calculation!IF (LongInMarket[1] and ExitLong[1]) THENLongExitPrice = open - HalfspreadLongInMarket=0ENDIF//step3: while LongInMarket use close prices for PL calculation!IF LongInMarket THENTradePL = (close-LongEntryPrice) * ordersizeEquity = Equity + TradePL - TradePL[1]ENDIF//step4: position closed, no matter what reason, use entry- and exit pricesIF LongInMarket[1]=1 and LongInMarket=0 THENTradePL = (LongExitPrice - LongEntryPrice) * ordersizeEquity = Equity + TradePL - TradePL[1]reset=1ENDIF//CALCULATE DRAWDOWNS ++++++++++++++++++++++++++++++++++++++++++++++++++++++++IF LongInMarket[1] or LongInMarket THEN//check on new equity high / runup highIF Equity>=RunupLevel THENRunupLevel=EquityDDLevel=EquityRunupIndex=BarIndexENDIF//New drawdown level low in equity curve?IF Equity<=DDLevel THENDDLevel=EquityENDIF//calculate DD (negativ sign) depending on algebraic level signsIF ((RunupLevel>0) AND (DDLevel>0)) THENDD = -(RunupLevel - DDLevel)ELSIF RunupLevel>=0 AND DDLevel<0 THENDD = (-RunupLevel + DDLevel)ENDIF//if new low in DD then save it to variable MaxDDIF DD<MaxDD THENMaxDD=DDENDIFENDIF//CALCULATE RUNUPS +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//check on new equity low / rundown lowOnce RUDDLevel=EquityOnce RULevel=EquityIF Equity<=RUDDLevel THENRUDDLevel=EquityRULevel=EquityRUDDIndex=BarIndexENDIF//New runup level high in equity curve?IF Equity>=RULevel THENRULevel=EquityENDIF//calculate DD (negativ sign) depending on algebraic level signsIF (RULevel>0) AND (RUDDLevel>=0) THEN //beide +RU = RULevel - RUDDLevelELSIF (RULevel<0) AND (RUDDLevel<0) THEN //beide -RU = abs(RUDDLevel) - abs(RULevel)ELSIF (RULevel>0) AND (RUDDLevel<0) THEN // RULevel+ RUDDLevel-RU = RULevel - RUDDLevelENDIFIF RU>MaxRU THENMaxRU=RUENDIF//CALCULATE DRAWDOWN CANDLES +++++++++++++++++++++++++++++++++++++++++++++++++DDCandles = BarIndex-RunupIndex //for graph function only (testing purposes)IF MaxDD<MaxDD[1] THENMaxDDCandles = BarIndex-RunUpIndexENDIF//CALCULATE RUNUP CANDLES ++++++++++++++++++++++++++++++++++++++++++++++++++++RUCandles = BarIndex-RUDDIndex //for graph function only (testing purposes)IF MaxRU>MaxRU[1] THENMaxRUCandles = BarIndex-RUDDIndexENDIFENDIF//END OF VIRTUAL TRADING ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Live Trading ////////////////////////////////////////////////////////////////////entry longIF NOT LongOnMarket AND buyLong THENbuy Ordersize contract at marketENDIF//exitLongIF LongOnMarket and ExitLong THENsell at marketENDIF//END OF LIVE TRADING //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////RESULTS////////////////////////////////////////////////////////////////////////GraphSWITCH = 9 //1=Equity 2=DD 3=DDCandles 4=RU 5=RUCandles 9=tmpIF GraphSWITCH=1 THEN //EquityGraph Equity as "Equity"ELSIF GraphSWITCH=2 THEN //DD valueGraph Equity as "Equity"Graph MaxDD coloured(200,0,0) as "MaxDD"Graph DD coloured(150,0,0) as "DD"Graph RunupLevel coloured(0,0,200) as "RunupLevel"graph DDLevel as "DDLevel"ELSIF GraphSWITCH=3 THEN //DD CandlesGraph RunupIndex coloured(0,200,0) as "RunupIndex"Graph MaxDDCandles coloured(200,0,0) as "MaxDDCandles"ELSIF GraphSWITCH=4 THEN //Runup valueGraph Equity as "Equity"Graph MaxRU coloured(200,0,0) as "MaxRU"Graph RU coloured(150,0,0) as "RU"Graph RUDDLevel coloured(0,0,200) as "RUDDLevel"graph RULevel as "RULevel"ELSIF GraphSWITCH=5 THEN //Runup CandlesGraph RUCandles coloured(0,0,200) as "RUCandles"Graph MaxRUCandles coloured(0,0,200) as "MaxRUCandles"ELSIF GraphSWITCH=9 THEN //individualGraph Equity as "Equity"Graph MaxDD coloured(200,0,0) as "MaxDD"Graph MaxRU coloured(0,200,0) as "MaxRU"Graph DD coloured(150,0,0) as "DD"Graph RU coloured(0,200,0) as "RU"Graph RunupLevel coloured(0,0,200) as "RunupLevel"graph DDLevel as "DDLevel"graph RUDDLevel as "RUDDLevel"graph RULevel as "RULevel"ENDIF//eof ///////////////////////////////////////////////////////////////////////////03/07/2021 at 1:37 PM #163360Hi I’am starting to test your idea. First thing I run into is I don’t have exits on a long only strategy based on a crossing. It’s only based on %loss & %gain.
So how would you tackle that? You can’t use a set command nor you can’t use positionperf if your not on market.
03/07/2021 at 2:03 PM #163362You must convert the % profit or loss into a price value and then check at each candle if that candle has crossed that value (value < high and value > low). Then do the calculation to work out what you would have lost or won exiting at that value and add that to your simulated equity.
03/07/2021 at 3:13 PM #163363Yes, the entry is easy to calculate, it’s more coding involved with the exit. Although I agree with Vonasis previous posts regarding this subject, I was too curious not to try it out on a couple of systems, one good and one bad.
I tried some different approaches, e.g. the one with setting the strategy on hold after a certain amount of draw-down, then starting it again when it has reached a new high in the equity curve.
I also optimized the variables involved, but not one combination led to a better equity curve than the original (without stopping the strategy).
However, with the bad strategy, once it stopped it never started again (due to not reaching a new high in the equity curve. So that strategy actually made better result not trading at all. 🙂
03/07/2021 at 3:47 PM #163364This is the latest version so far (3d). I think I fixed the “DEFPARAM Preloadbars” problem. One additional problem was, that the level-variables have to be adjusted to possible capital variable settings!! Since I did this “initializing” of the variables all is perfectly fine. At least I did not see wrong results for equity, DDMax and RUmax anymore! All good!?
Please help testing this code before using it. If have any results different from PRT please tell! Thank you!
03/07/2021 at 4:12 PM #163366As I see it: Simulated trading to me is not a means to improve backtesting results but to pay heed to the fact, that there are periods on market, were robots fail. With simulated trading you can tell when the robot seems to be back on track (of course there will never be a guarantee about that) and then you can restart trading. Our concept as we intend to use it:
- We already do have good robots on the market and proper results of backtesting drawdowns and live trading drawdowns
- Let’s say max drawdown as of backtesting and/or live trading was 1.000€
- This maxDD was produced within 20.000 MaxDDCandles
- Now we start live trading with the simulated trading algo implemented!
- Suddenly our drawdown drops below 1.000€
- So we put live trading on hold.
- Instead, we (re-)start simulated trading.
- So simulated equity curve calculation starts from the very beginning as if the robot was just taken live. Profit/Loss=0.
- After 20.000 candles we look at the equity curve
- If there is a profit, we re-start live trading
- If there is still a loss, we restart simulated trading and wait for the next 20.000 candles to decide on live trading again!
- and so on …
.
.
Of course this is just one possible idea how to use this code. I am sure there are plenty of other ways to use it.🙂 Peter
-
AuthorPosts
Find exclusive trading pro-tools on