How to use Leverage and Margin in PineScript
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Peter_O

//@version=4
strategy(title="How to use Leverage and Margin in PineScript", overlay=false, pyramiding=100,
     default_qty_type = strategy.percent_of_equity, default_qty_value=3000,
     margin_long=1./30*50, margin_short=1./30*50)

periodK = input(13, title="K", minval=1)
periodD = input(3, title="D", minval=1)
smoothK = input(4, title="Smooth", minval=1)
k = sma(stoch(close, high, low, periodK), smoothK)
d = sma(k, periodD)
plot(k, title="%K", color=color.blue)
plot(d, title="%D", color=color.orange)
h0 = hline(80)
h1 = hline(20)
fill(h0, h1, color=color.purple, transp=75)

GoLong=crossover(k,d) and k<80
GoShort=crossunder(k,d) and k>20

TP=input(100, title="Take Profit (in ticks)")

strategy.entry("Long", strategy.long, when=GoLong)
strategy.exit("tp_long", from_entry="Long", profit=TP)
strategy.entry("Short", strategy.short, when=GoShort)
strategy.exit("tp_short", from_entry="Short", profit=TP)
