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

Sell Limit to close an open long not open a short #1000

Answered by kernc
eervin123 asked this question in Q&A
Discussion options

I'm struggling with what might be an obvious thing to many.

If I have multiple trades (long only) and now I want to enter a sell limit for all of them to close them out at a specified price. (Basically I'm looking to set a self.position.close(limit=XXX), but close doesn't offer the limit feature. However, if I enter a self.sell(size=self.position.size, limit = XXX) I'm not sure I'm getting the desired result.

Is there a simple way to do this or do I need to loop through all the open trades and set the tp = XXX?

You must be logged in to vote

However, if I enter a self.sell(size=self.position.size, limit = XXX) I'm not sure I'm getting the desired result.

As long as assert self.position.size > 0, self.sell() like this looks reasonable. What result do you get?

Replies: 4 comments

Comment options

However, if I enter a self.sell(size=self.position.size, limit = XXX) I'm not sure I'm getting the desired result.

As long as assert self.position.size > 0, self.sell() like this looks reasonable. What result do you get?

You must be logged in to vote
0 replies
Answer selected by eervin123
Comment options

Okay then I think it has something to do with my logic because it is popping an AssertionError. Perhaps I'm placing an order in the same bar that I'm calculating the position size (which doesn't account for that order), and it is putting me into a short position. I'll debug and come back with answers or questions. :) Thanks for the quick response.

You must be logged in to vote
0 replies
Comment options

looping back on this. I had some take profits and stop losses in my opening trades, this was causing the problem because that created multiple sell orders sitting waiting in the order book. The sell(limit) wasn't contingent on anything so it wouldn't get canceled if a tp was hit. then I would have a short order sitting in the order book.

You must be logged in to vote
0 replies
Comment options

So how did you end up solving this? I have a similar use case where I have both a tp and sl that are dynamic and are of different size, so I cant use the build in tp and sl arguments. Anyway to force the sell orders to be contingent?

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

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