Advertisement
podolandrasa

[Learn] DCA using signal

Mar 19th, 2022
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.43 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © podolkerod
  3.  
  4. //@version=5
  5. varip str_script_title = "[Learn] DCA using signal"
  6. //-----------------------------------------------------------------------------------------------------------------------------------------------------------------{
  7.  
  8. indicator(str_script_title, overlay=true) // timeframe="", timeframe_gaps=true)
  9.  
  10. // Colors
  11. varip color_ma01 = #d1d4dc // #f5eb5d // #ffe0b2
  12. varip color_ma02 = #f1c21b // #f5b771 // #33DBF7
  13. varip color_ma03 = #00e2ff // #f57d51 // #FFA7CF
  14. varip color_ma04 = #f55151 // #EBADF7
  15. varip color_ma05 = #cc125b // #FF8A5E
  16. varip color_ma06 = #b249c3
  17. varip color_supertrendUp = #2ABEB3
  18. varip color_supertrendDn = #FF7A65
  19. varip color_LongA = #2ABEB3
  20. varip color_LongB = #00C09F
  21. varip color_LongC = #00C6C7
  22. varip color_LongD = #00BA45
  23. varip color_LongE = #00B1FF
  24. varip color_LongF = #008AC4
  25. varip color_ShrtA = #E61C5D
  26. varip color_ShrtB = #FF791B
  27. varip color_ShrtC = #FF55F3
  28. varip color_ShrtD = #FF4766
  29. varip color_ShrtE = #FF7A65
  30. varip color_ShrtF = #E59C24
  31. varip color_enterL_Price = #ffffff // #f7894980
  32. varip color_longSL = #4a0d70
  33. varip color_longTP = #53B71A
  34. varip color_longSLhit = #961EE1
  35. varip color_longTPhit = #53B71A
  36. varip color_enterS_Price = #ffffff // #f7894980
  37. varip color_shortSL = #4a0d70
  38. varip color_shortTP = #1ECBE1
  39. varip color_shortSLhit = #961EE1
  40. varip color_shortTPhit = #1AA2B7
  41.  
  42. //═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
  43. // MOVING AVERAGES
  44. //═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════{
  45.  
  46.  
  47. i_emaLength1 = input.int(title="EMA 1", defval=8)
  48. i_emaLength2 = input.int(title="EMA 2", defval=20)
  49. i_emaLength3 = input.int(title="EMA 3", defval=50)
  50. i_emaLength4 = input.int(title="EMA 4", defval=100)
  51.  
  52. ema1 = ta.ema(close, i_emaLength1)
  53. ema2 = ta.ema(close, i_emaLength2)
  54. ema3 = ta.ema(close, i_emaLength3)
  55. ema4 = ta.ema(close, i_emaLength4)
  56.  
  57. plot(ema1, color=color.blue)
  58. plot(ema2, color=color.green)
  59. plot(ema3, color=color.red)
  60. plot(ema4, color=color.yellow)
  61.  
  62. //---------------------------------------------------------------------------------------------------------------------------------
  63. // END of MOVING AVERAGES
  64. //---------------------------------------------------------------------------------------------------------------------------------}
  65. //═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
  66. // SIGNAL FILTERS & RULES
  67. //═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════{
  68. // Inputs
  69. //----------------------------------//----------------------------------//{
  70. varip groupShowSignals = "Show/Hide Signals"
  71.  
  72. i_draw_LongA = input(false, 'Long A', group = groupShowSignals, inline="L01")
  73. i_draw_LongB = input(false, 'Long B', group = groupShowSignals, inline="L01")
  74. i_draw_LongC = input(false, 'Long C', group = groupShowSignals, inline="L01")
  75. i_draw_ShrtA = input(false, 'Short A', group = groupShowSignals, inline="L02")
  76. i_draw_ShrtB = input(false, 'Short B', group = groupShowSignals, inline="L02")
  77. i_draw_ShrtC = input(false, 'Short C', group = groupShowSignals, inline="L02")
  78.  
  79. // Filters
  80. ema1_crossOver_ema2 = ta.crossover(ema1, ema2)
  81. ema2_crossOver_ema3 = ta.crossover(ema2, ema3)
  82. ema3_crossOver_ema4 = ta.crossover(ema3, ema4)
  83. ema1_crossUnder_ema2 = ta.crossunder(ema1, ema2)
  84. ema2_crossUnder_ema3 = ta.crossunder(ema2, ema3)
  85. ema3_crossUnder_ema4 = ta.crossunder(ema3, ema4)
  86.  
  87. // Entry Exit Conditions
  88. entryLong_A = ema1_crossOver_ema2
  89. entryShrt_A = ema2_crossOver_ema3
  90. entryLong_B = ema3_crossOver_ema4
  91. entryShrt_B = ema1_crossUnder_ema2
  92. entryLong_C = ema2_crossUnder_ema3
  93. entryShrt_C = ema3_crossUnder_ema4
  94.  
  95. entryLong_All = entryLong_A or entryLong_B or entryLong_C
  96. entryShrt_All = entryShrt_A or entryShrt_B or entryShrt_C
  97.  
  98. signal_Off = 0
  99.  
  100. //----------------------------------//----------------------------------//}
  101.  
  102. //-----------------------------------------------------------------------------------------------------------------------------------------------------------------
  103. // END OF SIGNAL FILTERS & RULES
  104. //-----------------------------------------------------------------------------------------------------------------------------------------------------------------}
  105. //═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
  106. // STRATEGY TESTER
  107. //═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════{
  108. // Input Signal
  109. //----------------------------------//----------------------------------//{
  110. varip groupBacktestStrat = "Visualize Backtest"
  111.  
  112. i_trigger_entryL = input.string(title="Long OP | Exit Trigger", defval="OFF", inline="line_bt_signal_0",
  113. options=[
  114. "LONG ALL",
  115. "Long A", "Long B", "Long C",
  116. "SHORT ALL",
  117. "Short A", "Short B", "Short C",
  118. "OFF"
  119. ], group = groupBacktestStrat)
  120.  
  121. i_trigger_exitL = input.string(title="", defval="OFF", inline="line_bt_signal_0",
  122. options=[
  123. "LONG ALL",
  124. "Long A", "Long B", "Long C",
  125. "SHORT ALL",
  126. "Short A", "Short B", "Short C",
  127. "OFF"
  128. ], group = groupBacktestStrat)
  129.  
  130. i_trigger_entryS = input.string(title="Short OP | Exit Trigger", defval="OFF", inline="line_bt_signal_1",
  131. options=[
  132. "LONG ALL",
  133. "Long A", "Long B", "Long C",
  134. "SHORT ALL",
  135. "Short A", "Short B", "Short C",
  136. "OFF"
  137. ], group = groupBacktestStrat)
  138.  
  139. i_trigger_exitS = input.string(title="", defval="OFF", inline="line_bt_signal_1",
  140. options=[
  141. "LONG ALL",
  142. "Long A", "Long B", "Long C",
  143. "SHORT ALL",
  144. "Short A", "Short B", "Short C",
  145. "OFF"
  146. ], group = groupBacktestStrat)
  147.  
  148. // Declarations
  149. enterL =
  150. i_trigger_entryL=="LONG ALL" ? entryLong_All :
  151. i_trigger_entryL=="Long A" ? entryLong_A :
  152. i_trigger_entryL=="Long B" ? entryLong_B :
  153. i_trigger_entryL=="Long C" ? entryLong_C :
  154. i_trigger_entryL=="SHORT ALL" ? entryShrt_All :
  155. i_trigger_entryL=="Short A" ? entryShrt_A :
  156. i_trigger_entryL=="Short B" ? entryShrt_B :
  157. i_trigger_entryL=="Short C" ? entryShrt_C :
  158. signal_Off
  159.  
  160. exitL =
  161. i_trigger_exitL=="LONG ALL" ? entryLong_All :
  162. i_trigger_exitL=="Long A" ? entryLong_A :
  163. i_trigger_exitL=="Long B" ? entryLong_B :
  164. i_trigger_exitL=="Long C" ? entryLong_C :
  165. i_trigger_exitL=="SHORT ALL" ? entryShrt_All :
  166. i_trigger_exitL=="Short A" ? entryShrt_A :
  167. i_trigger_exitL=="Short B" ? entryShrt_B :
  168. i_trigger_exitL=="Short C" ? entryShrt_C :
  169. signal_Off
  170.  
  171. enterS =
  172. i_trigger_entryS=="LONG ALL" ? entryLong_All :
  173. i_trigger_entryS=="Long A" ? entryLong_A :
  174. i_trigger_entryS=="Long B" ? entryLong_B :
  175. i_trigger_entryS=="Long C" ? entryLong_C :
  176. i_trigger_entryS=="SHORT ALL" ? entryShrt_All :
  177. i_trigger_entryS=="Short A" ? entryShrt_A :
  178. i_trigger_entryS=="Short B" ? entryShrt_B :
  179. i_trigger_entryS=="Short C" ? entryShrt_C :
  180. signal_Off
  181.  
  182. exitS =
  183. i_trigger_exitS=="LONG ALL" ? entryLong_All :
  184. i_trigger_exitS=="Long A" ? entryLong_A :
  185. i_trigger_exitS=="Long B" ? entryLong_B :
  186. i_trigger_exitS=="Long C" ? entryLong_C :
  187. i_trigger_exitS=="SHORT ALL" ? entryShrt_All :
  188. i_trigger_exitS=="Short A" ? entryShrt_A :
  189. i_trigger_exitS=="Short B" ? entryShrt_B :
  190. i_trigger_exitS=="Short C" ? entryShrt_C :
  191. signal_Off
  192.  
  193. // Input TP SL (%)
  194. varip groupBacktestTPSL = "TP / SL"
  195.  
  196. i_strat_longTPpercent = input.float(title="Long TP | Long SL (%)", minval=0.0, step=0.1, defval=0.66, inline='L01', group = groupBacktestTPSL) / 100
  197. i_strat_longSLpercent = input.float(title="", minval=0.0, step=0.1, defval=5.1, inline='L01', group = groupBacktestTPSL) / 100
  198. i_strat_shrtTPpercent = input.float(title="Short TP | Short SL (%)", minval=0.0, step=0.1, defval=0.66, inline='L02', group = groupBacktestTPSL) / 100
  199. i_strat_shrtSLpercent = input.float(title="", minval=0.0, step=0.1, defval=5.1, inline='L02', group = groupBacktestTPSL) / 100
  200. //----------------------------------//----------------------------------//}
  201. // Calculation
  202. //----------------------------------//----------------------------------//{
  203.  
  204. // Detect what was last signal (long or short)
  205. enterL_enterS = 0
  206. last_enterL = enterL and (nz(enterL_enterS[1]) == 0 or nz(enterL_enterS[1]) == -1)
  207. last_enterS = enterS and (nz(enterL_enterS[1]) == 0 or nz(enterL_enterS[1]) == 1)
  208. enterL_enterS := last_enterL ? 1 : last_enterS ? -1 : enterL_enterS[1]
  209.  
  210. // Entry price
  211. enterL_Price = ta.valuewhen(last_enterL, close, 0)
  212. enterS_Price = ta.valuewhen(last_enterS, close, 0)
  213.  
  214. // Fixed TP SL prices
  215. longSL = enterL_Price * (1 - i_strat_longSLpercent)
  216. shortSL = enterS_Price * (1 + i_strat_shrtSLpercent)
  217. longTP = enterL_Price * (1 + i_strat_longTPpercent)
  218. shortTP = enterS_Price * (1 - i_strat_shrtTPpercent)
  219.  
  220. // Remove first bar for SL/TP (you can't enter a trade at bar close THEN hit your SL on that same bar)
  221. longBar1 = ta.barssince(last_enterL)
  222. longBar2 = longBar1 >= 1 ? true : false
  223. shortBar1 = ta.barssince(last_enterS)
  224. shortBar2 = shortBar1 >= 1 ? true : false
  225.  
  226. // Check for SL hit during a bar
  227. longSLhit = enterL_enterS==1 and longBar2 and low < longSL
  228. shortSLhit = enterL_enterS==-1 and shortBar2 and high > shortSL
  229.  
  230. // Check for TP hit during bar
  231. longTPhit = enterL_enterS==1 and longBar2 and high > longTP
  232. shortTPhit = enterL_enterS==-1 and shortBar2 and low < shortTP
  233.  
  234. // Reset enterL_enterS if SL/TP hit during bar
  235. enterL_enterS := (enterL_enterS==1 or enterL_enterS==0) and longBar2 and (longSLhit or longTPhit) ? 0 :
  236. (enterL_enterS==-1 or enterL_enterS==0) and shortBar2 and (shortSLhit or shortTPhit) ? 0 : enterL_enterS
  237.  
  238. //----------------------------------//----------------------------------//}
  239. // Plot
  240. //----------------------------------//----------------------------------//{
  241. // Plot Entry Price
  242. plot(enterL_enterS==1 ? enterL_Price : na, style=plot.style_linebr, color=color_enterL_Price, linewidth=2, title="Entry Long")
  243. plot(enterL_enterS==-1 ? enterS_Price : na, style=plot.style_linebr, color=color_enterS_Price, linewidth=2, title="Entry Short")
  244.  
  245. // plot TP SL lines
  246. plot(enterL_enterS==1 ? longSL : na, style=plot.style_linebr, color=color_longSL, linewidth=1, title="Long Fixed SL")
  247. plot(enterL_enterS==-1 ? shortSL : na, style=plot.style_linebr, color=color_shortSL, linewidth=1, title="Short Fixed SL")
  248. plot(enterL_enterS==1 ? longTP : na, style=plot.style_linebr, color=color_longTP, linewidth=1, title="Long Fixed TP")
  249. plot(enterL_enterS==-1 ? shortTP : na, style=plot.style_linebr, color=color_shortTP, linewidth=1, title="Short Fixed TP")
  250.  
  251. // Plot SL Hit
  252. plotshape(longSLhit, style=shape.labelup, location=location.belowbar, color=color_longSLhit, size=size.tiny, title="Long SL Hit", text=" Long SL", textcolor=color.white)
  253. plotshape(shortSLhit, style=shape.labeldown, location=location.abovebar, color=color_shortSLhit, size=size.tiny, title="Short SL Hit", text=" Short SL", textcolor=color.white)
  254.  
  255. // Plot TP Hit
  256. plotshape(longTPhit, style=shape.labeldown, location=location.abovebar, color=color_longTPhit, size=size.tiny, title="Long TP Hit", text="Long TP", textcolor=color.white)
  257. plotshape(shortTPhit, style=shape.labelup, location=location.belowbar, color=color_shortTPhit, size=size.tiny, title="Short TP Hit", text="Short TP", textcolor=color.white)
  258.  
  259. //----------------------------------//----------------------------------//}
  260. // Connector for [BT] Robotempur or TESW - Robotempur
  261. //----------------------------------//----------------------------------//{
  262. strat_trigger = 0
  263. var trade_cond = 0
  264. var StopLoss = float ( na )
  265. var TakeProfit = float ( na )
  266. var EntryPrice = float ( na )
  267.  
  268. if enterL
  269. StopLoss := enterL_Price * ( 1 - i_strat_longSLpercent )
  270. TakeProfit := enterL_Price * ( 1 + i_strat_longTPpercent )
  271. EntryPrice := enterL_Price
  272. trade_cond := 1
  273. strat_trigger := 1
  274. else if enterS
  275. StopLoss := enterS_Price * ( 1 + i_strat_shrtSLpercent )
  276. TakeProfit := enterS_Price * ( 1 - i_strat_shrtTPpercent )
  277. EntryPrice := enterS_Price
  278. trade_cond := -1
  279. strat_trigger := -1
  280.  
  281. strat_TP_long = longTPhit
  282. strat_SL_long = longSLhit
  283. strat_TP_shrt = shortTPhit
  284. strat_SL_shrt = shortSLhit
  285.  
  286. if trade_cond > 0 and strat_TP_long
  287. strat_trigger := 3
  288. else if trade_cond < 0 and strat_TP_shrt
  289. strat_trigger := -3
  290.  
  291. // For [BT] Robotempur
  292. if trade_cond > 0 and ( exitL or strat_SL_long or strat_TP_long)
  293. strat_trigger := 2
  294. EntryPrice := float ( na )
  295. trade_cond := 0
  296. else if trade_cond < 0 and ( exitS or strat_SL_shrt or strat_TP_shrt)
  297. strat_trigger := -2
  298. EntryPrice := float ( na )
  299. trade_cond := 0
  300.  
  301. //----------------------------------//----------------------------------//}
  302.  
  303.  
  304. //-----------------------------------------------------------------------------------------------------------------------------------------------------------------
  305. // END OF STRATEGY TESTER
  306. //-----------------------------------------------------------------------------------------------------------------------------------------------------------------}
  307. //═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
  308. // ALERTS
  309. //═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════{
  310.  
  311. // Create Alert Conditions
  312. alertcondition(entryLong_All, "++LONG ALL", message="Long All" + ' on {{ticker}} {{interval}} at {{close}}')
  313. alertcondition(entryShrt_All, "--SHORT ALL", message="Short All" + ' on {{ticker}} {{interval}} at {{close}}')
  314. alertcondition(entryLong_A, "Long A", message="Long A" + ' on {{ticker}} {{interval}} at {{close}}')
  315. alertcondition(entryLong_B, "Long B", message="Long B" + ' on {{ticker}} {{interval}} at {{close}}')
  316. alertcondition(entryLong_C, "Long C", message="Long C" + ' on {{ticker}} {{interval}} at {{close}}')
  317. alertcondition(entryShrt_A, "Short A", message="Short A" + ' on {{ticker}} {{interval}} at {{close}}')
  318. alertcondition(entryShrt_B, "Short B", message="Short B" + ' on {{ticker}} {{interval}} at {{close}}')
  319. alertcondition(entryShrt_C, "Short C", message="Short C" + ' on {{ticker}} {{interval}} at {{close}}')
  320.  
  321. // Plot Signals
  322. plotchar(i_draw_LongA ? entryLong_A : na, title = 'Long A', char='.', color=color.new(color_LongA, 0), location = location.belowbar, size = size.normal, text='L-A')
  323. plotchar(i_draw_LongB ? entryLong_B : na, title = 'Long B', char='.', color=color.new(color_LongB, 0), location = location.belowbar, size = size.normal, text='L-B')
  324. plotchar(i_draw_LongC ? entryLong_C : na, title = 'Long C', char='.', color=color.new(color_LongC, 0), location = location.belowbar, size = size.normal, text='L-C')
  325. plotchar(i_draw_ShrtA ? entryShrt_A : na, title = 'Short A', char='.', color=color.new(color_ShrtA, 0), location = location.abovebar, size = size.normal, text='S-A')
  326. plotchar(i_draw_ShrtB ? entryShrt_B : na, title = 'Short B', char='.', color=color.new(color_ShrtB, 0), location = location.abovebar, size = size.normal, text='S-B')
  327. plotchar(i_draw_ShrtC ? entryShrt_C : na, title = 'Short C', char='.', color=color.new(color_ShrtC, 0), location = location.abovebar, size = size.normal, text='S-C')
  328.  
  329. // Plot Strategy Tester
  330. plot(strat_trigger, "═══════ [BT] Strategy Trigger ═══════", color ( na ), editable = false, display = display.none )
  331.  
  332.  
  333. //-----------------------------------------------------------------------------------------------------------------------------------------------------------------
  334. // END OF ALERTS
  335. //-----------------------------------------------------------------------------------------------------------------------------------------------------------------}
  336.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement