xmd79

Grospector DCA V.2

Aug 6th, 2022
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.61 KB | None | 0 0
  1. //@version=5
  2. strategy(title="Grospector DCA V.2", shorttitle="GDCA V.2",precision=7, overlay=true)
  3.  
  4. //------------------------------ MA ------------------------------------------------------------------
  5. len = input.int(title="MA Period", defval=12*4)
  6. strongLen = input.int(title="Strong Period from MA Period", defval=12)
  7. sellMulti = input.float(title="Strong Sell Multi", defval=2)
  8. strongSellMulti = input.float(title="Sell Multi", defval=5)
  9. labelSwitch = input.bool(title='Turn on assistive text', defval=true)
  10. zoneSwitch = input.int(title='Turn on zone',tooltip="1 = all, 2 = chart only, 3 = data only,4 = none", defval=2)
  11. DCA = input.int(title="DCA Plan (USD)",defval=5)
  12. WaitingDCA = input.float(title="Waiting DCA (USD)",defval=0)
  13. Invested = input.int(title="Invested (USD)",defval=0)
  14. strategySwitch = input.bool(title='Turn on strategy', defval=true)
  15. seperateDayOfMonth = input.int(title='Seperate day of month',tooltip="0 = none, 1-31 day", defval=28,minval=0,maxval=31)
  16. useContribution = input.bool(title='Turn on use contribution', defval=true)
  17.  
  18. StartSellZone = input.int(title="Start Sell Zone %",defval=100,minval=0,maxval=100)
  19. EndSellZone = input.int(title="End Sell Zone %",defval=0,minval=0,maxval=100)
  20. StartNormalZone = input.int(title="Start Normal Zone %",defval=0,minval=0,maxval=100)
  21. EndNormalZone = input.int(title="End Normal Zone %",defval=100,minval=0,maxval=100)
  22. StartBuyZone = input.int(title="Start Buy Zone %",defval=100,minval=0)
  23. EndBuyZone = input.int(title="End Buy Zone %",defval=200,minval=0)
  24.  
  25. bgcolor(dayofmonth == seperateDayOfMonth ? color.new(color.red, 70) : na,display=seperateDayOfMonth > 0 and seperateDayOfMonth < 32?display.all:display.none)
  26.  
  27. //Sell
  28. lenSell = len
  29. srcSell = close
  30. offsetSell = 0
  31. multiSell = sellMulti
  32. maSell = ta.sma(srcSell*multiSell, lenSell)
  33. displayZone = zoneSwitch == 1?display.all:zoneSwitch == 2?display.pane:zoneSwitch == 3?display.data_window:zoneSwitch == 4?display.none:display.none
  34.  
  35. plot(maSell*multiSell, color=#f23645, title="Sell", offset=offsetSell, linewidth=5,display=displayZone)
  36.  
  37. //Strong Sell
  38. lenStrongSell = strongLen
  39. offsetStrongSell = 0
  40. multiStrongSell = strongSellMulti
  41. maStrongSell = ta.sma(maSell*multiStrongSell, lenStrongSell)
  42. plot(maStrongSell, color=#b22833, title="Strong Sell", offset=offsetStrongSell, linewidth=5,display=displayZone)
  43.  
  44. //Buy
  45. lenBuy = len
  46. srcBuy = close
  47. offsetBuy = 0
  48. multiBuy = 1
  49. maBuy = ta.sma(srcBuy*multiBuy, lenBuy)
  50. plot(maBuy, color=color.green, title="Buy", offset=offsetBuy, linewidth=5,display=displayZone)
  51.  
  52. //Strong Buy
  53. lenStrongBuy = strongLen
  54. offsetStrongBuy = 0
  55. multiStrongBuy = 1
  56. maStrongBuy = ta.sma(maBuy*multiStrongBuy, lenStrongBuy)
  57. plot(maStrongBuy, color=#1b5e20, title="Strong Buy", offset=offsetStrongBuy,linewidth=5,display=displayZone)
  58.  
  59. //channel
  60. higherSell = maSell*multiSell > maStrongSell ? maSell*multiSell : maStrongSell
  61. lowerSell = maSell*multiSell < maStrongSell ? maSell*multiSell : maStrongSell
  62. higherBuy = maBuy > maStrongBuy ? maBuy : maStrongBuy
  63.  
  64. gapSell = (higherSell - lowerSell)/10
  65. gapNormal = (lowerSell - higherBuy)/10
  66. gapBuy = (higherBuy - maStrongBuy)/10
  67.  
  68. channelSell1 = higherSell - (gapSell*1)
  69. channelSell2 = higherSell - (gapSell*2)
  70. channelSell3 = higherSell - (gapSell*3)
  71. channelSell4 = higherSell - (gapSell*4)
  72. channelSell5 = higherSell - (gapSell*5)
  73. channelSell6 = higherSell - (gapSell*6)
  74. channelSell7 = higherSell - (gapSell*7)
  75. channelSell8 = higherSell - (gapSell*8)
  76. channelSell9 = higherSell - (gapSell*9)
  77. channelSell10 = higherSell - (gapSell*10)
  78.  
  79. plot(channelSell1, color=#b22833, title="Strong Sell Channel 1", linewidth=1,display=displayZone)
  80. plot(channelSell2, color=#b22833, title="Strong Sell Channel 2", linewidth=1,display=displayZone)
  81. plot(channelSell3, color=#b22833, title="Strong Sell Channel 3", linewidth=1,display=displayZone)
  82. plot(channelSell4, color=#b22833, title="Strong Sell Channel 4", linewidth=1,display=displayZone)
  83. plot(channelSell5, color=#b22833, title="Strong Sell Channel 5", linewidth=1,display=displayZone)
  84. plot(channelSell6, color=#b22833, title="Strong Sell Channel 6", linewidth=1,display=displayZone)
  85. plot(channelSell7, color=#b22833, title="Strong Sell Channel 7", linewidth=1,display=displayZone)
  86. plot(channelSell8, color=#b22833, title="Strong Sell Channel 8", linewidth=1,display=displayZone)
  87. plot(channelSell9, color=#b22833, title="Strong Sell Channel 9", linewidth=1,display=displayZone)
  88. plot(channelSell10, color=#b22833, title="Strong Sell Channel 10", linewidth=1,display=displayZone)
  89.  
  90. channelNormal1 = lowerSell - (gapNormal*1)
  91. channelNormal2 = lowerSell - (gapNormal*2)
  92. channelNormal3 = lowerSell - (gapNormal*3)
  93. channelNormal4 = lowerSell - (gapNormal*4)
  94. channelNormal5 = lowerSell - (gapNormal*5)
  95. channelNormal6 = lowerSell - (gapNormal*6)
  96. channelNormal7 = lowerSell - (gapNormal*7)
  97. channelNormal8 = lowerSell - (gapNormal*8)
  98. channelNormal9 = lowerSell - (gapNormal*9)
  99. channelNormal10 = lowerSell - (gapNormal*10)
  100.  
  101. plot(channelNormal1, color=#f57f17, title="Normal Channel 1", linewidth=1,display=displayZone)
  102. plot(channelNormal2, color=#f57f17, title="Normal Channel 2", linewidth=1,display=displayZone)
  103. plot(channelNormal3, color=#f57f17, title="Normal Channel 3", linewidth=1,display=displayZone)
  104. plot(channelNormal4, color=#f57f17, title="Normal Channel 4", linewidth=1,display=displayZone)
  105. plot(channelNormal5, color=#f57f17, title="Normal Channel 5", linewidth=1,display=displayZone)
  106. plot(channelNormal6, color=#f57f17, title="Normal Channel 6", linewidth=1,display=displayZone)
  107. plot(channelNormal7, color=#f57f17, title="Normal Channel 7", linewidth=1,display=displayZone)
  108. plot(channelNormal8, color=#f57f17, title="Normal Channel 8", linewidth=1,display=displayZone)
  109. plot(channelNormal9, color=#f57f17, title="Normal Channel 9", linewidth=1,display=displayZone)
  110. plot(channelNormal10, color=#f57f17, title="Normal Channel 10", linewidth=1,display=displayZone)
  111.  
  112. channelBuy1 = higherBuy - (gapBuy*1)
  113. channelBuy2 = higherBuy - (gapBuy*2)
  114. channelBuy3 = higherBuy - (gapBuy*3)
  115. channelBuy4 = higherBuy - (gapBuy*4)
  116. channelBuy5 = higherBuy - (gapBuy*5)
  117. channelBuy6 = higherBuy - (gapBuy*6)
  118. channelBuy7 = higherBuy - (gapBuy*7)
  119. channelBuy8 = higherBuy - (gapBuy*8)
  120. channelBuy9 = higherBuy - (gapBuy*9)
  121. channelBuy10 = higherBuy - (gapBuy*10)
  122.  
  123. plot(channelBuy1, color=#1b5e20, title="Strong Buy Channel 1", linewidth=1,display=displayZone)
  124. plot(channelBuy2, color=#1b5e20, title="Strong Buy Channel 2", linewidth=1,display=displayZone)
  125. plot(channelBuy3, color=#1b5e20, title="Strong Buy Channel 3", linewidth=1,display=displayZone)
  126. plot(channelBuy4, color=#1b5e20, title="Strong Buy Channel 4", linewidth=1,display=displayZone)
  127. plot(channelBuy5, color=#1b5e20, title="Strong Buy Channel 5", linewidth=1,display=displayZone)
  128. plot(channelBuy6, color=#1b5e20, title="Strong Buy Channel 6", linewidth=1,display=displayZone)
  129. plot(channelBuy7, color=#1b5e20, title="Strong Buy Channel 7", linewidth=1,display=displayZone)
  130. plot(channelBuy8, color=#1b5e20, title="Strong Buy Channel 8", linewidth=1,display=displayZone)
  131. plot(channelBuy9, color=#1b5e20, title="Strong Buy Channel 9", linewidth=1,display=displayZone)
  132. plot(channelBuy10, color=#1b5e20, title="Strong Buy Channel 10", linewidth=1,display=displayZone)
  133.  
  134. //---------------------------------------------------------------------------------------------------
  135.  
  136. //------------------------------------------ Calculate ----------------------------------------------
  137. sell = close[1] > lowerSell
  138. halfBuy = close[1] < lowerSell and close[1] > higherBuy
  139. buy = close[1] < higherBuy
  140.  
  141. percent = 0
  142. percentPeriodOfZoneSell = math.abs(StartSellZone - EndSellZone)/10
  143. percentPeriodOfZoneNormal = math.abs(StartNormalZone - EndNormalZone)/10
  144. percentPeriodOfZoneBuy = math.abs(StartBuyZone - EndBuyZone)/10
  145.  
  146. if(sell)
  147. percent := close[1] > channelSell1 ? StartSellZone-(percentPeriodOfZoneSell*1) :
  148. close[1] > channelSell2 ? StartSellZone-(percentPeriodOfZoneSell*2) :
  149. close[1] > channelSell3 ? StartSellZone-(percentPeriodOfZoneSell*3) :
  150. close[1] > channelSell4 ? StartSellZone-(percentPeriodOfZoneSell*4) :
  151. close[1] > channelSell5 ? StartSellZone-(percentPeriodOfZoneSell*5) :
  152. close[1] > channelSell6 ? StartSellZone-(percentPeriodOfZoneSell*6) :
  153. close[1] > channelSell7 ? StartSellZone-(percentPeriodOfZoneSell*7) :
  154. close[1] > channelSell8 ? StartSellZone-(percentPeriodOfZoneSell*8) :
  155. close[1] > channelSell9 ? StartSellZone-(percentPeriodOfZoneSell*9) :
  156. close[1] > channelSell10 ? StartSellZone-(percentPeriodOfZoneSell*10) : StartSellZone-(percentPeriodOfZoneSell*10)
  157. else if(halfBuy)
  158. percent := close[1] > channelNormal1 ? StartNormalZone+(percentPeriodOfZoneNormal*1) :
  159. close[1] > channelNormal2 ? StartNormalZone+(percentPeriodOfZoneNormal*2) :
  160. close[1] > channelNormal3 ? StartNormalZone+(percentPeriodOfZoneNormal*3) :
  161. close[1] > channelNormal4 ? StartNormalZone+(percentPeriodOfZoneNormal*4) :
  162. close[1] > channelNormal5 ? StartNormalZone+(percentPeriodOfZoneNormal*5) :
  163. close[1] > channelNormal6 ? StartNormalZone+(percentPeriodOfZoneNormal*6) :
  164. close[1] > channelNormal7 ? StartNormalZone+(percentPeriodOfZoneNormal*7) :
  165. close[1] > channelNormal8 ? StartNormalZone+(percentPeriodOfZoneNormal*8) :
  166. close[1] > channelNormal9 ? StartNormalZone+(percentPeriodOfZoneNormal*9) :
  167. close[1] > channelNormal10 ? StartNormalZone+(percentPeriodOfZoneNormal*10) : StartNormalZone+(percentPeriodOfZoneNormal*10)
  168. else if(buy)
  169. percent := close[1] > channelBuy1 ? StartBuyZone+(percentPeriodOfZoneBuy*1) :
  170. close[1] > channelBuy2 ? StartBuyZone+(percentPeriodOfZoneBuy*2) :
  171. close[1] > channelBuy3 ? StartBuyZone+(percentPeriodOfZoneBuy*3) :
  172. close[1] > channelBuy4 ? StartBuyZone+(percentPeriodOfZoneBuy*4) :
  173. close[1] > channelBuy5 ? StartBuyZone+(percentPeriodOfZoneBuy*5) :
  174. close[1] > channelBuy6 ? StartBuyZone+(percentPeriodOfZoneBuy*6) :
  175. close[1] > channelBuy7 ? StartBuyZone+(percentPeriodOfZoneBuy*7) :
  176. close[1] > channelBuy8 ? StartBuyZone+(percentPeriodOfZoneBuy*8) :
  177. close[1] > channelBuy9 ? StartBuyZone+(percentPeriodOfZoneBuy*9) :
  178. close[1] > channelBuy10 ? StartBuyZone+(percentPeriodOfZoneBuy*10) : StartBuyZone+(percentPeriodOfZoneBuy*10)
  179. //---------------------------------------------------------------------------------------------------
  180.  
  181. //-------------------------------------------- Display ----------------------------------------------
  182. labeltcolor = color.white
  183. textLabel = buy ? 'Buy' : halfBuy ? "Buy" : "Sell"
  184. labelbgcolor = buy ? color.green : halfBuy ? color.orange : color.red
  185. contibutingAmount = 0.0
  186. contibutingAmount := barstate.isfirst ? WaitingDCA : contibutingAmount[1]
  187.  
  188. amountBuy = 0.0
  189. amountSell = 0.0
  190. if(percent > 100 and useContribution)
  191. amountBuy := (contibutingAmount*(percent - 100)/100) + (DCA*percent/100)
  192. else
  193. amountBuy := (DCA*percent/100)
  194.  
  195. if(percent < 0 and useContribution)
  196. amountSell := ((contibutingAmount*(percent)/100) + (Invested*(percent)/100))/close[1]
  197. else
  198. amountSell := (Invested*(percent)/100)/close[1]
  199.  
  200. currency = buy ? syminfo.currency : halfBuy ? syminfo.currency : syminfo.basecurrency
  201. textAmount = buy ? str.format("+{0}",amountBuy) : halfBuy ? str.format("+{0}",amountBuy) : str.format("-{0}",amountSell)
  202.  
  203. waitingAmount = amountBuy <= DCA ? (DCA - amountBuy) : (WaitingDCA - amountBuy)
  204. textWaitingAmount = buy ? str.format("+{0}",waitingAmount) : halfBuy ? str.format("+{0}",waitingAmount) : str.format("-{0}",waitingAmount)
  205.  
  206. l1 = label.new(bar_index, na,
  207. text=str.format("{0} {1}%\n amount {2} ({3})\n waiting amount {4} ({5})", textLabel, percent,textAmount,currency,textWaitingAmount,syminfo.currency),
  208. color=labelbgcolor,
  209. textcolor=labeltcolor,
  210. yloc=yloc.abovebar,
  211. style=label.style_label_down)
  212.  
  213. label.delete(labelSwitch ? l1[1] : l1)
  214.  
  215. //---------------------------------------------------------------------------------------------------
  216.  
  217. //------------------------------------------ Strategy -----------------------------------------------
  218. qty = 0.0
  219. bar = barstate.isnew
  220. last_bar = barstate.islastconfirmedhistory
  221.  
  222. //Calculate QTY and Waiting DCA
  223. if(buy)
  224. qty := amountBuy/close[1]
  225. contibutingAmount := percent > 100 and useContribution ? contibutingAmount * (percent-100)/100 : contibutingAmount + (DCA - amountBuy)
  226. else if(halfBuy)
  227. qty := amountBuy/close[1]
  228. contibutingAmount := percent > 100 and useContribution ? contibutingAmount * (percent-100)/100 : contibutingAmount + (DCA - amountBuy)
  229. else if(sell)
  230. qty := strategy.position_size * percent/100
  231.  
  232. plot(contibutingAmount,title="contibutingAmount",display=display.data_window)
  233. plot(qty,title="qty",display=display.data_window)
  234. plot(percent,title="percent",display=display.data_window)
  235. plot(amountBuy/close[1],title="amountBuy",display=display.data_window)
  236.  
  237. //Order
  238. if(strategySwitch)
  239. if(bar and buy)
  240. strategy.order(str.format("DCA Buy {0}% = {1} {2}",percent,qty,syminfo.basecurrency),strategy.long,qty=qty)
  241. else if(bar and halfBuy)
  242. strategy.order(str.format("DCA Normal {0}% = {1} {2}",percent,qty,syminfo.basecurrency),strategy.long,qty=qty)
  243. else if(bar and sell)
  244. strategy.order(str.format("DCA Sell {0}% = {1} {2}",percent,qty,syminfo.basecurrency),strategy.short,qty=qty)
  245.  
  246. //END
  247. //if(last_bar)
  248. //strategy.close_all()
  249.  
  250. //------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment