Guest User

Untitled

a guest
Aug 14th, 2023
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. //@version=5
  2. strategy("CopyTrading Script", overlay = true, initial_capital = 110, default_qty_type = strategy.commission.cash_per_order, default_qty_value = 300, commission_value = 0.055)
  3.  
  4. timePeriod = time > timestamp("UTC", 2023, 8, 9, 0, 0, 0)
  5.  
  6. // STs (Strategies)
  7. // ST1 - Double Moving Average Proximity
  8. st1FastMaLength = input(22, "Fast MA Length", group = "ST1")
  9. st1SlowMaLength = input(46, "Slow MA Length", group = "ST1")
  10. st1FastMaType = input.string("SMA", "Fast MA Type", ["SMA", "EMA"], group = "ST1")
  11. st1SlowMaType = input.string("SMA", "Slow MA Type", ["SMA", "EMA"], group = "ST1")
  12. proximityPercentageMultiplier = input(310, "Proximity % Multiplier", group = "ST1")
  13.  
  14. st1FastMaTypeFunc(source, length) =>
  15. if st1FastMaType == "SMA"
  16. ta.sma(source, length)
  17. else
  18. if st1FastMaType == "EMA"
  19. ta.ema(source, length)
  20. st1FastMa = st1FastMaTypeFunc(close, st1FastMaLength)
  21. st1SlowMaTypeFunc(source, length) =>
  22. if st1SlowMaType == "SMA"
  23. ta.sma(source, length)
  24. else
  25. if st1SlowMaType == "EMA"
  26. ta.ema(source, length)
  27. st1SlowMa = st1SlowMaTypeFunc(close, st1SlowMaLength)
  28.  
  29. maProximityPercentage = ((st1SlowMa - st1FastMa) / close) * 100
  30. maProximity = maProximityPercentage * proximityPercentageMultiplier
  31.  
  32. float st1BuyConf = 0
  33. float st1SellConf = 0
  34.  
  35. if maProximity < 0
  36. st1BuyConf := 100 + maProximity
  37. if maProximity > 0
  38. st1SellConf := 100 - maProximity
  39.  
  40. // ST2 - Double Moving Average Crossover
  41. st2FastMaLength = input(7, "Fast MA Length", group = "st2")
  42. st2SlowMaLength = input(22, "Slow MA Length", group = "st2")
  43. st2FastMaType = input.string("SMA", "Fast MA Type", ["SMA", "EMA"], group = "st2")
  44. st2SlowMaType = input.string("SMA", "Slow MA Type", ["SMA", "EMA"], group = "st2")
  45.  
  46. st2FastMaTypeFunc(source, length) =>
  47. if st2FastMaType == "SMA"
  48. ta.sma(source, length)
  49. else
  50. if st2FastMaType == "EMA"
  51. ta.ema(source, length)
  52. st2FastMa = st2FastMaTypeFunc(close, st2FastMaLength)
  53. st2SlowMaTypeFunc(source, length) =>
  54. if st2SlowMaType == "SMA"
  55. ta.sma(source, length)
  56. else
  57. if st2SlowMaType == "EMA"
  58. ta.ema(source, length)
  59. st2SlowMa = st2SlowMaTypeFunc(close, st2SlowMaLength)
  60.  
  61. st2BuyConf = 0
  62. st2SellConf = 0
  63.  
  64. if ta.crossover(st2FastMa, st2SlowMa)
  65. st2BuyConf := 100
  66. if ta.crossunder(st2FastMa, st2SlowMa)
  67. st2SellConf := 100
  68.  
  69. // ST3 - Supertrend
  70. SPTatrPeriod = input(35, "Supertrend ATR Length", group = "st3")
  71. SPTfactor = input(3.0, "Supertrend Factor", group = "st3")
  72.  
  73. [superTrend, direction] = ta.supertrend(SPTfactor, SPTatrPeriod)
  74.  
  75. st3BuyConf = 0
  76. st3SellConf = 0
  77.  
  78. if direction < 0
  79. st3BuyConf := 100
  80. if direction > 0
  81. st3SellConf := 100
  82.  
  83. // Managers
  84. // ST (Strategies) Manager
  85. st1Weight = input(0.3, "ST1", group = "Weight Options")
  86. st2Weight = input(0.3, "ST2", group = "Weight Options")
  87. st3Weight = input(0.4, "ST3", group = "Weight Options")
  88.  
  89. st1BuyRltv = st1BuyConf * st1Weight
  90. st2BuyRltv = st2BuyConf * st2Weight
  91. st3BuyRltv = st3BuyConf * st3Weight
  92. st1SellRltv = st1SellConf * st1Weight
  93. st2SellRltv = st2SellConf * st2Weight
  94. st3SellRltv = st3SellConf * st3Weight
  95.  
  96. globalBuyConf = st1BuyRltv + st2BuyRltv + st3BuyRltv
  97. globalSellConf = st1SellRltv + st2SellRltv + st3SellRltv
  98.  
  99. // Global Manager
  100. longConfTreshold = input(94.0, "Long Confidence Treshhold", group = "Confidence")
  101. shortConfTreshold = input(94.0, "Short Confidence Treshhold", group = "Confidence")
  102.  
  103. // Pietro shit starts here, be careful!
  104. //Order value
  105. orderValue = input(300.0, "Trade Value", group = "Trade")
  106.  
  107. contracts = orderValue / close
  108. roundedContracts = (math.floor(contracts * 1000)) / 1000
  109. inLongTrade = strategy.position_size > 0
  110. inShortTrade = strategy.position_size < 0
  111. notInTrade = not inLongTrade and not inShortTrade
  112.  
  113. //Backtesting
  114. backtestYN = input.string("No", "Backtest?", ["Yes", "No"], group = "Backtest")
  115.  
  116. //Long trade
  117. if backtestYN == "Yes"
  118. if globalBuyConf > longConfTreshold
  119. strategy.close("short")
  120. strategy.entry("long", strategy.long, roundedContracts)
  121. else
  122. if globalBuyConf > longConfTreshold and timePeriod
  123. strategy.close("short")
  124. strategy.entry("long", strategy.long, roundedContracts)
  125.  
  126. //short trade
  127. if backtestYN == "Yes"
  128. if globalSellConf > shortConfTreshold
  129. strategy.close("long")
  130. strategy.entry("short", strategy.short, roundedContracts)
  131. else
  132. if globalSellConf > shortConfTreshold and timePeriod
  133. strategy.close("long")
  134. strategy.entry("short", strategy.short, roundedContracts)
  135.  
  136. //Aesthetics
  137. plot(st1FastMa, "ST1 Fast MA", color.yellow, 2)
  138. plot(st1SlowMa, "ST1 Slow MA", color.green, 2)
  139.  
  140. plot(st2FastMa, "ST2 Fast MA", color.red, 2)
  141. plot(st2SlowMa, "ST2 Slow MA", color.yellow, 2)
  142.  
  143. directionNegative = direction < 0
  144. plot(superTrend, "SuperTrend Line", directionNegative ? color.green : color.red, 2)
  145.  
  146. tbl = table.new(position.top_right, 2, 2, color.black, color.white, 2)
  147. table.cell(tbl, 0, 0, "Buy Confidence:", text_color = color.white)
  148. table.cell(tbl, 0, 1, "Sell Confidence:", text_color = color.white)
  149. table.cell(tbl, 1, 0, str.tostring(globalBuyConf), text_color = color.white)
  150. table.cell(tbl, 1, 1, str.tostring(globalSellConf), text_color = color.white)
Advertisement
Add Comment
Please, Sign In to add comment