Demand:
- Use tushare historical market data package to obtain a stock.
- The output of the stock of all closed more than 3% than the date opened up.
- The output of the shares opened 2% of all date than the day before closing down more than.
- If I start from January 1, 2010, the first trading day each month to buy a hand stock, sell all the stock last trading day of each year, to date, how my earnings?
Use tushare get a package of historical stock market data
-
pip install tushare
import tushare as ts df = ts.get_k_data('600519',start='1900-01-01') df.to_csv('./maotai.csv') # Outputs the stock closed than opened up to all more than 3% of the date DF = pd.read_csv ( ' ./maotai.csv ' ) df.drop(labels='Unnamed: 0',axis=1,inplace=True) # Verification data type of data in the column data type (DF [ ' DATE ' ] [. 9 ]) # The date data is converted into the first row of this type was then time as the original data row index DF = pd.read_csv ( ' ./maotai.csv ' , index_col = ' date ' , parse_dates = [ ' date ' ]) df.drop(labels='Unnamed: 0',axis=1,inplace=True) # (Closing - opening) / opening> 0.03 (df [ ' use Close ' ] - df [ ' Open ' ]) / df [ ' Open ' ]> 0.03 # True: meet # false: not satisfied # Returns the row of data needs df.loc [(df [ ' Close ' ] - DF [ ' Open ' ]) / DF [ ' Open ' ]> 0.03 ] # acquired date to meet the needs of df.loc [(df [ ' Close ' ] - DF [ ' Open ' ]) / DF [ ' Open ' ]> 0.03 ] .index # Conclusion: If the row index acquired a set of Boolean values, Boolean shuffling followed directly as meta data (DF [ ' Open ' ] - DF [ ' Close ' ] .shift (. 1)) / DF [ ' Close ' ] .shift (1) <-0.02 # Line data to meet the demand df.loc [(DF [ ' Open ' ] - DF [ ' Close ' ] .shift (. 1)) / DF [ ' Close ' ] .shift (. 1) <-0.02 ] .index df_new = DF [ ' 2010 ' : ' 2019 ' ] # resampled data mechanisms (resample): designating data in accordance with specified rules good extraction df_monthly = df_new.resample ( ' M ' ) .first () # calculated buy stocks spent a total of how much money cost_monry = df_monthly [ ' Open ' ] .sum () * 100 df_yearly = df_new.resample ( ' a ' ) .last () df_yearly = df_yearly[:-1] recv_monry = df_yearly['open'].sum()*1200 last_price = df.iloc[-1]['close'] cunHuo_price = last_price * 900 # Calculate total revenue cunHuo_price + recv_monry-cost_monry