ValueError: Long orders require: SL < LIMIT < TP
ValueError: Long orders require: SL < LIMIT < TP
Loading saved threads...
Zeeshan Ansari · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Zeeshan Ansari
Original post: https://stackoverflow.com/questions/75962157
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
This error message appears when I run my code:
I am writing code for a bot in Python, but when I run it, I get the ValueError exception. This is limiting me from increasing the take profit, which I don't need. I want to put a lesser value for the take profit, but it is not allowing me. Could anyone please help me solve this issue? Here is the code that I am writing:
def support_trade_level(self, support, resistance, lower_support):
if self.close <= support + self.buy_point1 and self.close >= support - self.buy_point2:
self.buy(sl= support - self.slo_buy_point, tp= resistance - self.tp_buy_point)
I know that increasing the take profit value will solve the problem, but that is a big issue for me. I need to solve it without increasing the take profit value.
Also, could anyone help me understand why the take profit value I enter at the time of position opening is considered a LIMIT?
Quote
Report
Sadabadi · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Sadabadi
Original post: https://stackoverflow.com/a/79627143
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I had a similar problem to yours.
The reason for this is a calculation error. The problem was that I had not correctly transferred the values received for calculating the take profit and the stop loss to the function. My code was as follows, which made the same error as yours; a constant value was considered for the entire loop.
from backtesting import Strategy
class MyStrategy(Strategy):
def init(self):
super().init()
self.signal1 = self.I(Signal)
def next(self):
super().next()
if self.signal1==1 and len(self.trades) == 0:
sl = data['fibLow'].iloc[-1]
tp = data['Close'].iloc[-1] + (data['Close'].iloc[-1] - sl)
self.buy(sl=sl, tp=tp, size=0.1)
elif self.signal1==-1 and len(self.trades) == 0:
sl = data['fibHigh'].iloc[-1]
tp = data['Close'].iloc[-1] - (sl - data['Close'].iloc[-1])
self.sell(sl=sl, tp=tp, size=0.1)
I modified the code as follows, and the problem was solved. In fact, I added a
self
prefix to the beginning of each value related to calculating the take profit and stop loss, so that, this value was considered correctly for each row calculation.
from backtesting import Strategy
class MyStrategy(Strategy):
def init(self):
super().init()
self.signal1 = self.I(Signal)
def next(self):
super().next()
if self.signal1==1 and len(self.trades) == 0:
sl = self.data['fibLow'][-1]
tp = self.data['Close'][-1] + (self.data['Close'][-1] - sl)
self.buy(sl=sl, tp=tp, size=0.1)
elif self.signal1==-1 and len(self.trades) == 0:
sl = self.data['fibHigh'][-1]
tp = self.data['Close'][-1] - (sl - self.data['Close'][-1])
self.sell(sl=sl, tp=tp, size=0.1)
So your problem is the
support
and
resistance
variables are fixed for all rows of your data. If you change them so that each row uses the same value for calculations, the problem will be solved. Since you didn't upload your entire code structure, it's not possible to tell exactly how to do this, but you can definitely do a similar solution to mine.
To give you a better idea, my method for sending values to the backtest function is as follows. In fact, I have standardized the data columns I want to send to the backtest function in the following way and then used them in the way shown above.
data = data.loc[:,["open", "high", "low", "close", "tick_volume", "position", "fibLow", "fibHigh"]]
data.reset_index(inplace=True)
data.columns = ['Local time', 'Open', 'High', 'Low', 'Close', 'Volume', 'signal', "fibLow", "fibHigh"]
data.index = pd.DatetimeIndex(data['Local time'])
Quote
Report
Post Reply
Checking account access…