numpy of common API statistics

Import numpy AS NP
 Import matplotlib.pyplot AS MP
 Import datetime dt AS
 Import matplotlib.dates AS MD 

'' ' 
    1. The arithmetic mean of: calculating the mean closing price 
    2. Weighted average: VWAP (the VWAP) --- trading volume reflects the market acceptance of current trading price, trading volume higher the current market price of the more recognized, the value of the stock price closer to the true value of 
    3. the most value: volatility 
    4. median 
    5. Sort 
    6. standard deviation 
'' ' 


# date conversion function 
DEF dmy2ymd (dmy):
     # string dmy format is converted into the format string ymd 
    dmy = STR (dmy, encoding = ' UTF-. 8 ' ) 
    D = dt.datetime .strptime (DMY, ' %% D- M-% the Y ' ) 
    D =d.date () 
    YMD = d.strftime ( ' %% Y-M-% D ' )
     return YMD 


a dates, opening_prices, highest_prices, lowest_prices, closing_prices, volumns = \ 
    np.loadtxt ( ' ./da_data/aapl.csv ' , DELIMITER = ' , ' , usecols = (. 1,. 3,. 4,. 5,. 6,. 7), the unpack = True, 
               DTYPE = ' M8 [D], F8, F8, F8, F8, F8 ' , Converters = {. 1 : dmy2ymd})   # converters the converter, performing the first run, where 1 denotes the time where the column index 

# evaluation AAPL stock volatility 
MAX_VAL = np.max (highest_prices) 
MIN_VAL= Np.min (lowest_prices)
 Print (MAX_VAL, ' ~ ' , MIN_VAL)
 # View high and low Date 
Print ( ' MAX_DATE: ' , a dates [np.argmax (highest_prices)])
 Print ( ' MIN_DATE: ' , a dates [np.argmin (lowest_prices)]) 

# draw closing price line graph 
mp.figure ( ' AAPL ' , facecolor = ' LightGray ' ) 
mp.title ( ' AAPL ' , 18 is fontSize = ) 
mp.xlabel ( ' DATE ' , fontsize = 12) 
Mp.ylabel ( ' closing_pricing ' , 12 is fontSize = ) 
mp.tick_params (labelsize = 10 ) 
mp.grid (lineStyle = ' : ' )
 # Sets the scale of the x-axis positioner, making it more suitable for displaying date data 
AX = MP .gca ()
 # to Monday as the main scale 
ma_loc = md.WeekdayLocator (byweekday = md.MO)
 # time scale, in addition to the date Monday 
mi_loc = md.DayLocator () 
ax.xaxis.set_major_locator (ma_loc) 
ax.xaxis. set_major_formatter (md.DateFormatter ( ' %% Y-M-% D ' )) 
ax.xaxis.set_minor_locator (mi_loc) 
# date data type conversion, more suitable for drawing
= a dates dates.astype (md.datetime.datetime) 
mp.plot (a dates, closing_prices, as linewidth = 2, lineStyle = ' - ' , Color = ' DodgerBlue ' , label = ' AAPL ' )
 # calculate the mean, rendering image 
mean = np.mean (closing_prices) 
mp.hlines (Mean, a dates [0], a dates [ -1], Color = ' OrangeRed ' , label = ' on Mean (the CP) ' )
 # calculates volume weighted average VWAP 
avg1 = np .average (closing_prices, weights = volumns) 
mp.hlines (AVG1, a dates [0], a dates [ -1], = Colors 'GreenYellow ' , label = ' the VWAP ' )
 # calculates TWAP --- closer the time-weighted average closing price higher the degree of influence of the current time on the mean 
W = np.linspace (. 1,. 7, 30 ) 
AVG2 = np.average (closing_prices, weights = W) 
mp.hlines (AVG2, a dates [0], a dates [ -1], = Colors ' Pink ' , label = ' TWAP ' )
 # calculate median 
median = np.median (closing_prices) 
MP .hlines (Median, a dates [0], a dates [ -1], Colors = ' Blue ' , label = ' Median (the CP) ' )
 Print (Median)
# Sort 
sorted_prices = np.msort (closing_prices)
 Print (sorted_prices) 
size = sorted_prices.size 
Median = ((sorted_prices [int ((size -. 1) / 2)]) + (sorted_prices [int (size / 2)])) / 2
 Print (Median)
 # standard deviation 
S = np.std (closing_prices) 
S1 = np.std (closing_prices, ddof =. 1 )
 Print ( ' population standard deviation is: ' , S)
 Print ( " sample standard deviation is: ' , S1) 

# manually count standard deviation 
Mean = np.mean (closing_prices) 
D = closing_prices -Mean 
D2 of = 2 ** D 
V = np.mean (D2 of) 
S = np.sqrt (V)
 Print (S) 

mp.tight_layout () 
mp.legend () 
# automatically formatted date display format x-axis (the most suitable displayed) 
mp.gcf (). autofmt_xdate () 
mp.show ()

 

Guess you like

Origin www.cnblogs.com/yuxiangyang/p/11161562.html