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


 





55 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
  19. Hi Rob,
    When I look at the annual return of my pysystemtrade backtest it shows 25K so far for 2018. But, when I look at the backtested daily returns for 2018, they sum up to zero. Do you know why? Thanks.

    ReplyDelete
    Replies
    1. NO but if you give me some code so I can reproduce your error I might be able to tell you why

      Delete
  20. Sorry, Rob. I realized I made a dumb formula error in my summation in Excel. So, the pysystemtrade backtest results are vindicated! Thanks, anyway, for offering to help.

    ReplyDelete
  21. Dear Rob,

    I came across your repository and subsequently your site after hearing suggestion from Michael Halls-Moore. I must say I am immensely impressed and overwhelmed by the amount of you had put into the Python module.

    I'm currently studying MSc in computer science and hoping to one day become a quant developer for a firm in New York or London. I would really appreciate it if you could give me some advice on achieving such goal.

    As a first step I am reading Michael Halls-Moore's ebook on algorithmic trading and would like to read your book soon. What are other things I should do? Thank you.

    ReplyDelete
  22. Hi Rob,

    I appreciate highly your work lightining this hard path for non professionals CS. I've learned a lot cause your blog. Now I feel more confident to start my own project and I have a doubt, if you don't mind:

    Despite I'm not pretending any HFT system, I'm still aiming to build one which could be fast enough for decent intraday work. Since I work with IB ( Interactivebrokers ) I'm studying its API. As you know it comes in several languages. I understand that using C/C++ is the best choice for max speed, but I see you use C++ with Phyton, I wonder if this set is optimal for speed or is there a toll ? I imagine that developing speed is better with Phyton but I worry that it might not be the fastest in live-work.
    A Note: I'm economist but absolutly enthusiast one by this work area. I can code in C# but never tried before C++ or Phyton.

    Should I follow working in C# ?
    Should I work in C++ plus Phyton?

    Thanks in advance!

    ReplyDelete
    Replies
    1. Sorry for the delay in responding.

      You've identified the issues: python is relatively slow compared to C++ (I don't know C# at all); but it's easier to develop in. So it comes down to whether python is too slow for what you intend to do. It might be worth asking here https://groups.io/g/twsapi/topics

      Delete
  23. Hi Rob,
    I have been running a lot of backtests with your system, and it seem like in all cases, when I leave out the Carry rule, I get a better sharpe. For example, if you run your base system, but change the allocation from 50% to 0%, you get a sharpe of .47, rather than .41 with Carry. This has happened to me when I backtested many more complicated systems: Carry is always a negative impact on the sharpe. Why do you think this is? Thanks.

    ReplyDelete
    Replies
    1. How many instruments are you using? If it's just a few, then you might by chance of picked instruments on which carry hasn't worked so well. In any case the reduction isn't statistically significant so I'd be cautious about taking carry out given the overwhelming evidence elsewhere that it has worked in the past.

      Delete
  24. In my example, I was running the exact system for which you show the plot on this page, so 5 instruments. But I get a similar percentage reduction in sharpe, when I run it with 16 instruments, and a couple of other flavors. May I ask if your current system seems to improve if you remove carry, as an experiment?

    ReplyDelete
    Replies
    1. Apologies for the slow response to your comment. 99% of the comments I get are spam, so inevitably when deleting them a few real comments get overlooked.

      No, removing carry in my current system (37 futures markets) is harmful to returns.

      Delete
  25. Hi Rob, wonderful book which came at the perfect time for me as I recently decide to leave a 20y finance career as a senior quant in electronic trading and was planning to spend some time trading for my own account. This framework was exactly what I was looking for!

    Also looking at your python framework that looks very well organized and planning to use it at least as as starting point.
    Quick question on that actually. The data api seems limited to futures and fx and there are no objects hierarchy for equities. Any reason for that? Should I use the futures data model for equities or is something you just haven't implemented yet?

    Thank you!

    Daniel

    ReplyDelete
    Replies
    1. I'm not planning to; equity trading is hard because of things like dividends and splits. It's more likely I'll add options, and that isn't very likely in the near future.

      Delete
  26. Hi all,

    First of all, thank you Rob for the great book on systematic trading and such a deeply thought out system.
    I am an novice in trading and very interested in automated trading systems (I am software engineer, dealing with code has been my profession for many years). I would love to connect with other traders to share thoughts, ideas and get an advice on trading.
    To begin with, I plan to set up pysystemtrade to see what I can get with a relatively small $50k portfolio (will it work at all?). Any recommendations for the instruments in this case?

    ReplyDelete
    Replies
    1. I've written on this subject extensively on the blog.

      Delete

Comments are moderated. So there will be a delay before they are published. Don't bother with spam, it wastes your time and mine.