Advertisement
mksamanes

Untitled

May 24th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.86 KB | None | 0 0
  1. //@version=3
  2. strategy(title='Timeframe Ichimoku Strategy v11', shorttitle='Ichimoku Strategy v11', initial_capital=10000, pyramiding= 0, default_qty_value=100,
  3. default_qty_type= strategy.percent_of_equity, commission_type=strategy.commission.percent, commission_value=0.1, overlay=true)
  4. tf = input(title='Timeframe:', type=integer, defval=1, minval=1)
  5. factor = input(title='Factor Multiplier Timeframe:', type=float, defval=1.0, minval=0.1, maxval=2, step=0.1)
  6. conversionPeriods = input(title='Conversion Periods', type=integer, defval=10, minval=1),
  7. basePeriods = input(title='Base Periods', type=integer, defval=30, minval=1)
  8. laggingSpan2Periods = input(title='Lagging Span', type=integer, defval=60, minval=1),
  9. displacement = input(title='Displacement', type=integer, defval=30, minval=1)
  10.  
  11.  
  12. //--------------------------Ichimoku multi time-frame-------------------------
  13.  
  14. //You can use bigger and even smaller than the timeframe ichimokus, these are useful for enlarging the backtesting in tradingview. This is done by dividing ichimoku parameters.
  15. //Options are:
  16. //Dividing by 2: Parameters 5,15,30,15 then ichimoku you are drawing is the timeframe you are in divided by 5 in the 1min timeframe
  17. //Example if you are in the 1h timeframe and divide the parameters by 2 you are drawing the 12min ichimoku of course with less resolution but useful for testing
  18. //Dividing by 5: Parameters 2,6,12,6 then ichimoku you are drawing is the timeframe you are in divided by 15 in the 1min timeframe
  19. //Example if you are in the 1h timeframe and divide the parameters by 5 you are drawing the 4min ichimoku of course with less resolution but useful for testing
  20. //Dividing by 2: Parameters 1,3,6,3 then ichimoku you are drawing is the timeframe you are in divided by 30 in the 1min timeframe
  21. //Example if you are in the 1h timeframe and divide the parameters by 10 you are drawing the 2min ichimoku timeframe of course with less resolution but useful for testing
  22. //Resolution here is quite bad but again still useful, in any case is preferable to use diving by 2 or dividing by 5 with better resolution in that order
  23. //Multiplying is quite forward if you multiply by 8 in the 1min timeframe you get the 8 min ichimoku
  24.  
  25. f_donchian(_len) => avg(lowest(_len), highest(_len))
  26.  
  27. f_ichimoku_cloud(_conversion_periods, _base_periods, _lagging_span)=>
  28. _conversion_line = f_donchian(_conversion_periods)
  29. _base_line = f_donchian(_base_periods)
  30. _lead_line1 = avg(_conversion_line, _base_line)
  31. _lead_line2 = f_donchian(_lagging_span)
  32. [_conversion_line, _base_line, _lead_line1, _lead_line2]
  33.  
  34. //timeframe transformation
  35.  
  36. [conversionLine, baseLine, leadLine1, leadLine2] = f_ichimoku_cloud(round(tf*factor*conversionPeriods), round(tf*factor*basePeriods), round(tf*factor*laggingSpan2Periods))
  37. s1 = leadLine1[round(tf*factor*displacement)]
  38. r1 = leadLine2[round(tf*factor*displacement)]
  39. c1 = conversionLine
  40. b1 = baseLine
  41. upperCloud = max(s1,r1)
  42. lowerCloud = min(r1,s1)
  43.  
  44. p1 = plot(leadLine1, offset = round(tf*factor*displacement), color=green, title="Span A")
  45. p2 = plot(leadLine2, offset = round(tf*factor*displacement), color=red, title="Span B")
  46. fill(p1, p2, color = leadLine1 > leadLine2 ? green : red, title="Cloud")
  47. plot(close, offset = -round(tf*factor*displacement), color=#459915, title="Lagging Span")
  48. plot(c1,color=lime, title="Kijun")
  49. plot(b1,color=red, title="Tenkan")
  50.  
  51.  
  52. //--------------------bollinger bands + EMA---------------------
  53.  
  54. //EMA
  55.  
  56. sma = input(title="Moving Average Length", defval=1, minval=0, maxval=200)
  57. man = input(title="EMA candle confirmation", defval=1, minval=0, maxval=15)
  58.  
  59. source = close
  60. ma = sma(source,sma)
  61.  
  62. plot(ma,color=blue)
  63.  
  64. //Bollinger bands
  65.  
  66. bandThreeshold = input(title="Big volatility threeshold", type=float, step=0.001, defval=0.035, minval=0, maxval=1)*close
  67. lenght = input(title="Bollinger Length", defval=5, minval=1)
  68.  
  69. basis = ema(source,lenght)
  70. dev = 2 * stdev(source,lenght)
  71. upper = basis + dev
  72. lower = basis - dev
  73.  
  74. plot(basis,color=blue)
  75. plot(upper,color=black)
  76. plot(lower,color=black)
  77.  
  78. //High volatility spikes
  79.  
  80. caidota=0
  81. subidota=0
  82.  
  83. if 2*dev>=bandThreeshold and ma[2]>ma
  84. caidota:=1
  85.  
  86. //plot(caidota*20+1700)
  87.  
  88. if 2*dev>=bandThreeshold and ma[2]<ma
  89. subidota:=1
  90.  
  91. //plot(subidota*20+1800)
  92.  
  93.  
  94. //--------------------Cloud characterization--------------
  95.  
  96. //Cloud thickness
  97. //if cloud thickness is very small and cloud is flat and very long price is in consolidation is better not to trade at all
  98. //Cloud thickness becomes bigger when there is volatility abd price is out of consolidation
  99. //Here price is around 0.365 and in consolidation, cloud thickness is in between 0.001 and 0.003 thats a 0.822% and 0.274% of current price using ema200
  100. //bollinger bands are also small with 200 Lenght and 1.5 multiplier whole length is between 0.005 and 0.009 thats a 1.3% and 2.4% of current price XLM1min
  101. //bollinger are small in this case BTC 200,2 with 118 precentage of price is 1.26%
  102.  
  103. cloudThick = input(title='Thin cloud Threeshold', type=float, defval=0.0085, minval=0, step=0.0001, maxval=1)*close
  104. thickMult = input(title='Thin cloud multiplier for flatness)', type=float, defval=1.5, minval=0, step=0.01, maxval=3)
  105. lenght2 = input(title="Cloud thickness EMA Length", defval=50, minval=1)
  106.  
  107. cloudThickness=sma(abs(leadLine1-leadLine2),lenght2)
  108.  
  109. //plot(cloudThickness+8700, offset = round(tf*factor*displacement))
  110. //hline(8700,color=blue)
  111. //plot(8700+cloudThick,color=blue)
  112.  
  113. //isflat(leadLine1, leadLine2) =>
  114. //can I use Tenkan and Kijun too close to decide?
  115. isflat=0
  116.  
  117. if cloudThickness[round(tf*factor*displacement)]<=cloudThick and cloudThickness<=cloudThick
  118.  
  119. if leadLine1>leadLine2
  120. top = leadLine2+thickMult*cloudThick
  121. bottom = leadLine2-thickMult*cloudThick
  122.  
  123. if top>=upperCloud and bottom<=lowerCloud or leadLine2==r1
  124. isflat:=1
  125. else
  126. isflat:=0
  127.  
  128. else
  129. bottom = leadLine2-thickMult*cloudThick
  130. top = leadLine2+thickMult*cloudThick
  131.  
  132. if top>=upperCloud and bottom<=lowerCloud or leadLine2==r1
  133. isflat:=1
  134. else
  135. isflat:=0
  136.  
  137. else
  138. //cloudThickness big enough I don't care if flat
  139. isflat:=0
  140.  
  141. //plot(isflat*10+1800, color=blue)
  142.  
  143.  
  144. //----------------------Entry Strategy-----------------------
  145.  
  146. noise = input(title='Candle confirmation Entry', type=integer, defval=1, minval=1, maxval=15)
  147. noise3 = input(title='Candle confirmation Entry thin cloud', type=integer, defval=0, minval=0, maxval=15)
  148. useLag = input(false,title="Use Lagging Span above cloud")
  149.  
  150. if useLag==true
  151. if cloudThickness[round(tf*factor*displacement)]>=cloudThick
  152. //normal conditions cloud thick enough to give real signals price not in accumulation or flat
  153. if (cross(ma[noise],upperCloud[noise]) and close>upperCloud and c1>=b1 and ma[man]<ma)
  154. or (low[noise]<=upperCloud[noise] and high[noise]>=upperCloud[noise] and close>upperCloud and c1>=b1 and ma[man]<ma)
  155. or (high[noise]<=upperCloud[noise] and low[noise-1]>=upperCloud[noise] and close>upperCloud and c1>=b1 and ma[man]<ma)
  156. //candle breaks Span accompanied by high volatility really bullish sign, early entry not waiting for confirmation
  157. or (low<=upperCloud and close>upperCloud and ma[man]<ma and subidota==1)
  158. strategy.entry("long", true, comment="Long Entry")
  159.  
  160. if (cross(ma[noise],lowerCloud[noise]) and close<lowerCloud and c1<=b1 and ma[man]>ma)
  161. or (high[noise]>=lowerCloud[noise] and low[noise]<=lowerCloud[noise] and close<lowerCloud and c1<=b1 and ma[man]>ma)
  162. or (low[noise]>=lowerCloud[noise] and high[noise-1]<=lowerCloud[noise] and close<lowerCloud and c1<=b1 and ma[man]>ma)
  163. //bar breaks completely both Spans accompanied by high volatility really bearish sign
  164. or (high>=lowerCloud and close<lowerCloud and ma[man]>ma and caidota==1)
  165. strategy.entry("short",false, comment="Short Entry")
  166.  
  167. else
  168. //small cloud more restraining coinditions to enter and exit
  169. if isflat==0
  170. //thin but not flat cloud
  171. //conditions are above cloud, above cloud noise3 candles before, TK cross, bullish cloud, bullish ema, lagging span above price and lagging span above cloud
  172. if close>upperCloud and close[noise3]>upperCloud[noise3] and c1>=b1 and leadLine1>=leadLine2 and ma[man]<ma and close>close[round(tf*factor*displacement)]
  173. and close>upperCloud[round(tf*factor*displacement)]
  174. strategy.entry("long", true, comment="Long Entry")
  175. if close<lowerCloud and close[noise3]<lowerCloud[noise3] and c1<=b1 and leadLine1<=leadLine2 and ma[man]>ma and close<close[round(tf*factor*displacement)]
  176. and close<lowerCloud[round(tf*factor*displacement)]
  177. strategy.entry("short",false, comment="Short Entry")
  178.  
  179. //with an small and flat cloud I don't want to trade so I wait till movement happens
  180.  
  181. else
  182. //Now all again without using lagging span
  183. if cloudThickness[round(tf*factor*displacement)]>=cloudThick
  184. //normal conditions cloud thick enough to give real signals price not in accumulation or flat
  185. if (cross(ma[noise],upperCloud[noise]) and close>upperCloud and c1>=b1 and ma[man]<ma)
  186. or (low[noise]<=upperCloud[noise] and high[noise]>=upperCloud[noise] and close>upperCloud and c1>=b1 and ma[man]<ma)
  187. or (high[noise]<=upperCloud[noise] and low[noise-1]>=upperCloud[noise] and close>upperCloud and c1>=b1 and ma[man]<ma)
  188. //candle breaks Span accompanied by high volatility really bullish sign, early entry not waiting for confirmation
  189. or (low<=upperCloud and close>upperCloud and ma[man]<ma and subidota==1)
  190. strategy.entry("long", true, comment="Long Entry")
  191.  
  192. if (cross(ma[noise],lowerCloud[noise]) and close<lowerCloud and c1<=b1 and ma[man]>ma)
  193. or (high[noise]>=lowerCloud[noise] and low[noise]<=lowerCloud[noise] and close<lowerCloud and c1<=b1 and ma[man]>ma)
  194. or (low[noise]>=lowerCloud[noise] and high[noise-1]<=lowerCloud[noise] and close<lowerCloud and c1<=b1 and ma[man]>ma)
  195. //bar breaks completely both Spans accompanied by high volatility really bearish sign
  196. or (high>=lowerCloud and close<lowerCloud and ma[man]>ma and caidota==1)
  197. strategy.entry("short",false, comment="Short Entry")
  198.  
  199. else
  200. //small cloud more restraining coinditions to enter and exit
  201. if isflat==0
  202. //thin but not flat cloud
  203. //conditions are above cloud, above cloud noise3 candles before, TK cross, bullish cloud, bullish ema, lagging span above price and lagging span above cloud
  204. if close>upperCloud and close[noise3]>upperCloud[noise3] and c1>=b1 and leadLine1>=leadLine2 and ma[man]<ma and close>close[round(tf*factor*displacement)]
  205. strategy.entry("long", true, comment="Long Entry")
  206. if close<lowerCloud and close[noise3]<lowerCloud[noise3] and c1<=b1 and leadLine1<=leadLine2 and ma[man]>ma and close<close[round(tf*factor*displacement)]
  207. strategy.entry("short",false, comment="Short Entry")
  208.  
  209. //with an small and flat cloud I don't want to trade so I wait till movement happens
  210.  
  211.  
  212. //---------------------------Close Strategy----------------------------
  213.  
  214. //noise2 to choose the reentry confirmation
  215. //to closelong I want to make sure price has made a minimum number of bars below cloud since a break out, also that is going down,
  216. //that the last bar is not above cloud. thats is probably gonna keep going down and last part is if it breaks the cloud completely Im out too
  217.  
  218. noise2 = input(title='Candle confirmation exit long', type=integer, defval=40, minval=1, maxval=250)
  219. noise4 = input(title='Candle confirmation exit short', type=integer, defval=20, minval=1, maxval=280)
  220. noise5 = input(title='Candle confirmation exit second span', type=integer, defval=1, minval=1, maxval=15)
  221. sl = input(title='Stop Loss:', type=integer, defval=1500, minval=1)
  222.  
  223. closelong=false
  224. countLong=0
  225.  
  226. if high[noise]<upperCloud[noise] and close<upperCloud
  227. for i=1 to noise2
  228. if high[i]<=upperCloud[i]
  229. countLong:=countLong+1
  230. else
  231. countLong:=countLong
  232. else
  233. countLong=0
  234.  
  235. //plot(countLong+9000)
  236. //plot(countLong)
  237.  
  238. if (countLong>=noise2-3 and ma[man]>ma and high<=upperCloud and c1<b1)
  239. or (countLong>=noise2-3 and ma[man]>ma and high<=upperCloud and c1==b1 and c1[1]>c1 and b1[1]>b1)
  240. or (high[noise5]<lowerCloud[noise5] and close<lowerCloud and ma[man]>ma)
  241. //If there is a big caidota when crossing first Span close early
  242. or (caidota==1 and close<upperCloud[noise] and close<upperCloud)
  243. closelong:=true
  244.  
  245. closeshort=false
  246. countShort=0
  247.  
  248. //to closeshort I want to make sure price has made a minimum of bars above cloud since a break out, also that is going up,
  249. //that the last bar is not below cloud. thats is probably gonna keep going down and last part is if it breaks the cloud completely Im out too
  250.  
  251. if low[noise]>lowerCloud[noise] and close>lowerCloud
  252. for i=1 to noise4
  253. if low[i]>=min(s1[i],r1[i])
  254. countShort:=countShort+1
  255. else
  256. countShort:=countShort
  257. else
  258. countShort=0
  259.  
  260. //plot(countShort+9000, color=red)
  261.  
  262. if (countShort>=noise4-3 and ma[man]<ma and low>=lowerCloud and c1>b1)
  263. or (countShort>=noise4-3 and ma[man]<ma and low>=lowerCloud and c1==b1 and c1[1]<c1 and b1[1]<b1)
  264. or (low[noise5]>upperCloud[noise5] and close>upperCloud and ma[man]<ma)
  265. //If there is a big subidota when crossing first Span close early
  266. or (subidota==1 and close>lowerCloud[noise] and close>lowerCloud)
  267. closeshort:=true
  268.  
  269. strategy.close(id = "long", when = closelong==true or strategy.openprofit<-sl)
  270. strategy.close(id = "short", when = closeshort==true or strategy.openprofit<-sl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement