Weekend Trend Trader

This notebook outlines experiments in getting my weekend trend trader code implemented.


In [4]:
import pandas as pd
import pandas.io.data as web
import datetime

Get Daily price data for Caltex.


In [5]:
# date ranges
end = datetime.date(2015, 1, 26)
start = end + datetime.timedelta(weeks=-21)
start


Out[5]:
datetime.date(2014, 9, 1)

In [6]:
# Get daily price data for Caltex
ctx = web.DataReader('ctx.ax', 'yahoo', start, end)
ctx


Out[6]:
Open High Low Close Volume Adj Close
Date
2014-09-01 28.55 28.93 28.52 28.60 447000 27.80
2014-09-02 28.60 29.18 28.57 29.05 797700 28.23
2014-09-03 29.05 29.08 28.82 28.95 666000 28.14
2014-09-04 28.95 29.27 28.89 29.09 1048000 28.27
2014-09-05 28.94 29.07 28.55 28.69 869300 28.16
2014-09-08 28.74 29.02 28.61 28.80 589300 28.27
2014-09-09 28.82 29.07 28.75 29.01 543200 28.47
2014-09-10 29.00 29.00 28.49 28.66 566000 28.13
2014-09-11 28.89 28.89 28.57 28.80 473600 28.27
2014-09-12 28.79 28.91 28.46 28.90 405500 28.37
2014-09-15 28.56 28.61 28.17 28.18 393300 27.66
2014-09-16 28.05 28.30 27.95 28.30 624700 27.78
2014-09-17 28.50 28.50 28.19 28.32 915400 27.80
2014-09-18 28.53 28.95 28.43 28.87 634000 28.34
2014-09-19 28.86 29.02 28.70 28.74 379000 28.21
2014-09-22 28.70 28.74 28.37 28.67 308800 28.14
2014-09-23 28.39 28.72 28.28 28.69 437100 28.16
2014-09-24 28.35 28.60 28.34 28.56 342300 28.03
2014-09-25 28.61 28.78 28.61 28.72 211700 28.19
2014-09-26 28.20 28.28 28.02 28.24 393000 27.72
2014-09-29 28.11 28.44 28.01 28.01 345300 27.49
2014-09-30 28.00 28.45 27.94 27.99 850500 27.47
2014-10-01 28.10 28.42 28.10 28.35 518000 27.83
2014-10-02 28.33 28.50 28.11 28.40 414100 27.88
2014-10-03 28.20 28.95 28.20 28.63 457700 28.10
2014-10-06 28.72 28.82 28.32 28.78 246000 28.25
2014-10-07 28.86 29.33 28.32 28.75 599000 28.22
2014-10-08 28.78 29.32 28.66 29.11 920200 28.57
2014-10-09 29.43 29.44 28.77 29.09 577500 28.55
2014-10-10 28.11 28.61 28.09 28.52 592100 27.99
... ... ... ... ... ... ...
2014-12-16 32.42 32.71 31.76 32.30 1002000 31.70
2014-12-17 32.70 33.31 32.60 32.86 851100 32.25
2014-12-18 33.44 33.79 33.08 33.29 944400 32.68
2014-12-19 34.00 34.42 33.67 33.94 1014800 33.31
2014-12-22 34.15 34.92 34.14 34.80 842600 34.16
2014-12-23 35.05 35.07 34.39 34.54 516800 33.90
2014-12-24 34.96 35.09 34.93 35.00 445600 34.35
2014-12-25 35.00 35.00 35.00 35.00 0 34.35
2014-12-26 35.00 35.00 35.00 35.00 0 34.35
2014-12-29 35.09 35.14 34.80 35.01 357000 34.36
2014-12-30 35.01 35.16 34.22 34.29 532100 33.66
2014-12-31 34.34 34.65 34.20 34.21 198200 33.58
2015-01-01 34.21 34.21 34.21 34.21 0 33.58
2015-01-02 34.19 34.71 34.19 34.44 318900 33.80
2015-01-05 34.75 35.44 34.61 35.30 435900 34.65
2015-01-06 35.02 35.10 34.54 34.81 731500 34.17
2015-01-07 35.18 35.18 34.20 34.46 725600 33.82
2015-01-08 34.68 34.80 34.24 34.40 886300 33.77
2015-01-09 35.10 35.44 34.91 35.35 513100 34.70
2015-01-12 35.41 35.42 34.84 34.93 423200 34.29
2015-01-13 34.76 35.23 34.50 35.16 618200 34.51
2015-01-14 35.23 35.48 34.69 34.98 461200 34.33
2015-01-15 35.04 35.41 34.64 34.77 418500 34.13
2015-01-16 34.35 34.58 33.65 33.88 345700 33.25
2015-01-19 33.99 34.38 33.91 33.98 403600 33.35
2015-01-20 33.76 34.04 33.46 33.54 318400 32.92
2015-01-21 33.87 35.20 33.87 34.44 611400 33.80
2015-01-22 34.44 34.48 32.83 33.09 814900 32.48
2015-01-23 33.43 33.92 32.47 33.82 664900 33.20
2015-01-26 33.82 33.82 33.82 33.82 0 33.20

106 rows × 6 columns

Transform to Weekly

Not sure if there is a better way of doing this? So far this is the best I have found. The Pandas API doco is rather poor in this area.


In [7]:
# resample to weekly ohlc data starting on Monday
weekly = pd.DataFrame()
weekly["Open"] = ctx['Open'].resample('W-FRI', how='first')
weekly["High"] = ctx['High'].resample('W-FRI', how='max')
weekly["Low"] = ctx['Low'].resample('W-FRI', how='min')
weekly["Close"] = ctx['Close'].resample('W-FRI', how='last')
weekly["Volume"] = ctx['Volume'].resample('W-FRI', how='mean')
weekly


Out[7]:
Open High Low Close Volume
Date
2014-09-05 28.55 29.27 28.52 28.69 765600
2014-09-12 28.74 29.07 28.46 28.90 515520
2014-09-19 28.56 29.02 27.95 28.74 589280
2014-09-26 28.70 28.78 28.02 28.24 338580
2014-10-03 28.11 28.95 27.94 28.63 517120
2014-10-10 28.72 29.44 28.09 28.52 586960
2014-10-17 27.93 29.29 27.45 28.56 784440
2014-10-24 28.95 30.48 28.25 30.39 735920
2014-10-31 30.80 31.25 30.67 31.04 711020
2014-11-07 31.12 31.58 30.79 31.48 725200
2014-11-14 31.60 32.38 31.15 31.62 600780
2014-11-21 31.93 32.69 30.57 30.89 806880
2014-11-28 31.10 31.79 30.03 30.25 1184520
2014-12-05 29.95 31.22 29.05 31.13 727940
2014-12-12 31.43 32.75 30.00 31.63 893920
2014-12-19 31.38 34.42 31.38 33.94 901140
2014-12-26 34.15 35.09 34.14 35.00 361000
2015-01-02 35.09 35.16 34.19 34.44 281240
2015-01-09 34.75 35.44 34.20 35.35 658480
2015-01-16 35.41 35.48 33.65 33.88 453360
2015-01-23 33.99 35.20 32.47 33.82 562640
2015-01-30 33.82 33.82 33.82 33.82 0

In [8]:
# Work out 20 week Rate of Change in price.
round((weekly["Close"][-1] - weekly["Close"][-21]) / weekly["Close"][-21], 2)


Out[8]:
0.17000000000000001

In [9]:
# Work out 20 week high of closing prices
weekly["Close"][-21:-1]


Out[9]:
Date
2014-09-12    28.90
2014-09-19    28.74
2014-09-26    28.24
2014-10-03    28.63
2014-10-10    28.52
2014-10-17    28.56
2014-10-24    30.39
2014-10-31    31.04
2014-11-07    31.48
2014-11-14    31.62
2014-11-21    30.89
2014-11-28    30.25
2014-12-05    31.13
2014-12-12    31.63
2014-12-19    33.94
2014-12-26    35.00
2015-01-02    34.44
2015-01-09    35.35
2015-01-16    33.88
2015-01-23    33.82
Freq: W-FRI, Name: Close, dtype: float64

In [10]:
weekly["Close"][-21:-1].max()


Out[10]:
35.350000000000001

All Ords Moving Average

To enter trade, 10 week moving average of All Ords must be greater than last week.


In [11]:
# date ranges
end = datetime.date(2015, 1, 26)
start = end + datetime.timedelta(weeks=-12)
all_ords = web.DataReader('^AORD', 'yahoo', start, end)
all_ords


Out[11]:
Open High Low Close Volume Adj Close
Date
2014-11-03 5505.0 5515.1 5478.3 5485.0 699734400 5485.0
2014-11-04 5485.0 5508.3 5482.3 5498.2 598414900 5498.2
2014-11-05 5498.2 5498.2 5459.7 5492.8 761462700 5492.8
2014-11-06 5492.8 5521.0 5467.6 5479.2 822648200 5479.2
2014-11-07 5479.2 5522.1 5474.3 5522.1 875994700 5522.1
2014-11-10 5522.1 5523.5 5480.5 5501.4 845126100 5501.4
2014-11-11 5501.4 5510.6 5486.5 5494.0 775103100 5494.0
2014-11-12 5494.0 5494.0 5439.5 5443.0 761391300 5443.0
2014-11-13 5443.0 5450.5 5415.8 5423.5 690032900 5423.5
2014-11-14 5423.5 5436.7 5401.8 5433.8 613263400 5433.8
2014-11-17 5433.8 5440.4 5390.2 5396.6 619406700 5396.6
2014-11-18 5396.6 5414.1 5372.9 5383.1 597001100 5383.1
2014-11-19 5383.1 5384.0 5350.9 5352.5 806696100 5352.5
2014-11-20 5352.5 5352.5 5302.4 5302.5 839300000 5302.5
2014-11-21 5302.5 5312.8 5281.4 5292.1 840697100 5292.1
2014-11-24 5292.1 5363.7 5292.1 5349.0 698192800 5349.0
2014-11-25 5349.0 5349.0 5286.0 5320.9 1067560600 5320.9
2014-11-26 5320.9 5380.3 5320.9 5380.3 686953400 5380.3
2014-11-27 5377.7 5379.4 5377.2 5379.4 0 5379.4
2014-11-28 5381.4 5381.4 5298.1 5298.1 825749500 5298.1
2014-12-01 5298.1 5300.5 5190.7 5190.7 0 5190.7
2014-12-02 5190.7 5263.1 5190.7 5260.0 901188900 5260.0
2014-12-03 5301.2 5301.2 5301.2 5301.2 0 5301.2
2014-12-04 5345.4 5345.4 5345.4 5345.4 0 5345.4
2014-12-05 5345.4 5364.7 5287.7 5313.6 831605500 5313.6
2014-12-08 5348.3 5348.3 5311.2 5315.6 85616100 5315.6
2014-12-09 5258.3 5258.3 5258.3 5258.3 0 5258.3
2014-12-10 5258.3 5263.7 5199.9 5237.1 0 5237.1
2014-12-11 5237.1 5237.1 5168.4 5207.4 0 5207.4
2014-12-12 5207.4 5218.2 5182.9 5196.9 877389600 5196.9
2014-12-15 5164.6 5164.6 5164.6 5164.6 0 5164.6
2014-12-16 5164.6 5164.6 5121.7 5131.0 0 5131.0
2014-12-17 5131.0 5166.1 5128.7 5140.6 0 5140.6
2014-12-18 5154.5 5239.4 5140.6 5189.7 0 5189.7
2014-12-19 5189.7 5316.4 5189.7 5312.7 0 5312.7
2014-12-22 5312.7 5414.1 5312.7 5414.1 0 5414.1
2014-12-23 5414.1 5414.1 5356.4 5356.4 0 5356.4
2014-12-24 5356.4 5372.7 5343.0 5369.5 0 5369.5
2014-12-29 5369.5 5453.2 5369.5 5447.0 0 5447.0
2014-12-30 5447.0 5458.3 5391.3 5392.3 0 5392.3
2014-12-31 5392.3 5413.3 5378.1 5388.6 0 5388.6
2015-01-02 5388.6 5415.0 5366.4 5415.0 0 5415.0
2015-01-05 5415.0 5447.6 5409.4 5429.5 0 5429.5
2015-01-06 5429.5 5429.5 5321.5 5346.2 0 5346.2
2015-01-07 5346.2 5348.9 5301.2 5334.5 0 5334.5
2015-01-08 5334.5 5373.4 5334.5 5359.4 0 5359.4
2015-01-09 5359.4 5440.1 5359.4 5440.1 0 5440.1
2015-01-12 5440.1 5440.1 5396.3 5399.5 0 5399.5
2015-01-13 5399.5 5399.5 5352.3 5382.1 0 5382.1
2015-01-14 5382.1 5382.7 5325.6 5332.2 0 5332.2
2015-01-15 5332.2 5332.2 5283.9 5310.6 0 5310.6
2015-01-16 5310.6 5311.8 5248.5 5278.8 0 5278.8
2015-01-19 5278.8 5348.9 5278.8 5289.0 0 5289.0
2015-01-20 5289.0 5293.3 5263.5 5286.8 0 5286.8
2015-01-21 5286.8 5367.4 5286.8 5367.4 0 5367.4
2015-01-22 5367.4 5404.2 5367.4 5390.5 0 5390.5
2015-01-23 5390.5 5468.2 5390.5 5468.2 0 5468.2
2015-01-26 5390.5 5468.2 5390.5 5468.2 0 5468.2

In [12]:
weekly_ords = pd.DataFrame()
weekly_ords["Close"] = all_ords["Close"].resample("W-FRI", how="last")
weekly_ords


Out[12]:
Close
Date
2014-11-07 5522.1
2014-11-14 5433.8
2014-11-21 5292.1
2014-11-28 5298.1
2014-12-05 5313.6
2014-12-12 5196.9
2014-12-19 5312.7
2014-12-26 5369.5
2015-01-02 5415.0
2015-01-09 5440.1
2015-01-16 5278.8
2015-01-23 5468.2
2015-01-30 5468.2

In [13]:
# 10 week moving average
pd.rolling_mean(weekly_ords["Close"], 10)


Out[13]:
Date
2014-11-07        NaN
2014-11-14        NaN
2014-11-21        NaN
2014-11-28        NaN
2014-12-05        NaN
2014-12-12        NaN
2014-12-19        NaN
2014-12-26        NaN
2015-01-02        NaN
2015-01-09    5359.39
2015-01-16    5335.06
2015-01-23    5338.50
2015-01-30    5356.11
Freq: W-FRI, dtype: float64

In [14]:
weekly_ords.plot()


Out[14]:
<matplotlib.axes._subplots.AxesSubplot at 0x107cfa160>

In [15]:
nums = pd.Series([1,2,3,4,5,6,7,8,9,10,11])
pd.rolling_mean(nums,5)


Out[15]:
0    NaN
1    NaN
2    NaN
3    NaN
4      3
5      4
6      5
7      6
8      7
9      8
10     9
dtype: float64