Global Trend [BEA]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Badshah.E.Alam

//@version=5
indicator('Global Trend', overlay=false, max_bars_back=200)
// Input for signal smoothing
use_sma=input(true,"Signal Smoothing",group="Trend smoothing")
slow_length = input(title="Length", defval=12,group="Trend smoothing")

//Inputs for plotting diffrent mareket trends
plot_asian=input(true,"plot Asian market trend",group="Plot Trend")
plot_Local=input(false,"plot Local market trend",group="Plot Trend")
plot_american=input(false,"plot American market trend",group="Plot Trend")
plot_european=input(false,"plot European market trend",group="Plot Trend")
plot_all=input(true,"plot Average of all market trend",group="Plot Trend")

use_todays_open=input(true,"Use Todays open instead of yesterdays close for calculation")
// function to calculate the % change w.r.t previous day close 
function_get_per_change() =>
    close_yest = use_todays_open?ta.valuewhen(ta.change(time('D')), open, 0):ta.valuewhen(ta.change(time('D')), close[1], 0)
    per = (close - close_yest) / close_yest * 100
    

// Local indices (i am situated in India so i have picked up local index)
s01 = input.symbol('NSE:NIFTY',group="Local Market indices")
s02 = input.symbol('NSE:BANKNIFTY',group="Local Market indices")
s03 = input.symbol('NSE:CNXIT',group="Local Market indices")
s04 = input.symbol('NSE:CNXINFRA',group="Local Market indices")
s05 = input.symbol('NSE:CNXAUTO',group="Local Market indices")
s06 = input.symbol('NSE:CNXMETAL',group="Local Market indices")
s07 = input.symbol('NSE:CNXFMCG',group="Local Market indices")
s08 = input.symbol('NSE:CNXPHARMA',group="Local Market indices")
s09 = input.symbol('NSE:CNXMEDIA',group="Local Market indices")
s10 = input.symbol('NSE:CNXENERGY',group="Local Market indices")

// security call to get the  % change w.r.t previous day close 
perc01 = request.security(s01, '', function_get_per_change())
perc02 = request.security(s02, '', function_get_per_change())
perc03 = request.security(s03, '', function_get_per_change())
perc04 = request.security(s04, '', function_get_per_change())
perc05 = request.security(s05, '', function_get_per_change())
perc06 = request.security(s06, '', function_get_per_change())
perc07 = request.security(s07, '', function_get_per_change())
perc08 = request.security(s08, '', function_get_per_change())
perc09 = request.security(s09, '', function_get_per_change())
perc10 = request.security(s10, '', function_get_per_change())

//  Major Asian market indices
a01 = input.symbol('NSE:NIFTY',group="Asian Market indices")
a02 = input.symbol('TVC:HSI',group="Asian Market indices")
a03 = input.symbol('TVC:NI225',group="Asian Market indices")
a04 = input.symbol('KRX:KOSPI',group="Asian Market indices")
a05 = input.symbol('TWSE_DLY:TAIEX',group="Asian Market indices")
a06 = input.symbol('BIST_DLY:XU100',group="Asian Market indices")
a07 = input.symbol('IDX_DLY:COMPOSITE',group="Asian Market indices")
a08 = input.symbol('TVC:STI',group="Asian Market indices")
a09 = input.symbol('BSE_DLY:SENSEX',group="Asian Market indices")
a10 = input.symbol('TFEX:S501!',group="Asian Market indices")

// security call to get the  % change w.r.t previous day close 
pera01 = request.security(a01, '', function_get_per_change())
pera02 = request.security(a02, '', function_get_per_change())
pera03 = request.security(a03, '', function_get_per_change())
pera04 = request.security(a04, '', function_get_per_change())
pera05 = request.security(a05, '', function_get_per_change())
pera06 = request.security(a06, '', function_get_per_change())
pera07 = request.security(a07, '', function_get_per_change())
pera08 = request.security(a08, '', function_get_per_change())
pera09 = request.security(a09, '', function_get_per_change())
pera10 = request.security(a10, '', function_get_per_change())

// Major Ameriacan market indices 
e01 = input.symbol('CURRENCYCOM:US500',group="American Market indices")
e02 = input.symbol('DJ:DJI',group="American Market indices")
e03 = input.symbol('NASDAQ_DLY:NDX',group="American Market indices")
e04 = input.symbol('FX:US2000',group="American Market indices")
e05 = input.symbol('CSE:CSE25',group="American Market indices")
e06 = input.symbol('BMV_DLY:60',group="American Market indices")
e07 = input.symbol('PEPPERSTONE:NAS100',group="American Market indices")
e08 = input.symbol('PEPPERSTONE:US30',group="American Market indices")
e09 = input.symbol('BSE_DLY:SENSEX',group="American Market indices")
e10 = input.symbol('TVC:NI225',group="American Market indices")

// security call to get the  % change w.r.t previous day close 
pere01 = request.security(e01, '', function_get_per_change())
pere02 = request.security(e02, '', function_get_per_change())
pere03 = request.security(e03, '', function_get_per_change())
pere04 = request.security(e04, '', function_get_per_change())
pere05 = request.security(e05, '', function_get_per_change())
pere06 = request.security(e06, '', function_get_per_change())
pere07 = request.security(e07, '', function_get_per_change())
pere08 = request.security(e08, '', function_get_per_change())
pere09 = request.security(e09, '', function_get_per_change())
pere10 = request.security(e10, '', function_get_per_change())


// Major European market indices
m01 = input.symbol('CURRENCYCOM:UK100',group="European Market indices")
m02 = input.symbol('EURONEXT_DLY:N100',group="European Market indices")
m03 = input.symbol('TVC:CAC40',group="European Market indices")
m04 = input.symbol('XETR_DLY:DAX',group="European Market indices")
m05 = input.symbol('SIX_DLY:SMI',group="European Market indices")
m06 = input.symbol('EURONEXT_DLY:BEL20',group="European Market indices")
m07 = input.symbol('EURONEXT_DLY:AEX',group="European Market indices")
m08 = input.symbol('TVC:SX5E',group="European Market indices")
m09 = input.symbol('MOEX:IMOEX',group="European Market indices")
m10 = input.symbol('FOREXCOM:ITA40',group="European Market indices")

// security call to get the  % change w.r.t previous day close 
perm01 = request.security(m01, '', function_get_per_change())
perm02 = request.security(m02, '', function_get_per_change())
perm03 = request.security(m03, '', function_get_per_change())
perm04 = request.security(m04, '', function_get_per_change())
perm05 = request.security(m05, '', function_get_per_change())
perm06 = request.security(m06, '', function_get_per_change())
perm07 = request.security(m07, '', function_get_per_change())
perm08 = request.security(m08, '', function_get_per_change())
perm09 = request.security(m09, '', function_get_per_change())
perm10 = request.security(m10, '', function_get_per_change())


// sum of all local indices if % change greater than zero
sum_if_great_zero = perc01 > 0 ? perc01 : 0 +
                         perc02 > 0 ? perc02 : 0 + 
                         perc03 > 0 ? perc03 : 0 + 
                         perc04 > 0 ? perc04 : 0 + 
                         perc05 > 0 ? perc05 : 0 + 
                         perc06 > 0 ? perc06 : 0 + 
                         perc07 > 0 ? perc07 : 0 + 
                         perc08 > 0 ? perc08 : 0 + 
                         perc09 > 0 ? perc09 : 0 + 
                         perc10 > 0 ? perc10 : 0

// sum of all local indices if % change less than zero
sum_if_less_zero = -(perc01 < 0 ? perc01 : 0 + 
                         perc02 < 0 ? perc02 : 0 + 
                         perc03 < 0 ? perc03 : 0 + 
                         perc04 < 0 ? perc04 : 0 + 
                         perc05 < 0 ? perc05 : 0 + 
                         perc06 < 0 ? perc06 : 0 + 
                         perc07 < 0 ? perc07 : 0 + 
                         perc08 < 0 ? perc08 : 0 + 
                         perc09 < 0 ? perc09 : 0 + 
                         perc10 < 0 ? perc10 : 0)

// sum of all asian indices if % change greater than zero
sum_if_great_zero_asian = pera01 > 0 ? pera01 : 0 + 
                         pera02 > 0 ? pera02 : 0 + 
                         pera03 > 0 ? pera03 : 0 + 
                         pera04 > 0 ? pera04 : 0 + 
                         pera05 > 0 ? pera05 : 0 + 
                         pera06 > 0 ? pera06 : 0 + 
                         pera07 > 0 ? pera07 : 0 + 
                         pera08 > 0 ? pera08 : 0 + 
                         pera09 > 0 ? pera09 : 0 + 
                         pera10 > 0 ? pera10 : 0 

// sum of all Asian indices if % change less than zero
sum_if_less_zero_asian = -(pera01 < 0 ? pera01 : 0 + 
                         pera02 < 0 ? pera02 : 0 + 
                         pera03 < 0 ? pera03 : 0 + 
                         pera04 < 0 ? pera04 : 0 + 
                         pera05 < 0 ? pera05 : 0 + 
                         pera06 < 0 ? pera06 : 0 + 
                         pera07 < 0 ? pera07 : 0 + 
                         pera08 < 0 ? pera08 : 0 + 
                         pera09 < 0 ? pera09 : 0 + 
                         pera10 < 0 ? pera10 : 0 )

// sum of all European indices if % change greater than zero
sum_if_great_zero_europe = perm01 > 0 ? perm01 : 0 + 
                         perm02 > 0 ? perm02 : 0 + 
                         perm03 > 0 ? perm03 : 0 + 
                         perm04 > 0 ? perm04 : 0 + 
                         perm05 > 0 ? perm05 : 0 + 
                         perm06 > 0 ? perm06 : 0 + 
                         perm07 > 0 ? perm07 : 0 + 
                         perm08 > 0 ? perm08 : 0 + 
                         perm09 > 0 ? perm09 : 0 + 
                         perm10 > 0 ? perm10 : 0 

// sum of all European indices if % change less than zero
sum_if_less_zero_europe = -(perm01 < 0 ? perm01 : 0 + 
                         perm02 < 0 ? perm02 : 0 + 
                         perm03 < 0 ? perm03 : 0 + 
                         perm04 < 0 ? perm04 : 0 + 
                         perm05 < 0 ? perm05 : 0 + 
                         perm06 < 0 ? perm06 : 0 + 
                         perm07 < 0 ? perm07 : 0 + 
                         perm08 < 0 ? perm08 : 0 + 
                         perm09 < 0 ? perm09 : 0 + 
                         perm10 < 0 ? perm10 : 0 )

// sum of all American indices if % change greater than zero
sum_if_great_zero_america = pere01 > 0 ? pere01 : 0 + 
                         pere02 > 0 ? pere02 : 0 + 
                         pere03 > 0 ? pere03 : 0 + 
                         pere04 > 0 ? pere04 : 0 + 
                         pere05 > 0 ? pere05 : 0 + 
                         pere06 > 0 ? pere06 : 0 + 
                         pere07 > 0 ? pere07 : 0 + 
                         pere08 > 0 ? pere08 : 0 + 
                         pere09 > 0 ? pere09 : 0 + 
                         pere10 > 0 ? pere10 : 0 

// sum of all American indices if % change less than zero
sum_if_less_zero_america = -(pere01 < 0 ? pere01 : 0 + 
                         pere02 < 0 ? pere02 : 0 + 
                         pere03 < 0 ? pere03 : 0 + 
                         pere04 < 0 ? pere04 : 0 + 
                         pere05 < 0 ? pere05 : 0 + 
                         pere06 < 0 ? pere06 : 0 + 
                         pere07 < 0 ? pere07 : 0 + 
                         pere08 < 0 ? pere08 : 0 + 
                         pere09 < 0 ? pere09 : 0 + 
                         pere10 < 0 ? pere10 : 0 )

// % calculation and signal smooting options
plot_final_pos =use_sma? ta.sma(sum_if_great_zero / (sum_if_great_zero + sum_if_less_zero) * 100,slow_length):sum_if_great_zero / (sum_if_great_zero + sum_if_less_zero) * 100
plot_final_pos_asian =use_sma? ta.sma(sum_if_great_zero_asian / (sum_if_great_zero_asian + sum_if_less_zero_asian) * 100,slow_length):sum_if_great_zero_asian / (sum_if_great_zero_asian + sum_if_less_zero_asian)*100
plot_final_pos_europe =use_sma? ta.sma(sum_if_great_zero_europe / (sum_if_great_zero_europe + sum_if_less_zero_europe) * 100,slow_length):sum_if_great_zero_europe / (sum_if_great_zero_europe + sum_if_less_zero_europe) * 100
plot_final_pos_america =use_sma? ta.sma(sum_if_great_zero_america / (sum_if_great_zero_america + sum_if_less_zero_america) * 100,slow_length):sum_if_great_zero_america / (sum_if_great_zero_america + sum_if_less_zero_america) * 100
average_all=use_sma?ta.sma(math.avg(plot_final_pos,plot_final_pos_asian,plot_final_pos_europe),slow_length):math.avg(plot_final_pos,plot_final_pos_asian,plot_final_pos_europe)

// bg color if at day change
bgcolor(ta.change(time('D')) ? color.new(color.red,90) : na, title="Session change")


//plots
plot(series=plot_Local?plot_final_pos:na, style=plot.style_circles, color=color.new(color.green, 0), linewidth=2,title="Local Market circle")
plot(series=plot_Local?plot_final_pos:na, style=plot.style_line, color=color.new(color.green, 0), linewidth=1,title="Local Market Trend line")

plot(series=plot_asian?plot_final_pos_asian:na, style=plot.style_circles, color=color.new(color.lime, 0), linewidth=2,title="Asian  Market Trend line")
plot(series=plot_asian?plot_final_pos_asian:na, style=plot.style_line, color=color.new(color.lime, 0), linewidth=1,title="Asian Market Trend line")

plot(series=plot_european?plot_final_pos_europe:na, style=plot.style_circles, color=color.new(#8AFF8A, 0), linewidth=2,title="European  Market Trend line")
plot(series=plot_european?plot_final_pos_europe:na, style=plot.style_line, color=color.new(#8AFF8A, 0), linewidth=1,title="European  Market Trend line")

plot(series=plot_american ?plot_final_pos_america:na, style=plot.style_circles, color=color.new(#8AFF8A, 0), linewidth=2,title="American  Market Trend line")
plot(series=plot_american ?plot_final_pos_america:na, style=plot.style_line, color=color.new(#8AFF8A, 0), linewidth=1,title="American  Market Trend line")

plot(series=plot_all?average_all:na, style=plot.style_circles, color=color.new(#488214, 0), linewidth=2,title="Average  Market Trend line")
plot(series=plot_all?average_all:na, style=plot.style_line, color=color.new(#488214, 0), linewidth=1,title="Average All four Market Trend line")

hline(80,"Upper line",color=color.green)
hline(20,"Lower line",color=color.red)

// lable with values
if barstate.islast and plot_Local
    l1=label.new(bar_index, plot_final_pos,'                                                      Local Market Trend ' + str.tostring(math.round(plot_final_pos,2)),color=color.new(color.red,100),textcolor=color.new(color.green,0))
    label.delete(l1[1])
if barstate.islast and plot_asian
    l2=label.new(bar_index,plot_final_pos_asian,   '                                                      Asian Market Trend ' + str.tostring(math.round(plot_final_pos_asian,2)),color=color.new(color.red,100),textcolor=color.new(color.lime,0))
    label.delete(l2[1])
if barstate.islast and plot_european
    l3=label.new(bar_index, plot_final_pos_europe,  '                                                      Europe Market Trend ' + str.tostring(math.round(plot_final_pos_europe,2)),color=color.new(color.red,100),textcolor=color.new(#8AFF8A,0))
    label.delete(l3[1])
if barstate.islast and plot_american 
    l5=label.new(bar_index, plot_final_pos_america,  '                                                      American Market Trend ' + str.tostring(math.round(plot_final_pos_europe,2)),color=color.new(color.red,100),textcolor=color.new(#9CBA7F,0))
    label.delete(l5[1])
if barstate.islast and plot_all
    l4=label.new(bar_index, average_all,  '                                                      Average All four Market Trend ' + str.tostring(math.round(average_all,2)),color=color.new(color.red,100),textcolor=color.new(#488214,0))
    label.delete(l4[1])
