Code

Drawing this wiggly line is easy. Making money off it, harder.


There are four python packages hosted on github which you may find useful, plus a collection of gists.


package: pysystemtrade


python 3.4

Git hub repo

More information on the dedicated page, here.

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.

gists: python-IB-API

python 3.5

This code shows how we can use the native python API for interactive brokers using python for a series of simple examples.

Start here: blog post, gist
Historic data: blog post, gist
Market data: blog post, gist
Placing orders: blog post, gist
Getting accounting data: blog post, gist







package: python-uk-trading-tax-calculator

python 2.7

Git hub repo

This is the package I use to calculate the UK tax liability for my interactive brokers trading. More in this blog post:

Using python to calculate your UK tax liability for trading profits



package: systematictradingexamples


python 2.7

Git hub repo

This is a collection of random code used to demonstrate some of the principles in my book. See these blog posts for more:


Python code for the trading rules in the book

Python code for optimisation (one period and bootstrapping), and producing different sample periods

Introducing random data

Random data: Trading the equity curve

Random data: Optimising portfolio weights the right way (TBC)


I intend to deprecate this repo, and replace it with examples built using pysystemtrade.



package: ibswigsystematicexamples


python 2.7

Git hub repo


This is effectively deprecated: I would use the native IB python API instead (see above).

22 comments:

  1. Dear Sir,

    I am using Python 2.7 and would like to reproduce the results from your codes. Upon executing your code "Introducing random data", on line 6, which reads
    "from common import arbitrary_timeseries"
    I always get the following error message:
    "ImportError: cannot import name arbitrary_timeseries"

    Have you got any solution to this problem?

    I am thankful for any help.

    Sincerely,
    Benjamin

    ReplyDelete
  2. You need to add the directory where the common module is to your python path
    http://stackoverflow.com/questions/3701646/how-to-add-to-the-pythonpath-in-windows-7
    https://scipher.wordpress.com/2010/05/10/setting-your-pythonpath-environment-variable-linuxunixosx/

    ReplyDelete
  3. Hi Robert,
    Not sure where best to post this question.

    Regarding the example data in pyststemtrade I pragmatically assume that this is actually a dump from the data you use yourself, I guess if thats the case then the data that you use, for example Eurodollar, will follow your preferences for trading 50 months out as you state in your excellent post about price stitching?

    Thanks

    Chris

    ReplyDelete
    Replies
    1. Yes, as far back as I can go it's around 50 months out; when data isn't available in the distant past I use nearer stuff.

      Delete
  4. Hi Rob,

    Any reason you prefer to use doctest instead of unittest?

    ReplyDelete
  5. Hi Rob,

    I was wondering what changes you would make from using pysystemtrade for backtesting purposes to using it for live trading?

    The approach I was thinking of was getting the rounded positions for each instrument and executing these, but then I get back to the problem of variable capital you posted about (in that my starting capital in 1960 isn't my current capital)

    So with that in mind I guess the best approach is to grab the forecasts for each instrument and then following the daily process steps from your book (chap 15)?

    Thanks Chris

    ReplyDelete
    Replies
    1. You could have something that reads you current capital in, changes the appropriate config parameter https://github.com/robcarver17/pysystemtrade/blob/master/docs/userguide.md#capital-scaling-parameters. That is pretty much what I would do in production.

      Delete
  6. Hi Rob, another code-related question, if I may. After running the below code, I get multiple instances of the following warning. Do you know what this means? Thanks, again, for all your generous help.

    Code:
    from systems.provided.futures_chapter15.estimatedsystem import futures_system
    system=futures_system()
    print(system.accounts.portfolio().sharpe())


    Warning that occurs multiple times while program is running:

    Warning (from warnings module):
    File "/home/peter/pysystemtrade/syscore/correlations.py", line 181
    corrmat[corrmat < 0] = 0.0
    RuntimeWarning: invalid value encountered in less

    ReplyDelete
    Replies
    1. It means that it's comparing a np.nan with a 0. This returns False (which is what we want) but isn't ideal. I've added it to the to do list.

      Delete
  7. Rob-
    I enjoyed your book and have pre-ordered your next one. I have developed my own trading app using a LAMP stack, but I'm moving some things like my daily data scraping to python.

    I downloaded your python package and have been playing around with it. I keep getting the error


    File "/home/tim/python/commodities_related/pysystemtrade/pysystemtrade/sysdata/data.py", line 68, in daily_prices
    dailyprice = instrprice.resample("1B").last()

    TypeError: last() missing 1 required positional argument: 'offset'

    Any ideas? I'm going to see if there is something I'm missing in the setup. I'm guessing something is mis-configured on my setup.
    Thanks.

    ReplyDelete
    Replies
    1. That error is coming from the pandas timeseries library (https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html).I just updated my pandas to the latest version (0.20.3) in case it was an unsupported API change... but it worked just fine. What version of pandas are you using?

      Delete
  8. 0.14.1. I am using an anaconda distribution. I updated the whole distribution and now it is working. Thanks for the reply. Looking forward to testing this out.https://qoppac.blogspot.com/logout?d=https://www.blogger.com/logout-redirect.g?blogID%3D261139923818144971%26postID%3D7518760355750133572

    ReplyDelete
  9. Hi Robert,

    I have a question from your book page 158, where you calculate instrument value volatility.
    So you calculate instrument daily volatility and then multiply by block value. To get value volatility you simply multiply this by the current exchange rate. Why you dont consider any currency volatility here, because if I am right you have exposure to the volatility of the currency as well.

    Thanks

    ReplyDelete
  10. Hi Robert,

    I can't seem to find an answer to this so forgive if it's a repeat question. Have you ever looked at multiprocessing? I have been playing around with it and since your system stages are instances I can't seem to get a pool to pickle those instance objects.

    Wonder if you've found a workaround.... thanks!

    ReplyDelete
    Replies
    1. It's on my to do list. Basically I will be able to do it, but only with certain components not entire stages. You might find this helpful in terms of what I'll probably end up doing: https://github.com/robcarver17/pysystemtrade/issues/15

      Delete
  11. Hi Robert,

    How are the values in Table 20 (What kind of losses do we see with particular vol target" computed? For instance, how is the value for "worst monthly loss in 10 years" calculated?

    Thanks much
    ashu

    ReplyDelete
  12. Ahh Systematic Trading ... I was curious if the values were out of simulation (back testing) especially for 'worst monthly or daily loss over 10 years/30 years".

    Thanks
    Ashu

    ReplyDelete
    Replies
    1. They're based on bootstrapping artifical distributions with the required properties.

      Delete
  13. Ahh ... OK ... I was thinking as much.

    Thanks, Rob
    Ashu

    ReplyDelete

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