Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

pine script - How exactly does trail_price, trail_offset work in pinescript?

Thank you all for your help so far. I have been coding many different strategies in pinescript so far, and I have read through pinescript manual and many google articles, but I am still confused as to how trailing stop work in pinescript.

For instance, for strategy.exit, I have a trail_price that marks the entry for trailing stop to activate. However, all my backtesting indicates that the trailing stops at the high of that specific candle bar, even if the trail_offset has not been hit. Is it just due to the fact that tradeview backtesting assumes that maximum profit is reached in one candle bar, even if subsequent candle bars continue to go in the direction you are aiming for?

For instance, here is an example of my strategy.exit. Strategy.exit("long_TP", "long", trail_price = entry_price + ATR, trail_offset = ATR, stop= entry_price - ATR). I noticed that I would earn 2x to 3x the trail_offset (in this case based off ATR, i.e., if ATR is 50 pips, I would earn 100 or even 150 pips), as long as the profit is taken before the close of that specific candle bar. Any subsequent candle bars, even if going long, and even if the trail_offset stop loss is not hit, is not taken into calculation (i.e., even if my ATR is 50 pips, I might earn 70 pips when the candle bar closes, even if subsequent candle bars continue to go long).

Are my assumptions incorrect (i.e., my code), or is this simply a limit of backtesting, since the program cannot know what is going inside the candle bar and only know the high, low, open, and close? However, I do wonder about this, because sometimes the trail_offset is not reached even with the low of the candle bar, so theoretically profits should continue to accumulate, not stopped out after the candle bar has closed.

Edit: I have added some more info for clarification- Here is a sample code with some explanation:

If condition == true
long = strategy.position_size[0] > strategy.position_size[1]  //go long if there is order entry
entry_price_long = valuewhen(long, open, 0) //entry price is the opening price, based off the closing price of the previous candle if condition is fulfilled
atr_long = valuewhen(long, atr, 0) //stop loss and 1st take profit based off the number of pips depending on average true range, period 14
long_TP = entry_price_long + atr_long //1st take profit limit
long_SL = entry_price_long - atr_long //stop loss

strategy.entry("long", strategy.long, when=go_long, comment="long", qty=positionSize) //enter a long position when condition fulfilled
if strategy.position_size[0] > strategy.position_size[1]
   strategy.exit("long_TP", "long", trail_price=long_TP, trail_offset=atr_long, stop=long_SL) //this is where I am confused. 

My strategy.exit states that long position is exited if the initial stop loss is hit. But if the market goes long as hoped, trailing stop is activated when the 1st take profit limit is hit, defined by the trail_price. The trail_offset (in # of pips) is based off the ATR. So if the trail_price is hit, profit should continually be taken with a trailing stop. But what happens in reality is that profit is taken as far as the high of that specific candle where I entered the trade. I have attached a picture for reference. Reference pic In the picture, we see that the 1st profit limit was reached and thus trailing is activated. The ATR was around 150 pips, so the distance from the entry price to the profit limit is around 150 pips. The trailing stop is set as ATR, so once the 1st profit limit is reached (profit=150pips), theoretically the trade should continue to take profit until the trailing stop is hit. But in the picture we see that in reality my position exited once the high of the candle is hit, without taking any further profits despite the continual uptrend (final profit=181pips). Why is this?

Thanks again for helping out. Thomas

question from:https://stackoverflow.com/questions/65839439/how-exactly-does-trail-price-trail-offset-work-in-pinescript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...