In [7]:
import yahooFinance as yf

In [8]:
yf.getQuote(['TSLA','AAPL'])


Out[8]:
PE change_pct last short_ratio time
TSLA NaN -0.63% 187.59 4.5 4:00pm
AAPL 16.82 -0.14% 124.25 1.1 4:00pm

In [9]:
tesla = yf.getHistoricData('TSLA',(2012,1,1))
print tesla.tail(20)


Got 815 days of data
            adj_close   close    high     low    open   volume
2015-03-04     202.44  202.44  202.52  197.21  199.25  4222000
2015-03-05     200.63  200.63  206.19  200.15  202.85  4877000
2015-03-06     193.88  193.88  200.75  192.15  199.21  6712400
2015-03-09     190.88  190.88  194.49  188.25  194.39  6736700
2015-03-10     190.32  190.32  193.50  187.60  188.46  5579700
2015-03-11     193.74  193.74  196.18  191.01  191.15  4974900
2015-03-12     191.07  191.07  194.45  189.75  193.75  4149300
2015-03-13     188.68  188.68  191.75  187.32  188.95  5434300
2015-03-16     195.70  195.70  195.91  189.80  192.00  5628800
2015-03-17     194.73  194.73  198.71  193.94  195.43  4894100
2015-03-18     200.71  200.71  200.88  193.11  194.96  4820900
2015-03-19     195.65  195.65  204.59  194.53  202.00  8458900
2015-03-20     198.08  198.08  198.99  195.62  197.45  4241000
2015-03-23     199.63  199.63  200.50  197.47  198.50  2620900
2015-03-24     201.72  201.72  203.79  199.75  201.58  3639600
2015-03-25     194.30  194.30  198.59  192.70  198.27  5718600
2015-03-26     190.41  190.41  194.79  189.70  193.92  4016600
2015-03-27     185.00  185.00  189.29  181.40  189.07  8526900
2015-03-30     190.57  190.57  192.25  181.80  185.85  9990600
2015-03-31     188.77  188.77  193.76  188.41  193.53  4906900

In [10]:
tesla.tail(2)['close']


Out[10]:
2015-03-30    190.57
2015-03-31    188.77
Name: close, dtype: float64

In [28]:
figsize(15,9)
from pandas import rolling_min, rolling_max, Series
# last n data point of tesla
n = 150
price = [el for el in tesla.tail(n)['close']]
x = range(n)
plot(x,price,'.')

# moving average
import numpy
def movingaverage(interval, window_size):
    window= numpy.ones(int(window_size))/float(window_size)
    return numpy.convolve(interval, window, 'same')
# generate moving average
window = 7 
y_av = movingaverage(price, window)


min_av = rolling_min(Series(price), window)
max_av = rolling_max(Series(price), window)
mins = set(min_av)
maxs = set(min_av)
plot(x, min_av, "r")
plot(x, max_av, "r")
plot(x, y_av, "b")
#Series(price).plot()
#plot(x, price, "b")
#plot(mins, maxs, "b")
xlim(window, n-window)


Out[28]:
(7, 143)

In [14]:
# find when to buy and sell
import trendy
import tradingWithPython as twp
order=trendy.iterlines(price, window = 25, charts = True)
order=[el*100 for el in order]



In [21]:
import tradingWithPython.lib.yahooFinance as yahoo 
import pandas as pd

price = yahoo.getHistoricData('TSLA')['adj_close'][-n:]
price.plot()


Got 1198 days of data
Out[21]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f330c0dda10>

In [22]:
# create a stratgy
strategy = pd.Series(index=price.index)
for i, idx in enumerate(price.index):
    if order[i]!=0:
        strategy[idx] = order[i]
        #print idx,order[i]
# force sell at the end
order[-1] = 0 

# generate backtest
bt = twp.Backtest(price, strategy, initialCash=100)
bt.plotTrades()

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

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


Out[22]:
<matplotlib.text.Text at 0x7f330a444fd0>
<matplotlib.figure.Figure at 0x7f330a509b50>

In [40]:
trendy.minitrends(y, window = 30, charts = True);



In [ ]: