I have coded this buy/sell strategy in Pine Editor/TradingView:
buy_condition= a<b sell_condition= a>b strategy.entry("Long", strategy.long, when=buy_condition) strategy.exit("L_Out", "Long", profit=1000, loss=500) strategy.entry("Short", strategy.short, when=sell_condition) strategy.exit("S_Out", "Short", profit=1000, loss=500)
While in a long or short position, it changes position when opposite condition become true, but i want it to keep in position until it hits profit or stop loss. Then it can enter any position that fits again. How can i code this rule?
Advertisement
Answer
Maybe you want to replace your:
buy_condition= a<b sell_condition= a>b
by:
def buy_condition(a, b): return a < b def sell_condition(a, b): return a > b
When you create buy_condition
, it compute the result at this moment, not later.
So if a<b
at this line, it will always be True
later.
If you pass those functions to your strategy.exit
method, you have to call the function with somethink like by_condition(a, b)
.