'Why Profit Target is not activated?
I have the following code, and I am trying when I have a trade signal to sell at a specific price or a specific stop loss.
But Trandingview always sells at 1.3% up and not at 4%, do you have any idea why this is happening?
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mpempi
//@version=5
strategy("Strategy HOLD", overlay = true)
rsi = ta.rsi(close, 14)
inTrade = strategy.position_size > 0
notInTrade = strategy.position_size <= 0
timeperiod = time >= timestamp(syminfo.timezone, 2021, 01, 01)
active = rsi > 70
active_close = close < ma200
buy_price = ta.valuewhen(notInTrade, close[1] + close[1] * 4 / 100, 1)
stop_price = ta.valuewhen(notInTrade, close[1] - close[1] * 1 / 100, 1)
if timeperiod and active
strategy.entry('long', strategy.long)
strategy.exit('long', profit = buy_price, stop = stop_price)
bgcolor(activate_long ? color.new(color.green, 50): na)
bgcolor(activate_buy ? color.new(color.purple, 50): na)
plot(buy_price, color = color.new(color.green, 0))
plot(stop_price, color = color.new(color.yellow, 0))
Solution 1:[1]
You calculations seem off.
Also, profit argument of the strategy.exit() function expect the target in ticks.
If you want to have a TP at 4% and SL at 1%, use the following template.
tp_per = input.float(4.0, "TP %", minval=0.0, step=0.1) * 0.01
sl_per = input.float(1.0, "TP %", minval=0.0, step=0.1) * 0.01
tp_price = strategy.position_avg_price * (1 + tp_per)
sl_price = strategy.position_avg_price * (1 - sl_per)
if timeperiod and active
strategy.entry('long', strategy.long)
if (strategy.position_size > 0)
strategy.exit('long', limit=tp_price, stop=sl_price)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
