Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- strategy(title='Timeframe Ichimoku Strategy v11', shorttitle='Ichimoku Strategy v11', initial_capital=10000, pyramiding= 0, default_qty_value=100,
- default_qty_type= strategy.percent_of_equity, commission_type=strategy.commission.percent, commission_value=0.1, overlay=true)
- tf = input(title='Timeframe:', type=integer, defval=1, minval=1)
- factor = input(title='Factor Multiplier Timeframe:', type=float, defval=1.0, minval=0.1, maxval=2, step=0.1)
- conversionPeriods = input(title='Conversion Periods', type=integer, defval=10, minval=1),
- basePeriods = input(title='Base Periods', type=integer, defval=30, minval=1)
- laggingSpan2Periods = input(title='Lagging Span', type=integer, defval=60, minval=1),
- displacement = input(title='Displacement', type=integer, defval=30, minval=1)
- //--------------------------Ichimoku multi time-frame-------------------------
- //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.
- //Options are:
- //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
- //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
- //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
- //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
- //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
- //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
- //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
- //Multiplying is quite forward if you multiply by 8 in the 1min timeframe you get the 8 min ichimoku
- f_donchian(_len) => avg(lowest(_len), highest(_len))
- f_ichimoku_cloud(_conversion_periods, _base_periods, _lagging_span)=>
- _conversion_line = f_donchian(_conversion_periods)
- _base_line = f_donchian(_base_periods)
- _lead_line1 = avg(_conversion_line, _base_line)
- _lead_line2 = f_donchian(_lagging_span)
- [_conversion_line, _base_line, _lead_line1, _lead_line2]
- //timeframe transformation
- [conversionLine, baseLine, leadLine1, leadLine2] = f_ichimoku_cloud(round(tf*factor*conversionPeriods), round(tf*factor*basePeriods), round(tf*factor*laggingSpan2Periods))
- s1 = leadLine1[round(tf*factor*displacement)]
- r1 = leadLine2[round(tf*factor*displacement)]
- c1 = conversionLine
- b1 = baseLine
- upperCloud = max(s1,r1)
- lowerCloud = min(r1,s1)
- p1 = plot(leadLine1, offset = round(tf*factor*displacement), color=green, title="Span A")
- p2 = plot(leadLine2, offset = round(tf*factor*displacement), color=red, title="Span B")
- fill(p1, p2, color = leadLine1 > leadLine2 ? green : red, title="Cloud")
- plot(close, offset = -round(tf*factor*displacement), color=#459915, title="Lagging Span")
- plot(c1,color=lime, title="Kijun")
- plot(b1,color=red, title="Tenkan")
- //--------------------bollinger bands + EMA---------------------
- //EMA
- sma = input(title="Moving Average Length", defval=1, minval=0, maxval=200)
- man = input(title="EMA candle confirmation", defval=1, minval=0, maxval=15)
- source = close
- ma = sma(source,sma)
- plot(ma,color=blue)
- //Bollinger bands
- bandThreeshold = input(title="Big volatility threeshold", type=float, step=0.001, defval=0.035, minval=0, maxval=1)*close
- lenght = input(title="Bollinger Length", defval=5, minval=1)
- basis = ema(source,lenght)
- dev = 2 * stdev(source,lenght)
- upper = basis + dev
- lower = basis - dev
- plot(basis,color=blue)
- plot(upper,color=black)
- plot(lower,color=black)
- //High volatility spikes
- caidota=0
- subidota=0
- if 2*dev>=bandThreeshold and ma[2]>ma
- caidota:=1
- //plot(caidota*20+1700)
- if 2*dev>=bandThreeshold and ma[2]<ma
- subidota:=1
- //plot(subidota*20+1800)
- //--------------------Cloud characterization--------------
- //Cloud thickness
- //if cloud thickness is very small and cloud is flat and very long price is in consolidation is better not to trade at all
- //Cloud thickness becomes bigger when there is volatility abd price is out of consolidation
- //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
- //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
- //bollinger are small in this case BTC 200,2 with 118 precentage of price is 1.26%
- cloudThick = input(title='Thin cloud Threeshold', type=float, defval=0.0085, minval=0, step=0.0001, maxval=1)*close
- thickMult = input(title='Thin cloud multiplier for flatness)', type=float, defval=1.5, minval=0, step=0.01, maxval=3)
- lenght2 = input(title="Cloud thickness EMA Length", defval=50, minval=1)
- cloudThickness=sma(abs(leadLine1-leadLine2),lenght2)
- //plot(cloudThickness+8700, offset = round(tf*factor*displacement))
- //hline(8700,color=blue)
- //plot(8700+cloudThick,color=blue)
- //isflat(leadLine1, leadLine2) =>
- //can I use Tenkan and Kijun too close to decide?
- isflat=0
- if cloudThickness[round(tf*factor*displacement)]<=cloudThick and cloudThickness<=cloudThick
- if leadLine1>leadLine2
- top = leadLine2+thickMult*cloudThick
- bottom = leadLine2-thickMult*cloudThick
- if top>=upperCloud and bottom<=lowerCloud or leadLine2==r1
- isflat:=1
- else
- isflat:=0
- else
- bottom = leadLine2-thickMult*cloudThick
- top = leadLine2+thickMult*cloudThick
- if top>=upperCloud and bottom<=lowerCloud or leadLine2==r1
- isflat:=1
- else
- isflat:=0
- else
- //cloudThickness big enough I don't care if flat
- isflat:=0
- //plot(isflat*10+1800, color=blue)
- //----------------------Entry Strategy-----------------------
- noise = input(title='Candle confirmation Entry', type=integer, defval=1, minval=1, maxval=15)
- noise3 = input(title='Candle confirmation Entry thin cloud', type=integer, defval=0, minval=0, maxval=15)
- useLag = input(false,title="Use Lagging Span above cloud")
- if useLag==true
- if cloudThickness[round(tf*factor*displacement)]>=cloudThick
- //normal conditions cloud thick enough to give real signals price not in accumulation or flat
- if (cross(ma[noise],upperCloud[noise]) and close>upperCloud and c1>=b1 and ma[man]<ma)
- or (low[noise]<=upperCloud[noise] and high[noise]>=upperCloud[noise] and close>upperCloud and c1>=b1 and ma[man]<ma)
- or (high[noise]<=upperCloud[noise] and low[noise-1]>=upperCloud[noise] and close>upperCloud and c1>=b1 and ma[man]<ma)
- //candle breaks Span accompanied by high volatility really bullish sign, early entry not waiting for confirmation
- or (low<=upperCloud and close>upperCloud and ma[man]<ma and subidota==1)
- strategy.entry("long", true, comment="Long Entry")
- if (cross(ma[noise],lowerCloud[noise]) and close<lowerCloud and c1<=b1 and ma[man]>ma)
- or (high[noise]>=lowerCloud[noise] and low[noise]<=lowerCloud[noise] and close<lowerCloud and c1<=b1 and ma[man]>ma)
- or (low[noise]>=lowerCloud[noise] and high[noise-1]<=lowerCloud[noise] and close<lowerCloud and c1<=b1 and ma[man]>ma)
- //bar breaks completely both Spans accompanied by high volatility really bearish sign
- or (high>=lowerCloud and close<lowerCloud and ma[man]>ma and caidota==1)
- strategy.entry("short",false, comment="Short Entry")
- else
- //small cloud more restraining coinditions to enter and exit
- if isflat==0
- //thin but not flat cloud
- //conditions are above cloud, above cloud noise3 candles before, TK cross, bullish cloud, bullish ema, lagging span above price and lagging span above cloud
- 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)]
- and close>upperCloud[round(tf*factor*displacement)]
- strategy.entry("long", true, comment="Long Entry")
- 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)]
- and close<lowerCloud[round(tf*factor*displacement)]
- strategy.entry("short",false, comment="Short Entry")
- //with an small and flat cloud I don't want to trade so I wait till movement happens
- else
- //Now all again without using lagging span
- if cloudThickness[round(tf*factor*displacement)]>=cloudThick
- //normal conditions cloud thick enough to give real signals price not in accumulation or flat
- if (cross(ma[noise],upperCloud[noise]) and close>upperCloud and c1>=b1 and ma[man]<ma)
- or (low[noise]<=upperCloud[noise] and high[noise]>=upperCloud[noise] and close>upperCloud and c1>=b1 and ma[man]<ma)
- or (high[noise]<=upperCloud[noise] and low[noise-1]>=upperCloud[noise] and close>upperCloud and c1>=b1 and ma[man]<ma)
- //candle breaks Span accompanied by high volatility really bullish sign, early entry not waiting for confirmation
- or (low<=upperCloud and close>upperCloud and ma[man]<ma and subidota==1)
- strategy.entry("long", true, comment="Long Entry")
- if (cross(ma[noise],lowerCloud[noise]) and close<lowerCloud and c1<=b1 and ma[man]>ma)
- or (high[noise]>=lowerCloud[noise] and low[noise]<=lowerCloud[noise] and close<lowerCloud and c1<=b1 and ma[man]>ma)
- or (low[noise]>=lowerCloud[noise] and high[noise-1]<=lowerCloud[noise] and close<lowerCloud and c1<=b1 and ma[man]>ma)
- //bar breaks completely both Spans accompanied by high volatility really bearish sign
- or (high>=lowerCloud and close<lowerCloud and ma[man]>ma and caidota==1)
- strategy.entry("short",false, comment="Short Entry")
- else
- //small cloud more restraining coinditions to enter and exit
- if isflat==0
- //thin but not flat cloud
- //conditions are above cloud, above cloud noise3 candles before, TK cross, bullish cloud, bullish ema, lagging span above price and lagging span above cloud
- 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)]
- strategy.entry("long", true, comment="Long Entry")
- 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)]
- strategy.entry("short",false, comment="Short Entry")
- //with an small and flat cloud I don't want to trade so I wait till movement happens
- //---------------------------Close Strategy----------------------------
- //noise2 to choose the reentry confirmation
- //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,
- //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
- noise2 = input(title='Candle confirmation exit long', type=integer, defval=40, minval=1, maxval=250)
- noise4 = input(title='Candle confirmation exit short', type=integer, defval=20, minval=1, maxval=280)
- noise5 = input(title='Candle confirmation exit second span', type=integer, defval=1, minval=1, maxval=15)
- sl = input(title='Stop Loss:', type=integer, defval=1500, minval=1)
- closelong=false
- countLong=0
- if high[noise]<upperCloud[noise] and close<upperCloud
- for i=1 to noise2
- if high[i]<=upperCloud[i]
- countLong:=countLong+1
- else
- countLong:=countLong
- else
- countLong=0
- //plot(countLong+9000)
- //plot(countLong)
- if (countLong>=noise2-3 and ma[man]>ma and high<=upperCloud and c1<b1)
- or (countLong>=noise2-3 and ma[man]>ma and high<=upperCloud and c1==b1 and c1[1]>c1 and b1[1]>b1)
- or (high[noise5]<lowerCloud[noise5] and close<lowerCloud and ma[man]>ma)
- //If there is a big caidota when crossing first Span close early
- or (caidota==1 and close<upperCloud[noise] and close<upperCloud)
- closelong:=true
- closeshort=false
- countShort=0
- //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,
- //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
- if low[noise]>lowerCloud[noise] and close>lowerCloud
- for i=1 to noise4
- if low[i]>=min(s1[i],r1[i])
- countShort:=countShort+1
- else
- countShort:=countShort
- else
- countShort=0
- //plot(countShort+9000, color=red)
- if (countShort>=noise4-3 and ma[man]<ma and low>=lowerCloud and c1>b1)
- or (countShort>=noise4-3 and ma[man]<ma and low>=lowerCloud and c1==b1 and c1[1]<c1 and b1[1]<b1)
- or (low[noise5]>upperCloud[noise5] and close>upperCloud and ma[man]<ma)
- //If there is a big subidota when crossing first Span close early
- or (subidota==1 and close>lowerCloud[noise] and close>lowerCloud)
- closeshort:=true
- strategy.close(id = "long", when = closelong==true or strategy.openprofit<-sl)
- strategy.close(id = "short", when = closeshort==true or strategy.openprofit<-sl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement