Module: Numpy

Code word is not easy, reproduced please indicate the source! Thank you very much!

Preparation: capture the information of a stock, every 30 minutes as a group of data, can refer to the previous article:

Note: Just to demonstrate how to make statistics, more accurate can capture a set of data every 5min

PS: If you need Python learning materials, please click on the link below to obtain them

Free Python learning materials and group communication solutions click to join

How to obtain raw stock data

Target: Output the opening, closing, high, and low prices of each day. Take a week (5 days, 40 data sets) as an example

1. Import data from CSV, need open, high, close and low

#import numpy as np
open = np.loadtxt('30min.csv', dtype = float, skiprows = 1, usecols = 2, delimiter=',', encoding = 'utf-8')
high = np.loadtxt('30min.csv', dtype = float, skiprows = 1, usecols = 3, delimiter=',', encoding = 'utf-8')
close = np.loadtxt('30min.csv', dtype = float, skiprows = 1, usecols = 4, delimiter=',', encoding = 'utf-8')
low = np.loadtxt('30min.csv', dtype = float, skiprows = 1, usecols = 5, delimiter=',', encoding = 'utf-8')
Copy the code

A print test yields the following data,

2. Import date data from CSV, and convert date to the format of working days

from datetime import datetime
def datestr(s):
    return datetime.strptime(s, '%Y/%m/%d').isoweekday()
#print(datestr('2020/12/7'))

dates = np.loadtxt('30min.csv',dtype = str, skiprows = 1, usecols = 0, converters = {0: datestr}, delimiter = ',', encoding = 'utf-8')
Copy the code

A print test yields the following data,

3. Find 40 sets of data for a certain week and confirm the opening time of the first day and the closing time of the last day

Close = close[0:40] dates = dates[0:40] first_monday = np.ravel(np.where(dates == 1))[-1 Last_friday = np.ravel(np.where(dates == 5))[0] # select last_friday = np.ravel(np.where(dates == 5))[0Copy the code

The print test yields 40 times in the following week

Create an array to store the index values for each day of the week

day_indices = np.arange(last_friday, first_monday +1)[::-1]
weeks_indices = np.split(day_indices, 5)
Copy the code

The print test yields the following output,

Write the summarize function, which returns a tuple of open, close, high, and Low for each day of the week

def summarize(a, o, h, c, l): Monday_open = o[a[0]] # Monday open is the last price day_high = np.max(np.take(h, a)) # day_low = np.min(np.take(l, b)) Friday_close = c[a[-1]] # Friday close return('lux', monday_open, day_high, friday_close, day_low)Copy the code

6. Generate weekly data

weeksummary = np.apply_along_axis(summarize, 1, weeks_indices, open, high, close, low)
print(' ****** open,  high,  close,  low \n', weeksummary)
Copy the code

Compare the data in the table and the results match

7. Save if necessary

Np.savetxt ('cw36_lux. CSV ', weeksummary, delimiter = ',', FMT = '%s') # in the same folder as 30min.csvCopy the code

The data saved after CSV is opened is as follows

Ok, the complete code is as follows:

import numpy as np
from datetime import datetime

def datestr(s):
    return datetime.strptime(s, '%Y/%m/%d').isoweekday()

dates, open, high, close, low = np.loadtxt('30min.csv', skiprows = 1, usecols = (0, 2, 3, 4, 5), converters = {0:datestr}, delimiter = ',', unpack = True, encoding = 'utf-8')
close = close[0:40]
dates = dates[0:40]
#print(dates)

first_monday = np.ravel(np.where(dates == 1))[-1]
#print(first_monday)
last_friday = np.ravel(np.where(dates == 5))[0]
#print(last_friday)

day_indices = np.arange(last_friday, first_monday +1)[::-1]
#print(day_indices)
weeks_indices = np.split(day_indices, 5)
#print(weeks_indices)

def summarize(a, o, h, c, l):
    monday_open = o[a[0]]
    day_high = np.max( np.take(h, a) )
    day_low = np.min( np.take(l, a) )
    friday_close = c[a[-1]]
    return('lux', monday_open, day_high, friday_close, day_low)

weeksummary = np.apply_along_axis(summarize, 1, weeks_indices, open, high, close, low)
#print(' ****** open,  high,  close,  low \n', weeksummary)

np.savetxt('cw36_lux.csv', weeksummary, delimiter = ',', fmt = '%s')
Copy the code

Welcome to discuss your study together.

Python3.7 Calculates the daily opening, closing, high, and low prices of a stock