I have coded this buy/sell strategy in Pine Editor/TradingView:
JavaScript
x
8
1
buy_condition= a<b
2
sell_condition= a>b
3
4
strategy.entry("Long", strategy.long, when=buy_condition)
5
strategy.exit("L_Out", "Long", profit=1000, loss=500)
6
strategy.entry("Short", strategy.short, when=sell_condition)
7
strategy.exit("S_Out", "Short", profit=1000, loss=500)
8
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:
JavaScript
1
3
1
buy_condition= a<b
2
sell_condition= a>b
3
by:
JavaScript
1
7
1
def buy_condition(a, b):
2
return a < b
3
4
5
def sell_condition(a, b):
6
return a > b
7
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)
.