def fImportAllTurnover(): data = gsd.get_code_list() codes = data["code"].values db = cx_Oracle.connect('c##stock', 'didierg160', 'myoracle') #创建连接 cr = db.cursor() for code in codes: data = ts.get_hist_data(code=code) if len(data) < 1: continue data['shi_jian'] = data.index data = data.reset_index(drop=True) shi_jians = data["shi_jian"].values turnovers = data["turnover"].values for (shi_jian, turnover) in zip(shi_jians, turnovers): sql = "update tb_stock_data_daily set huan_sou_lv = " + str( turnover ) + " where code = '" + code + "' and shi_jian=to_date('" + shi_jian + " 15:00:00','yyyy-mm-dd hh24:mi:ss')" cr.execute(sql) db.commit() cr.close() db.close()
def updateData(): codes = gsd.get_code_list() codes = codes["code"].values db = cx_Oracle.connect('c##stock', 'didierg160', 'myoracle') #创建连接 cr = db.cursor() sql = "truncate table tb_stock_101_return" cr.execute(sql) for code in codes: data = getCodeReturns(code) rets = data["return"].values shi_jians = data["shi_jian"].values for ret, shi_jian in zip(rets, shi_jians): sql = "insert into tb_stock_101_return (CODE,SHI_JIAN,RETURN) values ('" + code + "','" + shi_jian + "'," + str( ret) + ")" try: cr.execute(sql) except: # print(sql) pass db.commit() print("code:", code, " 5day return done") sql = "update tb_stock_101_return set ret_std=return" cr.execute(sql) sql = "update tb_stock_101_return set RET_HOT=1 where return>0 " cr.execute(sql) sql = "update tb_stock_101_return set RET_HOT=0 where return<=0" cr.execute(sql) db.commit() print(" 5day return all done")
for aid in range(98,99) : Alphas=alpha.alpha(aid) try : if Alphas == "id not found": continue except: pass fieldName = "ALPHA_" + ("000" + str(aid))[-3:] all_fieldName.append(fieldName) all_alpha.append(Alphas) print(fieldName , " done") codes = gsd.get_code_list() codes = codes["code"].values # codes = ['600668','600009'] db=cx_Oracle.connect('c##stock','didierg160','myoracle') #创建连接 cr=db.cursor() for code in codes : # if code not in not_done : # print("code:",code , "skip") # continue # if code in done : # print("code:",code , "skip") # continue
def handle_bar(context, bar_dict): import getStockData as gsd # import ta_lib_data as ta1 list = gsd.get_code_list() list = list["code"].values cur_position = 0.0 for code in list: if code[0] == "6": context.s1 = code + ".XSHG" else: context.s1 = code + ".XSHE" df = gsd.get_stock_data_daily_rqalpha(context.s1[0:6], end=str(context.now)) # print("get_stock_data_daily_rqalpha") df = df.astype(dtype='float64') if len(df) < 100: continue kdj_k = df["kdj_k"].values kdj_d = df["kdj_d"].values kdj_j = df["kdj_j"].values MACD = df["macd_macd"].values MA12 = df["ma12"].values MA20 = df["ma20"].values amount = df["amount"].values # print(df) # print(code,MACD) plot("MACD", MACD[-1]) plot("MA12", MA12[-1]) plot("MA20", MA20[-1]) # 计算现在portfolio中股票的仓位 cur_position = context.portfolio.positions[context.s1].quantity # 计算现在portfolio中的现金可以购买多少股票 shares = context.portfolio.cash / bar_dict[context.s1].close # max_price = 0.0 # drawdown = 0.0 # returnRate = 0.0 # price = [0.0] # if cur_position > 0: # returnRate = (bar_dict[context.s1].close - context.buyPrice)/context.buyPrice # df1 = gsd.get_stock_data_daily_rqalpha(context.s1[0:6], end=str(context.now)) # price = df1["price"].values # max_price = gsd.get_stock_data_max_price_rqalpha(context.s1[0:6],start= str( context.buyTime ) ,end= str( context.now )) # drawdown = ( max_price - price[-1])/max_price # print("drawdown:%f maxprice:%f price:%f"% ( drawdown , max_price , price[-1])) # print(returnRate) if MA12[-2] < MA20[-2] and cur_position > 0: # 进行清仓 selres = order_target_value(context.s1, 0) cur_position = context.portfolio.positions[context.s1].quantity if cur_position <= 0: context.buyPrice = 0.0 context.buyTime = "" print("已清仓 ") return if (MA12[-2] > MA20[-2] and MA12[-3] > MA20[-3] and MA12[-4] > MA20[-4] and cur_position <= 0): if (MACD[-4] > 0 and MACD[-3] > MACD[-4] and MACD[-2] > MACD[-3]): if (amount[-5] * 1.5 < amount[-4] and abs( (amount[-3] - amount[-4]) / amount[-4]) <= 0.3 and abs( (amount[-2] - amount[-3]) / amount[-3]) <= 0.3): # 满仓入股 order_shares(context.s1, shares) print("已买入") cur_position = context.portfolio.positions[context.s1].quantity if cur_position > 0: context.buyPrice = bar_dict[context.s1].close context.buyTime = context.now print("仓位 :%f" % cur_position)