Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.2 kB
7
Indexable
Never
//@version=5

strategy(title='Arty', overlay=true, initial_capital = 1000, currency = currency.USD, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value=0.04)


// INPUT VARIABLES
// RSI
rsiLen = input.int(14, title='RSI Length', group='RSI')

rsiLong = input.int(30, title='RSI Long Cross', group='RSI')
rsiShort = input.int(70, title='RSI Short Cross', group='RSI')

// Entry
entryMethod = input.string('Single Entry', title='Entry Method', options=['Single Entry', 'Cumulative Entries'], group='Entry Settings')
buyInput = input.float(20, 'Entry Size (%) ', group='Entry Settings')/100

// Exit Inputs
exitMethod = input.string('Percent', title='Stop Loss Method', options=['Percent', 'EMA'], group='Exit Settings')

profitPerc = input.float(title="Take Profit (%)",minval=0.0, step=0.1, defval=3, group='Exit Settings') * 0.01
lossPerc = input.float(title="Stop Loss (%)",minval=0.0, step=0.1, defval=3, group='Exit Settings') * 0.01

exitEma1Len = input.int(5, title='Exit EMA 1', group='Exit Settings')
exitEma2Len = input.int(100, title='Exit EMA 2', group='Exit Settings')

// Alert Input
longMessage= input.string("", title="Long Bot Message", group='Alerts')
shortMessage= input.string("", title="Short Bot Message", group='Alerts')

// RSI
rsi = ta.rsi(close, rsiLen)                                       

// EMA CALCUTION
ema1Exit = ta.ema(close, exitEma1Len)
ema2Exit = ta.ema(close, exitEma2Len)

// Entry
bool Buy = na
bool Sell = na

if entryMethod == 'Cumulative Entries'
    Buy := ta.crossunder(rsi, rsiLong) and strategy.position_size >= 0 and not na(ema2Exit)
    Sell := ta.crossover(rsi, rsiShort) and strategy.position_size <= 0 and not na(ema2Exit)

if entryMethod == 'Single Entry'
    Buy := ta.crossunder(rsi, rsiLong) and strategy.position_size == 0 and not na(ema2Exit)
    Sell := ta.crossover(rsi, rsiShort) and strategy.position_size == 0 and not na(ema2Exit)



// Exit logics
longExit = ta.crossunder(ema1Exit, ema2Exit)
shortExit = ta.crossover(ema1Exit, ema2Exit)

longExitPrice  = strategy.position_avg_price * (1 + profitPerc)
shortExitPrice = strategy.position_avg_price * (1 - profitPerc)

longStopPrice = strategy.position_avg_price * (1 - lossPerc)
shortStopPrice = strategy.position_avg_price * (1 + lossPerc)

// Position size 
posQty = buyInput * strategy.equity / close

// Strategy
// Entry
if Buy
    strategy.order('Long', strategy.long, qty=posQty)
    alert(longMessage, alert.freq_once_per_bar_close)

if Sell
    strategy.order('Short', strategy.short, qty=posQty)
    alert(shortMessage, alert.freq_once_per_bar_close)

// Exit
if exitMethod == 'Percent'
    if (strategy.position_size > 0)
        strategy.exit("Long Exit", limit=longExitPrice, stop=longStopPrice)

    if (strategy.position_size < 0)
        strategy.exit("Short Exit", limit=shortExitPrice, stop=shortStopPrice)

if exitMethod == 'EMA'
    if longExit
        strategy.close('Long', 'Long Exit')

    if shortExit
        strategy.close('Short', 'Short Exit')

    if (strategy.position_size > 0)
        strategy.exit("Long TP", limit=longExitPrice)

    if (strategy.position_size < 0)
        strategy.exit("Short TP", limit=shortExitPrice)



plotshape(Buy, title='Long Entry', location=location.belowbar, style=shape.labelup, color=color.new(#2C9670, 0), text='Long', textcolor=color.new(color.white, 0))
plotshape(Sell, title='Short Entry', location=location.abovebar, style=shape.labeldown, color=color.red, text='Short', textcolor=color.new(color.white, 0))

plotshape(longExit and strategy.position_size > 0, title='Long Exit', location=location.abovebar, style=shape.labeldown, color=color.new(#2C9670, 0), text='Long Exit', textcolor=color.new(color.white, 0))
plotshape(shortExit and strategy.position_size < 0, title='Short Exit', location=location.belowbar, style=shape.labelup, color=color.red, text='Short Exit', textcolor=color.new(color.white, 0))

linePlot1 = plot(ema1Exit, title='Exit EMA 1', color=color.white, linewidth=1)
linePlot2 = plot(ema2Exit, title='Exit EMA 2', color=color.blue, linewidth=1)

fill(linePlot1, linePlot2, color=color.rgb(255, 255, 255, 90))

entryLevel = plot(strategy.position_avg_price, title='Entry Price', color=color.white, style=plot.style_linebr, linewidth=1)
longExitPlot = plot((strategy.position_size > 0) ? longExitPrice : na, color=color.teal, style=plot.style_linebr, linewidth=1, title="Long Take Profit")
shortExitPlot = plot((strategy.position_size < 0) ? shortExitPrice : na,color=color.teal, style=plot.style_linebr,linewidth=1, title="Short Take Profit")
longSLPlot = plot((strategy.position_size > 0) and exitMethod == 'Percent' ? longStopPrice : na, color=color.red, style=plot.style_linebr, linewidth=1, title='Long Stop Loss')
shortSLPlot = plot((strategy.position_size < 0) and exitMethod == 'Percent' ? shortStopPrice : na, color=color.red, style=plot.style_linebr, linewidth=1, title='Short Stop Loss')

fill(entryLevel, longExitPlot, color=color.rgb(0, 137, 123, 80))
fill(entryLevel, shortExitPlot, color=color.rgb(0, 137, 123, 80))

fill(entryLevel, longSLPlot, color=color.rgb(255, 82, 82, 90))
fill(entryLevel, shortSLPlot, color=color.rgb(255, 82, 82, 90))