Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. from pathlib import Path
  2. import indicator as ind
  3. import download as dl
  4. import signals as si
  5. import datetime
  6.  
  7. #/Users/apple/Desktop/uci/ics32 /API_KEY.txt
  8. #https://www.alphavantage.co
  9. #TR <1.5 >0.5
  10.  
  11. '''
  12. /Users/apple/Desktop/uci/ics32 /API_KEY.txt
  13. https://www.alphavantage.co
  14. MSFT
  15. 2019-12-01
  16. 2019-12-25
  17. MP 10'''
  18.  
  19. def read_api_path() -> str:
  20.  
  21. input_path = input()
  22. api_path = Path(input_path)
  23.  
  24. with open(api_path, 'r') as f1:
  25. contents = f1.read()
  26. f1.close()
  27. return contents
  28.  
  29.  
  30. def input_url() -> str:
  31. url = input()
  32. return url
  33.  
  34. def input_symbol() -> str:
  35. symbol = input()
  36. return symbol
  37.  
  38.  
  39. def get_time_range(start,end) -> int:
  40.  
  41. date1 = datetime.date.fromisoformat(start)
  42. date2 = datetime.date.fromisoformat(end)
  43.  
  44. daydiff = date2.weekday() - date1.weekday()
  45.  
  46. days = int(((date2 - date1).days - daydiff) / 7 * 5 + min(daydiff, 5) - (max(date2.weekday() - 4, 0) % 5))
  47. return days
  48.  
  49.  
  50. def print_result(sym,day_range,strategy):
  51. print(sym)
  52. print(day_range)
  53. print(strategy)
  54. print('Date\tOpen\tHigh\tLow\tClose\tVolume\tIndicator\tBuy?\tSell?')
  55.  
  56. def truerange_ind(info,smaller,bigger):
  57. list_indicator = []
  58. list_indicator.append('')
  59. for i in range(1,len(info)):
  60. high = float(info[i].high)
  61. low = float(info[i].low)
  62. close = float(info[i-1].close)
  63.  
  64. a = ind.TrueRange(high,low,close)
  65. tr = a.indicator()
  66. info[i].setIndicator(tr)
  67. list_indicator.append(tr)
  68. return list_indicator
  69.  
  70.  
  71. def truerange_sig(list_ind,smaller,bigger):
  72. list_signals = ['']
  73. for i in list_ind[1:]:
  74. b = si.TrueRange(i,smaller,bigger)
  75. list = b.signal
  76. list_signals.append(list)
  77. return list_signals
  78.  
  79.  
  80. def Mp_ind(info,days):
  81. cur_day_close = []
  82. previous_day_close = []
  83. pre1_close = []
  84. pre1_avg_close = []
  85. for i in range(len(info)):
  86. if i >= int(days) - 1:
  87. for x in range(i - int(days) + 1):
  88. close = float(info[x].close)
  89. c = ind.MP(info,days)
  90. avg_close = c.indicator()
  91. info[x].setIndicator(avg_close)
  92. cur_day_close.append(close)
  93. previous_day_close.append(avg_close)
  94. #for x in range(i - int(days)):
  95. #close1 = float(info[x].close)
  96. #c = ind.MP(info,days)
  97. #avg_close1 = c.indicator()
  98. #info[x].setIndicator(avg_close1)
  99. #pre1_avg_close.append(avg_close1)
  100. #pre1_close.append(close1)
  101. list1 = [cur_day_close , previous_day_close]
  102. return list1
  103.  
  104.  
  105. def Mp_sig(list):
  106. list_signals = []
  107. for i in list:
  108. d = si.MP(list)
  109. list = d.signal()
  110. list_signals.append(list)
  111. return list_signals
  112.  
  113.  
  114.  
  115.  
  116. def print_board(info,ind,sigs):
  117. for i in range(len(info)):
  118. date = info[i].date
  119. open = info[i].open
  120. high = info[i].high
  121. low = info[i].low
  122. close = info[i].close
  123. volume = info[i].volume
  124. ind1 = str(ind[i])
  125. if len(sigs[i]) == 0:
  126. signal = ''
  127. elif len(sigs[i]) == 1 and sigs[i][0] == 'BUY':
  128. signal = 'BUY' + '\t'
  129. elif len(sigs[i]) == 1 and sigs[i][0] == 'SELL':
  130. signal = '\t' + 'SELL'
  131. elif len(sigs[i]) == 2:
  132. signal = 'BUY' + '\t' + 'SELL'
  133. print(f'{date}\t{open}\t{high}\t{low}\t{close}\t{volume}\t{ind1}\t{signal}')
  134.  
  135. if __name__ == '__main__':
  136. apikey = read_api_path()
  137. partial_url = input_url()
  138. symbol = input_symbol()
  139. start_date = input()
  140. end_date = input()
  141. time_range = get_time_range(start_date, end_date) + 1
  142.  
  143. ind_strategy = input()
  144. full_url = dl.build_search_url(apikey,partial_url,symbol,ind_strategy)
  145. jason_return = dl.get_result(full_url)
  146. info = dl.process_info(jason_return,start_date,end_date)
  147. print(info)
  148.  
  149. command = ind_strategy[0:2]
  150. if command == 'TR':
  151. command1, smaller, bigger = ind_strategy.split(' ')
  152. list_tr = truerange_ind(info,smaller,bigger)
  153. signals = truerange_sig(list_tr,float(smaller[1:]),float(bigger[1:]))
  154. print_result(symbol, time_range, ind_strategy)
  155. print_board(info,list_tr,signals)
  156.  
  157. elif command == 'MP':
  158. command1, day_num = ind_strategy.split(' ')
  159. list_mp = Mp_ind(info,day_num)
  160. signals = Mp_sig(list_mp)
  161. print(signals)
  162. print_result(symbol, time_range, ind_strategy)
  163. print_board(info,list_mp,signals)
  164.  
  165. elif command == 'DP':
  166. command1, days , buy, sell = ind_strategy.split(' ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement