You are on page 1of 3

//@version=2

//Strategy combining Stochastic Crosses in the Overbought/Oversold Area with a


trend determined by two EMAs
//Default setup seems to work best on 4HR timeframe for BTC

strategy(title = "Strategy Stoch/EMA Cross", shorttitle = "Strategy Stoch/EMA


Cross", overlay = true, pyramiding = 0, default_qty_type =
strategy.percent_of_equity, default_qty_value = 100, currency = currency.USD,
commission_value=0.01,commission_type=strategy.commission.percent,
initial_capital=1000)

// === GENERAL INPUTS ===


SectionInd = input(defval = true ,title = "------------ INDICATORS
------------")
maFastLength = input(defval = 55, title = "Fast MA Period", minval = 1)
maSlowLength = input(defval = 89, title = "Slow MA Period", minval = 1)
StochLength = input(defval = 14, title = "Stochastic Length", minval=1)
smoothK = input(defval = 6, title = "%K Smooth", minval=1)
smoothD = input(defval = 3, title = "%D Smooth", minval=1)
overbought = 80
oversold = 20
HighlightOBOS = input(defval = true, title = "Highlight Stoch Cross?")
HighlightTrend = input(defval = true ,title = "Highlight Trend?")

//DATE AND TIME


SectionFrom = input(defval = true ,title = "--------------- FROM
---------------")
fromDay = input(defval = 01, title = "From day", minval=1)
fromMonth = input(defval = 1, title = "From month", minval=1)
fromYear = input(defval = 2019, title = "From year", minval=2014)
SectionTo = input(defval = true, title = "---------------- TO
----------------")
toDay = input(defval = 31, title = "To day", minval=1)
toMonth = input(defval = 12, title = "To month", minval=1)
toYear = input(defval = 2020, title = "To year", minval=2014)

// === STRATEGY RELATED INPUTS ===


SectionStra = input(defval = true ,title = "------------- STRATEGY
-------------")

// Include Shorts or only trade Long Positions?


includeShorts = input(defval = true, title = "Include Short Positions?")

// === RISK MANAGEMENT INPUTS ===

inpTakeProfit = input(defval = 8, title = "Take Profit % (0 to deactivate)",


minval = 0)
inpStopLoss = input(defval = 0, title = "Stop Loss % (0 to deactivate)",
minval = 0)

StopLossPerc = inpStopLoss * 0.01


TakeProfitPerc = inpTakeProfit * 0.01

StopLossV = close * StopLossPerc / syminfo.mintick


TakeProfitV = close * TakeProfitPerc / syminfo.mintick

// === RISK MANAGEMENT VALUE PREP ===


// if an input is less than 1, assuming not wanted so we assign 'na' value to
disable it.
TakeProfitVal = inpTakeProfit >= 1 ? TakeProfitV : na
StopLossVal = inpStopLoss >= 1 ? StopLossV : na

// === EMA SERIES SETUP ===


maFast = ema(close, maFastLength)
maSlow = ema(close, maSlowLength)
diff = maFast - maSlow

// === STOCHASTIC SETUP ===


k = sma(stoch(close, high, low, StochLength), smoothK)
d = sma(k, smoothD)

// Stochastic Long/Short Entry determination


stochLong = crossover(k,d) and (k < oversold)
stochShort = crossunder(k,d) and (k > overbought)

// Stochastic Long/Short Exit determination


stochLongEx = (crossover (d, overbought)) or (crossunder(maFast, maSlow))
stochShortEx = (crossunder(d, oversold)) or (crossover (maFast, maSlow))

// === PLOTTING EMAs ===


fast = plot(maFast, title = "Fast MA", color = yellow, linewidth = 1, style = line,
transp = 10)
slow = plot(maSlow, title = "Slow MA", color = white, linewidth = 1, style = line,
transp = 10)

// === Vertical Coloring for Crosses in Overbought/Oversold zone and for MA Trend
Zones ===
b_color = stochLong ? green : stochShort ? red : na
bgcolor(HighlightOBOS ? b_color : na, title="Overbought / Oversold",
transp=65) //Highlight the Overbought/Oversold Stoch Crossings
t_color = diff>=0 ? green : diff<0 ? red : na
bgcolor(HighlightTrend ? t_color : na, title="Trend up / Trend down",
transp=90) //Highlight the EMA Trend

// Show EMA Trend as symbols at the bottom


bullTrend = diff>=0
bearTrend = diff<0
plotchar(bullTrend, title = "BullTrend", char="_", location = location.bottom,
color=#0CAB07, transp = 0, size=size.tiny)
plotchar(bearTrend, title = "BearTrend", char="_", location = location.bottom,
color=#DE071C, transp = 0, size=size.tiny)

// Show EMA Convergence/Divergence Trend as symbols at the top


//bullTrendCD = (diff>=0 and diff>=diff[1]) or (diff<0 and diff>diff[1])
//bearTrendCD = (diff<0 and diff<=diff[1]) or (diff>=0 and diff<diff[1])
//plotchar(bullTrendCD, title = "BullTrend CD", char="o", location = location.top,
color=#0CAB07, transp = 0, size=size.tiny)
//plotchar(bearTrendCD, title = "BearTrend CD", char="x", location = location.top,
color=#DE071C, transp = 0, size=size.tiny)

// === STRATEGY LOGIC ===


// Time Restriction
timeInRange = (time > timestamp(fromYear, fromMonth, fromDay, 00, 00)) and (time <
timestamp(toYear, toMonth, toDay, 23, 59))
// === STRATEGY - LONG POSITION EXECUTION ===
if stochLong and (diff >=0) and timeInRange //Open Long when Stoch crossing in
Oversold area and EMA Trend is up
strategy.entry(id = "Long", long = true)
if stochLongEx and timeInRange //Close Long when Stoch is getting
Overbought or Trend changes to Bearish
strategy.close(id = "Long")

// === STRATEGY - SHORT POSITION EXECUTION ===


if stochShort and (diff <0) and timeInRange and includeShorts //Open Short when
Stoch crossing in Overbought area and EMA Trend is down
strategy.entry(id = "Short", long = false)
if stochShortEx and timeInRange //Close Short when
Stoch is getting Oversold or Trend changes to Bullish
strategy.close(id = "Short")

// === STRATEGY RISK MANAGEMENT EXECUTION ===


strategy.exit("Exit Long", from_entry = "Long", profit = TakeProfitVal, loss =
StopLossVal)
strategy.exit("Exit Short", from_entry = "Short", profit = TakeProfitVal, loss =
StopLossVal)

You might also like