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/27/2021 at 12:40 PM #16271102/27/2021 at 2:02 PM #16271202/27/2021 at 3:03 PM #162715
I think Vonasi equivocate, this a great idea.
My views on it have varied but I think my overwhelming thoughts on simulated trading is that it can be just another thing to curve fit. A decision has to be made on what is to be used as the filter that switches trading on and off and we inevitably choose this by curve fitting the filter to history to see what worked best.
02/27/2021 at 3:08 PM #162716Just started to analyse this problem.
Coded a Trading system with graphical output of Equity Curve, Drawdowns and Drawdown Candles to better understand how PRT works and calculates variables.
Next step is to replace system variables with own variables to enable simulated trading.
I hope the code helps! 🙂
02/27/2021 at 3:12 PM #162718Equity, DD and DD candles as graphs! system variables to be replaced!123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131// EquityDD_SystemVariables (Reference Algo!)// 27.02.21// We use the system STRATEGYQUIT variable to cease trading once our max drawdown// barrier as of backtesting has been touched. Problem: we are not able to restart// trading automatically, again.// Our intention is to replace the "STRATEGYQUIT" variable by an own virtual// strategy quit, enabling us to restart trading automatically.// Restarting trading will depend on parameters we see fit telling us that the// robot is back on its usual trading behaviour. For this analysis the number of// drawdown candles might come in handy! So we want to track these candles, too!// In a nutshell: we have to analyse how the the robot would have continued trading// after a strategy quit event!// Problem is to get this data without real trading! Instead we have to proceed with// a virtual trading algo delivering same results as in real trading!// This trading robot is a first step in recalculating Equity Curve, Drawdown,// MaxDDCandles and MostDDCandles by using system variables "LONGONMARKET",// "TRADEPRICE", "BUY" and SELL"!// USE THIS ROBOT TO GET FAMILIAR WITH PROPER CALCULATION ALGOS FOR THE FOUR// SYSTEM VARIABLES MENTIONED ABOVE!// USE THE "GraphSWITCH" TO SEE THE RESULTS OF THE CALCULATIONS!!// *** Copy This algo to a new robot and try to replace system variables! ***//// GOOD LUCK! :)//DEFPARAM CumulateOrders=falseDEFPARAM PreLoadbars = 0//Variables and SWITCHESOnce Equity = 10000 //chose your equity or set to "0"Once Spread = 2 //You can set a backtesting spread here!Halfspread = Spread/2Once Ordersize = 1//select graph outputGraphSWITCH=1 //1=Equity 2=maxDD 3=MaxDDCandles+MostDDCandles//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 --------------------------------------------//STEP 1: entry longIF NOT LongOnMarket AND BuyLong THENbuy Ordersize contract at marketENDIF//STEP 2: during long tradeIF LongOnMarket THENLongPrice=Tradeprice //Halfspread included!!TradePL=(close-LongPrice)Equity=Equity+TradePL -TradePL[1]//check on new equity high / Runup highIF Equity>RunupLevel THENRunupLevel=EquityRunupIndex=BarIndexDDLevel=RunUpLevelENDIFIF Equity<DDLevel THENDDLevel=EquityENDIF//calculate DD (negativ sign) depending on algebraic signsIF RunupLevel>0 AND DDLevel>0 THENDD = -(RunupLevel - DDLevel)ELSIF RunupLevel>=0 AND DDLevel<0 THENDD =-RunupLevel+DDLevelENDIFIF DD<MaxDD THENMaxDD=DDENDIFENDIF//STEP 3: count DD candlesMaxDDCandles = BarIndex-RunupIndexIF MaxDDCandles>MostDDCandles THENMostDDCandles=MaxDDCandlesENDIF//STEP 4: exit long tradeIF LongOnMarket and ExitLong THENsell at marketLongPrice=0TradePL=0ENDIF//STEP 5: after exit at open price adjust equityIF LongOnMarket=0 and LongOnMarket[1]=1 THENopenclosediff = open-close[1]Equity = Equity + openclosediff-HalfspreadENDIF//RESULTS///////////////////////////////////////////////////////////////////////////GRAPH FUNCTIONS ----------------------------------------------------------------IF GraphSWITCH=1 THENGraph Equity as "Equity"ELSIF GraphSWITCH=2 THENGraph 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//Graph DDCandles as "DDCandles"Graph RunupIndex coloured(0,200,0) as "RunupIndex"Graph MaxDDCandles coloured(200,0,0) as "MaxDDCandles"Graph MostDDCandles coloured(250,100,100) as "MostDDCandles"ENDIF02/27/2021 at 4:57 PM #16272102/27/2021 at 7:41 PM #162728https://www.prorealcode.com/blog/learning/how-to-improve-a-strategy-with-simulated-trades-1/
Maybe we could study this old idea and article by Nicolas with the idea of doing it with Maxprofit and drawdown instead of moving average equity curve.
I think the base of Nicolas’ code is good and could be reprogrammed with Pedro’s code.
02/27/2021 at 7:52 PM #162729I honestly think that not enough interest has been given to simulated operations, except for a few Vonasi codes.
I think that simulated operations can be very important in many strategies, although the idea of Nicolas in his code with moving average equity curve I do not think it is the best.
The Maxprofit and drawdown thing could work very well.
But I am sure that the members of this forum could have very good and different ideas.
02/27/2021 at 9:31 PM #162730Using an average of the equity curve or just stopping trading after too many losses or even just one big loss are all not viable working options.
The problem with averaging an equity curve is that you curve fit the average period to history to get the best result but the future does not care about what period of equity average worked best in the past.
The problem with stopping trading after a few losses or even just one big loss is that the next few trades could all be winners and then your simulated trading tells you to get back on the market just in time for the next series of losers or the next big loser. You can just as easily simulate your way to hitting every loser and missing every winner as you can simulate your way to hitting more winners than if you took every trade.
It’s better to create a robust strategy than look to something like simulated trading to turn a donkey into a racehorse I think…. plus simulating strategies with things like partial closure and trailing stop losses can be a lot of work to create with very complicated code and also not totally accurate due to not knowing exact entry and exit prices because spread, slippage and fees of real trades can not be known.
02/28/2021 at 8:41 AM #162739Maybe it could be easier than it seems.
We program two indicators:
The first calculates the Maxprofit of the system, and when an operation fails, it PAUSES the system or operates with the minimum accepted by the broker (example, Dax € 0.5).
The second calculates the Maxprofit of the system, but when the first indicator fails an operation it continues calculating the performance of the simulated operations. When the second indicator crosses above the first indicator the system returns to real money trading or trading the usual € pip amount.
02/28/2021 at 9:09 AM #162745Yes it is perfectly doable and really only requires one indicator and one strategy…. but you have to ensure that the indicator and the strategy stay in synch – which is harder than you think. When the strategy and the indicator have a position open is is possible for the indicator to keep its position open and the strategy to close its position due to the difference between real spread and simulated spread. Then the strategy could open a new position whilst the simulation is still holding the old position. Also with varying spread and real life slippage and fees simulated profit and loss can be very different to actual profit and loss so we could be basing our decision to keep trading switched on when losses are actually much bigger than we think.
It is better to have a simulated indicator that looks at and analyses every possible trade that there could be (so even ones that the real strategy missed because it already had a position open) and then use this as a filter to turn on or off trading. With this method we can see much more quickly if a strategy idea is quite simply failing or stops working due to a change in the character of the market. Simulating every possible trade in an indicator can be difficult to code if a strategy is very complicated.
02/28/2021 at 10:54 AM #162754OK, an idea.
In Pedro’s code:
Program 2 indicators, the first to operate with real money only when EQUITY is above DDLEVEL.
The second to keep track of simulated operations, and when Equity is again above DDLEVEL in simulated operations, it will return to real operations.
In this way we have two exactly the same performance indicators
02/28/2021 at 12:17 PM #16275902/28/2021 at 12:22 PM #162761You don’t need two indicators. You need one strategy and one indicator or just one strategy with simulated trading built in.
Personally I’d go for one indicator that records the reults of every possible trade (even ones that the strategy would miss because it was already on the market) and then one strategy that just uses an on/off signal called from the indicator…. but as I said before I don’t see much if any benefit to adding simulated trading to a strategy as it can just as easily keep you in for every loser and out for every winner as it can improve performance.
02/28/2021 at 1:09 PM #162763EquityDD_OwnVariables_L123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147// --------------------------------------------------------------------------//// EquityDD_OwnVariables_L //// --------------------------------------------------------------------------//// [EquityDD_SystemVariables_L (Reference Algo!)]// 28.02.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.// 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 must be set to "0"!! - Can somebody tell me why?!?!// - no parameters included for currency trading// - or trading other contractsizes than "1"// - no short trading so farDEFPARAM CumulateOrders=falseDEFPARAM PreLoadbars = 0//Variables and SWITCHESOnce Equity = 0 //chose your equity or set to "0"Once Spread = 2 //You can set a backtesting spread here!Halfspread = Spread/2Once Ordersize = 1//select graph outputGraphSWITCH=3 //1=Equity//2=maxDD - set "ONCE equity=0" to improve graph layout//3=MaxDDCandles+MostDDCandles//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! /////////////////////////////////////////////////////////////STEP 1: entry longIF NOT LongInMarket[2] AND BuyLong[1] THENLongInMarket=1LongEntryPrice = open + HalfspreadENDIF//STEP 2: during long tradeIF LongOnMarket THENLongPrice=LongEntryPriceTradePL=(close-LongPrice)Equity=Equity+TradePL -TradePL[1]//check on new equity high / Runup highIF Equity>RunupLevel THENRunupLevel=EquityRunupIndex=BarIndexDDLevel=RunUpLevelENDIFIF Equity<DDLevel THENDDLevel=EquityENDIF//calculate DD (negativ sign) depending on algebraic signsIF RunupLevel>0 AND DDLevel>0 THENDD = -(RunupLevel - DDLevel)ELSIF RunupLevel>=0 AND DDLevel<0 THENDD =-RunupLevel+DDLevelENDIFIF DD<MaxDD THENMaxDD=DDENDIFENDIF//STEP 3: count DD candlesMaxDDCandles = BarIndex-RunupIndexIF MaxDDCandles>MostDDCandles THENMostDDCandles=MaxDDCandlesENDIF//STEP 4: exit long tradeIF (LongInMarket[2] and ExitLong[1]) or (BuyLong[2] and ExitLong[1]) THENLongInMarket=0LongExitPrice = open - Halfspreadopenclosediff = LongExitPrice-close[1]Equity = Equity + openclosediff//resetLongPrice=0TradePL=0ENDIF//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 ----------------------------------------------------------------IF GraphSWITCH=1 THENGraph Equity as "Equity"ELSIF GraphSWITCH=2 THENGraph 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//Graph DDCandles as "DDCandles"Graph RunupIndex coloured(0,200,0) as "RunupIndex"Graph MaxDDCandles coloured(200,0,0) as "MaxDDCandles"Graph MostDDCandles coloured(250,100,100) as "MostDDCandles"ENDIF//eof----------------------------------------------------------------------------- -
AuthorPosts
Find exclusive trading pro-tools on