Stock analysis

# Tushare Finance packet data interface

Use tushare historical market data package to obtain a stock.
Import PANDAS AS PD
 from PANDAS Import Series, DataFrame
 Import numpy AS NP
 Import tushare AS TS 
DF = ts.get_k_data (code = ' 600,519 ' , Start = ' 2000-01-01 ' ) # acquires line data k 
df.head ()   # First 5 rows 
df.to_csv ( ' ./maotai.csv ' ) # data write CSV 
# the date data row converted into a time series, and then the column as a whole, the data source row index as a row index of a column of 
data pd.read_csv = ( ' ./maotai.csv ' , index_col = ' DATE ', parse_dates = [ ' DATE ' ]) # read out data DF 
data.drop (Labels = ' break Unnamed: 0 ' , Axis =. 1, InPlace = True) 0 row label # 1 
data.head ( 5) # front row 5 
data.index [0] # Timestamp ( '2001-08-27 00:00:00')

# Outputs the stock of all closed than opened up more than 3% of the date
# (closed - the opening) / opening> 0.03
indexs = (Data [ 'Close'] - Data [ 'Open']) / Data [ 'Open']> 0.03
data.loc [indexs] .index # specific time to get

# The output of the shares opened 2% of all date than the day before closing down more than.
= indexs (Data [ 'Open'] - Data [ 'Close'] Shift (. 1).) / Data [ 'Close'] Shift (. 1) <-0.02.
data.loc [indexs] .index

# 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?

price_last = data [ 'open'] [- 1] # last trading day opening price
df = data [ '2010': '2019'] # end to remove unnecessary data
#Pandas resample function provides a convenient way for the time sequence resampling, downsampling and upsampling divided according to the time granularity larger or smaller:
. df_monthly df.resample = ( "M") First ()
. df_yearly df.resample = ( "a") Last () [: -1] # remove the last year of the Y-
cost_money = 0 # initial capital
hold = 0 # holdings of shares each year
for year in the Range (2010, 2020):
  cost_money - = df_monthly.loc [str (year)] [ 'Open'] SUM () * 100.
  HOLD + = len (df_monthly [STR (year)] [ 'Open']) * 100
  IF year = 2019:!
    cost_money + = df_yearly.loc [STR (year)] [ ' Open '] [0] * the hOLD
    the hOLD = 0 # held annually stock
cost_money + = hold * price_last

print(cost_money)

 

Guess you like

Origin www.cnblogs.com/zhangchen-sx/p/11116596.html