Second, commonly used functions

Source: Internet
Author: User
Tags square root unpack

(a) Read and write files

(1) Create the unit matrix (both the main diagonal elements are 1 and the remaining elements are 0)

i5 = Eye (5)

Note: the eye () function

NumPy. Eye (n,m=none, k=0, dtype=<type ' float ' >)

Just focus on the first third parameter.

First parameter: Scale of the output phalanx (number of rows = number of columns), number of lines or columns

The third parameter: By default the output is diagonal full "1", the rest of the whole "0" of the square, if K is a positive integer, then the upper right section K diagonal Full "1" The remaining full "0", K is a negative integer in the lower left section K diagonal Full "1" The remaining full "0".

(2) Write file Savetxt function

Savetxt ("Eye.txt", i5)

(3) Read file Loadtxt

i = loadtxt (' eye.txt ')

(ii) read into CSV file

The stock price data is stored in a CSV file, first listed as the stock code to identify the stock (Apple stock code is AAPL), the second column is in the dd-mm-yyyy format, the third column is empty, followed by the open, high, low, and close prices, The last column is the volume of the day. The following is a row of data for the "data.csv" file:

aapl,28-01-2011,, 344.17,344.4,333.53,336.1,21144800

To read the CSV file, we loaded the closing price and volume into two arrays: Loadtxt () function

C,v = Loadtxt (' data.csv ', delimiter= ', ', usecols= (6,7), unpack=true)

Delimiter: The delimiter is set to ","

Usecols: parameter is a tuple to get 7th, 8 columns of data

Unpack: parameter is set to True to split the data that stores different columns, that is, the values of the closing price and the volume are assigned to the variable C and V, respectively

(iii) Average

(1) Volume weighted average price (VWAP)

VWAP (volume-weighted Average Price, Volume weighted average) is a very important amount of economics,
It represents the "average" price of financial assets. The higher the volume of a price, the greater the weight of the price. VWAP
Is the weighted average value calculated with the volume as the weight, which is often used for algorithmic trading.

Cases:

Stock A, 1000 shares, Price 10, Stock b,2000 shares, price 15; arithmetic mean = (10 + 15)/2 = 12.5; weighted average = (10 * 1000 + 15 * 2000)/(1000 + 2000) = 13.33

C,v = Loadtxt (' data.txt ', delimiter= ', ', usecols= (6,7), unpack=true)

Vwap = Average (c,weights=v)

(2) Average

Mean_c = Mean (c)

(3) Time weighted average price

In economics, TWAP (time-weighted Average Price, time-weighted average) is another "average"
The price of the indicator. Now that we have calculated the Vwap, let's calculate the Twap. Actually, Twap is just a variant.
, the basic idea is that the price is more important recently, so we should give a higher weight to the recent price.
The simplest method is to use the Arange function to create a sequence of natural numbers that starts from 0, in turn, and the number of natural numbers is
The number of disk prices. Of course, this is not necessarily the right way to calculate twap. In fact, in this book, the stock price analysis of the big
Some examples are just to illustrate the problem. The code to calculate the Twap is as follows.

t = Arange (len (c))

Twap = Average (c,weights=t)

(d) Extreme values, very poor

Read the data and load the daily high and low prices into the array separately

h,l = Loadtxt (' data.csv ', delimiter= ', ', usecols= (4,5), unpack=true)

(1) Max Max (): Maximum of daily high value

highest = max (h)

(2) Min. min (): Minimum daily minimum price

lowest = min (l)

(3) Very poor PTP ():

The extreme difference of the highest price:

Spread_high_price = PTP (h)

Equivalent to: Spread_high_price = max (h)-min (h)

(v) Simple statistical analysis

(1) median price median ()

c = loadtxt (' data.csv ', delimiter= ', ', usecols= (6,), unpack=true)

C_median = Median (c)

(2) Variance var ()

C_variance = var (c)

(vi) Stock return rate

(1) Simple rate of Return: diff (), STD ()

The diff function in NumPy can return a difference between the elements of an adjacent array
An array of value components. This is somewhat analogous to the differential in calculus. To calculate the yield, we also need to divide the difference by the day before
The price. Note, however, that the array returned by diff has one element less than the closing price array:

returns = Np.diff (c)/c[:-1]

Notice that we did not divide the last value in the closing price array. Next, use the STD function to calculate the standard deviation:
print "Standard deviation =", np.std (returns)

(2) Logarithmic rate of return: Log (), diff ()

The log function is used to get the logarithm of each closing price, and then the diff function is used for the result.

Logreturns = Np.diff (Np.log (c))

(3) Filter the element with positive return, where ()

Posretindices = Np.where (returns > 0)

(4) Fluctuation rate:

In investment studies, volatility (volatility) is a measure of price movement. Historical volatility can be based on historical rates
Calculated by the grid data. Logarithmic rate of return is required when calculating historical volatility, such as annual volatility or monthly volatility. Annual fluctuations
the rate equals the standard deviation of the logarithmic rate of return divided by its mean, divided by the square root of the reciprocal of the trading day, usually taking 252 days on the trading day. I
They are calculated using the STD and mean functions, and the code looks like this:

annual_volatility = NP.STD (logreturns)/np.mean (logreturns)
annual_volatility = annual_volatility/np.sqrt (1./252.)
Print Annual_volatility

Note: sqrt uses floating-point numbers, the resulting knot is correct, as a floating-point number

(vii) Analysis of date data

Dates, close=np.loadtxt (' data.csv ', delimiter= ', ', usecols= (1,6), unpack=true)

After executing the above code, you will get an error message:

Valueerror:could not convert string to Float:b ' 28-01-2011 '

NumPy attempts to convert the date into a floating-point number. Follow these steps to process the date:

(1) Date conversion function

What we need to do is explicitly tell numpy how to convert the date, and we need to use a specific parameter converters in the Loadtxt function,

It is a dictionary of mappings between a data column and a conversion function.

    • Redefine the Datestr2num () function:

def datestr2num (s):

s = str (s, ' utf-8 ')

Return Datetime.datetime.strptime (S, "%d-%m-%y"). Date (). Weekday ()

    • To hang the date function:

Dates,close = Loadtxt (' data.csv ', delimiter= ', ', usecols= (1,6), converters={1:datestr2num},unpack=true)

    • Create an array of 5 elements representing 5 working days of the week. The array element is initialized to 0.

Averages = Np.zeros (5)

    • Traversing a date identifier of 0 to 4, or traversing from Monday to Friday, then

The where function is used to get the index values of the working days and stored in the indices array. Get these index values using the Take function
The corresponding element value. Finally, we calculate the average value for each weekday in the averages array.

For I in range (5):
indices = WHERE (dates = = i)
Prices = Take (close, indices)
AVG = mean (prices)
Print ("day:%d,prices:%s,average:%f"% (I,PRICES,AVG))
Averages[i] = Avg

Second, common functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.