Im testing a few strategies and there are a couple of issues I found:
1- trail_price
acts differently on live data even when the offset value is set to a number
2- Although limit
should exit on the defined price, sometimes the exit is at a different price.
This is rather a simple strategy script and should execute with no issues. I've written it to test if the results of trail_price in strategy.exit() are reliable or not. I think this might be a results of an issue in pine script itself or maybe I'm missing something! Here is the code:
//// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RayZa41
//@version=5
strategy(Test Exits
, overlay=true, calc_on_every_tick=true, use_bar_magnifier=true)
size = input.float(2, 'Size')
inp_offset = input(5,'Trail Offset in $ on BTC')
diffs = input(10,'TP')
confEMA1 = ta.ema(close, input.int(5, 'Confirmation EMA Fast'))
confEMA2 = ta.ema(close, input.int(15, 'Confirmation EMA Slow'))
ema_confLong = confEMA1>confEMA2
ema_confShort = confEMA1<confEMA2
plot(confEMA1,'Fast Conf. EMA', color=color.red)
plot(confEMA2,'Slow Conf. EMA',color=color.blue)
long = ema_confLong and strategy.position_size<=0 and close>confEMA1
short = ema_confShort and strategy.position_size>=0 and close<confEMA1
fromMonth = input.int(defval = 12, title = From Month
, minval = 1, maxval = 12, group = Tradinng Period
)
fromDay = input.int(defval = 10, title = From Day
, minval = 1, maxval = 31, group = Tradinng Period
)
fromYear = input.int(defval = 2022, title = From Year
, minval = 1900, group = Tradinng Period
)
hour_ = input.int(defval = 0, title = From Hour
)
min_ = input.int(defval = 0, title = From Minutes
)
start = timestamp('GMT-4',fromYear, fromMonth, fromDay, hour_, min_)
window() => time >= start
if long and strategy.position_size<=0 and window() and barstate.isconfirmed
strategy.entry('Long', strategy.long, size, comment = 'Long')
if short and strategy.position_size>=0 and window() and barstate.isconfirmed
strategy.entry('Short', strategy.short, size, comment = 'Short')
tpl = strategy.position_avg_price+diffs
tps = strategy.position_avg_price-diffs
if strategy.position_size>0
strategy.exit('TPLong', 'Long', strategy.position_size, trail_price= tpl, trail_offset= inp_offset, comment = 'TPLong')
if strategy.position_size<0
strategy.exit('TPShort', 'Short', strategy.position_size, trail_price= tps, trail_offset= inp_offset, comment = 'TPShort')
var myTable = table.new(position.bottom_right, 1, 6, frame_color=color.black)
if barstate.islast
table.cell(myTable, 0, 0, 'Signal: '+(long?'Long':(short?'Short':'None')))
table.cell(myTable, 0, 1, 'Position: '+(strategy.position_size>0?'Long':(strategy.position_size<0?'Short':'None')))
table.cell(myTable, 0, 2, 'Size: '+str.tostring(strategy.position_size))
table.cell(myTable, 0, 3, 'Entry: '+str.tostring(strategy.position_avg_price))
table.cell(myTable, 0, 4, 'TP: '+str.tostring(strategy.position_size>0?tpl:(strategy.position_size<0?tps:0)))
table.cell(myTable, 0, 5, 'Current_price: '+str.tostring(close))
//
it doesnt really matter if calc_on_every_tick is true or false, both result in the issue. Does anyone have a solution/recommendations