Diversified Investment EMA Cross Strategy Simulator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Dicargo_Beam

//@version=5
indicator("Diversified Investment EMA Cross Only Long Strategy Capital Change",format=format.inherit,precision=0)


//Thank you Pinecoders. About security function without repainting
f_security(_sym, _res, _src) => 
    barstate.isconfirmed? request.security(_sym, _res, _src[barstate.isrealtime ? 1 : 0])[barstate.isrealtime ? 0 : 1] :na


Plot = input.string("Total Assets Capital Change",title="Capital Change Graph", options=["Total Assets Capital Change", "Each Asset Capital Change", "Asset 1 Capital Change", "Asset 2 Capital Change", "Asset 3 Capital Change", "Asset 4 Capital Change", "Asset 5 Capital Change", "Asset 6 Capital Change", "Asset 7 Capital Change", "Asset 8 Capital Change"])

//Start Date
start_year  = input.int(2016,   "Start Year")
start_month = input.int(1,      "Start Month")
start_day   = input.int(1,      "Start Day")

bool start  = false
if barstate.isconfirmed
    start := time > timestamp(start_year, start_month, start_day)

initial_capital = input.float(1000.0,"Initial Capital")
loss_percent = -input.float(2.00,"Loss Percent %",tooltip = "Stop loss when the asset price falls by ?%")/100
commission = 1-input.float(0.0200,"Commission %")/100

c1 = input.color(#FA5858,"", inline="1")
c2 = input.color(#F7BE81,"", inline="2")
c3 = input.color(#F4FA58,"", inline="3")
c4 = input.color(#2EFE2E,"", inline="4")
c5 = input.color(#2EFEF7,"", inline="5")
c6 = input.color(#2E64FE,"", inline="6")
c7 = input.color(#AC58FA,"", inline="7")
c8 = input.color(#FE2EC8,"", inline="8")

Asset_1 = input.symbol("COMEX:GC1!",        title = "Asset 1 (Gold)　　　　 　", inline="1")  //Gold
Asset_2 = input.symbol("CBOT:ZC1!",         title = "Asset 2 (Corn)　　　　 　", inline="2")  //Corn
Asset_3 = input.symbol("ICEUS:SB1!",        title = "Asset 3 (Sugar)　　　  　", inline="3")  //Sugar
Asset_4 = input.symbol("CME:LE1!",          title = "Asset 4 (Cattle)　　　 　", inline="4")  //Cattle
Asset_5 = input.symbol("CME:6J1!",          title = "Asset 5 (Japanese yen) 　", inline="5")  //Japanese yen
Asset_6 = input.symbol("CBOT:ZN1!",         title = "Asset 6 (10y T-Note)　 　", inline="6")  //10 Year T-Note Futures
Asset_7 = input.symbol("COMEX:SI1!",        title = "Asset 7 (Silver)　　　　 ", inline="7")  //Silver
Asset_8 = input.symbol("NYMEX:CL1!",        title = "Asset 8 (Crude Oil)　  　", inline="8")  //Crude Oil WTI

//array(0 price, 1 entry_price, 2 exit_price, 3 input_capital, 4 output_capital, 5 direction (1,-1), 6 on_trade (1, 0))

var float[] asset_1 = array.new_float(7)
var float[] asset_2 = array.new_float(7)
var float[] asset_3 = array.new_float(7)
var float[] asset_4 = array.new_float(7)
var float[] asset_5 = array.new_float(7)
var float[] asset_6 = array.new_float(7)
var float[] asset_7 = array.new_float(7)
var float[] asset_8 = array.new_float(7)


_get_value(arr, ticker, loss_percent, commission) =>
    if start and not start[1]
        array.set(arr, 3, initial_capital/8)
        array.set(arr, 4, initial_capital/8)
        array.set(arr, 6, 0)
    c = f_security(ticker,"D",close)
    sep = f_security(ticker,"D",ta.ema(close,50)-ta.ema(close,200))

    array.set(arr, 0, c)
    array.set(arr, 5, sep > 0? 1 : -1)
    profit = array.get(arr,0)/array.get(arr,1)-1
    
    //set entry and on trade
    if ta.crossover(array.get(arr,5),0)
        array.set(arr, 1, c)
        array.set(arr,6, 1)
    //set exit and off trade
    else if ta.crossunder(array.get(arr,5),0)
        array.set(arr,6, 0)
        array.set(arr,2, c)
    else if profit < loss_percent and profit[1] > loss_percent
        array.set(arr,6, 0)
        array.set(arr,2, c)
    
    //capital change        
    if array.get(arr,6) == 1
        array.set(arr,4,array.get(arr,0)/array.get(arr,1) * array.get(arr,3)*commission)
    else if array.get(arr,6) == 0 and array.get(arr,6)[1] == 1
        array.set(arr,4,array.get(arr,2)/array.get(arr,1) * array.get(arr,3)*commission)
    else if array.get(arr,6) == 0 and array.get(arr,6)[1] == 0
        array.set(arr,3,array.get(arr,4))

if barstate.isconfirmed and start
    _get_value(asset_1, Asset_1, loss_percent, commission)
    _get_value(asset_2, Asset_2, loss_percent, commission)
    _get_value(asset_3, Asset_3, loss_percent, commission)
    _get_value(asset_4, Asset_4, loss_percent, commission)
    _get_value(asset_5, Asset_5, loss_percent, commission)
    _get_value(asset_6, Asset_6, loss_percent, commission)
    _get_value(asset_7, Asset_7, loss_percent, commission)
    _get_value(asset_8, Asset_8, loss_percent, commission)

bool p0 = false, p1 = false, p2 = false, p3 = false, p4 = false, p5 = false, p6 = false, p7 = false, p8 = false

if Plot == "Total Assets Capital Change"
    p0 := true
else if Plot == "Each Asset Capital Change"
    p1 := true, p2 := true, p3:=true, p4:=true, p5:=true, p6:=true, p7:=true, p8:=true
else if Plot == "Asset 1 Capital Change"
    p1 := true
else if Plot == "Asset 2 Capital Change"
    p2 := true
else if Plot == "Asset 3 Capital Change"
    p3 := true
else if Plot == "Asset 4 Capital Change"
    p4 := true
else if Plot == "Asset 5 Capital Change"
    p5 := true
else if Plot == "Asset 6 Capital Change"
    p6 := true
else if Plot == "Asset 7 Capital Change"
    p7 := true
else if Plot == "Asset 8 Capital Change"
    p8 := true


t_cap = array.get(asset_1,4)+array.get(asset_2,4)+array.get(asset_3,4)+array.get(asset_4,4)+array.get(asset_5,4)+array.get(asset_6,4)+array.get(asset_7,4)+array.get(asset_8,4)
a1_cap = array.get(asset_1,4), a2_cap = array.get(asset_2,4), a3_cap = array.get(asset_3,4), a4_cap = array.get(asset_4,4)
a5_cap = array.get(asset_5,4), a6_cap = array.get(asset_6,4), a7_cap = array.get(asset_7,4), a8_cap = array.get(asset_8,4)



plot(p0? t_cap:na,  title = "Total Assets Capital Change",  color=#F6CED8)
plot(p1? a1_cap:na, title = "Asset 1 Capital Change",       color=c1)
plot(p2? a2_cap:na, title = "Asset 2 Capital Change",       color=c2)
plot(p3? a3_cap:na, title = "Asset 3 Capital Change",       color=c3)
plot(p4? a4_cap:na, title = "Asset 4 Capital Change",       color=c4)
plot(p5? a5_cap:na, title = "Asset 5 Capital Change",       color=c5)
plot(p6? a6_cap:na, title = "Asset 6 Capital Change",       color=c6)
plot(p7? a7_cap:na, title = "Asset 7 Capital Change",       color=c7)
plot(p8? a8_cap:na, title = "Asset 8 Capital Change",       color=c8)

plot(start and p0? initial_capital : na, title = "Initial Capital", color=color.gray)
plot(start and (not(p0) or Plot == "Each Asset Capital Change")? initial_capital/8 : na, title = "Initial Capital / 8", color=color.gray)



signal(arr) =>
    if array.get(arr,6)==1 and array.get(arr,6)[1]==0
        1
    else if array.get(arr,6)==0 and array.get(arr,6)[1]==1
        0



_label(Plot) =>
    bool buy = false
    bool sell = false
    float y = 0.0
    var label buy_label = na
    var label sell_label = na
    if Plot == "Asset 1 Capital Change"
        buy := signal(asset_1) == 1
        sell := signal(asset_1) == 0
        y := array.get(asset_1,4)
    else if Plot == "Asset 2 Capital Change"
        buy := signal(asset_2) == 1
        sell := signal(asset_2) == 0
        y := array.get(asset_2,4)
    else if Plot == "Asset 3 Capital Change"
        buy := signal(asset_3) == 1
        sell := signal(asset_3) == 0
        y := array.get(asset_3,4)
    else if Plot == "Asset 4 Capital Change"
        buy := signal(asset_4) == 1
        sell := signal(asset_4) == 0
        y := array.get(asset_4,4)
    else if Plot == "Asset 5 Capital Change"
        buy := signal(asset_5) == 1
        sell := signal(asset_5) == 0
        y := array.get(asset_5,4)
    else if Plot == "Asset 6 Capital Change"
        buy := signal(asset_6) == 1
        sell := signal(asset_6) == 0
        y := array.get(asset_6,4)
    else if Plot == "Asset 7 Capital Change"
        buy := signal(asset_7) == 1
        sell := signal(asset_7) == 0
        y := array.get(asset_7,4)
    else if Plot == "Asset 8 Capital Change"
        buy := signal(asset_8) == 1
        sell := signal(asset_8) == 0
        y := array.get(asset_8,4)
    if buy
        buy_label   := label.new(bar_index, y*0.98, color=color.green, size=size.tiny, style=label.style_label_up, text="buy", textcolor=color.white)
    if sell
        sell_label  := label.new(bar_index, y*1.02, color=color.red, size=size.tiny, style=label.style_label_down, text = "sell", textcolor=color.white)
    
_label(Plot)    




var table Table = na
Table := table.new(position = position.bottom_right, columns = 1, rows = 8, bgcolor = na, border_width = 1)
if barstate.islast
    table.cell(Table, column = 0, row = 0, text = "1. " + Asset_1, text_color= c1, text_halign = text.align_left)
    table.cell(Table, column = 0, row = 1, text = "2. " + Asset_2, text_color= c2, text_halign = text.align_left)
    table.cell(Table, column = 0, row = 2, text = "3. " + Asset_3, text_color= c3, text_halign = text.align_left)
    table.cell(Table, column = 0, row = 3, text = "4. " + Asset_4, text_color= c4, text_halign = text.align_left)
    table.cell(Table, column = 0, row = 4, text = "5. " + Asset_5, text_color= c5, text_halign = text.align_left)
    table.cell(Table, column = 0, row = 5, text = "6. " + Asset_6, text_color= c6, text_halign = text.align_left)
    table.cell(Table, column = 0, row = 6, text = "7. " + Asset_7, text_color= c7, text_halign = text.align_left)
    table.cell(Table, column = 0, row = 7, text = "8. " + Asset_8, text_color= c8, text_halign = text.align_left)
