Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. //@version=2
  2. //
  3. // @author MRodr
  4. //
  5. // If you use this code in its original/modified form, do drop me a note. last stable version was 5
  6. //
  7. strategy(title = "Fisher VFI Strategy ", shorttitle="FVFI-strat")
  8.  
  9. length = input(130, title="VFI length")
  10. coef = input(0.2)
  11. vcoef = input(2.5, title="Max. vol. cutoff")
  12. signalLength=input(5)
  13. smoothVFI=input(false, type=bool) //delete
  14. showVFI=input(false, type=bool)
  15. showInvFisher=input(false, type=bool) //delete
  16. ma(x,y) => smoothVFI ? sma(x,y) : x
  17. showHisto=input(true,type=bool)
  18.  
  19. typical=hlc3
  20. inter = log( typical ) - log( typical[1] )
  21. vinter = stdev(inter, 30 )
  22. cutoff = coef * vinter * close
  23. vave = sma( volume, length )[1]
  24. vmax = vave * vcoef
  25. vc = iff(volume < vmax, volume, vmax) //min( volume, vmax )
  26. mf = typical - typical[1]
  27. vcp = iff( mf > cutoff, vc, iff ( mf < -cutoff, -vc, 0 ) )
  28.  
  29. vfi = ma(sum( vcp , length )/vave, 3)
  30. vfima=ema( vfi, signalLength )
  31. d=vfi-vfima
  32.  
  33. //plot(showHisto ? d/2 : na, style=histogram, color=gray, linewidth=4, transp=10)
  34. plot( showVFI ? vfima : na , title="EMA of vfi", color=orange)
  35. plot( showVFI ? vfi : na , title="vfi", color=green,linewidth=2)
  36.  
  37.  
  38.  
  39. /// applying Fisher Transforms
  40.  
  41.  
  42. /// User input length
  43. use_fisherlen=input(true, type=bool)
  44.  
  45. fisherlen = input(130, minval=1, title="Fisher Length")
  46.  
  47. MaxH = highest(vfi, use_fisherlen?fisherlen:length)
  48. MinL = lowest(vfi, use_fisherlen?fisherlen:length)
  49.  
  50. nValue1 = max(-0.9999, min(0.9999, 0.5 * 2 * ((vfi - MinL) / (MaxH - MinL) - 0.5) + 0.5 * nz(nValue1[1])))
  51. nValue2 = iff(nValue1 > .99, .999,iff(nValue1 < -.99, -.999, nValue1))
  52.  
  53. Fish = 0.25 * log((1 + nValue2) / (1 - nValue2))+ 0.5 * nz(Fish[1]) //main calc
  54.  
  55. /// Plot
  56. //plot(nValue2, color=green)
  57. //plot(showInvFisher?na:Fish, color=white,linewidth=3)
  58.  
  59.  
  60. v1=nValue2*5
  61. v2=wma(v1, 9)
  62. invFish=(exp(2*v2)-1)/(exp(2*v2)+1)
  63.  
  64.  
  65. hist_vfi_color=vfi>0? ( vfi>vfi[1] ? lime : green) : ( vfi>vfi[1] ? purple : red)
  66.  
  67. plot(showInvFisher?invFish:Fish, color=hist_vfi_color, linewidth=5)
  68.  
  69.  
  70. slow = input(26, "smoothing period")
  71.  
  72. maSlow = ema( Fish, slow )
  73. plot( showInvFisher?na:maSlow, style=line, color=aqua, linewidth=2) //aqua line as baseline
  74. plot(showInvFisher?na:(cross(maSlow,Fish) ? maSlow : na), style = circles, linewidth = 2 , color = aqua) //cross marks
  75.  
  76.  
  77.  
  78. h_color=d>=0? (d>d[1]?green:orange) : (d<d[1]?red:orange)
  79.  
  80.  
  81. dMaxH = highest(d, use_fisherlen?fisherlen:length)
  82. dMinL = lowest(d, use_fisherlen?fisherlen:length)
  83.  
  84. dValue1 = max(-0.9999, min(0.9999, 0.5 * 2 * ((d - MinL) / (MaxH - MinL) - 0.5) + 0.5 * nz(nValue1[1])))
  85. dValue2 = iff(nValue1 > .99, .999,iff(nValue1 < -.99, -.999, nValue1))
  86.  
  87. dv1=dValue2*5
  88. dv2=wma(dv1, 9)
  89. inv_d=(exp(2*dv2)-1)/(exp(2*dv2)+1)
  90.  
  91. deltadjust=input(1)
  92. showhist=input(false, type=bool)
  93.  
  94. plot(showhist ? ((showInvFisher?inv_d:d) / deltadjust ): na, style=histogram, color=h_color, linewidth=4)
  95.  
  96. quarter_up = vfi/MaxH
  97. quarter_down = vfi / MinL
  98.  
  99. c_green=color(green,0)
  100. c_blue = color(#8080C0,0)
  101. c_purple=color(#500050,0)
  102. c_orange = color(#FF9F20,0)
  103. c_soft_red=color(#CC8888,0)
  104. c_red = color(red,0)
  105. c_gray = color(#666666,0)
  106.  
  107. range_color = vfi>0? ( quarter_up>0.95? color(lime,0) : quarter_up>0.75 ? c_green : quarter_up>0.50 ? c_blue: quarter_up>0.25 ? c_orange : c_gray ): ( quarter_down>0.95? color(black,0) :quarter_down>0.75 ? c_red : quarter_down>0.50 ? c_purple : quarter_down>0.25 ? c_soft_red : c_gray)
  108. plotshape(true, style = shape.square, location = location.bottom, color = range_color, transp = 90, editable=true, size =1)
  109.  
  110.  
  111.  
  112.  
  113.  
  114. //slow2 = input(12, "Short period")
  115. //fast2 = input(26, "Long period")
  116. //signal2 = input(9, "Smoothing period")
  117.  
  118.  
  119. //maFast2 = ema( Fish * volume, fast2 ) / ema( Fish, fast2 )
  120. //maSlow2 = ema( Fish * close, slow2 ) / ema( Fish, slow2 )
  121. //maFast2 = ema( Fish, fast2 )
  122. //maSlow2 = ema( Fish, slow2 )
  123. //d2 = maSlow2 - maFast2
  124. //maSignal2 = ema( d2, signal2 )
  125. //dm2=d2>0? min(d2-maSignal2,100):max(d2-maSignal2,-100)
  126. //dm2=d2>0? d2-maSignal2:d2-maSignal2
  127.  
  128. //h_color2=dm2>=0? (dm2>dm2[1]?green:orange) : (dm2<dm2[1]?red:orange)
  129.  
  130. //plot( dm2>0.1?1:dm2<-0.1?-1:dm2*10, style=histogram, color=h_color2, linewidth=4,transp=50)
  131. //plot( dm2*10, style=histogram, color=h_color2, linewidth=4,transp=50)
  132. //plot( dm2*10, style=histogram, color=gray, linewidth=4,transp=50)
  133.  
  134. xtreme=input(false,type=bool)
  135. a1=hline(3, title='very overbought', color=red, linestyle=dotted, linewidth=1)
  136. a2=hline(2, title='Pi', color=purple, linestyle=dotted, linewidth=1)
  137. z1=hline(1, title='Pi', color=white, linestyle=dotted, linewidth=1)
  138. z2=hline(0, title='Pi', color=white, linestyle=dotted, linewidth=0)
  139. z3=hline(-1, title='Pi', color=white, linestyle=dotted, linewidth=1)
  140. b1=hline(-2, title='Pi', color=green, linestyle=dotted, linewidth=1)
  141. b2=hline(-3, title='Pi', color=lime, linestyle=dotted, linewidth=1)
  142. p3=fill(a1,a2, color=red,transp=90)
  143. p4=fill(b1,b2, color=green,transp=90)
  144. //fill(hline(4, title='Pi', color=lime, linestyle=dotted, linewidth=1),a1, color=red,transp=70)
  145. //p6=fill(hline(-4, title='Pi', color=lime, linestyle=dotted, linewidth=1),b2, color=green,transp=70):
  146. filterSwingVFI=input(true,type=bool)
  147. tradeMomentum=input(false,type=bool)
  148. mmOverbought=input(defval=2,minval=1,maxval=3,title="momentum Overbought threshold")
  149. mmOversold=input(defval=-2,minval=-3,maxval=-1,title="momentum Overbought threshold")
  150. tradeSwing=input(true,type=bool)
  151. swingOverbought=input(defval=3.4,minval=2.5,maxval=4,title="Swing Overbought threshold")
  152. swingOversold=input(defval=-3.4,minval=-4,maxval=-2.5,title="Swing Overbought threshold")
  153. tradeScalp=input(false,type=bool)
  154. scalpOverbought=input(defval=2.5,minval=2,maxval=3.5,title="Swing Overbought threshold")
  155. scalpOversold=input(defval=-2.5,minval=-3.5,maxval=-2,title="Swing Overbought threshold")
  156.  
  157. usevfiup=filterSwingVFI?vfi<-5:true
  158. usevfidown=filterSwingVFI?vfi>5:true
  159.  
  160. if (tradeSwing and Fish[1]<swingOversold and Fish>-3.1 and usevfiup and Fish[2]>Fish[1] and Fish[1]<Fish)
  161. strategy.entry("Swing+", strategy.long,qty=2)
  162. strategy.entry("Swing+.", strategy.long,qty=2)
  163. strategy.entry("Swing+..", strategy.long,qty=1)
  164.  
  165.  
  166. if (Fish>-0.1)
  167. strategy.close("Swing+")
  168. if (Fish>0.9)
  169. strategy.close("Swing+.")
  170. if (Fish>1.9)
  171. strategy.close("Swing+..")
  172.  
  173.  
  174. if (tradeSwing and Fish[1]>swingOverbought and Fish<3.1 and usevfidown and Fish[2]<Fish[1] and Fish[1]>Fish)
  175. strategy.entry("Swing-",strategy.short, qty=2)
  176. strategy.entry("Swing-.",strategy.short, qty=2)
  177. strategy.entry("Swing-..",strategy.short, qty=1)
  178.  
  179.  
  180. if (Fish<0.1)
  181. strategy.close("Swing-")
  182. if (Fish<-0.9)
  183. strategy.close("Swing-.")
  184. if (Fish<-1.9)
  185. strategy.close("Swing-..")
  186.  
  187.  
  188.  
  189.  
  190. // Enters scalp trades
  191. if (tradeScalp and (Fish[1]<scalpOversold or Fish[2]<scalpOversold) and Fish>-2 and usevfiup)// and Fish[2]>Fish[1] and Fish[1]<Fish)
  192. strategy.entry("Scalp+", strategy.long)
  193. strategy.entry("Scalp+.", strategy.long)
  194.  
  195. // Exit trade
  196. if (Fish>-0.5)
  197. strategy.close("Scalp+")
  198. if (Fish>0.9)
  199. strategy.close("Scalp+.")
  200.  
  201.  
  202. if (tradeScalp and (Fish[1]>scalpOverbought or Fish[2]>scalpOverbought) and Fish<2 and usevfidown)// and Fish[2]<Fish[1] and Fish[1]>Fish)
  203. strategy.entry("Scalp-", strategy.short)
  204. strategy.entry("Scalp-.", strategy.short)
  205.  
  206. // Exit trade on red or green dot
  207. if (Fish<0.5)
  208. strategy.close("Scalp-")
  209.  
  210. if (Fish<-0.9)
  211. strategy.close("Scalp-.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement