Untitled

 avatar
unknown
javascript
4 years ago
5.0 kB
51
Indexable
//@version=4
strategy(title = "SMA/EMA Simpleton", overlay = true, initial_capital = 100000, currency = "USD", commission_type=strategy.commission.percent, commission_value=0,  pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, calc_on_every_tick=true)

// Base
price = close

unrate_price = security("FRED:UNRATE", "M", close, lookahead = barmerge.lookahead_on)
sma_unrate = sma(unrate_price, 12)

// check if something happened
// use: check_bars(var, lookback)
check_bars(_cond, _lookback) =>
    bool _happened = false
    for i = 1 to _lookback
        if _cond[i]
            _happened := true
    _happened


// Indicators
[macd_line, macd_signal, _] = macd(close, 12, 26, 9)
ema10 = ema(price, 10)
sma10 = sma(price, 10)
ema21 = ema(price, 21)
sma50 = sma(price, 50)
ema200 = ema(price, 200)
sma200 = sma(price, 200)
rsi = rsi(price, 14)
rsi_short = ema(rsi, 50)
rsi_long = ema(rsi, 200)


// Filter MA on
// 1. UNRATE
// RSI High (if high = MA on)



// ------------------------------------------------------
//
// Primary conditions
//
// ------------------------------------------------------

txt_entry = 'Entry'
txt_exit = 'Exit'
enter_long = false
exit_long = false


var choice = input(defval="Price <> SMA10", group = "Base Strategy", options=["Price <> SMA10", "Price <> SMA200", "MACD", "RSI Trends", "Donchian Channels (5 high/5 low)", "MACD + RSI-Trend"])


// Primary Actions
if choice == "Price <> SMA10"
    enter_long := price > sma10
    exit_long := price < sma10
    

if choice == "Price <> SMA200"
    if price > sma200
        enter_long := true
        txt_entry := 'SMA200 Entry'

    if price < sma200
        exit_long := true
        

if choice == "MACD"
    if macd_line > macd_signal
        enter_long := true
        txt_entry := 'MACD Entry'

    if macd_line < macd_signal
        exit_long := true
        

if choice == "RSI Trends"
    rsi_s = ema(rsi(price, 14), 5)
    rsi_l = ema(rsi(price, 14), 10)
    

    if rsi_s > rsi_l
        enter_long := true
        txt_entry := 'RSI-T Entry'

    if rsi_s < rsi_l
        exit_long := true
        
        
if choice == "Donchian Channels (5 high/5 low)"
    dc_upper = highest(high, 5)[1]
    dc_lower = lowest(low, 5)[1]
    
    if price > dc_upper
        enter_long := true
        txt_entry := 'DC Entry'
    
    if price < dc_lower
        exit_long := true
        
        
if choice == "MACD + RSI-Trend"
    rsi_s = ema(rsi(price, 14), 5)
    rsi_l = ema(rsi(price, 14), 10)
    dc10_lower = lowest(low, 10)[1]

    if macd_line > macd_signal or rsi_s > rsi_l
        enter_long := true
        txt_entry := 'MACD+RSI Entry'

    if macd_line < macd_signal or rsi_s < rsi_l or price < dc10_lower
        enter_long := false
        exit_long := true
        


// ------------------------------------------------------
// Filters
// ------------------------------------------------------


if input(title='UNRATE: Ingore exits if UNRATE Trend is OK', type=input.bool, defval=true, group = 'Noise filters')
    // ignore exits if UNRATE (Unemployment Rate) is ok
    if unrate_price and unrate_price < sma_unrate // check if UNRATE avaiable (data starts year 1940+)
        exit_long := false

if input(title='MOM: RSI Trends must be negative', type=input.bool, defval=false, group = 'Noise filters')
    rsi_s = rsi(close, 10)
    rsi_l = rsi(close, 21)
    if rsi_s > rsi_l
        exit_long := false

if input(title='RSI: Refuse entries if RSI > 85', type=input.bool, defval=false, group = 'Noise filters')
    if rsi > 85
        enter_long := false

// Exits
if input(title='Attempt calling the tops using RSI > 85', type=input.bool, defval=false, group = 'Exits')
    if rsi > 85
        exit_long := true
        txt_exit := 'RSI > 85'

if input(title='Attempt calling the tops using RSI Trends', type=input.bool, defval=false, group = 'Exits')
    if crossunder(rsi_short, rsi_long)
        exit_long := true
        txt_exit := 'RSI Trends Exit'






// ------------------------------------------------------
//
// Execute
//
// ------------------------------------------------------

// if not unrate_price and unrate_active
//     enter_long := false

// check if there was a long position taken (not only a long signal)
was_long = false
wait_bars = 1
if strategy.position_size > 0
    was_long := true

// act
if enter_long and not check_bars(was_long, wait_bars)
    strategy.entry("Long", strategy.long, comment = txt_entry)

if exit_long
    strategy.close("Long", comment = txt_exit)

// plots
plot(sma10, title = 'SMA10', color = color.yellow, linewidth = 2)
plot(sma50, title = 'SMA50', color = color.yellow, display = display.none)
plot(sma200, title = 'SMA200', color = color.yellow, linewidth = 2, display = display.none)
Editor is loading...