Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pathlib import Path
- import indicator as ind
- import download as dl
- import signals as si
- import datetime
- #/Users/apple/Desktop/uci/ics32 /API_KEY.txt
- #https://www.alphavantage.co
- #TR <1.5 >0.5
- '''
- /Users/apple/Desktop/uci/ics32 /API_KEY.txt
- https://www.alphavantage.co
- MSFT
- 2019-12-01
- 2019-12-25
- MP 10'''
- def read_api_path() -> str:
- input_path = input()
- api_path = Path(input_path)
- with open(api_path, 'r') as f1:
- contents = f1.read()
- f1.close()
- return contents
- def input_url() -> str:
- url = input()
- return url
- def input_symbol() -> str:
- symbol = input()
- return symbol
- def get_time_range(start,end) -> int:
- date1 = datetime.date.fromisoformat(start)
- date2 = datetime.date.fromisoformat(end)
- daydiff = date2.weekday() - date1.weekday()
- days = int(((date2 - date1).days - daydiff) / 7 * 5 + min(daydiff, 5) - (max(date2.weekday() - 4, 0) % 5))
- return days
- def print_result(sym,day_range,strategy):
- print(sym)
- print(day_range)
- print(strategy)
- print('Date\tOpen\tHigh\tLow\tClose\tVolume\tIndicator\tBuy?\tSell?')
- def truerange_ind(info,smaller,bigger):
- list_indicator = []
- list_indicator.append('')
- for i in range(1,len(info)):
- high = float(info[i].high)
- low = float(info[i].low)
- close = float(info[i-1].close)
- a = ind.TrueRange(high,low,close)
- tr = a.indicator()
- info[i].setIndicator(tr)
- list_indicator.append(tr)
- return list_indicator
- def truerange_sig(list_ind,smaller,bigger):
- list_signals = ['']
- for i in list_ind[1:]:
- b = si.TrueRange(i,smaller,bigger)
- list = b.signal
- list_signals.append(list)
- return list_signals
- def Mp_ind(info,days):
- cur_day_close = []
- previous_day_close = []
- pre1_close = []
- pre1_avg_close = []
- for i in range(len(info)):
- if i >= int(days) - 1:
- for x in range(i - int(days) + 1):
- close = float(info[x].close)
- c = ind.MP(info,days)
- avg_close = c.indicator()
- info[x].setIndicator(avg_close)
- cur_day_close.append(close)
- previous_day_close.append(avg_close)
- #for x in range(i - int(days)):
- #close1 = float(info[x].close)
- #c = ind.MP(info,days)
- #avg_close1 = c.indicator()
- #info[x].setIndicator(avg_close1)
- #pre1_avg_close.append(avg_close1)
- #pre1_close.append(close1)
- list1 = [cur_day_close , previous_day_close]
- return list1
- def Mp_sig(list):
- list_signals = []
- for i in list:
- d = si.MP(list)
- list = d.signal()
- list_signals.append(list)
- return list_signals
- def print_board(info,ind,sigs):
- for i in range(len(info)):
- date = info[i].date
- open = info[i].open
- high = info[i].high
- low = info[i].low
- close = info[i].close
- volume = info[i].volume
- ind1 = str(ind[i])
- if len(sigs[i]) == 0:
- signal = ''
- elif len(sigs[i]) == 1 and sigs[i][0] == 'BUY':
- signal = 'BUY' + '\t'
- elif len(sigs[i]) == 1 and sigs[i][0] == 'SELL':
- signal = '\t' + 'SELL'
- elif len(sigs[i]) == 2:
- signal = 'BUY' + '\t' + 'SELL'
- print(f'{date}\t{open}\t{high}\t{low}\t{close}\t{volume}\t{ind1}\t{signal}')
- if __name__ == '__main__':
- apikey = read_api_path()
- partial_url = input_url()
- symbol = input_symbol()
- start_date = input()
- end_date = input()
- time_range = get_time_range(start_date, end_date) + 1
- ind_strategy = input()
- full_url = dl.build_search_url(apikey,partial_url,symbol,ind_strategy)
- jason_return = dl.get_result(full_url)
- info = dl.process_info(jason_return,start_date,end_date)
- print(info)
- command = ind_strategy[0:2]
- if command == 'TR':
- command1, smaller, bigger = ind_strategy.split(' ')
- list_tr = truerange_ind(info,smaller,bigger)
- signals = truerange_sig(list_tr,float(smaller[1:]),float(bigger[1:]))
- print_result(symbol, time_range, ind_strategy)
- print_board(info,list_tr,signals)
- elif command == 'MP':
- command1, day_num = ind_strategy.split(' ')
- list_mp = Mp_ind(info,day_num)
- signals = Mp_sig(list_mp)
- print(signals)
- print_result(symbol, time_range, ind_strategy)
- print_board(info,list_mp,signals)
- elif command == 'DP':
- command1, days , buy, sell = ind_strategy.split(' ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement