Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- directory = r'C:\Users\mnix0\Desktop\Stock Project Data\Data\nyse stocks'
- # loads a a file in the form
- # 20050225,4.1553,4.2213,4.148,4.1774,344615,0\n
- # Where
- # [date, open, high, low, close, volume, 0\n]
- # and converts line contents to floats for processing
- def load_list_from_file():
- input_file = open(r'C:\Users\mnix0\Desktop\Stock Project Data\agn.us.txt', 'r')
- data_list = []
- for line in input_file:
- # creates temporary list used for appending index
- # of each line once converted from string
- new_list = []
- # for some reason "line" was at times being read as '\n'
- # this fixes it, don't know why it happened
- if line != '\n':
- # splits string into list, removing
- # last index containing newline character
- value = line.split(',')[:-1]
- for item in value:
- try:
- # appends float value to temporary list
- new_list.append(float(item))
- # use to handle first line which consists of
- #
- # Date,Open,High,Low,Close,Volume,OpenInt
- #
- # in files used for data
- except ValueError:
- print('OK')
- # appends to data list containing
- # contents of entire file
- data_list.append(new_list)
- # write function for testing
- # write_to_file(data_list)
- input_file.close()
- data_list= data_list[1:]
- add_price_move(data_list)
- def write_data_list_to_file(title_list, write_list):
- output_file = open(r'C:\Users\mnix0\Desktop\Stock Project Data\data_output.txt', 'w')
- sep = ','
- temp_str = ''
- # writes title list to file
- for index in title_list:
- temp_str += str(index) + ','
- output_file.write(temp_str+'\n')
- # joins each list found in each index
- # of main data list into string
- # by mapping index of of main data list
- # and joining that with , separator
- for index in write_list:
- temp_str = ''
- for item in index:
- temp_str += str(item).format('.4f')+','
- output_file.write(temp_str[:-1] + '\n')
- output_file.close()
- # ITS POSSIBLY BROKEN
- # This should work, it might be worth spot checking it again somewhere in the middle
- # of a data file it hasn't been tested periods other than 5 as of 12/6/2019
- # it returns a list with an average of the price increase / decrease
- # for the length of time defined by period
- # totals (current days close - previous days close)
- # then divides it by the period
- # IT WOULD BE A GOOD IDEA TO MODIFY THIS FOR THE LIST TO
- # BE CONTAINING THE AVERAGE CHANGE BETWEEN CLOSES AS A
- # PERCENTAGE OF THE STOCKS TOTAL PRICE
- def calc_rolling_avg_price_move(data_list, period):
- avg_move_list = []
- # starts looping processes at index after range size
- # avoiding indexing errors
- for i in range(period+1, len(data_list)):
- temp_total = 0
- for x in range(period,0, -1):
- index = i-x
- prev_close_index = index-1
- change_for_day = data_list[index][4] - data_list[prev_close_index][4]
- temp_total += change_for_day
- temp_avg = temp_total/period
- avg_move_list.append([data_list[i][0],temp_avg])
- title_list = ["date", 'average move trailing ' + str(period)+ ' days']
- write_data_list_to_file(avg_move_list, title_list)
- # currently accepts list and period
- # calculates simple moving average for
- # period default = 50
- def calc_moving_average(data_list, period = 50):
- list_of_moving_averages = []
- for curr_date in range(period+1, len(data_list)):
- sma = 0
- sum_previous_days = 0
- for days_ago in range(period, 0, -1):
- sum_previous_days += data_list[curr_date-days_ago][4]
- sma = sum_previous_days/period
- temp_list = [data_list[curr_date][0], sma]
- list_of_moving_averages.append(temp_list)
- title_list = ['Date', 'SMA '+ str(period)]
- write_data_list_to_file(list_of_moving_averages, title_list)
- def add_price_move(data_list, period = 14, di = 0, oi =1, hi = 2,
- li = 3, ci = 4, vi = 5):
- move_list = [] # up, down
- for curr_date in range(1, len(data_list)):
- prev_date = curr_date - 1
- curr_day_close = data_list[curr_date][ci]
- prev_day_close = data_list[prev_date][ci]
- price_move = curr_day_close - prev_day_close
- if price_move > 0:
- move_up = price_move
- move_down = 0
- elif price_move < 0:
- move_up = 0
- move_down = -(price_move)
- else:
- move_up = 0
- move_down = 0
- # appends to list [DATE, UP MOVEMENT, DOWN MOVEMENT]
- date = data_list[curr_date][di]
- open = data_list[curr_date][oi]
- high = data_list[curr_date][hi]
- low = data_list[curr_date][li]
- close = data_list[curr_date][ci]
- vol = data_list[curr_date][vi]
- new_list = [date, open, high, low, close, move_up, move_down, vol]
- data_list[curr_date] = new_list
- title_list = ['Date', 'open','High','Low','Close','Move up', 'Move down','vol']
- write_data_list_to_file(title_list,data_list[1:])
- def test_main():
- load_list_from_file()
- #crawl_directory(directory)
- test_main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement