Files
Stock/robot.py
T
jens 1af960bdae - fixed USD
git-svn-id: http://moon:8086/svn/projects/Stock@352 fda53097-d464-4ada-af97-ba876c37ca34
2020-01-07 20:47:23 +00:00

123 lines
3.1 KiB
Python

# Stock Investors Financial Math
# https://www.fmlabs.com/reference/default.htm?url=SimpleMA.htm
# Alpha Vantage
# https://www.alphavantage.co/documentation/
# Pandas
# https://pandas.pydata.org/pandas-docs/stable/index.html
# Knowledge
# https://www.investopedia.com/articles/technical/02/050602.asp
# https://ntguardian.wordpress.com/2016/09/19/introduction-stock-market-data-python-1
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rc('figure', max_open_warning = 0)
from stock import Stock
import argparse
import json
params = {
'analyze_range_days' : 5,
'plot_range_days' : 0,
'btfd' : {'thresh_max' : -7, 'thresh_min' : 1, 'cand_window' : 5},
'k_euro' : 1 / 1.11,
'ema_alpha' : 0.75,
'sma_days' : 10,
'q_days' : 10,
'fetch_on_outdated' : True
}
show_symbols = []
parser = argparse.ArgumentParser()
parser.add_argument('--symbol', help='Symbol to fetch')
parser.add_argument('--analyze-range', type=int, help='Analyze window [days]')
parser.add_argument('--plot-range', type=int, help='Plot range [days]')
args = parser.parse_args()
f = open('stocks.json', 'r')
s = f.read()
symbols = json.loads(s)
f.close()
if args.symbol is not None:
show_symbols = [args.symbol]
if args.symbol not in symbols:
symbols[args.symbol] = {'name' : 'Full name here', 'currency' : 'USD'}
s = json.dumps(symbols, sort_keys=True, indent=4)
f = open('stocks.json', 'w')
f.write(s)
f.close()
else:
print ("{} is already in database".format(args.symbol))
if args.analyze_range is not None:
params['analyze_range_days'] = args.analyze_range
if args.plot_range is not None:
params['plot_range_days'] = args.plot_range
do_plot = params['plot_range_days'] > 0
buy_list = {}
def buy_callback(data):
name = data['name']
item = data['item']
if name not in buy_list:
buy_list[name] = {'items': [item]}
else:
buy_list[name]['items'].append(item)
print(data)
figNum = 1
if len(show_symbols) > 0:
for symbol in show_symbols:
if symbol in symbols:
title = Stock(params, symbol, symbols[symbol])
title.fetch()
title.statistics()
try:
ind = title.analyze(buy_callback, range_days=params['analyze_range_days'])
if do_plot:
title.show(ind, figNum=figNum)
figNum += 1
except:
print ("Exception occurred for {}".format(symbol))
else:
for symbol in symbols:
title = Stock(params, symbol, symbols[symbol])
title.fetch()
title.statistics()
try:
ind = title.analyze(buy_callback, range_days=params['analyze_range_days'])
if Stock.has_candidate(ind, 'BTFD'):
print('-----------------------------------------------')
if do_plot:
title.show(ind, figNum=figNum)
figNum += 1
except:
print ("Exception occurred for {}".format(symbol))
gain_accum = 0
num_stocks = 0
for symbol in buy_list:
items = buy_list[symbol]['items']
title = Stock(params, symbol, symbols[symbol])
title.fetch()
title.statistics()
gain = title.get_latest('close_n') - items[0]['value']
gain_accum += gain
num_stocks += 1
print ('{}: Gain = {} %'.format(symbol, gain))
if num_stocks > 0:
print ('Gain total = {} %'.format(gain_accum/num_stocks))
if do_plot:
plt.show()