0

I have combine supertrend and ema and created a table that gives me buy and sell signal with default values set according to my day trading style. However I want to use that indicator for swing trading and for that need to change the input value of supertrend and ema. Like for day trading input for ema is 21 and supertrend is 5, 1.2 but for swing trading I want ema as 50 and supertrend to be 10, 3. Is there any way to add conditional input, and select from drop down menu and if condition 1 is selected the input will be different and condition 2 is selected the input will be different.



//@version=5
indicator("MultiTime Buy/sell", overlay=true)



emaPeriod = input(8, "EMA Period")
emaPeriod2 = input(21, "EMA Period")
atrPeriod = input(5, "ATR Period")
supertrendFactor = input(1.2, "Supertrend Factor")

tableAlignment = input.string(position.top_right, "Table Alignment", options = [position.bottom_center, position.bottom_left, position.bottom_right, position.middle_center, position.middle_left, position.middle_right, position.top_center, position.top_left, position.top_right])

getValueForTime(timeStr, expr) =>
    request.security(syminfo.tickerid, timeStr, expr) 

getValueForTime1(timeStr1, expr1) =>
    request.security(ticker.heikinashi(syminfo.tickerid), timeStr1, expr1) 

getColor(value) =>
    color.from_gradient(value, 0, 100, color.rgb(0, 255, 0), color.rgb(255, 0, 0))

getColor1(value1) =>
    color.from_gradient(value1, 0, 100, color.rgb(0, 255, 0), color.rgb(255, 0, 0))

var indicatorTable = table.new(position = tableAlignment, columns = 7, rows = 4, bgcolor = color.rgb(0,0,0, 50), border_width = 1)


table.cell(table_id = indicatorTable, column = 0, row = 1,  text = "ALGO", text_color = color.white)
table.cell(table_id = indicatorTable, column = 0, row = 2,  text = "EMA (" + str.tostring(emaPeriod) + ")", text_color = color.white)
table.cell(table_id = indicatorTable, column = 0, row = 3,  text = "EMA (" + str.tostring(emaPeriod2) + ")", text_color = color.white)

table.cell(table_id = indicatorTable, column = 1, row = 0,  text = "1m", text_color = color.white)
table.cell(table_id = indicatorTable, column = 2, row = 0,  text = "2m", text_color = color.white)
table.cell(table_id = indicatorTable, column = 3, row = 0,  text = "5m", text_color = color.white)
table.cell(table_id = indicatorTable, column = 4, row = 0,  text = "10m", text_color = color.white)
table.cell(table_id = indicatorTable, column = 5, row = 0,  text = "15m", text_color = color.white)
table.cell(table_id = indicatorTable, column = 6, row = 0,  text = "30m", text_color = color.white)

makeTableCell(column, row, bgColor, cellText) =>
    table.cell(table_id = indicatorTable, column = column, row = row,  text = cellText, bgcolor = bgColor, text_color = color.white)

makeRow(row, bgcolors, cellTexts) =>
    for int i = 0 to 5
        makeTableCell(1 + i, row, array.get(bgcolors, i), array.get(cellTexts, i))

fillArrayWithTime(timeStr, expr, mode, cellTexts, bgColors) =>
    value = getValueForTime(timeStr, expr)
    
    if (mode == "float")
        array.push(bgColors, getColor(value))
        array.push(cellTexts, str.tostring(math.round(value, 2)))
    else if (mode == "signal")
        array.push(bgColors, getColor(value == 1 ? 100 : value == -1 ? 0 : 50))
        array.push(cellTexts, value == -1 ? "" : value == 1 ? "" : "")
    [cellTexts, bgColors]

makeMultitimeIndicatorArray(expr, mode) =>
    cellTexts = array.new_string(0, "")
    bgColors = array.new_color(0, color.black)
    
    fillArrayWithTime("1", expr, mode, cellTexts, bgColors)
    fillArrayWithTime("2", expr, mode, cellTexts, bgColors)
    fillArrayWithTime("5", expr, mode, cellTexts, bgColors)
    fillArrayWithTime("10", expr, mode, cellTexts, bgColors)
    fillArrayWithTime("15", expr, mode, cellTexts, bgColors)
    fillArrayWithTime("30", expr, mode, cellTexts, bgColors)
    
    
    [cellTexts, bgColors]
    
fillArrayWithTime1(timeStr1, expr1, mode1, cellTexts, bgColors) =>
    value1 = getValueForTime1(timeStr1, expr1)
    
    if (mode1 == "float")
        array.push(bgColors, getColor1(value1))
        array.push(cellTexts, str.tostring(math.round(value1, 2)))
    else if (mode1 == "signal")
        array.push(bgColors, getColor1(value1 == 1 ? 100 : value1 == -1 ? 0 : 50))
        array.push(cellTexts, value1 == -1 ? "" : value1 == 1 ? "" : "")
    [cellTexts, bgColors]

makeMultitimeIndicatorArray1(expr1, mode1) =>
    cellTexts = array.new_string(0, "")
    bgColors = array.new_color(0, color.black)
    
    fillArrayWithTime1("1", expr1, mode1, cellTexts, bgColors)
    fillArrayWithTime1("2", expr1, mode1, cellTexts, bgColors)
    fillArrayWithTime1("5", expr1, mode1, cellTexts, bgColors)
    fillArrayWithTime1("10", expr1, mode1, cellTexts, bgColors)
    fillArrayWithTime1("15", expr1, mode1, cellTexts, bgColors)
    fillArrayWithTime1("30", expr1, mode1, cellTexts, bgColors)
 
    

supertrendDirection() =>
    [supertrend, direction] = ta.supertrend(supertrendFactor, atrPeriod)
    direction

[supertrendTexts, supertrendBgColors] = makeMultitimeIndicatorArray1(supertrendDirection(), "signal")
[maTrendTexts, maTrendBgColors] = makeMultitimeIndicatorArray(ta.ema(close, 8) > close ? 1 : -1, "signal")
[maTrendTexts1, maTrendBgColors1] = makeMultitimeIndicatorArray(ta.ema(close, 21) > close ? 1 : -1, "signal")


makeRow(1, supertrendBgColors, supertrendTexts)
makeRow(2, maTrendBgColors, maTrendTexts)
makeRow(3, maTrendBgColors1, maTrendTexts1)




















if someone can help me to figure out how to add conditional input in the script. I am not an expert coder so please an easy solution.

0 Answers0