Advertisement
cryptoscum

Untitled

Jan 17th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.31 KB | None | 0 0
  1. //@version=4
  2.  
  3. strategy("Backtest Range", shorttitle=" ", overlay=true, max_bars_back=200,initial_capital=1000,precision=1)
  4.  
  5. // Revision:        1
  6. // Author:          @copy-paste-master
  7.  
  8. // Risk Management Setup
  9. leverage=input(1,"leverage",step=.5)
  10. tp=input(11,"take profit %",step=1)
  11. sl=input(3,"stoploss %",step=1)
  12. stoploss=1-(sl/100)
  13. plot(stoploss)
  14. level=input(.70,"level to initiate trade",step=.02)
  15. closelevel=input(0.0,"level to close trade",step=.02)
  16. levelshort=input(.68,"level to initiate trade",step=.02)
  17. closelevelshort=input(0.0,"level to close trade",step=.02)
  18.  
  19. // === INPUT BACKTEST RANGE ===
  20. FromMonth = input(defval=1, title="From Month", minval=1, maxval=12)
  21. FromDay = input(defval=1, title="From Day", minval=1, maxval=31)
  22. FromYear = input(defval=2019, title="From Year", minval=2017)
  23. ToMonth = input(defval=1, title="To Month", minval=1, maxval=12)
  24. ToDay = input(defval=1, title="To Day", minval=1, maxval=31)
  25. ToYear = input(defval=9999, title="To Year", minval=2017)
  26.  
  27. // === BACKTEST WINDOW ===
  28. start = timestamp(FromYear, FromMonth, FromDay, 00, 00)  // backtest start window
  29. finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)  // backtest finish window
  30. window() =>  // create function "within window of time"
  31.     time >= start and time <= finish ? true : false
  32.  
  33. // === SENTIMENT ===
  34.  
  35. wa=input(1.158,"weight a",step=.2)
  36. wb=input(1.119,"weight b",step=.2)
  37. wc=input(1.153,"weight c",step=.2)
  38. wd=input(1.272,"weight d",step=.2)
  39. we=input(1.295,"weight e",step=.2)
  40. wf=input(1.523,"weight f",step=.2)
  41. wg=input(1.588,"weight g",step=.2)
  42. wh=input(2.100,"weight h",step=.2)
  43. wi=input(1.816,"weight i",step=.2)
  44. wj=input(2.832,"weight j",step=.2)
  45. a=1
  46. b=2
  47. c=3
  48. d=5
  49. e=8
  50. f=13
  51. g=21
  52. h=34
  53. i=55
  54. j=89
  55.  
  56. n=0
  57. n:=if volume > -1
  58.     nz(n[1])+1
  59.  
  60.  
  61.  
  62. ra=highest(high,a)-lowest(low,a)
  63. aa=sma(ohlc4,a)
  64. ha=aa[1]+ra[1]/2
  65. la=aa[1]-ra[1]/2
  66.  
  67. rb=highest(high,b)-lowest(low,b)
  68. ab=sma(ohlc4,b)
  69. hb=ab[1]+rb[1]/2
  70. lb=ab[1]-rb[1]/2
  71.  
  72. rc=highest(high,c)-lowest(low,c)
  73. ac=sma(ohlc4,c)
  74. hc=ac[1]+rc[1]/2
  75. lc=ac[1]-rc[1]/2
  76.  
  77. rd=highest(high,d)-lowest(low,d)
  78. ad=sma(ohlc4,d)
  79. hd=ad[1]+rd[1]/2
  80. ld=ad[1]-rd[1]/2
  81.  
  82. re=highest(high,e)-lowest(low,e)
  83. ae=sma(ohlc4,e)
  84. he=ae[1]+re[1]/2
  85. le=ae[1]-re[1]/2
  86.  
  87. rf=highest(high,f)-lowest(low,f)
  88. af=sma(ohlc4,f)
  89. hf=af[1]+rf[1]/2
  90. lf=af[1]-rf[1]/2
  91.  
  92. rg=highest(high,g)-lowest(low,g)
  93. ag=sma(ohlc4,g)
  94. hg=ag[1]+rg[1]/2
  95. lg=ag[1]-rg[1]/2
  96.  
  97. rh=highest(high,h)-lowest(low,h)
  98. ah=sma(ohlc4,h)
  99. hh=ah[1]+rh[1]/2
  100. lh=ah[1]-rh[1]/2
  101.  
  102. ri=highest(high,i)-lowest(low,i)
  103. ai=sma(ohlc4,i)
  104. hi=ai[1]+ri[1]/2
  105. li=ai[1]-ri[1]/2
  106.  
  107. rj=highest(high,j)-lowest(low,j)
  108. aj=sma(ohlc4,j)
  109. hj=aj[1]+rj[1]/2
  110. lj=aj[1]-rj[1]/2
  111.  
  112. placea=((close-la)/(ha-la)-.5)*-100
  113. placeb=((close-lb)/(hb-lb)-.5)*-100
  114. placec=((close-lc)/(hc-lc)-.5)*-100
  115. placed=((close-ld)/(hd-ld)-.5)*-100
  116. placee=((close-le)/(he-le)-.5)*-100
  117. placef=((close-lf)/(hf-lf)-.5)*-100
  118. placeg=((close-lg)/(hg-lg)-.5)*-100
  119. placeh=((close-lh)/(hh-lh)-.5)*-100
  120. placei=((close-li)/(hi-li)-.5)*-100
  121. placej=((close-lj)/(hj-lj)-.5)*-100
  122.  
  123. sentiment=((placea/j)*ra*wa+(placeb/i)*rb*wb+(placec/h)*rc*wc+(placed/g)*rd*wd+(placee/f)*re*we+(placef/e)*rf*wf+(placeg/d)*rg*wg+(placeh/c)*rh*wh+(placei/b)*ri*wi+(placej/a)*rj*wj)/(wa+wb+wc+wd+we+wf+wg+wh+wi+wj)
  124.  
  125. deltalong=0.0
  126. deltalong:=if sentiment>0
  127.     nz(deltalong[1])+sentiment-sentiment[1]
  128. else
  129.     0
  130. deltashort=0.0  
  131. deltashort:=if sentiment<0
  132.     nz(deltashort[1])+((sentiment-sentiment[1])*-1)
  133. else
  134.     0
  135.  
  136. //plot(sentiment*-1,color=color.blue)    
  137. //plot(deltalong,color=color.red)
  138. //plot(deltashort,color=color.lime)
  139.  
  140. peakfindlong=highest(deltalong,j)*level
  141.  
  142.  
  143. peakfindshort=highest(deltashort,j)*levelshort
  144.  
  145.  
  146. contracts=(strategy.equity/close)*leverage
  147.  
  148.  
  149. //reason for o is this strategy makes dumb trades before the sentiment line crosses the 0 point the first time
  150. o=0
  151. o:=if cross(0,sentiment) and n>j
  152.     1
  153. else
  154.     nz(o[1])
  155.  
  156. long=deltashort>peakfindlong and o==1
  157.  
  158. short=deltalong>peakfindshort and o==1
  159.  
  160.  
  161. longstart=0.0
  162. longstart:=if strategy.position_size>0 and strategy.position_size[1]<=0
  163.     close
  164. else
  165.     nz(longstart[1])
  166.  
  167. shortstart=0.0
  168. shortstart:=if strategy.position_size<0 and strategy.position_size[1]>=0
  169.     close
  170. else
  171.     nz(shortstart[1])    
  172.  
  173. highsincelong = 0.0
  174. highsincelong := if strategy.position_size>0
  175.     max(max(highsincelong[1],high),high[1])
  176. else
  177.     0
  178.  
  179. lowsinceshort = 1000000.0
  180. lowsinceshort := if strategy.position_size<0
  181.     min(min(lowsinceshort[1],low),low[1])
  182. else
  183.     10000000
  184.  
  185. closelong=strategy.position_size > 0 and ((highsincelong/longstart-1)*100) > tp
  186. closeshort=strategy.position_size < 0 and ((shortstart/lowsinceshort-1)*100) > tp
  187.  
  188. stoptrade=0
  189. stoptrade:= if closelong
  190.     1
  191. else
  192.     nz(stoptrade[1])
  193.  
  194. stoptrade:= if short and stoptrade[1]==1
  195.     0
  196. else
  197.     stoptrade
  198.  
  199. stoptrade:= if closeshort
  200.     -1
  201. else
  202.     stoptrade
  203.    
  204. stoptrade:= if long and stoptrade[1]==-1
  205.     0
  206. else
  207.     stoptrade    
  208.  
  209. if(closelong)
  210.     strategy.close("Long1")  
  211.  
  212. pnllong = ((close - strategy.position_avg_price) / strategy.position_avg_price)*100
  213. pnlshort = ((strategy.position_avg_price-close) / strategy.position_avg_price) *100
  214. plot (strategy.position_size > 0 ?(highsincelong/longstart-1)*100 : 0.0,color=color.lime,linewidth=2)
  215. plot (strategy.position_size < 0 ?(shortstart/lowsinceshort-1)*100 : 0.0,color=color.red,linewidth=2)  
  216. plot( strategy.position_size > 0 ? pnllong:0, color=strategy.position_size > 0 ?color.yellow:color.black,linewidth=2 )
  217. plot( strategy.position_size < 0 ? pnlshort:0, color=strategy.position_size < 0 ?color.orange:color.black,linewidth=2)
  218. longuntilshort=0
  219. longuntilshort:=if (long and window())
  220.     1
  221. else
  222.     if (short and window())
  223.         -1
  224.     else
  225.         nz(longuntilshort[1])
  226. bgcolor(stoptrade!=0?color.black:longuntilshort==1?color.lime:longuntilshort==-1?color.red:na,transp=70)  
  227.  
  228. if(long and stoptrade==0)
  229.     strategy.entry("Long1",strategy.long,when=window(),qty=max(1,min(contracts,1000000000)))
  230.  
  231. if(closelong)
  232.     strategy.close("Long1")
  233.    
  234. strategy.exit("Long1",stop=longstart * stoploss,when = strategy.position_size>0)
  235.  
  236. if(short and stoptrade==0)    
  237.     strategy.entry("Short1",strategy.short,when=window(),qty=max(1,min(contracts,1000000000)))
  238.  
  239. if(closeshort)
  240.     strategy.close("Short1")
  241.  
  242. strategy.exit("Long1",stop=shortstart / stoploss,when = strategy.position_size<0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement