ohenrik

Pivot + VMA + EMA - 1 Hour chart

Short
BITFINEX:BTCUSD   Bitcoin
Code:

//@version=2
strategy("Pivot EMA", overlay=true, initial_capital=10000, currency=currency.USD, commission_value=0.2)

//////////////////////////////////////////////////////////////////////
// Example usage:
// if testPeriod()
// strategy.entry("LE", strategy.long)

//////////////////////////////////////////////////////////////////////
// Simply copy the code below and paste it into your own strategy

//////////////////////////////////////////////////////////////////////
// Component Code Start
testStartYear = input(2018, "Backtest Start Year")
testStartMonth = input(3, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)

testStopYear = input(2018, "Backtest Stop Year")
testStopMonth = input(4, "Backtest Stop Month")
testStopDay = input(1, "Backtest Stop Day")
testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)

// A switch to control background coloring of the test period
testPeriodBackground = input(title="Color Background?", type=bool, defval=true)
testPeriodBackgroundColor = testPeriodBackground and (time >= testPeriodStart) and (time <= testPeriodStop) ? #00FF00 : na
bgcolor(testPeriodBackgroundColor, transp=97)

testPeriod() =>
time >= testPeriodStart and time <= testPeriodStop ? true : false

leftBars = input(4)
rightBars = input(2)

emaValue = input(48, "ema")

emaStopLossLong = input(0.0, "EMA SL Long")
emaStopLossShort = input(150.0, "EMA SL Short")


src=close
l =input(6, title="VMA Length")
std=input(true, title="Show Trend Direction")
bc=input(false, title="Color bars based on Trend")
k = 1.0/l
pdm = max((src - src), 0)
mdm = max((src - src), 0)
pdmS = ((1 - k)*nz(pdmS) + k*pdm)
mdmS = ((1 - k)*nz(mdmS) + k*mdm)
s = pdmS + mdmS
pdi = pdmS/s
mdi = mdmS/s
pdiS = ((1 - k)*nz(pdiS) + k*pdi)
mdiS = ((1 - k)*nz(mdiS) + k*mdi)
d = abs(pdiS - mdiS)
s1 = pdiS + mdiS
iS = ((1 - k)*nz(iS) + k*d/s1)
hhv = highest(iS, l)
llv = lowest(iS, l)
d1 = hhv - llv
vI = (iS - llv)/d1
vma = (1 - k*vI)*nz(vma) + k*vI*src
vmaC=(vma > vma) ? green : (vma<vma) ? red : (vma==vma) ? blue : black
plot(vma, color=std?vmaC:black, linewidth=3, title="VMA")
barcolor(bc?vmaC:na)



emaLine = ema(close, emaValue)
plot(emaLine, linewidth=3)


tradeLong = input(true)
tradeShort = input(true)

swh = pivothigh(leftBars, rightBars)
swl = pivotlow(leftBars, rightBars)

swh_cond = not na(swh)
hprice = swh_cond ? swh : hprice
le = swh_cond ? true : (le and high > hprice ? false : le)

swl_cond = not na(swl)
lprice = swl_cond ? swl : lprice
se = swl_cond ? true : (se and low < lprice ? false : se)


if (tradeLong and swl_cond and (swl > emaLine) and testPeriod() and (vma > vma))
strategy.entry("Long", strategy.long, comment="Manual entry long")
// stopLoss = emaLine

if (tradeShort and swh_cond and (swh < emaLine) and testPeriod() and (vma < vma))
strategy.entry("Short", strategy.short, comment="PivRevSE") //, stop=lprice - syminfo.mintick - 100


// === STRATEGY RISK MANAGEMENT EXECUTION ===
// finally, make use of all the earlier values we got prepped
SLL = emaLine - emaStopLossLong
strategy.exit("Exit Long", from_entry = "Long", stop = max(SLL, SLL))

SLS = emaLine + emaStopLossShort
strategy.exit("Exit Short", from_entry = "Short", stop = min(SLS, SLS))

inLong = strategy.position_entry_name == "Long"
slLong = emaLine - emaStopLossLong //inLong ? emaLine - emaStopLossLong : na
plot(slLong, title="SL Long", color=(inLong ? color(red, 0) : color(gray, 100)))

inShort = strategy.position_entry_name == "Short"
slShort = emaLine + emaStopLossShort // inShort ? emaLine + emaStopLossShort : na
plot(slShort, title="SL short", color=(inShort ? color(red, 0) : color(gray, 100)))

// plot(hprice, color=green, linewidth=3)
// plot(lprice, color=red, linewidth=3)

// ph = pivothigh(leftBars, rightBars)
// plot(ph, style=cross, linewidth=3, color= green, offset=-

Related Ideas

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.