Tushare is a free, open-source python Finance data interface package. The main achievement of stocks and other financial data from data acquisition , cleansing processing to the data storage process can provide rapid financial analysts, clean, and a variety of easy data analysis for data in terms of their access to greatly reduce the workload, the they are more focused on the study and implementation of strategies and models. Considering the Python pandas package reflects the quantitative analysis of the financial advantage, Tushare return of most of the data formats are pandas DataFrame type.
For example use
Import numpy AS NP Import PANDAS AS PD Import matplotlib.pyplot AS PLT Import tushare AS TS # used tushare obtain each stock market data DF = ts.get_k_data ( ' 600,519 ' , Start = ' 2008-01-01 ' ) Print ( type (DF)) df.to_csv ( ' 600519.csv ' ) DF = pd.read_csv ( ' 600519.csv ' , index_col = ' DATE ' , parse_dates = [ ' DATE ' ]) [[ ' Open ' ,' Close ' , ' High ' , ' Low ' ]] Print (DF) # outputs the stock closed than opened up to all of 3% or date Print (DF [(DF [ ' Close ' ] -df [ ' Open ' ]) / DF [ ' Open ' ]> 0.03 ] .index) # df.shift () moves downward moving positive, negative moved upwardly # outputs the opened more than 2% of all the stock than the day before closing down date df [(df [ ' Open ' ] -df [ ' Close ' ] .shift (. 1)) / DF [ ' Close ' ].shift(1)<=-0.02] .index # If I from January 1, 2008 start, first trading day of each month to buy a hand stock, sell all the stock last trading day of each year, to date, how my earnings? price_last = DF [ ' Open ' ] [-. 1 ] DF = DF [ ' 2008-01 ' : ' 2018-11 ' ] # remove unnecessary data inclusive df_monthly = df.resample ( " the MS " ) .first () # per The first day of May Print ( " df_monthly 2008: " ) Print (df_monthly) Print ( " df_yearly: " ) df_yearly= df.resample("A").last()[:-1] # 每年最后一天 print(df_yearly) cost_money=0 hold = 0 for year in range(2008,2018): cost_money += df_monthly[str(year)]['open'].sum() * 100 hold += len(df_monthly[str(year)]['open'])*100 cost_money -= df_yearly[str(year)]['open'][0] * hold hold = 0 print( ' Cost_money: S% ' % (0- cost_money)) # request and the 5-day moving average 30 DF = pd.read_csv ( ' 601318.csv ' , index_col = ' DATE ' , parse_dates = [ ' DATE ' ]) [ [ ' Open ' , ' Close ' , ' Low ' , ' High ' ]] Print (df.head ()) DF [ ' MA5 ' ] = np.NAN DF [ ' MA30 '] =np.NAN # # for I in Range (. 4, len (DF)): # df.loc [df.index [I], 'MA5'] = DF [ 'Close'] [. 4-I: I +. 1] .mean () # # for I in Range (29, len (DF)): # df.loc [df.index [I], 'MA30'] = DF [ 'Close'] [I-29: I +. 1 ] .mean () # # Print (df.head (50)) DF [ ' MA5 ' ] = DF [ ' Close ' ] .rolling (. 5) .mean () # window scroll down 5 DF [ ' MA30 ' ] = DF [ ' Close ' ] .rolling (30) .mean () # window scroll down 30 Print (DF.head(50)) # Videos FIG average DF DF = [: 800 ] DF [[ ' Close ' , ' MA5 ' , ' MA30 ' .]] Plot () plt.show () # MACD Sicha and date golden_cross = [] death_cross = [ ] for I in Range (. 1 , len (DF)): IF DF [ ' MA5 ' ] [I]> = DF [ ' MA30 ' ] [I] and DF [ ' MA5 ' ] [I-. 1] <DF [ ' MA30 ' ] [I. 1-]: golden_cross.append(df.index[i].to_pydatetime()) if df['ma5'][i] <= df['ma30'][i] and df['ma5'][i - 1] > df['ma30'][i - 1]: death_cross.append(df.index[i]) print(golden_cross[:5]) sr1 = df['ma5'] < df['ma30'] sr2 = df['ma5'] >= df[''Ma30] death_cross = df[sr1 & sr2.shift(1)].index golden_cross = df[~(sr1 | sr2.shift(1))].index print(death_cross)