Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=3
- //study(title = "Alerts", shorttitle = "AL", overlay = true)
- strategy(title = "MACD Strategy V2.0 BCH/BTC 2H", shorttitle = "Strategy BCH/BTC", overlay=false,currency=currency.USD, initial_capital=100, default_qty_type=strategy.percent_of_equity,default_qty_value=100, commission_type=strategy.commission.percent, commission_value = 0.2)
- fastLength = input(21, minval=1, title="First Tuning")
- slowLength=input(27,minval=1, title="Second Tuning")
- // === Main Script Here ===
- source = close
- useCurrentRes = input(true, title="Use Current Chart Resolution?")
- resCustom = input(title="Use Different Timeframe? Uncheck Box Above", type=resolution, defval="60")
- smd = input(true, title="Show MacD & Signal Line? Also Turn Off Dots Below")
- sd = input(true, title="Show Dots When MacD Crosses Signal Line?")
- sh = input(true, title="Show Histogram?")
- macd_colorChange = input(true,title="Change MacD Line Color-Signal Line Cross?")
- hist_colorChange = input(true,title="MacD Histogram 4 Colors?")
- res = useCurrentRes ? period : resCustom
- signalLength = input(9,minval=1)
- fastMA = ema(source, fastLength)
- slowMA = ema(source, slowLength)
- macd = fastMA - slowMA
- signal = sma(macd, signalLength)
- hist = macd - signal
- outMacD = security(tickerid, res, macd)
- outSignal = security(tickerid, res, signal)
- outHist = security(tickerid, res, hist)
- histA_IsUp = outHist > outHist[1] and outHist > 0
- histA_IsDown = outHist < outHist[1] and outHist > 0
- histB_IsDown = outHist < outHist[1] and outHist <= 0
- histB_IsUp = outHist > outHist[1] and outHist <= 0
- //MacD Color Definitions
- macd_IsAbove = outMacD >= outSignal
- macd_IsBelow = outMacD < outSignal
- plot_color = hist_colorChange ? histA_IsUp ? aqua : histA_IsDown ? blue : histB_IsDown ? red : histB_IsUp ? maroon :yellow :gray
- macd_color = macd_colorChange ? macd_IsAbove ? lime : red : red
- signal_color = macd_colorChange ? macd_IsAbove ? blue : blue : lime
- circleYPosition = outSignal
- long = crossover(outMacD, outSignal)
- short = crossunder(outMacD, outSignal)
- plot(smd and outMacD ? outMacD : na, title="MACD", color=macd_color, linewidth=4)
- plot(smd and outSignal ? outSignal : na, title="Signal Line", color=signal_color, style=line ,linewidth=2)
- plot(sh and outHist ? outHist : na, title="Histogram", color=plot_color, style=histogram, linewidth=4)
- plot(sd and cross(outMacD, outSignal) ? circleYPosition : na, title="Cross", style=circles, linewidth=4, color=macd_color)
- hline(0, '0 Line', linestyle=solid, linewidth=2, color=white)
- ////////////////////////////////////////////////////////////////////////////
- // //
- // INSERT SCRIPT HERE //
- // //
- ////////////////////////////////////////////////////////////////////////////
- // === /END
- ////////////////////////===ANION=CODE====///////////////////////////////////
- // //
- // ULTIMATE PINE INJECTOR V1.2 + rs fix //
- // //
- ////////////////////////////////////////////////////////////////////////////
- // === Conditions ===
- ////////////////////////////////////////////////////////////////////////////
- long_entry = long //Long Or Buy Condition Here
- short_entry = short //Short Or Sell Condition Here
- ////////////////////////////////////////////////////////////////////////////
- //Replace Only If Valid Exit Conditions
- long_exit = short_entry //Close Long Condition Here (Optional)
- short_exit = long_entry //Close Short Condition Here (Optional)
- // === /END
- ///////////////////////////////////////////////////////////////////////////
- // init these values here, they will get updated later as more decisions are made
- last_long_close = na
- last_short_close = na
- // === Long position detection ===
- // longs open
- longo = 0
- longo := nz(longo[1])
- // longs closed
- longc = 0
- longc := nz(longc[1])
- if long_entry
- longo := longo + 1
- longc := 0
- if long_exit
- longc := longc + 1
- longo := 0
- // === /END
- // === Short position detection ===
- shorto = 0
- shorto := nz(shorto[1])
- shortc = 0
- shortc := nz(shortc[1])
- if short_entry
- shorto := shorto + 1
- shortc := 0
- if short_exit
- shortc := shortc + 1
- shorto := 0
- // === /END
- // === Pyramiding Settings ===
- //pyr = input(1, title="Pyramiding Setting")
- pyr = 1
- longCondition = long_entry and longo <= pyr
- longX = long_exit and longc <= pyr
- shortCondition = short_entry and shorto <=pyr
- shortX = short_exit and shortc <=pyr
- // === /END
- // === Get Last Position Price ===
- last_open_longCondition = na
- last_open_shortCondition = na
- // last open prices
- last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
- last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
- // === /END
- // === Check For Long/Short ===
- last_longCondition = na
- last_shortCondition = na
- // last open times
- last_longCondition := longCondition ? time : nz(last_longCondition[1])
- last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
- last_longClose = longX ? time : nz(last_long_close[1])
- last_shortClose = shortX ? time : nz(last_short_close[1])
- in_longCondition = last_longCondition > last_shortCondition and last_longCondition >= last_longClose
- in_shortCondition = last_shortCondition > last_longCondition and last_shortCondition >= last_shortClose
- // === /END
- // === Stop Loss (Long) ===
- isSLl = input(false, "Stop Loss (Long)")
- sll = input(0, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
- long_call_sl = last_open_longCondition * (1 - sll)
- long_sl = isSLl and low <= long_call_sl and longCondition == 0
- // === /END
- // === Stop Loss (Short) ===
- isSLs = input(false, "Stop Loss (Short)")
- sls = input(0, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
- short_call_sl = last_open_shortCondition * (1 + sls)
- short_sl = isSLs and high >= short_call_sl and shortCondition == 0
- // === /END
- // === Trailing Stop ===
- last_high = na
- last_low = na
- last_high := in_longCondition ? (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1]) : na
- last_low := in_shortCondition ? (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1]) : na
- isTSl = input(false, "Trailing Stop Long")
- tsil = input(19, "Activate Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
- tsl = input(2, "Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
- long_call_ts = last_high * (1 - tsl)
- long_call_tsi = last_open_longCondition * (1 + tsil)
- long_ts = isTSl and not na(last_high) and low <= long_call_ts and longCondition == 0 and last_high >= long_call_tsi
- isTSs = input(false, "Trailing Stop Short")
- tsis = input(19, "Activate Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
- tss = input(2, "Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
- short_call_ts = last_low * (1 + tss)
- short_call_tsi = last_open_shortCondition * (1 - tsis)
- short_ts = isTSs and not na(last_low) and high >= short_call_ts and shortCondition == 0 and last_low <= short_call_tsi
- // === /END
- // === Create Single Close For All Closing Conditions ===
- closelong = long_sl or long_ts or longX
- closeshort = short_sl or short_ts or shortX
- // Get Last Close
- last_long_close := closelong ? time : nz(last_long_close[1])
- last_short_close := closeshort ? time : nz(last_short_close[1])
- // Check For Close Since Last Open
- if closelong and last_long_close[1] > last_longCondition
- closelong := 0
- if closeshort and last_short_close[1] > last_shortCondition
- closeshort := 0
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // === Alarm Settings ===
- alertcondition(longCondition==1, title='LONG', message='LONG')
- alertcondition(closelong==1, title='EXIT LONG', message='EXIT LONG')
- alertcondition(shortCondition==1, title='SHORT', message='SHORT')
- alertcondition(closeshort==1, title='EXIT SHORT', message='EXIT SHORT')
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // === Debugs Here ===
- //Remove "//" To Check/Debug The Code Above
- // Signal Shapes
- //plotshape(longCondition[1]==1, title='LONG', style=shape.triangleup, size=size.large, color=#02CB80, location= location.belowbar)
- //plotshape(shortCondition[1]==1, title='SHORT', style=shape.triangledown, size=size.large, color=#DC143C, location=location.abovebar)
- //plotshape(shortCondition[1]==0 and closelong[1]==1, title='EXIT LONG', style=shape.xcross, color=#02CB80, location=location.belowbar, transp=0)
- //plotshape(longCondition[1]==0 and closeshort[1]==1, title='EXIT SHORT', style=shape.xcross, color=#DC143C, location=location.abovebar, transp=0)
- // SL Plot
- //slColor = (isSLl or isSLs) and (in_longCondition or in_shortCondition) ? red : white
- //plot(isSLl and in_longCondition ? long_call_sl : na, "Long SL", slColor, style=3, linewidth=2)
- //plot(isSLs and in_shortCondition ? short_call_sl : na, "Short SL", slColor, style=3, linewidth=2)
- // TP Plot
- //tpColor = isTP and (in_longCondition or in_shortCondition) ? purple : white
- //plot(isTP and in_longCondition ? long_call_tp : na, "Long TP", tpColor, style=3, linewidth=2)
- //plot(isTP and in_shortCondition ? short_call_tp : na, "Short TP", tpColor, style=3, linewidth=2)
- // TS Plot
- tsColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? orange : white
- tsiColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? white : orange
- plot(isTSl and in_longCondition ? long_call_tsi : na, "Long Trailing", tsiColor, style=3, linewidth=2)
- plot(isTSs and in_shortCondition ? short_call_tsi : na, "Short Trailing", tsiColor, style=3, linewidth=2)
- plot(isTSl and in_longCondition and last_high > long_call_tsi ? long_call_ts : na, "Long Trailing", tsColor, style=2, linewidth=2)
- plot(isTSs and in_shortCondition and last_low < short_call_tsi ? short_call_ts : na, "Short Trailing", tsColor, style=2, linewidth=2)
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // //
- // REMOVE THE CODE BELOW FOR STUDY CONVERSION //
- // //
- ////////////////////////////////////////////////////////////////////////////
- // === Strategy Direction Switch ===
- direction = input(title = "Strategy Direction", defval="Both", options=["Both", "Long", "Short"])
- // === /END
- // === Backtesting Dates ===
- testPeriodSwitch = input(true, "Custom Backtesting Dates")
- testStartYear = input(2017, "Backtest Start Year")
- testStartMonth = input(8, "Backtest Start Month")
- testStartDay = input(1, "Backtest Start Day")
- testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
- testStopYear = input(2018, "Backtest Stop Year")
- testStopMonth = input(12, "Backtest Stop Month")
- testStopDay = input(31, "Backtest Stop Day")
- testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
- testPeriod() =>
- time >= testPeriodStart and time <= testPeriodStop ? true : false
- isPeriod = testPeriodSwitch == true ? testPeriod() : true
- // === /END
- // === Strategy ===
- if isPeriod and direction=="Both"
- if (longCondition)
- strategy.entry("Long",strategy.long)
- if (closelong) and not shortCondition
- strategy.close("Long")
- if (shortCondition)
- strategy.entry("Short",strategy.short)
- if (closeshort) and not longCondition
- strategy.close("Short")
- if isPeriod and direction=="Long"
- if (longCondition)
- strategy.entry("Long",strategy.long)
- if (closelong)
- strategy.close("Long")
- if isPeriod and direction=="Short"
- if (shortCondition)
- strategy.entry("Short",strategy.short)
- if (closeshort)
- strategy.close("Short")
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // //
- // ULTIMATE PINE INJECTOR V1.2 //
- // //
- //////////////////////===ANION=CODE=END====/////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement