-
Notifications
You must be signed in to change notification settings - Fork 851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed positions #111
Closed positions #111
Conversation
Yes sorry, still work-in-progress, I don't think it'll be ready to merge to merge for a few days. Dict are slower than list, and for the closed positions we never need to look up by index, so I think dict doesn't provide anything useful there. My branch is storing closed positions, indexed by the ticker. Has the side-effect of overwriting any closed position if we try to add the same closed position twice, so it really does need to be a Good pickup RE moving open positions over at end of backtest. Would that be so Statistics can access all positions, including current ones? |
Yes I have used Yes |
Thanks @nwillemse I saw you wrote something in the Wiki, I've been meaning to write a CONTRIBUTING.md or something. Basically as a rule of thumb, features should be branched from master on your own fork, set your fork up to test with Travis.ci and ensure test suite passes before doing a PR. Once we have actual Releases the complexity will step up a notch to something like Do you have a branch set up on your fork? Feel free to @ me over there if you need any help with anything. |
@mhallsmoore ready to go. Turned out to be a fairly simple solution. Tested some 10 year backtests on 2,000 odd daily bars and performance was rock solid. Closes #91. Allows #90 to move forward. Primary difference is that we archive closed positions in "Live" value; that is value of currently open positions, is still calculated in @nwillemse -- I felt that merging live & closed positions at the end of a backtest from within |
Great work, everyone. Are we all happy that this is ready to go in? |
@ryankennedyio looks very good. I would like to have a position_id added to the What do you think?
|
Hmmm. What's the benefit there @nwillemse ? If closed positions are stored in Maybe in future if more than one strategy has an open position on the same ticker... Either way, maybe make the change in your branch if it's necessary for your code, then we can review it with respect to your changes :). Just a bit easier to manage, as well as see how/why/alternatives if viewing in context to the rest of the changes an author has made. Once this has been merged into master, it should be pretty easy for you to pull master, then merge master back into your branch. |
@ryankennedyio the benefit will be you can sort the list various ways, by exit_date, entry_date, symbol, and always go back to sort by position_id to get back to the original sequence of the positions taken. Also whenever you store these objects in a database, you always need id's. @mhallsmoore lets merge this and I can test and update my branch. |
I think it is quite unlikely that we'll ever need two simultaneously open positions on the same ticker. I don't see how it would work if we simultaneously longed and shorted an identical quantity of the same ticker in any system. We would be both "in" and "out" of the market (and down on transaction costs!). In Interactive Brokers, for instance, I don't think it is possible to have two separate open positions on the same ticker in the same account. Correct me if I'm wrong though! In fact, this is why I designed the Position class like this in the first place, so that it always tracked the net longed/shorted. I kept thinking about this sort of situation: Time 1: LONG 100 -> NET 100 LONG It gets a bit tricky... |
@mhallsmoore True, was assuming there were some good reasons RE Position design to begin with! I'd be comfortable with the distinction that a position with exactly 0 quantity has been "fully closed". Anything on the same ticker in future would be "New". Anyway, too tired to partake in much more thinking right now! Can get very tricky indeed. Glad we have a decent test suite here to fall back on as we change things, was very helpful in doing this feature. |
@mhallsmoore @ryankennedyio Trading simultaneously the same instrument in opposite directions particularly happen i.e. when mixing different strategies (trend following vs. mean reversion) and timeframes (i.e. daily vs. intraday). A way i previously used (oanda) to avoid the mess of tracking long/short positions within the same account, was to include a strategy id/tag at the entries in the position class, and long/short positions are traded in different accounts. Might this be of use here? I am curious if there is a wiser way of doing the same operation within a single account. |
@ryankennedyio, @nwillemse, @hpsilva: I'm happy to partition them across several accounts or via a strategy ID, as @hpsilva pointed out that it does occur. This makes a lot of sense. However, I don't think it is ever going to be wise to allow two open positions with the same strategy/account and same ticker! :-) We will need to handle this at the Portfolio/PositionSizer level as well, because if two strategies are giving opposing signals we should either a) let them occur because of @hpsilva's scenario or b) just net them out (i.e. a simultaneous long of 100 and short of 50 equals an order of long 50), if only for reducing transaction costs. |
Merged for now, though! |
We might see hedging system (vs netting system) in MT5 Quantopian also have a concept named "round trips" I prefer how MT5 is working |
Ryan, shouldn't self.close_positions be a list of all the close
Positions
classes instead of a dict with tickers?When the position is closed, it should get added to the list. If at the end the backtest the position is still open, it also needs to be appended at the end with status 'open'.