Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. directory = r'C:\Users\mnix0\Desktop\Stock Project Data\Data\nyse stocks'
  2.  
  3.  
  4. # loads a a file in the form
  5. # 20050225,4.1553,4.2213,4.148,4.1774,344615,0\n
  6. # Where
  7. # [date, open, high, low, close, volume, 0\n]
  8. # and converts line contents to floats for processing
  9. def load_list_from_file():
  10. input_file = open(r'C:\Users\mnix0\Desktop\Stock Project Data\agn.us.txt', 'r')
  11.  
  12. data_list = []
  13. for line in input_file:
  14. # creates temporary list used for appending index
  15. # of each line once converted from string
  16. new_list = []
  17. # for some reason "line" was at times being read as '\n'
  18. # this fixes it, don't know why it happened
  19. if line != '\n':
  20. # splits string into list, removing
  21. # last index containing newline character
  22. value = line.split(',')[:-1]
  23. for item in value:
  24. try:
  25. # appends float value to temporary list
  26. new_list.append(float(item))
  27. # use to handle first line which consists of
  28. #
  29. # Date,Open,High,Low,Close,Volume,OpenInt
  30. #
  31. # in files used for data
  32. except ValueError:
  33. print('OK')
  34. # appends to data list containing
  35. # contents of entire file
  36. data_list.append(new_list)
  37.  
  38.  
  39. # write function for testing
  40. # write_to_file(data_list)
  41. input_file.close()
  42. data_list= data_list[1:]
  43. add_price_move(data_list)
  44.  
  45. def write_data_list_to_file(title_list, write_list):
  46. output_file = open(r'C:\Users\mnix0\Desktop\Stock Project Data\data_output.txt', 'w')
  47. sep = ','
  48. temp_str = ''
  49. # writes title list to file
  50. for index in title_list:
  51. temp_str += str(index) + ','
  52. output_file.write(temp_str+'\n')
  53.  
  54. # joins each list found in each index
  55. # of main data list into string
  56. # by mapping index of of main data list
  57. # and joining that with , separator
  58. for index in write_list:
  59. temp_str = ''
  60. for item in index:
  61. temp_str += str(item).format('.4f')+','
  62. output_file.write(temp_str[:-1] + '\n')
  63. output_file.close()
  64.  
  65.  
  66. # ITS POSSIBLY BROKEN
  67. # This should work, it might be worth spot checking it again somewhere in the middle
  68. # of a data file it hasn't been tested periods other than 5 as of 12/6/2019
  69.  
  70. # it returns a list with an average of the price increase / decrease
  71. # for the length of time defined by period
  72. # totals (current days close - previous days close)
  73. # then divides it by the period
  74.  
  75. # IT WOULD BE A GOOD IDEA TO MODIFY THIS FOR THE LIST TO
  76. # BE CONTAINING THE AVERAGE CHANGE BETWEEN CLOSES AS A
  77. # PERCENTAGE OF THE STOCKS TOTAL PRICE
  78. def calc_rolling_avg_price_move(data_list, period):
  79. avg_move_list = []
  80. # starts looping processes at index after range size
  81. # avoiding indexing errors
  82. for i in range(period+1, len(data_list)):
  83. temp_total = 0
  84. for x in range(period,0, -1):
  85. index = i-x
  86. prev_close_index = index-1
  87. change_for_day = data_list[index][4] - data_list[prev_close_index][4]
  88. temp_total += change_for_day
  89.  
  90. temp_avg = temp_total/period
  91. avg_move_list.append([data_list[i][0],temp_avg])
  92. title_list = ["date", 'average move trailing ' + str(period)+ ' days']
  93. write_data_list_to_file(avg_move_list, title_list)
  94.  
  95.  
  96. # currently accepts list and period
  97. # calculates simple moving average for
  98. # period default = 50
  99. def calc_moving_average(data_list, period = 50):
  100. list_of_moving_averages = []
  101. for curr_date in range(period+1, len(data_list)):
  102. sma = 0
  103. sum_previous_days = 0
  104. for days_ago in range(period, 0, -1):
  105. sum_previous_days += data_list[curr_date-days_ago][4]
  106. sma = sum_previous_days/period
  107. temp_list = [data_list[curr_date][0], sma]
  108. list_of_moving_averages.append(temp_list)
  109. title_list = ['Date', 'SMA '+ str(period)]
  110. write_data_list_to_file(list_of_moving_averages, title_list)
  111.  
  112.  
  113. def add_price_move(data_list, period = 14, di = 0, oi =1, hi = 2,
  114. li = 3, ci = 4, vi = 5):
  115. move_list = [] # up, down
  116.  
  117. for curr_date in range(1, len(data_list)):
  118. prev_date = curr_date - 1
  119. curr_day_close = data_list[curr_date][ci]
  120. prev_day_close = data_list[prev_date][ci]
  121. price_move = curr_day_close - prev_day_close
  122. if price_move > 0:
  123. move_up = price_move
  124. move_down = 0
  125. elif price_move < 0:
  126. move_up = 0
  127. move_down = -(price_move)
  128. else:
  129. move_up = 0
  130. move_down = 0
  131. # appends to list [DATE, UP MOVEMENT, DOWN MOVEMENT]
  132. date = data_list[curr_date][di]
  133. open = data_list[curr_date][oi]
  134. high = data_list[curr_date][hi]
  135. low = data_list[curr_date][li]
  136. close = data_list[curr_date][ci]
  137. vol = data_list[curr_date][vi]
  138.  
  139. new_list = [date, open, high, low, close, move_up, move_down, vol]
  140. data_list[curr_date] = new_list
  141.  
  142. title_list = ['Date', 'open','High','Low','Close','Move up', 'Move down','vol']
  143.  
  144. write_data_list_to_file(title_list,data_list[1:])
  145.  
  146.  
  147.  
  148. def test_main():
  149. load_list_from_file()
  150. #crawl_directory(directory)
  151.  
  152. test_main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement