Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

stop loss = 0 with long orders is throwing a ValueError #1078

Unanswered
shparekh asked this question in Q&A
Discussion options

Hello,
I can't seem to figure out why I get a ValueError when stop loss = 0. I do not intend to use limit orders.

I have following next() in my strategy -

 def next(self):
 super().init()
 price = self.data.Close
 # Long Exit
 if (self.position) and crossover(self.lema, self.lrs):
 # Close any existing long trades, and sell the asset
 self.position.close()
 # Long Entry
 elif (not self.position) and crossover(self.lrs, self.lema) and (price >= self.ema):
 self.buy(sl=(1-self.stop_loss/100)*price)

I am running it with -

 bt = Backtest(df, myStrategy, cash=10000, commission=0, trade_on_close=True, exclusive_orders=False, hedging=False)

with stop_loss = 0, it throws following error -

File ~\AppData\Roaming\Python\Python311\site-packages\backtesting\backtesting.py:713, in _Broker.new_order(self, size, limit, stop, sl, tp, trade)
 711 if is_long:
 712 if not (sl or -np.inf) < (limit or stop or adjusted_price) < (tp or np.inf):
--> 713 raise ValueError(
 714 "Long orders require: "
 715 f"SL ({sl}) < LIMIT ({limit or stop or adjusted_price}) < TP ({tp})")
 716 else:
 717 if not (tp or -np.inf) < (limit or stop or adjusted_price) < (sl or np.inf):
ValueError: Long orders require: SL (0.4557679295539856) < LIMIT (0.4557679295539856) < TP (None)

What am I doing wrong?

Thank you.
Best regards

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /