Zipline: Add Currencies to Zipline #currency #fx

Created on 27 Oct 2015  路  4Comments  路  Source: quantopian/zipline

Hi Everyone,

I'm looking at adding a new feature to zipline to handle currency risk (GBP base currencies and perhaps buying in another denomination USD most probably) and have been looking through the source and wondered if the team would have any good places to start working on it?

There doesn't seem to be much in the base code around base_capital you put in place (that I can see) so I think I would need to have the asset_finder have further details on the SID's ccy (currency) and a reference to a FX rate lookup table. Has anyone done this before?

I have started a local branch extending the Asset classes and asset finder and putting some tests in, would this be an accepted pull request to put into the main code once I complete it?

Currencies Feature Request

Most helpful comment

Any updates on this matter? :blush:

All 4 comments

@michaeljohnbennett thanks for the interest in Zipline (and sorry for the radio silence on the Google Group. I started typing up a reply when you first posted but I never finished it.)

Can you elaborate a bit more on what you'd want to be able to do with additional currency information? A couple things jump to mind off the top of my head, which would require changes of varying complexity:

  • Allowing purchases of assets whose prices are quoted in non USD currencies.

    • Probably requires each asset to be tagged with a currency, and would require a historical lookup table of exchange rates. Is such data freely available anywhere?

  • Allowing algorithms to hold cash positions in non-USD currencies:

    • The simple version of this is to allow an algo to specify a "base currency", and convert any assets in another currency to that base currency.

    • The more complicated version of this is allowing multiple simultaneous cash positions. This would be necessary (I think?) if you wanted to so something like trade foreign exchange pairs. This essentially turns each currency into a new asset type.

ping @jfkirk as well, who's in charge of the team that would eventually be working on something like this.

Thanks @ssanderson, @jfkirk,

I'm writing up a CurrencyPair(Asset) class in the cython end of the world of zipline and putting in some simple tests.

Will extend the AssetFinder as well to have the ability to find a currencypair object by

find_by_symbol('USDGBP') - this is analogous to SID but that is an INT and I'm using CSI for the data so I link it to their internal ID I get the file back in.

find_all_by_major('USD') - would return me a list of currency pairs:
[Currency('USDGBP'), Currency('USDEUR)]

find_all_by_minor('GBP') - would return where the minor of the pair is 'GBP'
[Currency('USDGBP'), Currency('EURGBP)] or similar

Once I have those and the metadata for each symbol from my provider I can then load the data for the FX rates in a data frame probably looking something like this:

| | sid(5902) | sid(5901) | sid(5900) |
| --- | --- | --- | --- |
| {date} | {sid1}.close | {sid2}.close | {sid3}.close |
| 2010-10-25 | 1.4512 | 1.34 | 0.56 |
| 2010-10-26 | 1.4522 | 1.35 | 0.52 |
| 2010-10-27 | 1.4532 | 1.36 | 0.53 |
| 2010-10-28 | 1.4542 | 1.37 | 0.59 |

and so on...

And CurrencyPairs of:

| sid | pair | major | minor | ...etc |
| --- | --- | --- | --- | --- |
| 5902 | 'USDGBP' | 'USD' | 'GBP' | ... |
| 5901 | 'USDCAD' | 'USD' | 'CAD' | ... |
| 5900 | 'USDJPY' | 'USD' | 'JPY' | ... |

The columns are the SID of each currency pair

We would then need a wrapper round these like the asset finder for currency where you have some nice fast map/dict like interface or a class with methods where we can say get_rate_on_date('USDGBP', datetime(2001,10,25)) and that would return is the rate value like 1.4512 to then use in calculation of the order amounts etc.

The benefit of having both major and minor is the ability to cross convert. You can then say if my value to convert is USD then I know to divide by the rate value to get GBP and vice-versa I multiply my GBP to get USD.

How that gets plugged in I would need your help with, I'm happy to wire up the helpers and base classes with tests, I would like to do a simple test of AAPL buy long or similar but starting off with a 10K GBP account and then calculating all our risk parameters back into GBP where appropriate.

I already have test data from another system and algo platform that is FX rate/risk aware so have the ability to test this.

In terms of getting data for this, do you have spot rates you can use for testing?

https://www.quandl.com/data/CURRFX/GBPUSD-Currency-Exchange-Rates-GBP-vs-USD

quandl has some nice data we could use. We would need a loader for it but it's not huge issue.

On another note I have a loader for CSI Data csv files in the works and possibly looking at metastock files too, is this of interest also?

There are some other things I would cleanup around the loading of benchmarks constantly from yahoo every time you run a test, seems excessive, is that hack line going to be removed soon? It's a pain when you are running offline (when I'm travelling on the boat to work with no wi-fi) and seems excessive? I have mocked out a no-op version to pass into the trading environment on the test cases but I can't figure out why it does that all the time? Any reason?

Haha, I just checked out... https://github.com/quantopian/zipline/commit/2686e3875aaadfa709be05f3a7224c03e72aacf5

and someone fixed it! @jfkirk a man after my own heart!

Any updates on this matter? :blush:

Was this page helpful?
0 / 5 - 0 ratings