Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.50 KB | None | 0 0
  1. //@version=3
  2. strategy("TD9 Strat", overlay=true,default_qty_type=strategy.fixed, default_qty_value=1, initial_capital=1000, currency=currency.EUR,commission_type=strategy.commission.percent,commission_value=0.05)
  3.  
  4. // === BACKTEST RANGE ===
  5. FromMonth = input(defval = 1, title = "From Month", minval = 1)
  6. FromDay   = input(defval = 1, title = "From Day", minval = 1)
  7. FromYear  = input(defval = 2018, title = "From Year", minval = 2014)
  8. ToMonth   = input(defval = 1, title = "To Month", minval = 1)
  9. ToDay     = input(defval = 1, title = "To Day", minval = 1)
  10. ToYear    = input(defval = 9999, title = "To Year", minval = 2014)
  11.  
  12. TPEnabled=1
  13. TPValue=45.0
  14. SLValue=25.0
  15.  
  16. tp_val = input(TPValue, title='TP', type=float)/100
  17. sl_val = input(SLValue, title='SL', type=float)/100
  18. short_tp = strategy.position_avg_price * (1 - tp_val)
  19. long_tp = strategy.position_avg_price * (1 + tp_val)
  20.  
  21. long_sl = strategy.position_avg_price * (1 - sl_val)
  22. short_sl = strategy.position_avg_price * (1 + sl_val)
  23.  
  24.  
  25.  
  26. // === SERIES SETUP ===
  27.  
  28. //SETUP
  29. //BUY SETUP
  30. compteur=0
  31. compteur:=nz(compteur[1],0)
  32. buySetupCondition = close < close[4]
  33. compteur := buySetupCondition ? compteur + 1 : compteur
  34. compteur := close >= close[4] and compteur!= 0 ? 0 : compteur
  35. buy= buySetupCondition
  36. c1 = compteur==1 and buy
  37. c2 = compteur==2 and buy
  38. c3 = compteur==3 and buy
  39. c4 = compteur==4 and buy
  40. c5 = compteur==5 and buy
  41. c6 = compteur==6 and buy
  42. c7 = compteur==7 and buy
  43. c8 = compteur==8 and buy
  44. c9 = compteur==9 and buy
  45. //SELL SETUP
  46. compteurS=0
  47. compteurS:=nz(compteurS[1],0)
  48. sellSetupCondition = close > close[4]
  49. compteurS := sellSetupCondition ? compteurS + 1 : compteurS
  50. compteurS := close <= close[4] and compteurS!= 0  ? 0 : compteurS
  51. sell= sellSetupCondition
  52. s1 = compteurS==1 and sell
  53. s2 = compteurS==2 and sell
  54. s3 = compteurS==3 and sell
  55. s4 = compteurS==4 and sell
  56. s5 = compteurS==5 and sell
  57. s6 = compteurS==6 and sell
  58. s7 = compteurS==7 and sell
  59. s8 = compteurS==8 and sell
  60. s9 = compteurS==9 and sell
  61.  
  62. plotchar(s1 or c1?close:na,char='1',color=s1?green:red)
  63. plotchar(s2 or c2?close:na,char='2',color=s2?green:red)
  64. plotchar(s3 or c3?close:na,char='3',color=s3?green:red)
  65. plotchar(s4 or c4?close:na,char='4',color=s4?green:red)
  66. plotchar(s5 or c5?close:na,char='5',color=s5?green:red)
  67. plotchar(s6 or c6?close:na,char='6',color=s6?green:red)
  68. plotchar(s7 or c7?close:na,char='7',color=s7?green:red)
  69. plotchar(s8 or c8?close:na,char='8',color=s8?green:red)
  70. plotchar(s9 or c9?close:na,char='9',color=s9?green:red)
  71. // INTERSECTIONS
  72. //LONG & SHORT
  73. intersectionConditionB = (compteur == 8 or compteur == 9) and (high >= low[3] or high >= low[4])
  74. plotchar(intersectionConditionB?close:na,char='*',color=aqua,size=size.small)
  75. intersectionConditionS = (compteurS == 8 or compteurS == 9) and (low <= high[3] or low <= high[4])
  76. plotchar(intersectionConditionS?close:na,char='*',color=green,size=size.small)
  77.  
  78. //COUNTDOWN
  79. countdownB=0
  80. countdownS=0
  81. countdownB:=nz(countdownB[1],0)
  82. countdownS:=nz(countdownS[1],0)
  83. cdConditionB = compteur>=9 and close < low[2]
  84. cdConditionS = compteurS>=9 and close > high[2]
  85. countdownB := cdConditionB and compteur>=9? countdownB + 1 : countdownB
  86. countdownS := cdConditionS and compteurS>=9? countdownS + 1 : countdownS
  87. countdownB := compteur<9 ? 0 : countdownB
  88. countdownS := compteurS<9 ? 0 : countdownS
  89. cdb1=cdConditionB and countdownB==1
  90. cds1=cdConditionS and countdownS==1
  91. cdb2=cdConditionB and countdownB==2
  92. cds2=cdConditionS and countdownS==2
  93. cdb3=cdConditionB and countdownB==3
  94. cds3=cdConditionS and countdownS==3
  95. cdb4=cdConditionB and countdownB==4
  96. cds4=cdConditionS and countdownS==4
  97. cdb5=cdConditionB and countdownB==5
  98. // cds5=cdConditionS and countdownS==5
  99. // cdb6=cdConditionB and countdownB==6
  100. // cds6=cdConditionS and countdownS==6
  101. // cdb7=cdConditionB and countdownB==7
  102. // cds7=cdConditionS and countdownS==7
  103. // cdb8=cdConditionB and countdownB==8
  104. // cds8=cdConditionS and countdownS==8
  105. // cdb9=cdConditionB and countdownB==9
  106. // cds9=cdConditionS and countdownS==9
  107. // cdb10=cdConditionB and countdownB==10
  108. // cds10=cdConditionS and countdownS==10
  109. // cdb11=cdConditionB and countdownB==11
  110. // cds11=cdConditionS and countdownS==11
  111. // cdb12=cdConditionB and countdownB==12
  112. // cds12=cdConditionS and countdownS==12
  113. // cdb13=cdConditionB and countdownB==13
  114. // cds13=cdConditionS and countdownS==13
  115.  
  116. plotchar(cdb1 or cds1?close:na,char='1',color=cdb1?aqua:green,size=size.small,location=location.belowbar)
  117. plotchar(cdb2 or cds2?close:na,char='2',color=cdb2?aqua:green,size=size.small,location=location.belowbar)
  118. plotchar(cdb3 or cds3?close:na,char='3',color=cdb3?aqua:green,size=size.small,location=location.belowbar)
  119. plotchar(cdb4 or cds4?close:na,char='4',color=cdb4?aqua:green,size=size.small,location=location.belowbar)
  120. // plotchar(cdb5 or cds5?close:na,char='5',color=cdb5?aqua:green,size=size.small,location=location.belowbar)
  121. // plotchar(cdb6 or cds6?close:na,char='6',color=cdb6?aqua:green,size=size.small,location=location.belowbar)
  122. // plotchar(cdb7 or cds7?close:na,char='7',color=cdb7?aqua:green,size=size.small,location=location.belowbar)
  123. // plotchar(cdb8 or cds8?close:na,char='8',color=cdb8?aqua:green,size=size.small,location=location.belowbar)
  124. // plotchar(cdb9 or cds9?close:na,char='9',color=cdb9?aqua:green,size=size.small,location=location.belowbar)
  125. // plotchar(cdb10 or cds10?close:na,char='⑩',color=cdb10?aqua:green,size=size.small,location=location.belowbar)
  126. // plotchar(cdb11 or cds11?close:na,char='⑪',color=cdb11?aqua:green,size=size.small,location=location.belowbar)
  127. // plotchar(cdb12 or cds12?close:na,char='⑫',color=cdb12?aqua:green,size=size.small,location=location.belowbar)
  128. // plotchar(cdb13 or cds13?close:na,char='⑬',color=cdb13?aqua:green,size=size.small,location=location.belowbar)
  129.  
  130. //MMA for bullish or bearish recognition
  131. lag1=input(1,minval=1,title="lag MMA 1")
  132. lag2=input(26,minval=1,title="lag MMA 2")
  133. mma1 = sma(close,lag1)
  134. mma2 = sma(close,lag2)
  135. plot(mma1,color=red,title="MMA 9")
  136. plot(mma2,color=green,title="MMA 18")
  137.  
  138.  
  139. //buycondition : green 1 and one in last 4 bars were in buycountdown (buyCD, depends on bullish or bearish trend)
  140. //sellcondition : green 1 and one in last 4 bars were in sellcountdown
  141. // plotchar(close>(buySetupCondition and compteur!=0 ? close[compteur] : 999999999),char='B',location=location.bottom,color=aqua,size=size.small)
  142. // plotchar(close<(sellSetupCondition and compteurS!=0 ? close[compteurS] : -9999999999),char='S',location=location.bottom,color=green,size=size.small)
  143. cd1 = input(1,minval=1,title="CD 1")
  144. cd2 = input(13,minval=1,title="CD 2")
  145. buyCD = mma2 < mma1 ? cd1 : cd2
  146. sellCD = mma2 > mma1? cd1 : cd2
  147. buycondition = compteurS==1
  148. sellcondition = compteur==1
  149. //plotchar(buycondition,char='B',location=location.bottom,color=aqua,size=size.small)
  150. //plotchar(sellcondition,char='S',location=location.bottom,color=green,size=size.small)
  151.  
  152. plotgreen = plot(buySetupCondition and nz(compteurS[1],0)==0 ? close[compteur] : na, color = red, linewidth=2,style=linebr)
  153. plotred = plot(sellSetupCondition and nz(compteur[1],0)==0 ? close[compteurS] : na, color = green, linewidth=2,style=linebr)
  154. plot(close,linewidth=2,color=yellow)
  155.  
  156.  
  157.  
  158. // === ALERTS ===
  159. strategy.entry("long", long=strategy.long,  when = buycondition and strategy.position_size <=0)
  160. strategy.close("long", when = sellcondition )
  161.  
  162.  
  163. strategy.entry("short", long=strategy.short,  when = sellcondition and strategy.position_size >=0)
  164. strategy.close("short", when = buycondition )
  165.  
  166.  
  167.  
  168.  
  169. if(TPEnabled)
  170.     strategy.exit("TP","long", limit=long_tp)
  171.     strategy.exit("TP","short", limit=short_tp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement