-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
175 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"tests/test_bittrade.py": true, | ||
"tests/test_google_client.py": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ Authors | |
======= | ||
|
||
* Junhao Wang - https://the-hao.com/ | ||
* Dongxu Li - https://leodongxu.wixsite.com/quant |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[pytest] | ||
addopts = --doctest-modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
click | ||
pandas | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
"""Module to retrieves Google Finance data. | ||
This module contains various price for stocks and crypto. | ||
Todo: | ||
* EMPTY | ||
* You have to also use ``sphinx.ext.todo`` extension | ||
""" | ||
from datetime import datetime | ||
|
||
import pandas as pd | ||
import requests | ||
|
||
|
||
def get_price_data(query): | ||
""" | ||
Use Google Finance to get data. | ||
Args: | ||
query (str): EMPTY | ||
Returns: | ||
pandas.DataFrame: EMPTY | ||
""" | ||
r = requests.get( | ||
"https://finance.google.com/finance/getprices", params=query, | ||
) | ||
lines = r.text.splitlines() | ||
data = [] | ||
index = [] | ||
basetime = 0 | ||
for price in lines: | ||
cols = price.split(",") | ||
if cols[0][0] == 'a': | ||
basetime = int(cols[0][1:]) | ||
index.append(datetime.fromtimestamp(basetime)) | ||
data.append([ | ||
float(cols[4]), float(cols[2]), float( | ||
cols[3], | ||
), float(cols[1]), int(cols[5]), | ||
]) | ||
elif cols[0][0].isdigit(): | ||
date = basetime + (int(cols[0])*int(query['i'])) | ||
index.append(datetime.fromtimestamp(date)) | ||
data.append([ | ||
float(cols[4]), float(cols[2]), float( | ||
cols[3], | ||
), float(cols[1]), int(cols[5]), | ||
]) | ||
return pd.DataFrame(data, index=index, columns=['Open', 'High', 'Low', 'Close', 'Volume']) | ||
|
||
|
||
def get_close_price(target, period='1Y'): | ||
""" | ||
Get the close price, for every target ticker. | ||
Args: | ||
target (EMPTY): EMPTY | ||
period (str): EMPTY | ||
Returns: | ||
EMPTY: EMPTY | ||
""" | ||
market, ticker = target.split(':') | ||
param = { | ||
'q': ticker, # Bitcoin price in USD | ||
'i': "86400", # Interval size in seconds ("86400" = 1 day intervals) | ||
'x': market, | ||
'p': period, # Period (Ex: "4Y" = 4 year) | ||
} | ||
df = get_price_data(param) | ||
price = df[['Close']] | ||
price = price.reset_index() | ||
price['index'] = price['index'].apply(lambda x: x.date()) | ||
price.columns = ['Date', ticker] | ||
return price | ||
|
||
|
||
def get_close_price_table(targetList, interpolation=True, period='1Y'): | ||
""" | ||
EMPTY | ||
""" | ||
df_table = pd.DataFrame() | ||
for target in targetList: | ||
price = get_close_price(target, period) | ||
if (df_table.shape == (0, 0)): | ||
df_table = price | ||
else: | ||
df_table = pd.merge(df_table, price, how='outer', on='Date') | ||
if (not interpolation): | ||
return df_table | ||
else: | ||
priceTable = df_table | ||
# linear interpolation is applied. | ||
priceTable['Date'] = pd.to_datetime(priceTable.Date) | ||
priceTable = priceTable.sort_values(by='Date') | ||
priceTable = priceTable.interpolate( | ||
method='linear', axis=0, | ||
).ffill().bfill() | ||
priceTable = priceTable.set_index('Date') | ||
return priceTable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from bittrade.data.google_client import get_close_price_table | ||
|
||
|
||
def test_get_close_price_table(): | ||
""" | ||
Getting the date we need from google finance API. | ||
currencyTicker represents the bitcoin price, and the most tradable currency include Japanese Yen, Swiss Franc, | ||
Australian Dollar, Canadian Dollar, Indian Rupee, Euro. The price data for these currency are all quoted in dollar. | ||
24 Most Liquid ETFs: | ||
SPDR S&P 500 (SPY) | ||
Financial Select Sector SPDR (XLF) | ||
Russell 2000 Index Fund Profile (IWM) | ||
MSCI Emerging Markets Index Fund (EEM) | ||
PowerShares QQQ (QQQ) | ||
Vanguard MSCI Emerging Markets ETF (VWO) | ||
MSCI EAFE Index Fund (EFA) | ||
FTSE China 25 Index Fund (FXI) | ||
Industrial Select Sector SPDR Fund (XLI) | ||
Daily Small Cap Bear 3X Shares Fundamentals (TZA) | ||
Energy Select Sector SPDR Fund (XLE) | ||
UltraShort S&P500 (SDS) | ||
Silver Trust ETF (SLV) | ||
Direxion Daily Financial Bear 3x Shares (FAZ) | ||
Market Vectors TR Gold Miners ETF (GDX) | ||
MSCI Japan Index Fund (EWJ) | ||
UltraPro Short S&P500 (SPXU) | ||
MSCI Brazil Index Fund (EWZ) | ||
Daily Financial Bull 3X Shares (FAS) | ||
VelocityShares Daily 2x VIX Short-Term ETN (TVIX) | ||
United States Oil Fund (USO) | ||
SPDR Gold Shares (GLD) | ||
UltraShort Barclays 20+ Year Treasury (TBT) | ||
United States Natural Gas Fund (UNG) | ||
""" | ||
currencyTickers = [ | ||
"CURRENCY:BTCUSD", "CURRENCY:GBPUSD", "CURRENCY:JPYUSD", "CURRENCY:CHFUSD", | ||
"CURRENCY:AUDUSD", "CURRENCY:CADUSD", "CURRENCY:INRUSD", "CURRENCY:EURUSD", | ||
] | ||
etfTickers = [ | ||
"NYSEARCA:XLF", "NYSEARCA:IWM", "NYSEARCA:EEM", "NASDAQ:QQQ", "NYSEARCA:VWO", "NYSEARCA:EFA", | ||
"NYSEARCA:FXI", "NYSEARCA:XLI", "NYSEARCA:TZA", "NYSEARCA:XLE", "NYSEARCA:SDS", "NYSEARCA:SLV", | ||
"NYSEARCA:FAZ", "NYSEARCA:GDX", "NYSEARCA:EWJ", "NYSEARCA:SPXU", "NYSEARCA:EWZ", "NYSEARCA:FAS", | ||
"NASDAQ:TVIX", "NYSEARCA:USO", "NYSEARCA:GLD", "NYSEARCA:TBT", "NYSEARCA:UNG", | ||
] | ||
table = get_close_price_table( | ||
currencyTickers + etfTickers, interpolation=True, period='1Y', | ||
) | ||
print(table.head()) | ||
assert(len(table) > 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters