Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
36 kB
3
Indexable
Never
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Β© ScryptoMB

//@version=5
// Visualization
colorScheme = input.string('CryptoGraph', title="Color Scheme    ", options=['CryptoGraph', 'Traditional'], group='🟣 Visualization', inline='colorScheme', tooltip='The indicative liquidation numbers and levels are for reference only. The realized numbers may be slightly different due to trading fees and funding fees')
liqLineCol = input.color(#fbc02d, title="Liquidation Line ", group='🟣 Visualization', inline='colorScheme')

showTable = input.bool(true, title="Backtest Results Table", group='🟣 Visualization', inline="Table1")
tableSize = input.string('normal', title="Sizeβ€€β€€β€€β€€β€€β€€β€Šβ€Šβ€Šβ€€β€€β€€β€€β€€β€€β€Šβ€Šβ€Šβ€Š", options=['normal'], group='🟣 Visualization', inline="Table2")
tablePosition = input.string('Top Right', title='Positionβ€€β€€β€€β€€β€€β€€β€Šβ€Šβ€Šβ€Š', options=['Top Right'], group='🟣 Visualization', inline="Table2")


// Strategy Signal Inputs
strategy = input.string('Long & Short', title="Strategy Direction", options=['Long & Short', 'Long', 'Short'], group='🟣 Strategy', inline='1',
 tooltip='Long bots profit when asset prices rise, Short bots profit when asset prices fall'
 + '\n\n' + 'Please note: to run a Short bot on a spot exchange account, you need to own the asset you want to trade. The bot will sell the asset at the current chart price and buy it back at a lower price - the profit made is actually trapped equity released from an asset you own that is declining in value.')
Profit_currency = input.string('USDT', 'Profit currency ', options=['USDT', 'BTC', '$'], group='🟣 Strategy', inline='1')

// Strategy settings
leverageType = input.string('Isolated', title='Leverage Type   β€Šβ€Šβ€Šβ€Šβ€Š', options=['Isolated', 'Cross'], group='🟣 Strategy', inline="Leverage & Liq Line")
leverage = input.float(10, title="Leverage Value", step=1, group="🟣 Strategy", inline="Leverage & Liq Line")

// Entry Conditions
useRSI = input.bool(false, title="RSI", group="🟣 Entry Conditions", inline="Entry C")
useEMA = input.bool(false, title="Moving Average", group="🟣 Entry Conditions", inline="Entry C")

rsiLength = input.int(14, title="Length­­     ", inline='RSIL', group="β€’ RSI")

rsiLong = input.string('Smaller Than', title="RSI Longβ€€β€€β€€β€€β€€β€€β€€", options=['Smaller Than', 'Greater Than',  'Crossing Up', 'Crossing Down'], inline='RSI 1', group="β€’ RSI")
rsiShort = input.string('Greater Than', title="RSI Shortβ€€β€€β€€β€€β€€β€€β€Šβ€Šβ€Šβ€Š", options=['Greater Than', 'Smaller Than', 'Crossing Up', 'Crossing Down'], inline='RSI 2', group="β€’ RSI")
rsiLongValue = input.int(25, title='', inline='RSI 1', group="β€’ RSI")
rsiShortValue = input.int(75, title='', inline='RSI 2', group="β€’ RSI")

emaType = input.string('EMA', title="MA Typeβ€€β€€β€€β€€β€€β€€β€€β€Š", options=['EMA', 'SMA', 'SMMA (RMA)', 'WMA', 'VWMA', 'HMA'], group="β€’ Moving Average", inline="EMA 1")
emaLength = input.int(200, title="MA Length", group="β€’ Moving Average", inline="EMA 1")


// Take Profit & Stop Loss
tpsl_method = input.string('%', title="Take Profit & Stop Loss Method      ", options=['%', 'ATR'], inline="TP", group='🟣 Take Profit & Stop Loss Settings',
 tooltip='This is the percentage/ATR factor distance that price needs to move in the opposite direction to your take profit or loss, at which point the bot will execute a Limit Order for profits or a Market Order for losses, on the exchange account to close the deal.'
 + '\n' + 'Please note, the Take Profit/ Stop Loss are calculated from the Average Entry Price.')

Target_profit_long1 = input.float(2.0, 'Take Profit 1    Long (%)', step=0.1, inline="TP1%", group='β€’ Take Profit & Stop Loss (%)') * 0.01
Target_profit_short1 = input.float(2.0, 'Short (%)', step=0.1, inline="TP1%", group='β€’ Take Profit & Stop Loss (%)') * 0.01
Target_profit_long2 = input.float(4.0, 'Take Profit 2    Long (%)', step=0.1, inline="TP2%", group='β€’ Take Profit & Stop Loss (%)') * 0.01
Target_profit_short2 = input.float(4.0, 'Short (%)', step=0.1, inline="TP2%", group='β€’ Take Profit & Stop Loss (%)') * 0.01

stop_loss_long = input.float(2.0, 'Stop Loss       Long (%)', step=0.1, inline="SL1%", group='β€’ Take Profit & Stop Loss (%)') * 0.01
stop_loss_short = input.float(2.0, 'Short (%)', step=0.1, inline="SL1%", group='β€’ Take Profit & Stop Loss (%)') * 0.01

atr_length = input.int(14, title="ATR Length                 β€‰β€Šβ€Šβ€Š", inline="ATR 3", group='β€’ Take Profit & Stop Loss (ATR)')
Target_profit_atr_long = input.float(2.0, 'Take Profit 1     Long (ATR)', step=0.1, inline='ATR 1', group='β€’ Take Profit & Stop Loss (ATR)') 
target_profit_atr_short = input.float(2.0, 'Short (ATR)', step=0.1, inline='ATR 1', group='β€’ Take Profit & Stop Loss (ATR)')
Target_profit_atr_long2 = input.float(4.0, 'Take Profit 2     Long (ATR)', step=0.1, inline='ATR 2', group='β€’ Take Profit & Stop Loss (ATR)') 
target_profit_atr_short2 = input.float(4.0, 'Short (ATR)', step=0.1, inline='ATR 2', group='β€’ Take Profit & Stop Loss (ATR)')

stop_loss_atr_long = input.float(2.0, 'Stop Loss       Long (ATR)', step=0.1, inline='ATR 1sl', group='β€’ Take Profit & Stop Loss (ATR)') 
stop_loss_atr_short = input.float(2.0, 'Short (ATR)', step=0.1, inline='ATR 1sl', group='β€’ Take Profit & Stop Loss (ATR)')

// Order Size Settings
orderSizeType = input.string('Margin (%)', title='Order Size Method', inline='OS', options=['Margin (%)', 'Margin ($)','Risk (%)', 'Risk ($)', 'Contracts'], group='🟣 Base Order Size Settings', tooltip="Margin (%): Position Size as Margin of Balance in Percentage\nMargin ($):Position Size in Dollars\nRisk (%): Risk in percentage of account balance\nRisk($): Risk in Dollars of account balance\nContracts: Position size in Contracts")
buyInputPerc = input.float(1, title='Margin (%)        ', inline='Order Size1', group='🟣 Base Order Size Settings')/100
buyInputDol = input.float(100, title='Margin ($)', inline='Order Size1', group='🟣 Base Order Size Settings')

percentRiskOrderSize = input.float(1, title='Risk (%)            ', inline='Order Size2', group='🟣 Base Order Size Settings')
dollarRiskOrderSize = input.int(50, title='Risk ($)    β€Š', inline='Order Size2', group='🟣 Base Order Size Settings')

contractOrderSize = input.float(1, title='Contracts          ', inline='Order Size3', group='🟣 Base Order Size Settings')

// DCA Inputs
useDCA = input.bool(false, title="Use Safety Orders (DCA)", group="🟣 DCA Order Settings", tooltip="Safety Orders are used to Average the cost of the asset being traded, this can help your bot to close deals faster with more profit. Safety Orders are also known as Dollar Cost Averaging and help when prices moves in the opposite direction to your bot's take profit target.")
input_option = input.string("Scaling", title="DCA Input Method", options = ["Scaling", "Manual"], group="🟣 DCA Order Settings")
Max_safety_trades_count = input.int(5, 'Max safety orders count', maxval=15, group='🟣 DCA Order Settings')

// Scaling Distance & Size Inputs
Price_deviation = input.float(0.25, 'Price deviation for first safety order (%)', step=0.01, group='β€’ Scaling DCA Inputs') * 0.01
Safety_order_step_scale = input.float(1.1, 'Safety order distance scale          ', step=0.01, group='β€’ Scaling DCA Inputs', tooltip="Enter the factor you wish your safety orders to deviate from each other.\n\nFor example:\nWith distance scale 2, safety order 1 is 1% away from initial entry, safety order 2 is 2% away from safety order 1, safety order 3 is 4% away from safety order 2.")
Safety_order_volume_scale = input.float(1.5, 'Safety order volume scale            ', step=0.01, group='β€’ Scaling DCA Inputs', tooltip="Enter the factor of your initial entry size your Safety Orders will use, to Average the cost of the asset being traded, this can help your bot to close deals faster with more profit. Safety Orders are also known as Dollar Cost Averaging and help when prices moves in the opposite direction to your bot's take profit target.\n\nFor example:\nInitial entry size of 1%, with volume scale of 2, makes the first safety order size 2% of your balance, second safety order 4% of your balance etc.")

// Manual Distance & Size Inputs
DCA1perc = input.float(0.25, title="Safety Order 1 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA1") / 100
DCA2perc = input.float(0.5, title="Safety Order 2 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA2") / 100
DCA3perc = input.float(0.75, title="Safety Order 3 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA3") / 100
DCA4perc = input.float(1.0, title="Safety Order 4 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA4") / 100
DCA5perc = input.float(1.25, title="Safety Order 5 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA5") / 100
DCA6perc = input.float(1.5, title="Safety Order 6 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA6") / 100
DCA7perc = input.float(1.75, title="Safety Order 7 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA7") / 100
DCA8perc = input.float(2.0, title="Safety Order 8 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA8") / 100
DCA9perc = input.float(2.25, title="Safety Order 9 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA9") / 100
DCA10perc = input.float(2.5, title="Safety Order 10 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA10") / 100
DCA11perc = input.float(2.75, title="Safety Order 11 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA11") / 100
DCA12perc = input.float(3.0, title="Safety Order 12 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA12") / 100
DCA13perc = input.float(3.25, title="Safety Order 13 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA13") / 100
DCA14perc = input.float(3.5, title="Safety Order 14 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA14") / 100
DCA15perc = input.float(3.75, title="Safety Order 15 (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA15") / 100

so_size_perc1 = input.float(0.25, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA1") / 100
so_size_perc2 = input.float(0.5, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA2") / 100
so_size_perc3 = input.float(1, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA3") / 100
so_size_perc4 = input.float(1, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA4") / 100
so_size_perc5 = input.float(1, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA5") / 100
so_size_perc6 = input.float(2, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA6") / 100
so_size_perc7 = input.float(2, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA7") / 100
so_size_perc8 = input.float(2, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA8") / 100
so_size_perc9 = input.float(4, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA9") / 100
so_size_perc10 = input.float(4, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA10") / 100
so_size_perc11 = input.float(4, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA11") / 100
so_size_perc12 = input.float(8, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA12") / 100
so_size_perc13 = input.float(8, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA13") / 100
so_size_perc14 = input.float(8, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA14") / 100
so_size_perc15 = input.float(10, title="Size (%)", step=0.1, group="β€’ Manual DCA Inputs", inline="DCA15") / 100


// Bot Confirguation
configureMethod = input.string('WickHunter Auto-Configuration', title='Bot Configuration Method', options=['WickHunter Auto-Configuration', 'Alertatron Auto-Configuration', 'Manual Configuration'], inline='Bot1', group="🟣 Bot Alert Configuration", tooltip="Always use alert function () calls only when setting up alerts with this strategy")
userID = input.string('uuid', title='WickHunter user ID      ', tooltip="Insert your WickHunter user ID", inline='WH', group="🟣 Bot Alert Configuration")
manual_Configure = input.text_area("", title='Place Manual Bot Configuration Below', group="🟣 Bot Alert Configuration")


strategy(title='CryptoGraph Strategizer', 
 overlay=true, 
 initial_capital=1000, 
 pyramiding=16, 
 calc_on_order_fills=false, 
 process_orders_on_close=true, 
 calc_on_every_tick=true,
 backtest_fill_limits_assumption=1,
 slippage=2,
 commission_type=strategy.commission.percent, 
 commission_value=0.03,
 use_bar_magnifier=true)

// Position
status_none  = strategy.position_size == 0 
status_long  = strategy.position_size[1] == 0 and strategy.position_size > 0
status_long_offset  = strategy.position_size[2] == 0 and strategy.position_size[1] > 0
status_short = strategy.position_size[1] == 0 and strategy.position_size < 0
status_increase = strategy.opentrades[1] < strategy.opentrades

// Currency
currency = 
 Profit_currency == 'USDT' ? ' USDT' :
 Profit_currency == 'BTC'  ? ' BTC' :
 Profit_currency == '$' ? ' $' : na


// Strategy Signal
ema1 = ta.ema(close, 10)
ema2 = ta.ema(close, 20)

Base_order_Condition_Long = ta.crossover(ema1, ema2) and barstate.isconfirmed and (strategy.position_size == 0) and ((strategy == 'Long & Short') or (strategy == 'Long'))
Base_order_Condition_Short = ta.crossunder(ema1, ema2) and barstate.isconfirmed and (strategy.position_size == 0) and ((strategy == 'Long & Short') or (strategy == 'Short'))
// ta.crossover(ema1, ema2)

// Safety Orders
pd = Price_deviation
ss = Safety_order_step_scale

step(i) =>
 i == 1 ? pd :
 i == 2 ? pd + pd * ss :
 i == 3 ? pd + (pd + pd * ss) * ss :
 i == 4 ? pd + (pd + (pd + pd * ss) * ss) * ss : 
 i == 5 ? pd + (pd + (pd + (pd + pd * ss) * ss) * ss) * ss : 
 i == 6 ? pd + (pd + (pd + (pd + (pd + pd * ss) * ss) * ss) * ss) * ss : 
 i == 7 ? pd + (pd + (pd + (pd + (pd + (pd + pd * ss) * ss) * ss) * ss) * ss) * ss : 
 i == 8 ? pd + (pd + (pd + (pd + (pd + (pd + (pd + pd * ss) * ss) * ss) * ss) * ss) * ss) * ss : 
 i == 9 ? pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + pd * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss : 
 i == 10 ? pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + pd * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss :
 i == 11 ? pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + pd * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss : 
 i == 12 ? pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + pd * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) *ss) * ss : 
 i == 13 ? pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + pd * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss : 
 i == 14 ? pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + pd * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss : 
 i == 15 ? pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + (pd + pd * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss) * ss : na

long_line(i) =>
 close[1] - close[1] * (step(i))

short_line(i) =>
 close[1] + close[1] * (step(i))

// Safety Order global variables
var float bo_size = 0.0

var float SO0 = 0.0
var float SO1 = 0.0
var float SO2 = 0.0
var float SO3 = 0.0
var float SO4 = 0.0
var float SO5 = 0.0
var float SO6 = 0.0
var float SO7 = 0.0
var float SO8 = 0.0
var float SO9 = 0.0
var float SO10 = 0.0
var float SO11 = 0.0
var float SO12 = 0.0
var float SO13 = 0.0
var float SO14 = 0.0
var float SO15 = 0.0

var float so_size1 = 0.0
var float so_size2 = 0.0
var float so_size3 = 0.0
var float so_size4 = 0.0
var float so_size5 = 0.0
var float so_size6 = 0.0
var float so_size7 = 0.0
var float so_size8 = 0.0
var float so_size9 = 0.0
var float so_size10 = 0.0
var float so_size11 = 0.0
var float so_size12 = 0.0
var float so_size13 = 0.0
var float so_size14 = 0.0
var float so_size15 = 0.0

// Take Profit & Stop Loss
//Percentage based
tp_perc_long1 = strategy.position_avg_price * (1 + Target_profit_long1) 
tp_perc_long2 = strategy.position_avg_price * (1 + Target_profit_long2) 
sl_perc_long = strategy.opentrades.entry_price(0) * (1 - stop_loss_long)

tp_perc_short1 = strategy.position_avg_price * (1 - Target_profit_short1) 
tp_perc_short2 = strategy.position_avg_price * (1 - Target_profit_short2) 
sl_perc_short = strategy.opentrades.entry_price(0) * (1 + stop_loss_short)
// ATR based
atr = ta.atr(atr_length)

stopLong = close - (atr * stop_loss_atr_long)
stopShort = close + (atr * stop_loss_atr_short)

longTP1 = close + (atr * Target_profit_atr_long)
longTP2 = close + (atr * Target_profit_atr_long2)

shortTP1 = close - (atr * target_profit_atr_short)
shortTP2 = close - (atr * target_profit_atr_short2)


// TPSL Input Options
TP_line1 = 0.0
TP_line2 = 0.0
SL_line = 0.0

if tpsl_method == '%' and Base_order_Condition_Long
    TP_line1 := tp_perc_long1
    TP_line2 := tp_perc_long2
    SL_line := sl_perc_long

if tpsl_method == '%' and Base_order_Condition_Short
    TP_line1 := tp_perc_short1
    TP_line2 := tp_perc_short2
    SL_line := sl_perc_short

if tpsl_method == 'ATR' and Base_order_Condition_Long
    TP_line1 := longTP1
    TP_line2 := longTP2
    SL_line := stopLong

if tpsl_method == 'ATR' and Base_order_Condition_Short
    TP_line1 := shortTP1
    TP_line2 := shortTP2
    SL_line := stopShort


// Base Order Size 
Base_order_size = 0.0
entrySize = 0.0
entrySizeType = ''


if orderSizeType == 'Margin (%)'
    Base_order_size := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * buyInputPerc) * leverage
    entrySize := buyInputPerc * leverage
    entrySizeType := '"PERCENT"'
if orderSizeType == 'Margin ($)'
    Base_order_size := (buyInputDol / close) * leverage
    entrySize := buyInputDol * leverage
    entrySizeType := '"DOLLAR"'

if orderSizeType == 'Risk (%)' and Base_order_Condition_Long
    Base_order_size := ((strategy.initial_capital + strategy.netprofit) / 100 * percentRiskOrderSize) / (((close-(close - stop_loss_atr_long*atr)) / close) * syminfo.pointvalue * close)
    entrySize := math.abs((percentRiskOrderSize / 100) / ((stopLong - close) / close)) 
    entrySizeType := '"PERCENT"'
if orderSizeType == 'Risk (%)' and Base_order_Condition_Short 
    Base_order_size := ((strategy.initial_capital + strategy.netprofit) / 100 * percentRiskOrderSize) / (((close-(close - stop_loss_atr_short*atr)) / close) * syminfo.pointvalue * close)
    entrySize := math.abs((percentRiskOrderSize / 100) / ((stopShort - close) / close)) 
    entrySizeType := '"PERCENT"'

if orderSizeType == 'Risk ($)' and Base_order_Condition_Long
    Base_order_size := dollarRiskOrderSize / (((close-(close - stop_loss_atr_long*atr)) / close) * syminfo.pointvalue * close)
    entrySize := dollarRiskOrderSize / ((close-(close - stop_loss_atr_long*atr)) / close) 
    entrySizeType := '"DOLLAR"'
if orderSizeType == 'Risk ($)' and Base_order_Condition_Short
    Base_order_size := dollarRiskOrderSize / (((close-(close - stop_loss_atr_short*atr)) / close) * syminfo.pointvalue * close)
    entrySize := dollarRiskOrderSize / ((close-(close - stop_loss_atr_short*atr)) / close) 
    entrySizeType := '"DOLLAR"'

if orderSizeType == 'Contracts'
    Base_order_size := contractOrderSize 
    entrySize := contractOrderSize * close
    entrySizeType := '"DOLLAR"'

Base_order_size_final = ta.valuewhen(Base_order_Condition_Long, Base_order_size, 0)
Base_order_size_final_S = ta.valuewhen(Base_order_Condition_Short, Base_order_size, 0)

plot(Base_order_size_final*100)

plot(Base_order_size*100)
// Liq base order
Base_order_size_liq = math.abs(((strategy.initial_capital + strategy.netprofit)/close) * (buyInputPerc+0.014))

// Safety Order Logics
if input_option == "Scaling"
    // Scaling SO Distance
    SO0 := ta.valuewhen(status_long, long_line(0), 0)
    SO1 := ta.valuewhen(status_long, long_line(1), 0)
    SO2 := ta.valuewhen(status_long, long_line(2), 0)
    SO3 := ta.valuewhen(status_long, long_line(3), 0)
    SO4 := ta.valuewhen(status_long, long_line(4), 0)
    SO5 := ta.valuewhen(status_long, long_line(5), 0)
    SO6 := ta.valuewhen(status_long, long_line(6), 0)
    SO7 := ta.valuewhen(status_long, long_line(7), 0)
    SO8 := ta.valuewhen(status_long, long_line(8), 0)
    SO9 := ta.valuewhen(status_long, long_line(9), 0)
    SO10 := ta.valuewhen(status_long, long_line(10), 0)
    SO11 := ta.valuewhen(status_long, long_line(11), 0)
    SO12 := ta.valuewhen(status_long, long_line(12), 0)
    SO13 := ta.valuewhen(status_long, long_line(13), 0)
    SO14 := ta.valuewhen(status_long, long_line(14), 0)
    SO15 := ta.valuewhen(status_long, long_line(15), 0)

    // Scaling Order size
    bo_size := Base_order_size_final
    so_size1 := Base_order_size_final * math.pow(Safety_order_volume_scale, 1)
    so_size2 := Base_order_size_final * math.pow(Safety_order_volume_scale, 2)
    so_size3 := Base_order_size_final * math.pow(Safety_order_volume_scale, 3)
    so_size4 := Base_order_size_final * math.pow(Safety_order_volume_scale, 4)
    so_size5 := Base_order_size_final * math.pow(Safety_order_volume_scale, 5)
    so_size6 := Base_order_size_final * math.pow(Safety_order_volume_scale, 6)
    so_size7 := Base_order_size_final * math.pow(Safety_order_volume_scale, 7)
    so_size8 := Base_order_size_final * math.pow(Safety_order_volume_scale, 8)
    so_size9 := Base_order_size_final * math.pow(Safety_order_volume_scale, 9)
    so_size10 := Base_order_size_final * math.pow(Safety_order_volume_scale, 10)
    so_size11 := Base_order_size_final * math.pow(Safety_order_volume_scale, 11)
    so_size12 := Base_order_size_final * math.pow(Safety_order_volume_scale, 12)
    so_size13 := Base_order_size_final * math.pow(Safety_order_volume_scale, 13)
    so_size14 := Base_order_size_final * math.pow(Safety_order_volume_scale, 14)
    so_size15 := Base_order_size_final * math.pow(Safety_order_volume_scale, 15)


if input_option == "Manual"
    // Manual SO Distance
    SO0 := strategy.opentrades.entry_price(0) * (1 - 0)
    SO1 := strategy.opentrades.entry_price(0) * (1 - DCA1perc)
    SO2 := strategy.opentrades.entry_price(0) * (1 - DCA2perc)
    SO3 := strategy.opentrades.entry_price(0) * (1 - DCA3perc)
    SO4 := strategy.opentrades.entry_price(0) * (1 - DCA4perc)
    SO5 := strategy.opentrades.entry_price(0) * (1 - DCA5perc)
    SO6 := strategy.opentrades.entry_price(0) * (1 - DCA6perc)
    SO7 := strategy.opentrades.entry_price(0) * (1 - DCA7perc)
    SO8 := strategy.opentrades.entry_price(0) * (1 - DCA8perc)
    SO9 := strategy.opentrades.entry_price(0) * (1 - DCA9perc)
    SO10 := strategy.opentrades.entry_price(0) * (1 - DCA10perc)
    SO11 := strategy.opentrades.entry_price(0) * (1 - DCA11perc)
    SO12 := strategy.opentrades.entry_price(0) * (1 - DCA12perc)
    SO13 := strategy.opentrades.entry_price(0) * (1 - DCA13perc)
    SO14 := strategy.opentrades.entry_price(0) * (1 - DCA14perc)
    SO15 := strategy.opentrades.entry_price(0) * (1 - DCA15perc)

    // Manual Order size
    bo_size := Base_order_size_final
    so_size1 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc1)
    so_size2 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc2)
    so_size3 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc3)
    so_size4 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc4)
    so_size5 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc5)
    so_size6 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc6)
    so_size7 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc7)
    so_size8 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc8)
    so_size9 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc9)
    so_size10 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc10)
    so_size11 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc11)
    so_size12 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc12)
    so_size13 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc13)
    so_size14 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc14)
    so_size15 := math.abs(((strategy.initial_capital + strategy.netprofit)/close) * so_size_perc15)

// Safety Order Functions
Safe_order_line(i) => 
 i == 0 ? SO0 :
 i == 1 ? SO1 :
 i == 2 ? SO2 :
 i == 3 ? SO3 :
 i == 4 ? SO4 : 
 i == 5 ? SO5 : 
 i == 6 ? SO6 : 
 i == 7 ? SO7 : 
 i == 8 ? SO8 : 
 i == 9 ? SO9 : 
 i == 10 ? SO10 :
 i == 11 ? SO11 : 
 i == 12 ? SO12 : 
 i == 13 ? SO13 : 
 i == 14 ? SO14 : 
 i == 15 ? SO15 : na

safety_order_size(i) => 
 i == 1 ? so_size1 :
 i == 2 ? so_size2 :
 i == 3 ? so_size3 :
 i == 4 ? so_size4 :
 i == 5 ? so_size5 :
 i == 6 ? so_size6 :
 i == 7 ? so_size7 :
 i == 8 ? so_size8 :
 i == 9 ? so_size9 :
 i == 10 ? so_size10 :
 i == 11 ? so_size11 :
 i == 12 ? so_size12 :
 i == 13 ? so_size13 :
 i == 14 ? so_size14 :
 i == 15 ? so_size15 : na

plot(safety_order_size(1)*100)
// Liquidation Levels Isolated/Cross
// ISOLATED Leverage
//long
liqIsoL = (strategy.position_avg_price * (1 - (1/leverage) + 0.005))
if liqIsoL <= 0
    liqIsoL := 0.5
//short
liqIsoS = (strategy.position_avg_price * (1 + (1/leverage) - 0.005))

// CROSS Leverage
sum_safety_order_size(i) =>
    sum_so = 0.0
    if i >= 1
        for x = 1 to i by 1
            temp = sum_so
            sum_so := temp + safety_order_size(x)
    sum_so
    
initial_margin = (Base_order_size + sum_safety_order_size(strategy.opentrades - 1)) * strategy.position_avg_price * 0.01
maintenance_margin = (Base_order_size + sum_safety_order_size(strategy.opentrades - 1)) * strategy.position_avg_price * 0.005
available_balance = strategy.equity - (((Base_order_size_liq * close)/leverage)) - ((sum_safety_order_size(strategy.opentrades - 1)*close)/leverage)
total_sustainable_loss = available_balance + initial_margin - maintenance_margin

//long
liqCrossLong = strategy.position_avg_price - (total_sustainable_loss/Base_order_size)

if liqCrossLong <= 0
    liqCrossLong := 0.5
//short
liqCrossSgirt = strategy.position_avg_price + (total_sustainable_loss/Base_order_size)


// Strategy entry/exit logics
// Base order
if Base_order_Condition_Long
    strategy.entry('Base order', strategy.long, qty=Base_order_size,
     comment='BO' + ' - ' + str.tostring(bo_size * close) + str.tostring(currency))


// Safety Orders
if Max_safety_trades_count >= 1 and useDCA
    for i = 1 to Max_safety_trades_count by 1
        i_s = str.tostring(i)
        strategy.entry('Safety order' + i_s, strategy.long, qty=safety_order_size(i),
         limit=Safe_order_line(i), when=(strategy.opentrades <= i) and strategy.position_size > 0 and not(strategy.position_size == 0), 
         comment='SO' + i_s + ' - ' + str.tostring(safety_order_size(i) * close)  + str.tostring(currency))

// Exit 
for i = 1 to Max_safety_trades_count by 1
    i_s = str.tostring(i)
    strategy.cancel('Safety order' + i_s, when=status_none)
    strategy.exit('TP/SL','Base order', limit=TP_line1, stop=SL_line, comment = Safe_order_line(100) > close ? 'SL' + i_s + ' - ' +  str.tostring(Base_order_size) + str.tostring(currency) : 'TP' + i_s + ' - ' +  str.tostring(Base_order_size*close) + str.tostring(currency)) 
    strategy.exit('TP/SL','Safety order' + i_s, limit=TP_line1, stop=SL_line, comment = Safe_order_line(100) > close ? 'SL' + i_s + ' - ' +  str.tostring(safety_order_size(i)*close) + str.tostring(currency) : 'TP' + i_s + ' - ' +  str.tostring(safety_order_size(i)*close) + str.tostring(currency)) 



// Plots
plot(ema1, color=color.yellow)
plot(ema2, color=color.orange)

plotshape(Base_order_Condition_Long, title='Long Entry', location=location.belowbar, style=shape.labelup, color=color.new(#2C9670, 0), text='Long', textcolor=color.new(color.white, 0))

var color soCol = na
var color tpCol = na
var color slCol = na 
var color tpBgTop = na
var color tpBgBot = na 
var color slBgTop = na
var color slBgBot = na


if colorScheme == 'CryptoGraph'
    soCol := #d86dff
    tpCol := #26c6da
    slCol := #ec407a
    tpBgTop := color.rgb(0, 188, 212, 65)
    tpBgBot := color.rgb(0, 188, 212, 92)
    slBgTop := color.rgb(177, 41, 255, 94)
    slBgBot := color.rgb(177, 41, 255, 70)

if colorScheme == 'Traditional'
    soCol := #ff5252
    tpCol := #00e676
    slCol := #ff5252
    tpBgTop := color.rgb(0, 255, 0, 65)
    tpBgBot := color.rgb(0, 255, 0, 98)
    slBgTop := color.rgb(255, 0, 0, 88)
    slBgBot := color.rgb(255, 0, 0, 66)

var float liqLineType = na

if leverageType == 'Isolated'
    liqLineType := liqIsoL

if leverageType == 'Cross'
    liqLineType := liqCrossLong


account_balance = plot(strategy.equity)

entryLevel = plot(strategy.position_avg_price, title="Entry level", color=color.white, linewidth=1, style=plot.style_linebr) 
tpLine = plot(strategy.position_size > 0 ? TP_line1 : na, 'Take Profit', color=tpCol, linewidth=2, style=plot.style_linebr)
slLine = plot(strategy.position_size > 0 ? SL_line : na, 'Stop Loss', color=slCol, linewidth=2, style=plot.style_linebr)
liqLine = plot(liqLineType, title="Isolated Liquidation Price", color=liqLineCol, linewidth=2, style=plot.style_linebr)
// Safety Orders
so1Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 1 and (strategy.opentrades <= 1) and useDCA ? Safe_order_line(1) : na, 'Safety order1', color=soCol, style=plot.style_linebr)
so2Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 2 and (strategy.opentrades <= 2) and useDCA ? Safe_order_line(2) : na, 'Safety order2', color=soCol, style=plot.style_linebr)
so3Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 3 and (strategy.opentrades <= 3) and useDCA ? Safe_order_line(3) : na, 'Safety order3', color=soCol, style=plot.style_linebr)
so4Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 4 and (strategy.opentrades <= 4) and useDCA ? Safe_order_line(4) : na, 'Safety order4', color=soCol, style=plot.style_linebr)
so5Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 5 and (strategy.opentrades <= 5) and useDCA ? Safe_order_line(5) : na, 'Safety order5', color=soCol, style=plot.style_linebr)
so6Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 6 and (strategy.opentrades <= 6) and useDCA ? Safe_order_line(6) : na, 'Safety order6', color=soCol, style=plot.style_linebr)
so7Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 7 and (strategy.opentrades <= 7) and useDCA ? Safe_order_line(7) : na, 'Safety order7', color=soCol, style=plot.style_linebr)
so8Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 8 and (strategy.opentrades <= 8) and useDCA ? Safe_order_line(8) : na, 'Safety order8', color=soCol, style=plot.style_linebr)
so9Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 9 and (strategy.opentrades <= 9) and useDCA ? Safe_order_line(9) : na, 'Safety order9', color=soCol, style=plot.style_linebr)
so10Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 10 and (strategy.opentrades <= 10) and useDCA ? Safe_order_line(10) : na, 'Safety order10', color=soCol, style=plot.style_linebr)
so11Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 11 and (strategy.opentrades <= 11) and useDCA ? Safe_order_line(11) : na, 'Safety order11', color=soCol, style=plot.style_linebr)
so12Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 12 and (strategy.opentrades <= 12) and useDCA ? Safe_order_line(12) : na, 'Safety order12', color=soCol, style=plot.style_linebr)
so13Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 13 and (strategy.opentrades <= 13) and useDCA ? Safe_order_line(13) : na, 'Safety order13', color=soCol, style=plot.style_linebr)
so14Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 14 and (strategy.opentrades <= 14) and useDCA ? Safe_order_line(14) : na, 'Safety order14', color=soCol, style=plot.style_linebr)
so15Line = plot(strategy.position_size > 0 and Max_safety_trades_count >= 15 and (strategy.opentrades <= 15) and useDCA ? Safe_order_line(15) : na, 'Safety order15', color=soCol, style=plot.style_linebr)
//Background
fill(tpLine, entryLevel, title="Background", top_value=TP_line1, bottom_value=strategy.position_avg_price,top_color=tpBgTop, bottom_color=tpBgBot)
fill(slLine, entryLevel, title="Background", top_value=strategy.position_avg_price, bottom_value=SL_line, top_color=slBgTop, bottom_color=slBgBot)


// bot_usage(i) =>
//  i == 1 ? Base_order_size + safety_order_size(1) :
//  i == 2 ? Base_order_size + safety_order_size(1) + safety_order_size(2) :
//  i == 3 ? Base_order_size + safety_order_size(1) + safety_order_size(2) + safety_order_size(3) :
//  i == 4 ? Base_order_size + safety_order_size(1) + safety_order_size(2) + safety_order_size(3) + safety_order_size(4) : 
//  i == 5 ? Base_order_size + safety_order_size(1) + safety_order_size(2) + safety_order_size(3) + safety_order_size(4) + safety_order_size(5) :
//  i == 6 ? Base_order_size + safety_order_size(1) + safety_order_size(2) + safety_order_size(3) + safety_order_size(4) + safety_order_size(5) + safety_order_size(6) : 
//  i == 7 ? Base_order_size + safety_order_size(1) + safety_order_size(2) + safety_order_size(3) + safety_order_size(4) + safety_order_size(5) + safety_order_size(6) + safety_order_size(7) : 
//  i == 8 ? Base_order_size + safety_order_size(1) + safety_order_size(2) + safety_order_size(3) + safety_order_size(4) + safety_order_size(5) + safety_order_size(6) + safety_order_size(7) + safety_order_size(8) : 
//  i == 9 ? Base_order_size + safety_order_size(1) + safety_order_size(2) + safety_order_size(3) + safety_order_size(4) + safety_order_size(5) + safety_order_size(6) + safety_order_size(7) + safety_order_size(8) + safety_order_size(9) :
//  i == 10 ? Base_order_size + safety_order_size(1) + safety_order_size(2) + safety_order_size(3) + safety_order_size(4) + safety_order_size(5) + safety_order_size(6) + safety_order_size(7) + safety_order_size(8) + safety_order_size(9) + safety_order_size(10) : na

// equity = strategy.equity
// bot_use = bot_usage(Max_safety_trades_count)
// bot_dev = float(step(Max_safety_trades_count)) * 100
// bot_ava = (bot_use / equity) * 100


// string label = 
//  'Balance                                      : ' + str.tostring(math.round(equity, 0), '###,###,###,###') + ' USDT' + '\n' + 
//  'Max amount for bot usage           : ' + str.tostring(math.round(bot_use, 0), '###,###,###,###') + ' USDT' + '\n' + 
//  'Max safety order price deviation : ' + str.tostring(math.round(bot_dev, 0), '##.##') + ' %' + '\n' + 
//  '% of available balance                : ' + str.tostring(math.round(bot_ava, 0), '###,###,###,###') + ' %' 
//  + (bot_ava > 100 ? '\n \n' +  '⚠ Warning! Bot will use amount greater than you have on exchange' : na) 

// if status_long
//     day_label = 
//      label.new(
//      x=time[1], 
//      y=high * 1.03, 
//      text=label, 
//      xloc=xloc.bar_time, 
//      yloc=yloc.price, 
//      color=bot_ava > 100 ? color.new(color.yellow, 0) : color.new(color.black, 50), 
//      style=label.style_label_lower_right, 
//      textcolor=bot_ava > 100 ? color.new(color.red, 0) : color.new(color.silver, 0), 
//      size=size.normal, 
//      textalign=text.align_left)