MACD 50x Leveraged Short Strategy with Real Equity
// 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 SHORT STRATEGY REAL EQUITY WITH 50X LEVERAGE",shorttitle = "MACD SHORT 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 = 3)

// 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))


// STRATEGY COMPONENTS
// SL values

// STOP VALUE ==> 
//The 50x leverage explodes at just 2% above the current entry price and the current position size melts.
//2% below the entry price is determined as the liquidation point so that the transaction is guaranteed.

stop_value = valuewhen(strategy.position_size != 0 and strategy.position_size[1] == 0,strategy.position_avg_price * 1.02 , 0)
//

// 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

// STRATEGY

if (crossunder(delta, 0) and term)
	strategy.entry("MacdSE",strategy.short,qty = possize)
if (crossover(delta, 0) and term)
	strategy.close("MacdSE", true, comment="CLOSE")

if(high >= stop_value and delta < 0  and term )

    strategy.close("MacdSE",true)

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


// REAL LEVERAGE SIMULATION --- 

_longmovement = strategy.position_size != 0 and strategy.position_size[1] == 0 
_exitmovement = strategy.position_size[1] != 0 and strategy.position_size == 0

// MODE : Indicating a situation for winners and losers, just like in the martingale system, to know if the strategy is losing or gaining.

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

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

_b = valuewhen(_longmovement,strategy.position_avg_price,0)
_s = valuewhen(_exitmovement,open,0)

pips = ((_s - _b) / _b) * 100  
//If we are in profit, how much will this affect all of our capital with leverage?
//If we are already at a loss, all of our position size will be gone.
// Now let's put this into expression : 

                                       
capital := _longmovement ? nz(capital[1], 0) + chg : _exitmovement and mode == 1 ? (capital[1] + chg) + ((-chg * leverage * pips)/100) : 
           _exitmovement and mode != 1 ? capital[1] : capital[1]

// NET CAPITAL
capita = 100 + capital
float netcapital = na
netcapital := netcapital[1] <= 0 ? 0 : capita

// Now, if we are ready, it's time to see the facts:
//If we had made a strategy using 1% position size and 50x leverage, that is, our entry point would be less than 2% liquidation point, the change in our capital would be in the form of a blue-colored area.
//The result are still not good.
// The first moment we switch to - is the moment we run out of cash and the game is over.
// NOTE:  It is recommended to use as chart bottom indicator.

plot(netcapital , color = color.teal , linewidth = 2)
