Page 1 of 1

Problem with calculation of SL

Posted: Thu Dec 01, 2022 7:29 pm
by lineer
I am fairly new to Pine script and have already created an automated system.
But the problem is with calculating SL. With TP I have no problem, it is always precise but SL is always way off what it should be.

My logic for SL, if I am going short the SL will be high[1] + SL for particular pair. Same for long with low[1] - SL. But I need an exact number of pips so I am doing (high[1] - low[1]) + SL. I do not know why, it calculating just wrong.

This is an example of my script. Thank you

i_stopLoss = switch syminfo.ticker
"CADJPY" => 18
"GBPCHF" => -10
"USDJPY" => 9
"GBPUSD" => 2
=> -2

commision = switch syminfo.ticker
"CADJPY" => 2
"GBPCHF" => 4
"USDJPY" => 1
"GBPUSD" => 2
=> 2

i_tp = switch syminfo.ticker
"CADJPY" => 80
"GBPCHF" => 95
"USDJPY" => 104
"GBPUSD" => 105
=> 95

ratio = switch syminfo.ticker
"USDJPY" => 100
"CADJPY" => 100
"GBPCHF" => 10000
"GBPUSD" => 10000
=> 10000

TP = i_tp
SL = i_stopLoss + commision

difference = (high[1] - low[1]) * ratio
total_sl = difference + SL

if something
strategy.entry('Buy Stop', strategy.long, stop=high, oca_name='Breakout', oca_type=strategy.oca.cancel, alert_message = "buy," + str.tostring(syminfo.ticker) + ".r,risk=" + str.tostring(risk) + ",sl=" + str.tostring(total_sl) + ",tp=" + str.tostring(TP))
strategy.entry('Sell Stop', strategy.short, stop=low, oca_name='Breakout', oca_type=strategy.oca.cancel, alert_message = "sell," + str.tostring(syminfo.ticker) + ".r,risk=" + str.tostring(risk) + ",sl=" + str.tostring(total_sl)+ ",tp=" + str.tostring(TP))