Skip to content

Commit

Permalink
Merge dc24793 into 033f948
Browse files Browse the repository at this point in the history
  • Loading branch information
dongxulee committed Feb 1, 2018
2 parents 033f948 + dc24793 commit 16b302b
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Authors
=======

* Junhao Wang - https://the-hao.com/
* Dongxu Li - https://leodongxu.wixsite.com/quant
83 changes: 83 additions & 0 deletions src/bittrade/database/googleclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from datetime import datetime
import pandas as pd
import requests

def get_price_data(query):
'''
Connected to google finance, get the price data, return to panda
Dataframe.
'''
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 getClosePrice(target, period='1Y'):
'''
Get the close price, for every target ticker.
'''
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 getClosePriceTable(targetList, interpolation=True, period='1Y'):
'''
Function built on getClosePrice, this function take a list of tickers
and return to a panda dataframe, where every column is the closed price
for the ticker in the tickerList.
The interpolation option: if it is true, NAN value in the table with be
fitted using interpolation method.
period is the time window we want to trace back.
'''
df_table = pd.DataFrame()
for target in targetList:
price = getClosePrice(target, period)
if (df_table.shape == (0, 0)):
df_table = price
else:
df_table = pd.merge(df_table, price, how='outer', on='Date')
if (interpolation == False):
return df_table
else:
priceTable = df_table
# The interpolation method I use here is linear interpolation.
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
42 changes: 42 additions & 0 deletions src/bittrade/database/googleclient_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from googleclient import getClosePriceTable
# 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.
currencyTickers = [
"CURRENCY:BTCUSD", "CURRENCY:GBPUSD", "CURRENCY:JPYUSD", "CURRENCY:CHFUSD",
"CURRENCY:AUDUSD", "CURRENCY:CADUSD", "CURRENCY:INRUSD", "CURRENCY:EURUSD",
]
# 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)
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 = getClosePriceTable(currencyTickers + etfTickers, interpolation=True, period='1Y')
print(table.head())

0 comments on commit 16b302b

Please sign in to comment.