-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
115 lines (111 loc) · 4.46 KB
/
app.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
from time import sleep
from alpaca_interface import buy_stock
from alpaca_interface import determine_buy_the_dip
from motley_fool_content import get_tickers
import yaml
import requests
import csv
import pandas as pd
import json
from io import StringIO
def get_stocks_to_buy(data_source, config):
if data_source == "motley_fool_stock_advisor":
res = {}
count = 0
while res == {}:
try:
tickers, new_tickers = get_tickers("stock_advisor", config)
for ticker in tickers:
res[ticker] = config['amount']
if 'custom' in config:
for custom_amount in config['custom']:
ticker = list(custom_amount.keys())[0]
res[ticker] = custom_amount[ticker]
for ticker in tickers:
if ticker in new_tickers:
res[ticker] = config['new_stock_amount']
except Exception as e:
print('Failure retrieving stock advisor picks. Attempt ' + count)
count += 1
print(e)
return res
elif data_source == "motley_fool_rule_breakers":
res = {}
count = 0
while res == {}:
try:
tickers, new_tickers = get_tickers("rule_breakers", config)
for ticker in tickers:
res[ticker] = config['amount']
if 'custom' in config:
for custom_amount in config['custom']:
ticker = list(custom_amount.keys())[0]
res[ticker] = custom_amount[ticker]
for ticker in tickers:
if ticker in new_tickers:
res[ticker] = config['new_stock_amount']
except Exception as e:
print('Failure retrieving rule breaker picks. Attempt ' + count)
count += 1
print(e)
return res
elif data_source == "google_sheet":
res = {}
default_amount = config['amount']
csv_url = config['sheet_url']
csv_obj = csv.reader(StringIO(requests.get(url=csv_url).content.decode("utf-8") ), delimiter=',')
it = iter(csv_obj)
next(it, None)
for row in it:
ticker = row[0]
if row[1] == '':
res[ticker] = int(default_amount)
else:
res[ticker] = int(row[1])
return res
else:
return {}
def get_stocks_to_not_buy(config):
res = []
csv_url = config['sheet_url']
csv_obj = csv.reader(StringIO(requests.get(url=csv_url).content.decode("utf-8") ), delimiter=',')
it = iter(csv_obj)
next(it, None)
for row in it:
res = res + row
return res
def main():
stocks_to_buy = {}
stocks_to_not_buy = []
enviornment = "paper"
with open('config.yml') as file:
config_file = yaml.load(file, Loader=yaml.FullLoader)
environment = config_file['environment']
sources = config_file['sources']
for source in sources:
if source == "do_not_buy_list":
stocks_to_not_buy = get_stocks_to_not_buy(sources[source])
else:
if stocks_to_buy == {}:
stocks_to_buy = get_stocks_to_buy(source, sources[source])
else:
new_stocks = get_stocks_to_buy(source, sources[source])
for new_stock in new_stocks.keys():
if new_stock in stocks_to_buy.keys() and new_stocks[new_stock] > stocks_to_buy[new_stock]:
stocks_to_buy[new_stock] = new_stocks[new_stock]
elif new_stock not in stocks_to_buy.keys():
stocks_to_buy[new_stock] = new_stocks[new_stock]
#stocks_to_buy.update(get_stocks_to_buy(source, sources[source]))
print('Stocks to buy and amounts')
print(stocks_to_buy)
for key in stocks_to_buy.keys():
print(key)
if key not in stocks_to_not_buy:
try:
do_we_dip_buy = determine_buy_the_dip(key)
print("are we buying the dip? " + (str(do_we_dip_buy)))
print(buy_stock(key,stocks_to_buy[key],environment, do_we_dip_buy))
except Exception as e:
print(e)
if __name__ == "__main__":
main()