Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

######################################################

# Stock Technical Analysis with Python #


# Parabolic Stop and Reverse SAR(0.02,0.2) #
# (c) Diego Fernandez Garcia 2015-2017 #
# www.exfinsis.com #
######################################################

# 1. Packages and Data

# Packages Importing
import numpy as np
import pandas as pd
# import pandas_datareader.data as web
import matplotlib.pyplot as plt
import talib as ta

# Data Downloading or Reading

# Google Finance
# No Adjusted Close Prices
# spy = web.DataReader('SPY', 'google', '2016-01-01', '2017-01-01')
# spy['Adj Close'] = spy['Close']

# Yahoo Finance
# spy = web.DataReader('SPY', 'yahoo', '2016-01-01', '2017-01-01')

# Data Reading
spy = pd.read_csv("Data//Stock-Technical-Analysis-Data.txt", index_col='Date',
parse_dates=True)

##########

# 2. Parabolic Stop and Reverse SAR(0.02,0.2) Calculation and Chart

# Technical Indicator Calculation


spy['sar'] = ta.SAR(np.asarray(spy['High']), np.asarray(spy['Low']),
acceleration=0.02, maximum=0.2)
# Technical Indicator Chart
fig, ax = plt.subplots()
ax.plot(spy['Close'])
ax.plot(spy['sar'], marker='o', linestyle='')
plt.title('SPY Close Prices & Parabolic Stop and Reverse SAR(0.02,0.2)')
plt.legend(loc='upper left')
plt.show()

##########

# 3. Stop and Reverse Trading Signals


# Previous Periods Data (avoid backtesting bias)
spy['Close(-1)'] = spy['Close'].shift(1)
spy['sar(-1)'] = spy['sar'].shift(1)
spy['Close(-2)'] = spy['Close'].shift(2)
spy['sar(-2)'] = spy['sar'].shift(2)
# Generate Trading Signals (buy=1 , sell=-1, do nothing=0)
spy['sarsig'] = 0
sarsig = 0
for i, r in enumerate(spy.iterrows()):
if r[1]['Close(-2)'] < r[1]['sar(-2)'] and r[1]['Close(-1)'] > r[1]['sar(-1)']:
sarsig = 1
elif r[1]['Close(-2)'] > r[1]['sar(-2)'] and r[1]['Close(-1)'] < r[1]['sar(-
1)']:
sarsig = -1
else:
sarsig = 0
spy.iloc[i, 11] = sarsig
# Trading Signals Chart
fig2, ax = plt.subplots(2, sharex=True)
ax[0].plot(spy['Close'])
ax[0].plot(spy['sar'], marker='o', linestyle='')
ax[0].legend(loc='upper left')
ax[1].plot(spy['sarsig'], marker='o', linestyle='')
ax[1].legend(loc='upper left')
plt.suptitle('SPY Close Prices & Parabolic Stop and Reverse SAR(0.02,0.2)')
plt.show()

##########

# 4. Stop and Reverse Trading Strategy


# Generate Trading Strategy (own stock=1 , not own stock=0, short-selling not
available)
spy['sarstr'] = 1
sarstr = 0
for i, r in enumerate(spy.iterrows()):
if r[1]['sarsig'] == 1:
sarstr = 1
elif r[1]['sarsig'] == -1:
sarstr = 0
else:
sarstr = spy['sarstr'][i-1]
spy.iloc[i, 12] = sarstr
# Trading Strategy Chart
fig3, ax = plt.subplots(2, sharex=True)
ax[0].plot(spy['Close'])
ax[0].plot(spy['sar'], marker='o', linestyle='')
ax[0].legend(loc='upper left')
ax[1].plot(spy['sarstr'], marker='o', linestyle='')
ax[1].legend(loc='upper left')
plt.suptitle('SPY Close Prices & Parabolic Stop and Reverse SAR(0.02,0.2)')
plt.show()

##########

# 5. Stop and Reverse Strategy Performance Comparison

# 5.1. Strategies Daily Returns


# Stop and Reverse Strategy Without Trading Commissions
spy['sardrt'] = ((spy['Close']/spy['Close'].shift(1))-1)*spy['sarstr']
spy.iloc[0, 13] = 0
# Stop and Reverse Strategy With Trading Commissions (1% Per Trade)
spy['sarstr(-1)'] = spy['sarstr'].shift(1)
spy['sartc'] = spy['sarsig']
sartc = 0
for i, r in enumerate(spy.iterrows()):
if (r[1]['sarsig'] == 1 or r[1]['sarsig'] == -1) and r[1]['sarstr'] != r[1]
['sarstr(-1)']:
sartc = 0.01
else:
sartc = 0.00
spy.iloc[i, 15] = sartc
spy['sardrtc'] = (((spy['Close']/spy['Close'].shift(1))-1)-
spy['sartc'])*spy['sarstr']
spy.iloc[0, 16] = 0
# Buy and Hold Strategy
spy['bhdrt'] = (spy['Close']/spy['Close'].shift(1))-1
spy.iloc[0, 17] = 0

# 5.2. Strategies Cumulative Returns


# Cumulative Returns Calculation
spy['sarcrt'] = np.cumprod(spy['sardrt']+1)-1
spy['sarcrtc'] = np.cumprod(spy['sardrtc']+1)-1
spy['bhcrt'] = np.cumprod(spy['bhdrt']+1)-1
# Cumulative Returns Chart
spy.plot(y=['sarcrt', 'sarcrtc', 'bhcrt'])
plt.title('Parabolic Stop and Reverse SAR(0.02,0.2) vs Buy & Hold')
plt.legend(loc='upper left')
plt.show()

# 5.3. Strategies Performance Metrics


# Annualized Returns
saryrt = spy.iloc[251, 18]
saryrtc = spy.iloc[251, 19]
bhyrt = spy.iloc[251, 20]
# Annualized Standard Deviation
sarstd = np.std(spy['sardrt'])*np.sqrt(252)
sarstdc = np.std(spy['sardrtc'])*np.sqrt(252)
bhstd = np.std(spy['bhdrt'])*np.sqrt(252)
# Annualized Sharpe Ratio
sarsr = saryrt/sarstd
sarsrc = saryrtc/sarstdc
bhsr = bhyrt/bhstd
# Summary Results Data Table
data = [{'0': '', '1': 'SAR(0.02,0.2)', '2': 'SAR(0.02,0.2)TC', '3': 'B&H'},
{'0': 'Annualized Return', '1': saryrt, '2': saryrtc, '3': bhyrt},
{'0': 'Annualized Standard Deviation', '1': sarstd, '2': sarstdc, '3':
bhstd},
{'0': 'Annualized Sharpe Ratio (Rf=0%)', '1': sarsr, '2': sarsrc, '3':
bhsr}]
table = pd.DataFrame(data)
print(spy)
print(table)

You might also like