Zazzamira 50-25-25 Trend System
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Hiubris_Indicators

//@version=5
strategy(title = "Zazzamira 50-25-25 Trend System", overlay = true, default_qty_value = 4, initial_capital=100000,default_qty_type=strategy.fixed, pyramiding=0, process_orders_on_close=true, max_lines_count=500)

// Open Session
tz0 = input.string('UTC-5', title="Timezone", inline='tz', group='Time-Range Settings')
auto_tz = input(true, title="Exchange Timezone", inline='tz', group='Time-Range Settings')
tz = auto_tz? syminfo.timezone : tz0

timeinput = input.session('0830-0915', title="Opening Range", group='Time-Range Settings') + ':1234567'
tses_time = time(timeframe.period, timeinput, tz)
tses = na(tses_time) ? 0 : 1
end_t = tses[1] and not tses
start_t = tses and not tses[1]

// Trading Session
session = input.session("0915-1300", title="Trading Time")+":1234567"
t = time(timeframe.period, session, tz)
trading_session_filter = na(t) ? 0 : 1

t_exit = time(timeframe.period, "1500-0000:1234567", tz)
trading_session_exit = na(t_exit) ? 0 : 1



// Highs Lows - Open sesssion
h = 0.0
l = 0.0
h := start_t ? high : tses and high > h[1] ? high : h[1]
l := start_t ? low : tses and low < l[1] ? low : l[1]

r = h-l

last_start_n = ta.valuewhen(start_t, bar_index, 0)

//col = input.color(color.orange, title="Levels Color")

draw_level(txt, x, col)=>
    if start_t
        line.new (bar_index-4, x, last_start_n[1], x, color=col)
        label.new(bar_index-10, x, style=label.style_none, text=txt, color=col, textcolor=col)

l1 = h+r*0.216
l2 = h+r*0.618
l3 = h+r*1
l4 = h+r*1.618

s1 = l-r*0.216
s2 = l-r*0.618
s3 = l-r*1
s4 = l-r*1.618

plot(tses? na : h, style=plot.style_linebr, title="H", color=color.green)
plot(tses? na : l, style=plot.style_linebr, title="L", color=color.green)

plot(tses? na : l1, style=plot.style_linebr, title="Fib 1", color=color.orange)
plot(tses? na : l2, style=plot.style_linebr, title="Fib 2", color=color.orange)
plot(tses? na : l3, style=plot.style_linebr, title="Fib 3", color=color.orange)
plot(tses? na : l4, style=plot.style_linebr, title="Fib 4", color=color.orange)
plot(tses? na : s1, style=plot.style_linebr, title="Fib 1", color=color.orange)
plot(tses? na : s2, style=plot.style_linebr, title="Fib 2", color=color.orange)
plot(tses? na : s3, style=plot.style_linebr, title="Fib 3", color=color.orange)
plot(tses? na : s4, style=plot.style_linebr, title="Fib 4", color=color.orange)

//draw_level('H'     , h[1], color.gray  )
//draw_level('Fib 23.6% ' , l1, color.yellow)
//draw_level('Fib 61.8% ' , l2, color.orange)
//draw_level('Fib 100% '  , l3, color.orange)
//draw_level('Fib 161.8% ', l4, color.orange)

//draw_level('H'     , l[1], color.gray  )
//draw_level('Fib 23.6% ' , s1, color.yellow)
//draw_level('Fib 61.8% ' , s2, color.orange)
//draw_level('Fib 100% '  , s3, color.orange)
//draw_level('Fib 161.8% ', s4, color.orange)

bgcolor(tses?color.new(color.orange  , 80):na)

plotshape(ta.crossover (close, h) and not tses and trading_session_filter, textcolor=color.lime, color=color.lime, style=shape.triangleup , title="Cross", text="" , location=location.belowbar,  offset=0, size=size.tiny)
plotshape(ta.crossunder(close, l) and not tses and trading_session_filter, textcolor=color.red,  color=color.red, style=shape.triangledown, title="Cross", text="", location=location.abovebar,  offset=0, size=size.tiny)

// ————— MA 
ribbon_grp = "MA SETTINGS"
ma(source, length, type) =>
    type == "SMA" ? ta.sma(source, length) : 
     type == "EMA" ? ta.ema(source, length) :
     type == "SMMA (RMA)" ? ta.rma(source, length) : 
     type == "WMA" ? ta.wma(source, length) :
     type == "VWMA" ? ta.vwma(source, length) :
     na
    
ma1_type   = input.string("EMA"  , ""     , inline="MA #1", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group=ribbon_grp)
ma1_source = input.source(close  , ""     , inline="MA #1", group=ribbon_grp)
ma1_length = input.int   (5    , ""     , inline="MA #1", minval=1, group=ribbon_grp)
ma1 = ma(ma1_source, ma1_length, ma1_type)
plot(ma1, color = color.green, title="MA №1")

ma2_type   = input.string("EMA"  , ""     , inline="MA #2", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group=ribbon_grp)
ma2_source = input.source(close  , ""     , inline="MA #2", group=ribbon_grp)
ma2_length = input.int   (13     , ""     , inline="MA #2", minval=1, group=ribbon_grp)
ma2 = ma(ma2_source, ma2_length, ma2_type)
plot(ma2, color = color.orange, title="MA №2")

use_3 = input(true, title='Use 3rd MA as Entry Condition')
ma3_type   = input.string("EMA"  , ""     , inline="MA #3", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group=ribbon_grp)
ma3_source = input.source(close  , ""     , inline="MA #3", group=ribbon_grp)
ma3_length = input.int   (34     , ""     , inline="MA #3", minval=1, group=ribbon_grp)
ma3 = ma(ma3_source, ma3_length, ma3_type)
plot(ma3, color = color.blue, title="MA №3")

ma4_type   = input.string("EMA"  , ""     , inline="MA #4", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group=ribbon_grp)
ma4_source = input.source(close  , ""     , inline="MA #4", group=ribbon_grp)
ma4_length = input.int   (50    , ""     , inline="MA #4", minval=1, group=ribbon_grp)
ma4 = ma(ma4_source, ma4_length, ma4_type)
plot(ma4, color = color.red, title="MA #4")

ma_long = ma1>ma2 and ma2>ma3 and ma3>ma4
ma_short= ma1<ma2 and ma2<ma3 and ma3<ma4

long0 = ma_long  and trading_session_filter and high<l1 and ta.crossover (close, h)
short0= ma_short and trading_session_filter and low >s1 and ta.crossunder(close, l)

long_setup = 0 
long_setup:= high>l1? 0 : long0? 1 : long_setup[1]

long = low<h and long_setup[1]==1

if long
    long_setup := 0

short_setup = 0 
short_setup:= low<s1? 0 : short0? 1 : short_setup[1]

short = high>l and short_setup[1]==1

if short
    short_setup := 0

retest = input(true, title='Use RETEST')

longf = retest? long  : long0 
shortf= retest? short : short0

// Position Management Tools
pos = 0.0
pos:= longf? 1 : shortf? -1 : pos[1]

longCond  = longf  and (pos[1]!= 1 or na(pos[1]))
shortCond = shortf and (pos[1]!=-1 or na(pos[1]))



// EXIT FUNCTIONS //
atr = ta.atr(14)

sl  = input.float(1.0, title="Initial Stop Loss ATR", minval=0, step=0.1)

atr_long  = ta.valuewhen(longCond , atr, 0)
atr_short = ta.valuewhen(shortCond, atr, 0)

tp_long  = l3
tp_short = s3

// Inprofit
inprofit_long  = 0
inprofit_short = 0
inprofit_long  := pos==0 or longCond ? 0 : high>l1[1]? 1 : inprofit_long [1]
inprofit_short := pos==0 or shortCond? 0 : low <s1[1]? 1 : inprofit_short[1]

inprofit_long2  = 0
inprofit_short2 = 0
inprofit_long2  := pos==0 or longCond ? 0 : high>l2[1]? 1 : inprofit_long2 [1]
inprofit_short2 := pos==0 or shortCond? 0 : low <s2[1]? 1 : inprofit_short2[1]

// Trailing Stop Loss
trail_long = 0.0, trail_short = 0.0
trail_long  := long0 ? h : longCond ? high : high>trail_long[1]? high : pos<1 ? 0  : trail_long[1]
trail_short := short0? l : shortCond? low  : low<trail_short[1]? low : pos>-1 ? 99999  : trail_short[1]
trail_long_final1   = trail_long  - sl * atr_long 
trail_short_final1  = trail_short + sl * atr_short
trail_long_final2   = trail_long  - r * 0.236
trail_short_final2  = trail_short + r * 0.236
trail_long_final3   = trail_long  - r * 0.382
trail_short_final3  = trail_short + r * 0.382

sl_long  = inprofit_long2 ? trail_long_final3  : inprofit_long ? trail_long_final2  : trail_long_final1
sl_short = inprofit_short2? trail_short_final3 : inprofit_short? trail_short_final2 : trail_short_final1

// Position Adjustment
long_sl  = low <sl_long[1]  and pos[1]== 1
short_sl = high>sl_short[1] and pos[1]==-1

final_long_tp  = high>tp_long[1]  and pos[1]== 1
final_short_tp = low <tp_short[1] and pos[1]==-1

if ((long_sl or final_long_tp) and not shortCond) or ((short_sl or final_short_tp) and not longCond)
    pos:=0
    
//  Strategy Backtest Limiting Algorithm
i_startTime = input.time(defval = timestamp("01 Sep 2002 13:30 +0000"), title = "Backtesting Start Time")
i_endTime   = input.time(defval = timestamp("30 Sep 2099 19:30 +0000"), title = "Backtesting End Time"  )
timeCond   = (time > i_startTime) and (time < i_endTime)

equity = strategy.initial_capital + strategy.netprofit

if equity>0 and timeCond
    if long0
        strategy.entry("long" , strategy.long , limit=h)
    if short0
        strategy.entry("short", strategy.short, limit=l)
    
    strategy.exit("SL/TP1", from_entry = "long" , stop=sl_long , limit=l1 , comment_profit ='TP1', comment_loss='SL', qty_percent=50)
    strategy.exit("SL/TP2", from_entry = "long" , stop=sl_long , limit=l2 , comment_profit ='TP2', comment_loss='SL', qty_percent=25)
    strategy.exit("SL/TP3", from_entry = "long" , stop=sl_long , limit=l3 , comment_profit ='TP3', comment_loss='SL')

    strategy.exit("SL/TP1", from_entry = "short", stop=sl_short, limit=s1, comment_profit ='TP1', comment_loss='SL', qty_percent=50)
    strategy.exit("SL/TP2", from_entry = "short", stop=sl_short, limit=s2, comment_profit ='TP2', comment_loss='SL', qty_percent=25)
    strategy.exit("SL/TP3", from_entry = "short", stop=sl_short, limit=s3, comment_profit ='TP3', comment_loss='SL')

if high>l1 or not trading_session_filter
    strategy.cancel('long')
if low<s1 or not trading_session_filter
    strategy.cancel('short')

long_exit = trading_session_exit and pos[1]== 1
short_exit= trading_session_exit and pos[1]==-1

if (long_exit  and not shortCond) or (short_exit and not longCond)
    pos:=0

strategy.close("long" , when=long_exit  and timeCond, comment="Exit")
strategy.close("short", when=short_exit and timeCond, comment="Exit")

// DEBUGGING
bgcolor(long ?color.new(color.green  , 80):na)
bgcolor(short?color.new(color.red    , 80):na)
bgcolor(false?color.new(color.white  , 80):na)
bgcolor(false?color.new(color.orange , 80):na)