Reputation: 37
I'm testing an example strategy just to learn how to set a specific amount percent of capital to risk in every position and a locked Risk / reward ratio. I got entry, stop and profit correctly but the results of the operations are all wrong.
--- this is my example code ---
//@version=5
strategy("Risk / Reward example", overlay=true, currency=currency.EUR, initial_capital=1000, default_qty_type=strategy.percent_of_equity)
// Capital and risk
var currency = currency.EUR
var capital = strategy.initial_capital
var risk = input.float(1.0, 'Risk per trade', minval=0.5, step=0.5, maxval=100)
var rr = input.float(3.0, 'Risk reward' , minval=1.0, step=0.5)
risk_pt = (capital * risk) / 100
// Currency conversion
opt1 = currency == syminfo.currency ? syminfo.ticker : syminfo.currency + currency
opt2 = currency == syminfo.currency ? 1 : request.security(opt1, "1", close)
// Mintick multiplier
mintick = syminfo.mintick == 0.00001 ? 10 : syminfo.mintick == 0.001 ? 1000 : 1000
// Conditions
entry = close[1] < open[1] and close > high[1] //Entry at every engulfing
val_entry = ta.valuewhen(entry, close, 0) //Valuewhen() function for pip calculation
stop = ta.lowest(low, 20)[1]
// Stop and lot size calculation
pip_fun(_high, _low) =>
res = math.abs((_high - _low) / syminfo.mintick)
res / 10
stopsize = pip_fun(val_entry, stop) //stop size in pip
lot_size = (risk_pt / stopsize / opt2) / mintick //Forex lot size
// SL and TP in tick
tick_sl = stopsize * 10 //Stop size in pip * 10 = stopsize in tick
tick_tp = tick_sl * rr //Stop size in tick * my risk reward input
// Backtest
backtest = time > timestamp(2021, 01, 01, 00,00) and time < timestamp(2021, 12, 31, 23, 59)
// Strategy
if entry and strategy.position_size == 0
strategy.entry('long' , strategy.long, qty=lot_size, when=backtest)
strategy.exit ('exitL', 'long', qty_percent=100, loss=tick_sl, profit=tick_tp)
I show you why it is not working properly:
As you can see, the results in the second column (profitto) are all random numbers. It should always lose 1% of capital when stop loss and gain 3% when take profit.
Upvotes: 0
Views: 2213
Reputation: 11
enter code here
// The Fixed Percent Stop Loss Code
// User Options to Change Inputs (%)
stopPer = input(1, title='percent Stop Loss %', type=input.float, minval = 0.0) / 100
takePer = input(1, title='percent Take Profit %', type=input.float, minval = 0.0) / 100
tsl = input(1.0, title = "trailing stop percent", type=input.float, minval =0.0)
tsl_percent = tsl/100
//detect what was last signal (long or short)
long_short = 0
long_last = longCondition and (nz(long_short[1]) == 0 or nz(long_short[1]) == -1)
short_last = shortCondition and (nz(long_short[1]) == 0 or nz(long_short[1]) == 1)
long_short := long_last ? 1 : short_last ? -1 : long_short[1]
//entry price
longPrice = valuewhen(long_last, close, 0)
shortPrice = valuewhen(short_last, close, 0)
//fixed sltp prices
longStop = longPrice * (1 - stopPer)
shortStop = shortPrice * (1 + stopPer)
longTake = longPrice * (1 + takePer)
shortTake = shortPrice * (1 - takePer)
// //long condition
// if (longCondition)
// strategy.entry("long", strategy.long)
// strategy.close("long")
// strategy.exit("s/p", "long", stop=longStop, limit=longTake)
// strategy.exit("T s/p", "long", trail_points = close * tsl_percent / syminfo.mintick, trail_offset = close * tsl_percent / syminfo.mintick)
// //short condition
// if (shortCondition)
// strategy.entry("short", strategy.short)
// strategy.close("short")
// strategy.exit("s/p", "long", stop=shortStop, limit=shortTake)
// strategy.exit("T s/p", "long", trail_points = close * tsl_percent / syminfo.mintick, trail_offset = close * tsl_percent / syminfo.mintick)
Upvotes: 0