-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
115 lines (93 loc) · 3.29 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import pandas as pd
import requests
def compute_bollinger(df, window_size, no_std_devs):
"""This function computes the Bollinger bands.
Parameters
----------
df : df column
This should be the stock price column of a Pandas dataframe.
window_size : int
This should be the number of days to calculate the rolling mean and standard deviation.
no_std_devs : int
Number of standard deviations to calculate the Bollinger bands.
Returns
-------
dataframe columns
The function returns a tuple of the bollinger bands, both low and high.
"""
# Calculate rolling mean and standard deviation
df_mean = df.rolling(window_size).mean()
df_std = df.rolling(window_size).std()
# Calculate bollinger bands
bollinger_low = df_mean - (df_std*no_std_devs)
bollinger_high = df_mean + (df_std*no_std_devs)
return bollinger_low, bollinger_high
def bollinger_strategy(df, window_size, no_std_devs):
"""This function performs the Bollinger Strategy and returns a dataframe that includes the relevant positions.
Parameters
----------
df : DataFrame
Dataframe which includes stock price.
Returns
-------
DataFrame
Returns a DataFrame which includes the position based on the Bollinger Strategy.
"""
# Compute Bollinger bands using above function
df['Bollinger Low'], df['Bollinger High'] = compute_bollinger(df=df['Open'], window_size=window_size, no_std_devs=no_std_devs)
# Initialize an empty Position column to fill in
df['Position'] = None
# Set initial status to open and iterate over each row index
mode = 'open'
for index in range(len(df)):
if index == 0:
continue
# Grab current and previous row
row = df.iloc[index]
prev_row = df.iloc[index-1]
# Hold long position
if mode == 'open' and row['Open'] <= row['Bollinger Low'] and prev_row['Open'] > prev_row['Bollinger Low']:
df.iloc[index, df.columns.get_loc('Position')] = 1
mode = 'close'
# Hold a short position
if mode == 'close' and row['Open'] >= row['Bollinger High'] and prev_row['Open'] < prev_row['Bollinger High']:
df.iloc[index, df.columns.get_loc('Position')] = -1
mode = 'open'
return df
def get_headlines(url):
"""This function returns a json object of headlines from the News API url.
Parameters
----------
url : string
URL to the News API.
Returns
-------
json
json object of headlines.
"""
# Access the url
page = requests.get(url)
# Read the API as a json object
headlines = page.json()
return headlines
def get_news(url):
"""This function creates a dataframe of news articles.
Parameters
----------
url : string
URL to the News API.
Returns
-------
dataframe
dataframe of news information, such as date, title, and source.
"""
headlines = get_headlines(url)
articles = headlines['articles']
dfs = []
for article in articles:
time = article['publishedAt']
title = article['title']
source = article['source']['name']
dfs.append([time, title, source])
data = pd.DataFrame(dfs, columns=['Date', 'Headline', 'Source'])
return data