Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=2
- //
- // @author MRodr
- //
- // If you use this code in its original/modified form, do drop me a note. last stable version was 5
- //
- strategy(title = "Fisher VFI Strategy ", shorttitle="FVFI-strat")
- length = input(130, title="VFI length")
- coef = input(0.2)
- vcoef = input(2.5, title="Max. vol. cutoff")
- signalLength=input(5)
- smoothVFI=input(false, type=bool) //delete
- showVFI=input(false, type=bool)
- showInvFisher=input(false, type=bool) //delete
- ma(x,y) => smoothVFI ? sma(x,y) : x
- showHisto=input(true,type=bool)
- typical=hlc3
- inter = log( typical ) - log( typical[1] )
- vinter = stdev(inter, 30 )
- cutoff = coef * vinter * close
- vave = sma( volume, length )[1]
- vmax = vave * vcoef
- vc = iff(volume < vmax, volume, vmax) //min( volume, vmax )
- mf = typical - typical[1]
- vcp = iff( mf > cutoff, vc, iff ( mf < -cutoff, -vc, 0 ) )
- vfi = ma(sum( vcp , length )/vave, 3)
- vfima=ema( vfi, signalLength )
- d=vfi-vfima
- //plot(showHisto ? d/2 : na, style=histogram, color=gray, linewidth=4, transp=10)
- plot( showVFI ? vfima : na , title="EMA of vfi", color=orange)
- plot( showVFI ? vfi : na , title="vfi", color=green,linewidth=2)
- /// applying Fisher Transforms
- /// User input length
- use_fisherlen=input(true, type=bool)
- fisherlen = input(130, minval=1, title="Fisher Length")
- MaxH = highest(vfi, use_fisherlen?fisherlen:length)
- MinL = lowest(vfi, use_fisherlen?fisherlen:length)
- nValue1 = max(-0.9999, min(0.9999, 0.5 * 2 * ((vfi - MinL) / (MaxH - MinL) - 0.5) + 0.5 * nz(nValue1[1])))
- nValue2 = iff(nValue1 > .99, .999,iff(nValue1 < -.99, -.999, nValue1))
- Fish = 0.25 * log((1 + nValue2) / (1 - nValue2))+ 0.5 * nz(Fish[1]) //main calc
- /// Plot
- //plot(nValue2, color=green)
- //plot(showInvFisher?na:Fish, color=white,linewidth=3)
- v1=nValue2*5
- v2=wma(v1, 9)
- invFish=(exp(2*v2)-1)/(exp(2*v2)+1)
- hist_vfi_color=vfi>0? ( vfi>vfi[1] ? lime : green) : ( vfi>vfi[1] ? purple : red)
- plot(showInvFisher?invFish:Fish, color=hist_vfi_color, linewidth=5)
- slow = input(26, "smoothing period")
- maSlow = ema( Fish, slow )
- plot( showInvFisher?na:maSlow, style=line, color=aqua, linewidth=2) //aqua line as baseline
- plot(showInvFisher?na:(cross(maSlow,Fish) ? maSlow : na), style = circles, linewidth = 2 , color = aqua) //cross marks
- h_color=d>=0? (d>d[1]?green:orange) : (d<d[1]?red:orange)
- dMaxH = highest(d, use_fisherlen?fisherlen:length)
- dMinL = lowest(d, use_fisherlen?fisherlen:length)
- dValue1 = max(-0.9999, min(0.9999, 0.5 * 2 * ((d - MinL) / (MaxH - MinL) - 0.5) + 0.5 * nz(nValue1[1])))
- dValue2 = iff(nValue1 > .99, .999,iff(nValue1 < -.99, -.999, nValue1))
- dv1=dValue2*5
- dv2=wma(dv1, 9)
- inv_d=(exp(2*dv2)-1)/(exp(2*dv2)+1)
- deltadjust=input(1)
- showhist=input(false, type=bool)
- plot(showhist ? ((showInvFisher?inv_d:d) / deltadjust ): na, style=histogram, color=h_color, linewidth=4)
- quarter_up = vfi/MaxH
- quarter_down = vfi / MinL
- c_green=color(green,0)
- c_blue = color(#8080C0,0)
- c_purple=color(#500050,0)
- c_orange = color(#FF9F20,0)
- c_soft_red=color(#CC8888,0)
- c_red = color(red,0)
- c_gray = color(#666666,0)
- 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)
- plotshape(true, style = shape.square, location = location.bottom, color = range_color, transp = 90, editable=true, size =1)
- //slow2 = input(12, "Short period")
- //fast2 = input(26, "Long period")
- //signal2 = input(9, "Smoothing period")
- //maFast2 = ema( Fish * volume, fast2 ) / ema( Fish, fast2 )
- //maSlow2 = ema( Fish * close, slow2 ) / ema( Fish, slow2 )
- //maFast2 = ema( Fish, fast2 )
- //maSlow2 = ema( Fish, slow2 )
- //d2 = maSlow2 - maFast2
- //maSignal2 = ema( d2, signal2 )
- //dm2=d2>0? min(d2-maSignal2,100):max(d2-maSignal2,-100)
- //dm2=d2>0? d2-maSignal2:d2-maSignal2
- //h_color2=dm2>=0? (dm2>dm2[1]?green:orange) : (dm2<dm2[1]?red:orange)
- //plot( dm2>0.1?1:dm2<-0.1?-1:dm2*10, style=histogram, color=h_color2, linewidth=4,transp=50)
- //plot( dm2*10, style=histogram, color=h_color2, linewidth=4,transp=50)
- //plot( dm2*10, style=histogram, color=gray, linewidth=4,transp=50)
- xtreme=input(false,type=bool)
- a1=hline(3, title='very overbought', color=red, linestyle=dotted, linewidth=1)
- a2=hline(2, title='Pi', color=purple, linestyle=dotted, linewidth=1)
- z1=hline(1, title='Pi', color=white, linestyle=dotted, linewidth=1)
- z2=hline(0, title='Pi', color=white, linestyle=dotted, linewidth=0)
- z3=hline(-1, title='Pi', color=white, linestyle=dotted, linewidth=1)
- b1=hline(-2, title='Pi', color=green, linestyle=dotted, linewidth=1)
- b2=hline(-3, title='Pi', color=lime, linestyle=dotted, linewidth=1)
- p3=fill(a1,a2, color=red,transp=90)
- p4=fill(b1,b2, color=green,transp=90)
- //fill(hline(4, title='Pi', color=lime, linestyle=dotted, linewidth=1),a1, color=red,transp=70)
- //p6=fill(hline(-4, title='Pi', color=lime, linestyle=dotted, linewidth=1),b2, color=green,transp=70):
- filterSwingVFI=input(true,type=bool)
- tradeMomentum=input(false,type=bool)
- mmOverbought=input(defval=2,minval=1,maxval=3,title="momentum Overbought threshold")
- mmOversold=input(defval=-2,minval=-3,maxval=-1,title="momentum Overbought threshold")
- tradeSwing=input(true,type=bool)
- swingOverbought=input(defval=3.4,minval=2.5,maxval=4,title="Swing Overbought threshold")
- swingOversold=input(defval=-3.4,minval=-4,maxval=-2.5,title="Swing Overbought threshold")
- tradeScalp=input(false,type=bool)
- scalpOverbought=input(defval=2.5,minval=2,maxval=3.5,title="Swing Overbought threshold")
- scalpOversold=input(defval=-2.5,minval=-3.5,maxval=-2,title="Swing Overbought threshold")
- usevfiup=filterSwingVFI?vfi<-5:true
- usevfidown=filterSwingVFI?vfi>5:true
- if (tradeSwing and Fish[1]<swingOversold and Fish>-3.1 and usevfiup and Fish[2]>Fish[1] and Fish[1]<Fish)
- strategy.entry("Swing+", strategy.long,qty=2)
- strategy.entry("Swing+.", strategy.long,qty=2)
- strategy.entry("Swing+..", strategy.long,qty=1)
- if (Fish>-0.1)
- strategy.close("Swing+")
- if (Fish>0.9)
- strategy.close("Swing+.")
- if (Fish>1.9)
- strategy.close("Swing+..")
- if (tradeSwing and Fish[1]>swingOverbought and Fish<3.1 and usevfidown and Fish[2]<Fish[1] and Fish[1]>Fish)
- strategy.entry("Swing-",strategy.short, qty=2)
- strategy.entry("Swing-.",strategy.short, qty=2)
- strategy.entry("Swing-..",strategy.short, qty=1)
- if (Fish<0.1)
- strategy.close("Swing-")
- if (Fish<-0.9)
- strategy.close("Swing-.")
- if (Fish<-1.9)
- strategy.close("Swing-..")
- // Enters scalp trades
- if (tradeScalp and (Fish[1]<scalpOversold or Fish[2]<scalpOversold) and Fish>-2 and usevfiup)// and Fish[2]>Fish[1] and Fish[1]<Fish)
- strategy.entry("Scalp+", strategy.long)
- strategy.entry("Scalp+.", strategy.long)
- // Exit trade
- if (Fish>-0.5)
- strategy.close("Scalp+")
- if (Fish>0.9)
- strategy.close("Scalp+.")
- if (tradeScalp and (Fish[1]>scalpOverbought or Fish[2]>scalpOverbought) and Fish<2 and usevfidown)// and Fish[2]<Fish[1] and Fish[1]>Fish)
- strategy.entry("Scalp-", strategy.short)
- strategy.entry("Scalp-.", strategy.short)
- // Exit trade on red or green dot
- if (Fish<0.5)
- strategy.close("Scalp-")
- if (Fish<-0.9)
- strategy.close("Scalp-.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement