Grid Short

 avatar
unknown
javascript
a year ago
12 kB
86
Indexable
// © omererkan
//@version=5
strategy("Grid Strategy with MA Short", overlay=true, initial_capital = 100, default_qty_type = strategy.cash, default_qty_value = 10, pyramiding = 10, commission_type = strategy.commission.percent, commission_value = 0.04)


//Inputs//
length = input.int(defval = 100, title = 'MA Length', group = 'MA')
MA_Type = input.string("SMA", title="MA Type", options=['EMA', 'HMA', 'LSMA', 'RMA', 'SMA', 'WMA'],group = 'MA')

logic = input.string(defval='ATR', title ='Grid Logic', options = ['ATR', 'Percent'])

band_mult = input.float(2.5, step = 0.1, title = 'Band Multiplier/Percent', group = 'Parameter')
atr_len = input.int(defval=100, title = 'ATR Length', group ='parameter')


/////////////////////
//Region : Function//
/////////////////////
getMA(source ,ma_type, length) =>
    maPrice = ta.ema(source, length)
    ema = ta.ema(source, length)
    sma = ta.sma(source, length)
    if ma_type == 'SMA'
        maPrice := ta.sma(source, length)
        maPrice
    if ma_type == 'HMA'
        maPrice := ta.hma(source, length)
        maPrice
    if ma_type == 'WMA'
        maPrice := ta.wma(source, length)
        maPrice
    if ma_type == "RMA"
        maPrice := ta.rma(source, length)
    if ma_type == "LSMA"
        maPrice := ta.linreg(source, length, 0)
    maPrice

main_plot = getMA(ohlc4, MA_Type, length)


atr = ta.atr(length)

premium_zone_1 = logic == 'ATR' ? ta.ema(main_plot + atr*(band_mult*1), 5) : ta.ema((main_plot*(1+band_mult*0.01*1)), 5)
premium_zone_2 = logic == 'ATR' ? ta.ema(main_plot + atr*(band_mult*2), 5) : ta.ema((main_plot*(1+band_mult*0.01*2)), 5)
premium_zone_3 = logic == 'ATR' ? ta.ema(main_plot + atr*(band_mult*3), 5) : ta.ema((main_plot*(1+band_mult*0.01*3)), 5)
premium_zone_4 = logic == 'ATR' ? ta.ema(main_plot + atr*(band_mult*4), 5) : ta.ema((main_plot*(1+band_mult*0.01*4)), 5)
premium_zone_5 = logic == 'ATR' ? ta.ema(main_plot + atr*(band_mult*5), 5) : ta.ema((main_plot*(1+band_mult*0.01*5)), 5)
premium_zone_6 = logic == 'ATR' ? ta.ema(main_plot + atr*(band_mult*6), 5) : ta.ema((main_plot*(1+band_mult*0.01*6)), 5)
premium_zone_7 = logic == 'ATR' ? ta.ema(main_plot + atr*(band_mult*7), 5) : ta.ema((main_plot*(1+band_mult*0.01*7)), 5)
premium_zone_8 = logic == 'ATR' ? ta.ema(main_plot + atr*(band_mult*8), 5) : ta.ema((main_plot*(1+band_mult*0.01*8)), 5)
//premium_zone_9 = ta.rma(main_plot + atr*(band_mult*9), 5)
//premium_zone_10 = ta.rma(main_plot + atr*(band_mult*10), 5)


discount_zone_1 = logic == 'ATR' ? ta.ema(main_plot - atr*(band_mult*1), 5) : ta.ema((main_plot*(1-band_mult*0.01*1)), 5)
discount_zone_2 = logic == 'ATR' ? ta.ema(main_plot - atr*(band_mult*2), 5) : ta.ema((main_plot*(1-band_mult*0.01*2)), 5)
discount_zone_3 = logic == 'ATR' ? ta.ema(main_plot - atr*(band_mult*3), 5) : ta.ema((main_plot*(1-band_mult*0.01*3)), 5)
discount_zone_4 = logic == 'ATR' ? ta.ema(main_plot - atr*(band_mult*4), 5) : ta.ema((main_plot*(1-band_mult*0.01*4)), 5)
discount_zone_5 = logic == 'ATR' ? ta.ema(main_plot - atr*(band_mult*5), 5) : ta.ema((main_plot*(1-band_mult*0.01*5)), 5)
discount_zone_6 = logic == 'ATR' ? ta.ema(main_plot - atr*(band_mult*6), 5) : ta.ema((main_plot*(1-band_mult*0.01*6)), 5)
discount_zone_7 = logic == 'ATR' ? ta.ema(main_plot - atr*(band_mult*7), 5) : ta.ema((main_plot*(1-band_mult*0.01*7)), 5)
discount_zone_8 = logic == 'ATR' ? ta.ema(main_plot - atr*(band_mult*8), 5) : ta.ema((main_plot*(1-band_mult*0.01*8)), 5)
//discount_zon_9 = ta.sma(main_plot - atr*(band_mult*9), 5)
//discount_zone_10 =ta.sma( main_plot - atr*(band_mult*10), 5)

//Region End//

////////////////////
// Region : Plots//
///////////////////

dis_low1 = plot(discount_zone_1, color=color.new(color.red, 80))
dis_low2 = plot(discount_zone_2, color=color.new(color.red, 70))
dis_low3 = plot(discount_zone_3, color=color.new(color.red, 60))
dis_low4 = plot(discount_zone_4, color=color.new(color.red, 50))
dis_low5 = plot(discount_zone_5, color=color.new(color.red, 40))
dis_low6 = plot(discount_zone_6, color=color.new(color.red, 30))
dis_low7 = plot(discount_zone_7, color=color.new(color.red, 20))
dis_low8 = plot(discount_zone_8, color=color.new(color.red, 10))
//dis_low9 = plot(discount_zone_9, color=color.new(color.red, 0))
//dis_low10 = plot(discount_zone_10, color=color.new(color.red, 0))

plot(main_plot, color =color.new(color.gray, 10))

pre_up1 = plot(premium_zone_1, color=color.new(color.green, 80))
pre_up2 = plot(premium_zone_2, color=color.new(color.green, 70))
pre_up3 = plot(premium_zone_3, color=color.new(color.green, 60))
pre_up4 = plot(premium_zone_4, color=color.new(color.green, 50))
pre_up5 = plot(premium_zone_5, color=color.new(color.green, 40))
pre_up6 = plot(premium_zone_6, color=color.new(color.green, 30))
pre_up7 = plot(premium_zone_7, color=color.new(color.green, 20))
pre_up8 = plot(premium_zone_8, color=color.new(color.green, 10))
//pre_up9 = plot(premium_zone_9, color=color.new(color.green, 0))
//pre_up10 = plot(premium_zone_10, color=color.new(color.green, 0))

fill(dis_low1, dis_low2, color=color.new(color.red, 95))
fill(dis_low2, dis_low3, color=color.new(color.red, 90))
fill(dis_low3, dis_low4, color=color.new(color.red, 85))
fill(dis_low4, dis_low5, color=color.new(color.red, 80))
fill(dis_low5, dis_low6, color=color.new(color.red, 75))
fill(dis_low6, dis_low7, color=color.new(color.red, 70))
fill(dis_low7, dis_low8, color=color.new(color.red, 65))
//fill(dis_low8, dis_low9, color=color.new(color.red, 60))
//fill(dis_low9, dis_low10, color=color.new(color.red, 55))

fill(pre_up1, pre_up2, color=color.new(color.green, 95))
fill(pre_up2, pre_up3, color=color.new(color.green, 90))
fill(pre_up3, pre_up4, color=color.new(color.green, 85))
fill(pre_up4, pre_up5, color=color.new(color.green, 80))
fill(pre_up5, pre_up6, color=color.new(color.green, 75))
fill(pre_up6, pre_up7, color=color.new(color.green, 70))
fill(pre_up7, pre_up8, color=color.new(color.green, 65))
//fill(pre_up8, pre_up9, color=color.new(color.green, 60))
//fill(pre_up9, pre_up10, color=color.new(color.green, 55))



//Var//

var int order_cond = 0
var bool order_1 = false
var bool order_2 = false
var bool order_3 = false
var bool order_4 = false
var bool order_5 = false
var bool order_6 = false
var bool order_7 = false
var bool order_8 = false
var bool order_9 = false
var bool order_10 = false
var bool order_11 = false
var bool order_12 = false
var bool order_13 = false
var bool order_14 = false
var bool order_15 = false


///////////////////////
//Region : Strategies//
///////////////////////

//Shorts//

ShortCondition1 = ta.crossover(close, discount_zone_7)
ShortCondition2 = ta.crossover(close, discount_zone_6)
ShortCondition3 = ta.crossover(close, discount_zone_5)
ShortCondition4 = ta.crossover(close, discount_zone_4)
ShortCondition5 = ta.crossover(close, discount_zone_3)
ShortCondition6 = ta.crossover(close, discount_zone_2)
ShortCondition7 = ta.crossover(close, discount_zone_1)
ShortCondition8 = ta.crossover(close, main_plot)
ShortCondition9 = ta.crossover(close, premium_zone_1)
ShortCondition10 = ta.crossover(close, premium_zone_2)
ShortCondition11 = ta.crossover(close, premium_zone_3)
ShortCondition12 = ta.crossover(close, premium_zone_4)
ShortCondition13 = ta.crossover(close, premium_zone_5)
ShortCondition14 = ta.crossover(close, premium_zone_6)
ShortCondition15 = ta.crossover(close, premium_zone_7)
ShortCondition16 = ta.crossover(close, premium_zone_8)

//Close//
longCondition1 = ta.crossunder(close, discount_zone_8)
longCondition2 = ta.crossunder(close, discount_zone_7)
longCondition3 = ta.crossunder(close, discount_zone_6)
longCondition4 = ta.crossunder(close, discount_zone_5)
longCondition5 = ta.crossunder(close, discount_zone_4)
longCondition6 = ta.crossunder(close, discount_zone_3)
longCondition7 = ta.crossunder(close, discount_zone_2)
longCondition8 = ta.crossunder(close, discount_zone_1)
longCondition9 = ta.crossunder(close, main_plot)
longCondition10 = ta.crossunder(close, premium_zone_1)
longCondition11 = ta.crossunder(close, premium_zone_2)
longCondition12 = ta.crossunder(close, premium_zone_3)
longCondition13 = ta.crossunder(close, premium_zone_4)
longCondition14 = ta.crossunder(close, premium_zone_5)
longCondition15 = ta.crossunder(close, premium_zone_6)
longCondition16 = ta.crossunder(close, premium_zone_7)


if (ShortCondition1) and order_1 == false
    strategy.entry("Short1", strategy.short)
    order_1 := true
if (ShortCondition2) and order_2 == false
    strategy.entry("Short2", strategy.short)
    order_2 := true
if (ShortCondition3) and order_3 == false
    strategy.entry("Short3", strategy.short)
    order_3 := true
if (ShortCondition4) and order_4 == false
    strategy.entry("Short4", strategy.short)
    order_4 := true
if (ShortCondition5) and order_5 == false
    strategy.entry("Short5", strategy.short)
    order_5 := true
if (ShortCondition6) and order_6 == false
    strategy.entry("Short6", strategy.short)
    order_6 := true
if (ShortCondition7) and order_7 == false
    strategy.entry("Short7", strategy.short)
    order_7 := true
if (ShortCondition8) and order_8 == false
    strategy.entry("Short8", strategy.short)
    order_8 := true
if (ShortCondition9) and order_9 == false
    strategy.entry("Short9", strategy.short)
    order_9 := true
if (ShortCondition10) and order_10 == false
    strategy.entry("Short10", strategy.short)
    order_10 := true
if (ShortCondition11) and order_11 == false
    strategy.entry("Short11", strategy.short)
    order_11 := true
if (ShortCondition12) and order_12 == false
    strategy.entry("Short12", strategy.short)
    order_12 := true
if (ShortCondition13) and order_13 == false
    strategy.entry("Short13", strategy.short)
    order_13 := true
if (ShortCondition14) and order_14 == false
    strategy.entry("Short14", strategy.short)
    order_14 := true
if (ShortCondition15) and order_15 == false
    strategy.entry("Short15", strategy.short)
    order_15 := true


if (longCondition1) and order_1 == true
    strategy.close("Short1")
    order_1 := false
if (longCondition2) and order_2 == true
    strategy.close("Short2")
    order_2 := false
if (longCondition3) and order_3 == true
    strategy.close("Short3")
    order_3 := false
if (longCondition4) and order_4 == true
    strategy.close("Short4")
    order_4 := false
if (longCondition5) and order_5 == true
    strategy.close("Short5")
    order_5 := false
if (longCondition6) and order_6 == true
    strategy.close("Short6")
    order_6 := false
if (longCondition7) and order_7 == true
    strategy.close("Short7")
    order_7 := false
if (longCondition8) and order_8 == true
    strategy.close("Short8")
    order_8 := false
if (longCondition9) and order_9 == true
    strategy.close("Short9")
    order_9 := false
if (longCondition10) and order_10 == true
    strategy.close("Short10")
    order_10 := false
if (longCondition11) and order_11 == true
    strategy.close("Short11")
    order_11 := false
if (longCondition12) and order_12 == true
    strategy.close("Short12")
    order_12 := false
if (longCondition13) and order_13 == true
    strategy.close("Short13")
    order_13 := false
if (longCondition14) and order_14 == true
    strategy.close("Short14")
    order_14 := false
if (longCondition15) and order_15 == true
    strategy.close("Short15")
    order_15 := false
Editor is loading...
Leave a Comment