示例#1
0
def test_histData():

    # one symbol
    hData = yf.getHistoricData(symbols[0], dumpDest="debug")
    hData.pct_change(
    )  # try a calculation. Will result in an error if data is rotten.

    assert hData.shape[1] == 6
    assert hData.shape[0] > 100

    hData = yf.getHistoricData(symbols)
示例#2
0
def SMA(symb,intvs,symbec='STK',eday=None):
	if eday==None:
		eday = datetime.now(timezone('US/Eastern'))
	bday=eday-timedelta(days=365)
	symbDict={'INDU':'^DJI','COMP':'^IXIC','SPX':'^GSPC','RUT':'^RUT'}
	if symb in symbDict.keys():
		symb=symbDict[symb]
	if symb=='^DJI':
		Data = Quandl.get("YAHOO/INDEX_DJI", authtoken='977FGLXLZ1wsdKt8DgUH',
							trim_start="%d-%02d-%02d" %(bday.year,bday.month,bday.day), 
							trim_end="%d-%02d-%02d" %(eday.year,eday.month,eday.day))

		sums=list()
		for n in intvs:
			if len(Data.index)<n: #no enough data
				sums.append(-1)
			else:
				sums.append(round(Data['Adjusted Close'][-n-1:-1].mean(),1))

	else:
		Data = yf.getHistoricData(symb,bday.timetuple()[0:3],eday.timetuple()[0:3])[['adj_close']] # get data from yahoo finance
		print Data.tail()
		sums=list()
		for n in intvs:
			if len(Data.index)<n: #no enough data
				sums.append(-1)
			else:
				sums.append(round(Data['adj_close'][-n-1:-1].mean(),1))

	return sums
示例#3
0
def getTradingDates():
    ''' get list of trading dates from yahoo, one year back'''
    startDate = (datetime.date.today() -
                 datetime.timedelta(days=365)).timetuple()[:3]
    df = yf.getHistoricData('SPY', sDate=startDate)
    dates = [d.date() for d in df.index]
    return dates
示例#4
0
文件: Pivots.py 项目: hezhenke/myIbPy
def Pivots(symb,symbec='STK',eday=None):
	if eday== None:
		eday = datetime.now(timezone('US/Eastern'))
	bday=eday-timedelta(days=10)
	symbDict={'INDU':'^DJI','COMP':'^IXIC','SPX':'^GSPC','RUT':'^RUT'}
	if symb in symbDict.keys():
		symb=symbDict[symb]

	if symb=='^DJI':
		Data = Quandl.get("YAHOO/INDEX_DJI", authtoken='977FGLXLZ1wsdKt8DgUH',
							trim_start="%d-%02d-%02d" %(bday.year,bday.month,bday.day), 
							trim_end="%d-%02d-%02d" %(eday.year,eday.month,eday.day))[0:1]
		H=Data['High'].values[-1]
		L=Data['Low'].values[-1]
		C=Data['Close'].values[-1]

	else:
		Data = yf.getHistoricData(symb,bday.timetuple()[0:3],eday.timetuple()[0:3])[['high','low','close','adj_close']] # get data from yahoo finance
		H=Data['high'].values[-1]
		L=Data['low'].values[-1]
		C=Data['close'].values[-1]

	P = (H + L + C) / 3
	#P = (H + L + C + C) / 4
	#P = (H + L + O + O) / 4
	R1 = 2*P-L
	S1 = 2*P-H
	R2 = P+(H-L)
	S2 = P-(H-L)
	R3 = R1+(H-L)
	S3 = S1-(H-L)
	return [round(R3,1),round(R2,1),round(R1,1),round(P,1),round(S1,1),round(S2,1),round(S3,1)]
示例#5
0
def Pivots(symb, symbec='STK', eday=None):
    if eday == None:
        eday = datetime.now(timezone('US/Eastern'))
    bday = eday - timedelta(days=10)
    symbDict = {'INDU': '^DJI', 'COMP': '^IXIC', 'SPX': '^GSPC', 'RUT': '^RUT'}
    if symb in symbDict.keys():
        symb = symbDict[symb]

    if symb == '^DJI':
        Data = Quandl.get(
            "YAHOO/INDEX_DJI",
            authtoken='977FGLXLZ1wsdKt8DgUH',
            trim_start="%d-%02d-%02d" % (bday.year, bday.month, bday.day),
            trim_end="%d-%02d-%02d" % (eday.year, eday.month, eday.day))[0:1]
        H = Data['High'].values[-1]
        L = Data['Low'].values[-1]
        C = Data['Close'].values[-1]

    else:
        Data = yf.getHistoricData(symb,
                                  bday.timetuple()[0:3],
                                  eday.timetuple()[0:3])[[
                                      'high', 'low', 'close', 'adj_close'
                                  ]]  # get data from yahoo finance
        H = Data['high'].values[-1]
        L = Data['low'].values[-1]
        C = Data['close'].values[-1]

    P = (H + L + C) / 3
    #P = (H + L + C + C) / 4
    #P = (H + L + O + O) / 4
    R1 = 2 * P - L
    S1 = 2 * P - H
    R2 = P + (H - L)
    S2 = P - (H - L)
    R3 = R1 + (H - L)
    S3 = S1 - (H - L)
    return [
        round(R3, 1),
        round(R2, 1),
        round(R1, 1),
        round(P, 1),
        round(S1, 1),
        round(S2, 1),
        round(S3, 1)
    ]
示例#6
0
@author: krisan
"""

#the following script aim to investigate overnight trading

#https://code.google.com/p/trading-with-python/source/browse/trunk/lib/functions.py
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import neighbors
import tradingWithPython.lib.yahooFinance as yf
import tradingWithPython.lib as lb1
#https://www.tradingview.com/v/EVXQPaR9/
# we first need ot get data.

ohlc=yf.getHistoricData('SPY')

ohlc['cc']=(ohlc['close']/ohlc['open'])-1
ohlc['co']=(ohlc['open']/ohlc['close'].shift(1))-1

#ret1=np.cumsum(ohlc['cc']).plot()
##ret2=np.cumsum(ohlc['co']).plot()

plt.plot(ohlc['co'].index,np.cumsum((ohlc['cc'])),'r')
plt.plot(ohlc['co'].index,np.cumsum(ohlc['co']),'b')

train=pd.DataFrame(columns=ohlc.columns)
test=pd.DataFrame(columns=ohlc.columns)

train=train.append(ohlc.iloc[0:1000])
test=test.append(ohlc.iloc[1000:])
today = dt.date.today().strftime('%m/%d/%Y')
now = dt.datetime.now()
tz = os.environ['TZ'] = 'America/New_York'  # local timezone

stock_count = len(symbols)
stock_size = int(stock_count / 2)

# create series of all tradings days within range
us_bd = offsets.CustomBusinessDay(calendar=holiday.USFederalHolidayCalendar())
bdays = pd.Series(pd.date_range(start, today, freq=us_bd, name='Date'))

print(tz + '\n' + now.strftime('%I:%M:%S%p') + ' ' + today + '\n\n' +
      'Downloading Data for %s Stocks...\n' % stock_count)

try:
    stocks = {s: yf.getHistoricData(s, start) for s in symbols}
    stocks = {s: tech.analyze(stocks[s]) for s in symbols}
except Exception as e:
    pass

elapsed = dt.datetime.now() - now
print(('\n' 'Completed Successfully in %s') % elapsed)

for s in symbols:
    stocks[s]["Date"] = stocks[s].index.date
    stocks[s].set_index("Date", drop=True, inplace=True)

close = pd.DataFrame({s: stocks[s]['adj_close'] for s in symbols})
first_close = {s: stocks[s]['adj_close'].head(1).transpose() for s in symbols}
last_close = close.tail(1).transpose()
daily_return = close.apply(lambda x: x / x[0])
    'VZ', 'T', 'S', 'TMUS', 'WFC', 'ORCL', 'JNJ', 'MSFT', 'DIS', 'WMT', 'MCD',
    'TGT', 'COKE', 'FB', 'AAPL', 'SNAP', 'INTC', 'HK'
]
# symbols = fdb.symbols[8:16] # pull symbols directly from database

start = 2000 / 1 / 1  # set starting date for historic data
tz = os.environ['TZ'] = 'America/New_York'  # set local timezone
today = dt.date.today()  # current day
now = dt.datetime.now()  # current time
count = len(symbols)  # ticker list size

print("Python: {}\n\n".format(sys.version), tz, '\n',
      now.strftime('%I:%M:%S%p'), ' ', today.strftime('%m/%d/%Y'), '\n\n',
      'Downloading Data for %s Stocks...\n' % count)

stocks = {s: yf.getHistoricData(s, start)
          for s in symbols}  # fetch price data for given tickers
stocks = {s: tech.analyze(stocks[s])
          for s in symbols}  # append technical indicators

for s in symbols:  # format date (index) column
    stocks[s]["Date"] = stocks[s].index.date
    stocks[s].set_index("Date", drop=True, inplace=True)

elapsed = dt.datetime.now() - now  # calculate total execution time
print(('\n' 'Completed Successfully in %s\n') % elapsed)

close = pd.DataFrame({s: stocks[s]['adj_close']
                      for s in symbols})  # all close data
price = {s: close.loc[:, s]
         for s in symbols}  # series of all close data for each stock
示例#9
0
stock_count = len(symbols)
stock_size = int(stock_count / 2)

# create series of all tradings days within range
us_bd = offsets.CustomBusinessDay(calendar=holiday.USFederalHolidayCalendar())
bdays = pd.Series(pd.date_range(start, today, freq=us_bd, name='Date'))

print(tz + '\n' + time + ' ' + today.strftime('%m/%d/%Y') + '\n\n' +
      'Downloading Data for %s Stocks...\n' % stock_count)

stocks = {}
close_p = {}
first_close = {}
for s in symbols:
    try:
        stocks.update({s: yf.getHistoricData(s, start)})
        stocks.update({s: tech.analyze(stocks[s])})
        stocks[s]["Date"] = stocks[s].index.date
        stocks[s].set_index("Date", drop=True, inplace=True)
        close = pd.DataFrame(close_p.update({s: stocks[s]['adj_close']}))
        first_close.update({s: stocks[s]['adj_close'].head(1).transpose()})
    except:
        pass

#stocks = {s: yf.getHistoricData(s, start) for s in symbols}
#stocks = {s: tech.analyze(stocks[s]) for s in symbols}
#close = pd.DataFrame({s: stocks[s]['adj_close'] for s in symbols})
#first_close = {s: stocks[s]['adj_close'].head(1).transpose() for s in symbols}

closefill = close.fillna(1)
last_close = close.tail(1).transpose()
示例#10
0
def lastTradingDate():
    """ determine last historic data date by downloading from yahoo finance"""
    data = yf.getHistoricData('SPY', sDate=(2016, 1, 1))
    return data.index[-1]
示例#11
0
import matplotlib
import matplotlib.pyplot as plt
import quadprog as qp
import numpy as np

start_date = tuple([int(strftime("%Y"))-2,int(strftime("%m")),int(strftime("%d"))])
tran_days = 1   #interval between tranch dates
tran_look = 20  #how far back to tranch

equity_list = ['VTI','VGK', 'VPL','VWO','VNQ', 'VNQI']
bond_list = ['BIV','BLV','BNDX','VWOB','VTIP', 'BSV']

symbol_list = equity_list + bond_list

# Get historical data information
prices = yf.getHistoricData(symbol_list,sDate=start_date)

# Calculate number of dates have
num_pts = len(prices.major_axis)

# Create rankings based on tranch settings
ranking = pd.DataFrame()
for tranch in range(0,tran_look+1,tran_days):
    ranking[str(tranch)] = (prices.iloc[:,num_pts-1].loc['adj_close'] 
            - prices.iloc[:,num_pts-1].loc['adj_close'])
    for num in range(126,256,1):
        current_price = prices.iloc[:,num_pts-1-tranch].loc['adj_close']
        past_price = prices.iloc[:,num_pts-1-num-tranch].loc['adj_close'] 
        ranking[str(tranch)] += (current_price - past_price)/past_price

# Find highest x funds and the largest bond fund
示例#12
0
! pip install tradingWithPython
import pandas as pd
import tradingWithPython as twp
assert twp.__version__ >= '0.0.12' , 'Please update your twp module '
import tradingWithPython.lib.yahooFinance as yahoo # yahoo finance module

price = yahoo.getHistoricData('SPY')['adj_close'][-2000:]

price.plot()

figsize(10,6)

# sumulate two trades
signal = pd.Series(index=price.index) #init signal series

# while setting values this way, make sure that the dates are actually in the index
signal[pd.datetime(2008,1,3)] = -10000 # go short 
signal[pd.datetime(2009,1,5)] = 10000 # go long 10k$ on this day
signal[pd.datetime(2013,1,8)] = 0 # close position


bt = twp.Backtest(price,signal,initialCash=0)
bt.plotTrades()

figure()
bt.pnl.plot()
title('pnl')

figure()
bt.data.plot()
title('all strategy data')