Reputation: 3
I'm trying to write a pinescript strategy on tradingview. I have a problem setting a limit exit profit target in percentage.
Hypothesis
Create a long in a determined condition, and set an exit with a limit order with take profit at 3%.
entry_id = str.tostring(bar_index)
long_profit_percent = input.float(3, title="Long Take Profit (%)", minval=0.0, step=1)/100
profit_target = strategy.position_avg_price * (1 + long_profit_percent)
strategy.entry(entry_id, strategy.long, comment = "long:"+entry_id)
strategy.exit(entry_id, entry_id, limit=profit_target,comment_profit="sell:"+entry_id)
Result
In the strategy tester I see the long not closing at 3% of profit, here is an example:
Here the long and exit, in the trades table
Entry price: 47.66
Close price: 47:66
What am I not seeing? I don't understand what I'm doing wrong. All the entries have the same behavior; the open and close price are the same.
I'm trying to set a long position and set (for every long) a take profit exit by 3% of profit.
My code:
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//
//@version=5
strategy("test", overlay=true, process_orders_on_close = true,initial_capital = 1000,default_qty_type = strategy.percent_of_equity,default_qty_value = 25,close_entries_rule = "ANY",pyramiding = 4,commission_type = format.percent, commission_value = 0.08, calc_on_every_tick = true)
fast_sma = ta.sma(close, 14)
slow_sma = ta.sma(close, 28)
long_condition = ta.crossover(fast_sma, slow_sma)
plot(fast_sma,color=color.blue)
plot(slow_sma,color=color.red)
entry_id = str.tostring(bar_index)
long_profit_percent = input.float(3, title="Long Take Profit (%)", minval=0.0, step=1)/100
profit_target = strategy.position_avg_price * (1 + long_profit_percent)
if (long_condition)
strategy.entry(entry_id, strategy.long, comment = "long:"+entry_id)
strategy.exit(entry_id, entry_id, limit=profit_target,comment_profit="sell:"+entry_id)
These are my table entries
These the candelstick signals:
It is as if all the trades are opening and closing in the same moment, at the same price, as I mentioned in the above images.
Upvotes: 0
Views: 642
Reputation: 21294
You have close_entries_rule = "ANY"
in your strategy()
. That means, you need to specify an exit for each trade individually.
You are calculating your TP with strategy.position_avg_price
. And then using that variable in strategy.exit()
under the same condition that triggers the entry.
However, strategy.position_avg_price
will be na
until you are officially in a trade.
The problem is, for the very first entry, there is no exit()
associated with it.
When long_condition
is true
for the very first trade, you call strategy.entry()
to enter the trade and right after that you call strategy.exit()
with profit_target
as your TP. At this point, profit_target
is na
because strategy.position_avg_price
is na
because you are not yet in a trade.
Due to this reason, the very first trade stays open throughout the bactest period. This price is very likely to be very low compared to current price. Therefore, it effects the average price for the next orders. strategy.position_avg_price
will still have the price from the very first entry which is probably quite low and causes your TP to be very low which also triggers your TP almost immediately.
You have pyramiding
and close_entries_rule = "ANY"
in your strategy. So, how you want to fix this is up to your strategy.
For a quick fix, you can use:
profit_target = close * (1 + long_profit_percent)
This will use the entry price for the trade in the TP calculation.
Upvotes: 0