Advertisement
nagito_123

monte carlo simulation for backtesting

Nov 4th, 2024
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. // This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © ZenAndTheArtOfTrading
  3. // @version=5
  4. strategy("TheArtOfTrading.com", overlay=true, calc_on_order_fills=true)
  5.  
  6. // Import library
  7. import ZenAndTheArtOfTrading/ZenLibrary/8 as zen
  8.  
  9. // User inputs
  10. float SKIP_TRADE_CHANCE = input.float(title="Monte Carlo Skip %", defval=10)
  11. float RANDOM_SPREAD = input.float(title="Random Spread", defval=1)
  12. int MONTE_CARLO_REFRESH = input.int(title="Monte Carlo Refresh", defval=0)
  13.  
  14. // Prepare monte carlo spread (if input MC spread is zero, we rng will not affect LOs at all)
  15. float monteCarloSpread = zen.toPips(zen.random(0, RANDOM_SPREAD))
  16.  
  17. // Trade stops & targets
  18. var float RISK_REWARD = 2
  19. var float tradeTakeProfit = na
  20. var float tradeStopLoss = na
  21. float atrValue = ta.atr(14)
  22. float stopDistance = atrValue
  23. float takeProfitDistance = atrValue * RISK_REWARD
  24.  
  25. // Long trades
  26. longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28)) and strategy.position_size == 0
  27. if (longCondition)
  28.     if (not zen.skipTradeMonteCarlo(SKIP_TRADE_CHANCE, true))
  29.         strategy.entry("Trade", strategy.long, comment="Long")
  30.  
  31. // Short trades
  32. shortCondition = ta.crossunder(ta.sma(close, 14), ta.sma(close, 28)) and strategy.position_size == 0
  33. if (shortCondition)
  34.     if (not zen.skipTradeMonteCarlo(SKIP_TRADE_CHANCE, true))
  35.         strategy.entry("Trade", strategy.short, comment="Short")
  36.  
  37. // Set fixed stops & targets
  38. if (strategy.position_size > 0 and na(tradeStopLoss)) // Long trade
  39.     tradeTakeProfit := strategy.position_avg_price + takeProfitDistance
  40.     tradeStopLoss := strategy.position_avg_price - stopDistance
  41. else if (strategy.position_size < 0 and na(tradeStopLoss)) // Short trade
  42.     tradeTakeProfit := strategy.position_avg_price - takeProfitDistance
  43.     tradeStopLoss := strategy.position_avg_price + stopDistance
  44. else if (strategy.position_size == 0) // No trade
  45.     tradeTakeProfit := na
  46.     tradeStopLoss := na
  47.  
  48. // Calculate randomized spread in realistic manner (harming outcome)
  49. float randomSpreadSL = 0
  50. float randomSpreadTP = 0
  51.  
  52. if (strategy.position_size > 0)
  53.     randomSpreadSL := monteCarloSpread
  54.     randomSpreadTP := monteCarloSpread * -1
  55. else if (strategy.position_size < 0)
  56.     randomSpreadSL := monteCarloSpread * -1
  57.     randomSpreadTP := monteCarloSpread
  58.  
  59. // Exit trades
  60. strategy.exit("Exit", "Trade", limit=tradeTakeProfit + randomSpreadTP, stop=tradeStopLoss + randomSpreadSL)
  61.  
  62. // Plots
  63. plot(ta.sma(close, 14), color=color.blue)
  64. plot(ta.sma(close, 28), color=color.orange)
  65. plot(strategy.position_size != 0 ? tradeTakeProfit : na, color=color.green, style=plot.style_linebr)
  66. plot(strategy.position_size != 0 ? tradeStopLoss : na, color=color.red, style=plot.style_linebr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement