Skip to content
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

Add get forex data #258

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,13 @@ fastquant_0.0.0.9000.tar.gz

# VSCode Workspace files
*.code-workspace

# PyCharm Workspace files
*.idea/

# tmp pickle test files
*.pickle

# tmp forex data
/python/fastquant/data/*.txt
/python/fastquant/data/*.zip
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
**fastquant** allows you to easily backtest investment strategies with as few as 3 lines of python code. Its goal is to promote data driven investments by making quantitative analysis in finance accessible to everyone.

## Features
1. Easily access historical stock data
1. Easily access historical stock and forex data
2. Backtest and optimize trading strategies with only 3 lines of code

<sup>`*` - Both Yahoo Finance and Philippine stock data data are accessible straight from fastquant<sup>

<sup>`*` - Support one minute foreign exchange data from Forextester.com and conversion to other time period<sup>

Check out our blog posts in the fastquant [website](https://enzoampil.github.io/fastquant-blog/) and this intro [article](https://towardsdatascience.com/backtest-your-trading-strategy-with-only-3-lines-of-python-3859b4a4ab44?source=friends_link&sk=ec647b6bb43fe322013248fd1d473015) on Medium!

## Installation
Expand Down Expand Up @@ -110,6 +112,39 @@ get_crypto_data("BTCUSDT", "2019-01-01", "2019-03-01")

*R does NOT have support for backtesting yet*

## Get forex data

All symbols from [forextester.com](https://forextester.com/) are accessible via `get_forex_data`.This method fetch one minute data from source and convert it to other time period, M1, M15, H1, D1, W1 are supported time period now.

### Python
First, call `get_forex_data` with the following parameters:
```
from fastquant import get_forex_data
df = get_forex_data('EURUSD', period='D1', read_from_local=False)
print(df)
dt open high low close
2001-01-02 23:01:00 0.9507 0.9509 0.9505 0.9507
2001-01-03 00:00:00 0.9506 0.9569 0.9262 0.9283
2001-01-04 00:00:00 0.9283 0.9536 0.9283 0.9532
2001-01-05 00:00:00 0.9532 0.9591 0.9464 0.9583
2001-01-07 23:02:00 0.9583 0.9585 0.9576 0.9584
... ... ... ... ...
2020-08-25 00:00:00 1.1792 1.1841 1.1784 1.1833
2020-08-26 00:00:00 1.1833 1.1839 1.1772 1.1839
2020-08-27 00:00:00 1.1839 1.1895 1.1767 1.1819
2020-08-28 00:00:00 1.1819 1.1918 1.1810 1.1906
2020-08-30 22:00:00 1.1906 1.1918 1.1901 1.1916
[6127 rows x 5 columns]
```
this process download online data, transfer it, save cache files in disk, first call will need 20 minutes or more and 2.3GB free RAM to generate cache files.

After then, set `read_from_local=Ture` and you will get a second response if don't need update local data to newest.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read_from_local=True


### R

```
pass
```
## Backtest trading strategies

*Note: Support for backtesting in R is pending*
Expand Down
3 changes: 3 additions & 0 deletions python/fastquant/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
# Twitter
from fastquant.data.web.twitter import tweepy_api
from fastquant.data.web.twitter import get_twitter_sentiment

# Forex
from fastquant.data.forex.forex import get_forex_data
Empty file.
49 changes: 49 additions & 0 deletions python/fastquant/data/forex/forex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np

# Import from config
from fastquant.config import DATA_FORMAT_COLS

# Import package
from fastquant.data.forex.forextester import get_forextester_data, get_local_data


def get_forex_data(symbol, start_date=None, end_date=None, source="forextester", period='D1', read_from_local=False):
"""Returns pricing data for a specified forex pair.

Parameters
----------
symbol : str
Symbol of the forex in the forextester.
https://forextester.com/data/datasources
start_date : str
Starting date (YYYY-MM-DD) of the period that you want to get data on
in most cases we need more wide time period to test strategy, so keep this to None
end_date : str
Ending date (YYYY-MM-DD) of the period you want to get data on
in most cases we need more wide time period to test strategy, so keep this to None
source : str
Source of forex history data
period : str
time period you want, support 1 minute,15 minutes,1 hour,1 day,1 week
this parameter must one of them:["M1", "M15", "H1", "D1", "W1"]
read_from_local : bool
if this parameter set False, method get data from online
if set it to True, method get data from local pickle file, faster than set it to False

Returns
-------
pandas.DataFrame
"""

if source == "forextester":
if read_from_local is False:
df = get_forextester_data(symbol, start_date, end_date, period)
else:
df = get_local_data(symbol, start_date, end_date, period)
else:
raise Exception("Source must be forextester")

return df