MACD 50x Leveraged Strategy Real Equity Simulation 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Noldo

//@version=4
strategy(title="MACD STRATEGY REAL EQUITY WITH 50X LEVERAGE",shorttitle = "MACD 50X REAL EQUITY", overlay=true, 
         initial_capital=100, 
         linktoseries = false,
         default_qty_type=strategy.cash, 
         default_qty_value=1, 
         commission_type=strategy.commission.percent, 
         commission_value=0.0,
         calc_on_order_fills = false,
         calc_on_every_tick  = true,
         max_bars_back = 5000,
         pyramiding = 0,
         precision = 0)

// Variables 
src = close 

fromyear = input(2016, defval = 2009, minval = 2000, maxval = 2100, title = "From Year")
toyear = input(2100, defval = 2100, minval = 1900, maxval = 2100, title = "To Year")
frommonth = input(04, defval = 04, minval = 01, maxval = 12, title = "From Month")
tomonth = input(12, defval = 12, minval = 01, maxval = 12, title = "To Month")
fromday = input(01, defval = 01, minval = 01, maxval = 31, title = "From day")
today = input(31, defval = 31, minval = 01, maxval = 31, title = "To day")
term = (time > timestamp(fromyear, frommonth, fromday, 00, 00) and time < timestamp(toyear, tomonth, today, 23, 59))

// LEVERAGE ==> 50X

leverage = 50 

// POSITION SIZE ==> %1 (0.01)

possize = 1

// MACD

fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)
MACD = ema(close, fastLength) - ema(close, slowlength)
aMACD = ema(MACD, MACDLength)
delta = MACD - aMACD

// DEFINITION : TREND

float trend = na 

trend:= delta > 0 ? 1 : -1

// SL values

stop_long = valuewhen(strategy.position_size > 0  and strategy.position_size[1] <= 0 and trend ==  1  ,strategy.position_avg_price * 0.98,0)
stop_short= valuewhen(strategy.position_size < 0  and strategy.position_size[1] >= 0 and  trend == -1 ,strategy.position_avg_price * 1.02,0)

// REAL LEVERAGE SIMULATION --- 


_lmov = strategy.position_size > 0 and strategy.position_size[1] == 0 
_elmov = strategy.position_size[1] > 0 and strategy.position_size == 0

_smov = strategy.position_size < 0 and strategy.position_size[1] == 0 
_esmov= strategy.position_size[1] < 0 and strategy.position_size == 0 

_dlmov = strategy.position_size[1] < 0 and strategy.position_size > 0 
_dsmov = strategy.position_size[1] > 0 and strategy.position_size < 0 


onlylong = barssince(_lmov) < barssince(_dlmov) and _lmov
onlyshort = barssince(_smov) < barssince (_dsmov) and _smov

onlylongexit  = barssince(_elmov) < barssince(_dsmov) and _elmov
onlyshortexit = barssince(_esmov) < barssince(_dlmov) and _esmov

directlong  = barssince(_dlmov) < barssince(_lmov) and _dlmov
directshort = barssince(_dsmov) < barssince(_smov) and _dsmov

// 
float capital = 0.00
chg = change(strategy.position_size)
ch  = strategy.position_size


_l  = valuewhen(strategy.position_size > 0 and strategy.position_size[1] == 0,strategy.position_avg_price,0) // Long after Short or Long Exit
_le = valuewhen(strategy.position_size[1] > 0 and strategy.position_size == 0 ,open,0)

_s  = valuewhen(strategy.position_size < 0 and strategy.position_size[1] == 0,strategy.position_avg_price,0) // Short after Long or Long Exit
_se = valuewhen(strategy.position_size[1] < 0 and strategy.position_size == 0 ,open,0)

_dds = valuewhen(strategy.position_size < 0 and strategy.position_size[1] >= 0,strategy.position_avg_price,0) // Close Short and Long == DirectLong
_ddl = valuewhen(strategy.position_size > 0 and strategy.position_size[1] <= 0,strategy.position_avg_price,0) // Close Long and Short == DirectShort

piplong = ((_le - _l) / _l) * 100 
pipshort= ((_se - _s) / _s) * 100 

pipdl = ((_ddl - _dds) / _dds) * 100  // Direct Long
pipds = ((_dds - _ddl) / _ddl) * 100   // Direct Short

// CONSTRUCTION 

enterLong = ((trend == 1 and term and  trend[1] != 1) or (trend == 1 and term  and trend[1] != 1 and low[1] <= stop_long) or 
             (trend == 1 and term and  trend[1] == 1 and low[1] <= stop_long)) // 2

sLong = low <= stop_long and trend == 1 and strategy.position_size > 0 and term  // 1

enterShort = ((trend == -1 and term and trend[1] != -1) or (trend == -1 and term and trend[1] != -1 and high[1] >= stop_short) or
              (trend == -1 and term and trend[1] == -1 and high[1] >= stop_short)) // -2

sShort = high >= stop_short and trend == -1  and strategy.position_size < 0 and term // -1

float mode = na
mode := change(strategy.wintrades) > 0 ? 1 : change(strategy.losstrades) > 0 ? -1 : nz(mode[1], 1)

pipsize = (abs(chg) - abs(ch)) 

capital := onlylong ? nz(capital[1], 0) - chg : 
           onlylongexit  ? capital[1] : onlyshort ? nz(capital[1], 0) + chg : 
           onlyshortexit ? capital[1] : 
           directlong and mode == 1 ? (capital[1]) - ch  + ((-pipsize * leverage * pipdl)/100) : directlong and mode != 1 ? capital[1] - ch  :
           directshort and mode == 1 ? (capital[1]) + ch + ((pipsize * leverage * pipds)/100)  : directshort and mode != 1 ? capital[1] + ch  : capital[1]
  
// NET CAPITAL
capita = 100 + capital
float netcapital = na
netcapital := netcapital[1] <= 0 ? 0 : capita

// STRATEGY

strategy.entry("Long",strategy.long, comment="LONG" ,when = enterLong , qty = possize)

strategy.entry("Short",strategy.short, comment="SHORT" ,when = enterShort , qty = possize)

if(sLong)

    strategy.close("Long",strategy.long , comment = "SL LONG")

if(sShort )

    strategy.close("Short" , comment = "SL SHORT")


if time > timestamp(toyear, tomonth, today, 23, 59)
    strategy.close_all()

plot(netcapital)
