pysystemtrade

pysystem trade is the open source version of my own backtesting engine that implements systems according to the framework outlined in my book "Systematic Trading".

Eventually pysystemtrade will include the following:
  • Backtesting enviroment that will work out of the box for the three examples in "Systematic Trading"
  • Implement all the optimisation and system design principles in the book.
  • Complete implementation of a fully automated system for futures trading (for interactive brokers only), including regularly updated data
  • Code to run the present, and future, examples on my blog (i.e. it will replace systematictradingexamples.
You can get it from the Git hub repo


Relevant blog posts


Original motivating post
Estimating forecast scalars
Instruments weights, correlations, and diversification multipliers
Optimising in the presence of costs
The breakout trading rule
Capital correction

Docker and automated trading systems



A note on support

This is an open source project, designed for people who are already comfortable using and writing python code, are capable of installing the dependencies, and who want a head start on implementing a system of their own. I do not have the time to provide support. Of course I am very happy if you get in touch with me on any of the following topics:


  • Confusing error messages
  • Missing or misleading documentation
  • Suggestions for extra features


However I can't guarantee that I will reply immediately, or at all. If you need that level of support then you are better off with another project. The most efficient way of doing this is by opening an issue on github.

I'll try and incorporate any feedback into the code, but this is a part time venture for me, and it will be competing with my other interests (writing books, blogging and research). But if you occasionally check github you will hopefully find it gradually improving. Offers to contribute will of course be gratefully accepted.


A very quick demo


There is a long demo, and an even longer user guide, in the repo so I don't need to repeat them here. Instead here is how you'd simulate the system in chapter 15 of my book, in three lines:


from systems.provided.futures_chapter15.basesystem import futures_system
from matplotlib.pyplot import show

system = futures_system()

system.accounts.portfolio().sharpe()
system.accounts.portfolio().curve().plot()
show()



The account curve for the system in chapter 15 of my book. Sharpe Ratio 0.48


 





40 comments:

  1. Bought the book from Amazon.co.uk

    Is there a forum for the user/adopter of the accompany tools?

    God blesses!!!

    Best regards,
    Sanyaade

    ReplyDelete
    Replies
    1. I guess this is the forum :-)

      You can also post on any of the threads I started on ET, like http://www.elitetrader.com/et/index.php?threads/oh-no-not-another-python-backtester.296589/
      or

      http://www.elitetrader.com/et/index.php?threads/fully-automated-futures-trading.289589/

      Delete
  2. Hello Robert,

    I've been working through your book (slowly, I might add, at weekends and so on) and actually came along to your recent talk to the MTA held at CMC Markets in Aldgate.

    Thank you for a great text!


    BTW I bought the book on amazon.co.uk ... do I need to register or anything to use the website?

    ReplyDelete
    Replies
    1. Thanks for the kind words. No registration is needed.

      Delete
  3. Hi Robert,
    Great book! Forgive my ignorance, but what does the y axis of your equity curve represent? Does it mean that if I had 100k in 1983, I would have 600k in profits in 2016? Thanks.

    ReplyDelete
    Replies
    1. Thanks glad you liked it.

      Yes, if you didn't compound your profits. See http://qoppac.blogspot.co.uk/2016/06/capital-correction-pysystemtrade.html

      Delete
  4. If I had put 100k in an sp500 index fund from 1982 till now, and just left it alone, I would have made about 10% per year. If I had instead put that money in your system, as plotted by your equity curve in this post, what would my average annualized return have been? Thanks.

    ReplyDelete
    Replies
    1. There's a simple answer to this and a complicated answer.

      The simple answer is: about 16.7% a year

      The complicated answer is:

      - the equity curve shown is non compounding (equivalent to a log scale). My average return can be compared directly with the s&p but if you plotted them on top of each other mine would look worse.

      - the vol on the s&p 500 was lower; around 15% a year compared to the 25% a year targeted here (and the 30% realised). You ought to chop my return in half to reflect it's higher risk: so call it 8.3%

      - trading futures you can put most of your money in the bank because you only need <10% for margin. So really you should add on LIBOR to all my performance figures, or deduct them from the S&P 500. That's probably about 4% a year on average, so my comparable return is really around 12.3%

      - no investor would hold a trend following system as their only investment (or the equivalent as a share of a CTA like hedge fund). They'd own equities, and bonds, as well. The main benefit of a trend following system is that it adds diversification to traditional portfolios.

      Delete
  5. Thanks for the thorough answer. Might I be better off investing in a strategy such as the one on page 52 of this paper?

    http://papers.ssrn.com/sol3/papers.cfm?abstract_id=962461

    It shows an average return of 19% over a very long backtest, with 15% volatility. Plus it only requires a monthly (rather than daily) rebalance. What do you think?

    ReplyDelete
    Replies
    1. Firstly I think I should state clearly that my 'raison d'etre' isn't to push one particular trading strategy over another. It's to provide people with the tools to intelligently evaluate and construct their own strategies.

      Having said that there are several points I'd make (in the spirit of teaching you how to evaluate)

      [I can't work out exactly what the other backtest is doing, so these comments are based on my impressions rather than a detailed analysis]



      - like the S&P 500 the other backtest also includes return on invested cash.

      - I think it is a 'long only' strategy, so it will have more beta exposure. To evaluate the two strategies properly you'd need to account for this.


      - you could rebalance my system monthly and it wouldn't affect the performance much

      - it looks like they're using a single moving average. Using a blend of three like in mine is more robust on an out of sample basis

      - it looks like the system is binary, going from fully long to flat with nothing in between. My system is continous. They don't include trading costs which will be higher in a binary system

      - VERY IMPORTANT POINT: backtested performance is subject to a large amount of statistical uncertainty. You should NEVER pick a system purely because it has a higher backtest than another because of the dangers of overfitting. I could have very easily presented a system which made 50% a year on 15% volatility (but it's not one I'd advise you trading!)

      These are just a few brief comments that you might want to consider when decididing what is the right strategy for you.

      Having said all that as I said above I'm not here to push one strategy over another.

      If the way the other system works suits you, and you think it's robust, then by all means trade it. Almost any trading system is better than none. This system doesn't seem to commit many of the crimes I mention in my book: in particular as it doesn't use leverage it's probably not too toxic.

      And if anything I'd rather you read my book (naturally!) and perhaps used it to build something that combined the elements of systems you like together.

      Delete
  6. Great points, thanks. Indeed, the system I referenced has performed quite unimpressively the past few years, calling into question whether it is overfitted to past data. However, it does simply use a 12 month look-back for all instruments. Also, don't you think there's something to be said for such a long backtest. Sure, a five year backtest may be meaningless, but a 30 year backtest (like your own) is very convincing to me.

    ReplyDelete
    Replies
    1. A 30 year backtest is better than a 5 year but still no panacea.

      Delete
  7. I'm surprised when you say you think that if you only rebalanced your system monthly, it wouldn't make much difference. I thought that one of the main benefits of rebalancing your system daily is that you'll quickly catch any dangerous dips before they become too large. Given that you don't use stop losses, I would think a daily rebalance would be quite important.

    ReplyDelete
    Replies
    1. Not really; because the underlying trading rules aren't super quick they aren't massively affected by less frequency rebalancing.

      Delete
  8. Good to know, thanks. I'm considering running the 8 instrument system with thresholding, using excel and placing orders manually, and only checking in on it once a month. In this scenario, do you think there's a benefit in incorporating stop losses?

    ReplyDelete
    Replies
    1. What trading rules are you using? If mainly trend following then you probably don't need seperate stop losses.

      Delete
  9. I was going to use the rules you use for your system in chapter 15: carry along with the three slower exponential moving averages.

    ReplyDelete
    Replies
    1. Then personally I don't think you need seperate stop losses.

      Delete
  10. Thanks for all your guidance, Robert. I'm finding that my broker doesn't offer the instruments in your list that are traded in euros. Given that fact, would it make sense for me to just replace them with the next three instruments on your list: 2yr notes, Lean hogs, and GBP?

    ReplyDelete
  11. Yes, they don't seem to offer european volatility, eurostoxx, or Korean 3 year bonds. It's TDAmeritrade. Have you ever heard of such a limitation?

    ReplyDelete
  12. It appears they only offer about 50 futures contracts. I would switch brokers, but there are few that allow futures trading in a US retirement account (IRA). Interactive brokers does, but they grossly inflate the maintenance margin required for futures in retirement accounts. Given this limitation, and my desire to diversify your system across about 8 instruments, should I replace the 3 unavailable instruments with the next 3 available ones on your recommended list? Thanks.

    ReplyDelete
    Replies
    1. So from here https://qoppac.blogspot.co.uk/2016/03/diversification-and-small-account-size.html for eight instruments I have KR3, V2X, Eurodollar, MXP, Corn, Eurostoxx, US Gas, Platinum

      You can't trade KR3, V2X and Eurostoxx. So you should replace them with a bond (US 2 year), a vol (VIX) and an equity (Nasdaq).

      Delete
  13. I see, makes good sense, thanks. I hope I am not cluttering your comments section with my very specific questions, but with that risk in mind, here's another question: I noticed this quote from your chapter 15: "With this relatively high volatility target [of 20% annually] you’ll be checking your account value, and adjusting your risk, every day." I am intending to run at 20% volatility, but am planning to adjust risk (or rebalance) only once a month. Granted, I will be diversified across 8 instruments rather than 6, but am I taking on too much risk in this case?

    ReplyDelete
    Replies
    1. With a vol target of 20% a year a one month one standard deviation move will be 5.7%. So if you have a 3 std dev move over one month that's 17% of your position you'd need to cut. Quite a big cut in your position to delay. Personally I'd run at a lower vol or rebalance weekly or daily.

      Delete
  14. Hi Rob,

    Quick question: in get_positions_from_forecasts in accounting.py, the "multiplier" formula is divided by 10; is this because your average_absolute_forecast is 10? That would mean we need to change that value if we're using a different average absolute value I believe. Thank you!

    ReplyDelete
    Replies
    1. Yes - but you don't need to change that value - in that part of the code it's just an arbitrary number. The p&l for forecasts doesn't know about the real trading system, and just produces a figure for an arbitrary risk target.

      If you want to change 10 where it matters in https://github.com/robcarver17/pysystemtrade/blob/master/systems/provided/defaults.yaml

      average_absolute_forecast: 10

      Delete
  15. Hi Rob, when I run the below code, I get a sharpe of -0.27. Might this indicate that the Carry rule simply doesn't work well for CORN? Should I exclude CORN from my list of instruments, and use something else like WHEAT? Thanks.

    from systems.provided.futures_chapter15.basesystem import futures_system
    system=futures_system()
    print(system.accounts.pandl_for_instrument_forecast("CORN", "carry").sharpe()) ## Sharpe for a specific trading rule variation

    ReplyDelete
    Replies
    1. Congratulations. You're well on your way to overfitting your trading system. By all means trade wheat as well if you have sufficient cash, but use proper robust fitting techniques in the code to decide what allocation to give to different forecasting rules and instruments.

      Delete
  16. Hi Rob. thank for the series of blogs. In the main page of the githug for the project, the backtesting and "trade with IB" is identified as eventual futures. Are they available now? What is the timeframe to have support for live IB trading?
    Thank you

    ReplyDelete
    Replies
    1. Not available. No idea when. I do this in my spare time, when I'm not doing other things.

      Delete
  17. Hello Rob,
    Supposing I wanted to use your full system for forecasting & portfolio design in the nightly backtesting routine of my own Python trading system (for private, individual use), would you say it'd be a painful and foolish endeavor to attempt to crop out all components linked to the 'simplesystem' script from Chapter 15 of your book, versus just keeping pysystemtrade intact as its own component of my algo? Apologies in advance for the brazenness of that question :)

    ReplyDelete
    Replies
    1. It would be trivial to delete all the trading rule codes, which is only a small fraction of the codebase anyway, although they appear in multiple places. But why would you want to do this?

      Delete
    2. Sorry, I misphrased that a bit...should've used "transfer all components" from 'simplesystem' instead of stating 'crop out'. Basically, I'm just looking to save time and mistakes by using your system, but I'm targeting a different data connector than IB, and so on...still, the differences are a moot point, I guess, if everything is partitioned off appropriately on my side.

      Delete
    3. Yes, hopefully it's sufficiently loosely coupled that you can replace the data class supplied with your own (read the docs but basically the data class is specific to a particular asset class and source, but inherits from more generic classes).

      Delete
  18. Hi Rob, I was running your latest pysystemtrade code with volatility set to 30. When I backtested it, it showed an annual standard deviation of 16.45. Shouldn't that be closer to 30?

    ReplyDelete
    Replies
    1. Please tell me exactly the code you ran.

      Delete
    2. Hi, Rob. As I investigate further, it looks like I forgot to change the volatility in the YAML config file from its default of 20. Sorry for the false alarm. On another note, I am now getting the warning "Carry is deprecated, used Carry2". Is this something I switch in the YAML file? It looks like Carry2 requires different arguments. Thanks.

      Delete
    3. No, don't worry about that.

      Delete