量化合約/合約量化/秒合約/永續合約/現貨期權期貨/交易所繫統開發案例及原始碼

xiaofufu發表於2023-02-23

  交易中心與演演算法工人內部架構,交易中心主要負責接收客戶端傳送過來的指令,透過風控層後將指令路由至演演算法工人,由演演算法工人處理訂單邏輯,如:條件單、追單、止損止盈單等,並最終將訂單報入交易所場內,同時將回報返回給交易中心,再由交易中心將回報返回給訂閱使用者。


  交易中心還負責路由使用者傳送的策略指令,並根據指令分發給策略回測工人或者策略模擬工人,對應的去執行回測指令或者啟動策略等。


  #測試所有程式碼


  def all_test(codes,names,days,begin_count,end_count,earnings_count=30,writefile='codes.csv'):


  count_earnings=0   


  count_suc=0


  count_fail=0


  earnings_count=(end_count-begin_count)*0.1


  name_array=np.array([]) 


  code_array=np.array([])


  suc_array=np.array([])


  fail_array=np.array([])


  earnings_array=np.array([])


  count_hold_days=0


  #for code in codes:


  for i in range(len(codes)):


  earnings,suc,fail,index_array,pct_array,hold_days=test(codes<i>,days=days,begin_count=begin_count,end_count=end_count,name=names<i>)


  if suc==-1: 

  continue


  count_earnings=count_earnings+earnings


  count_suc=count_suc+suc


  count_fail=count_fail+fail


  count_hold_days=count_hold_days+hold_days


  if earnings>earnings_count:


  #print('test==',code,suc,fail,price)


  name_array=np.append(name_array,names<i>)


  code_array=np.append(code_array,codes<i>)


  suc_array=np.append(suc_array,suc)


  fail_array=np.append(fail_array,fail)


  earnings_array=np.append(earnings_array,earnings)


  print('count_suc',count_suc)


  print('count_fail',count_fail)


  print('count_earnings','%.1f'%count_earnings)


  print("suc",'%.1f'%(count_suc/(count_suc+count_fail+1)*100))


  print("earnings pre",i,'%.1f'%(count_earnings/(count_suc+count_fail+1)),count_hold_days)


  data=[code_array,name_array,suc_array,fail_array,earnings_array]


  data=np.transpose(data)


  ser2=pd.DataFrame(data,columns=['ts_code','name','suc','fail','earnings'])


  #ser2=pd.Series(suc_array)


  #ser2=pd.Series(fail_array)


  #ser2=pd.Series(price_array)


  ser2.to_csv(writefile,encoding="utf_8_sig")


  #測試指定檔案對應的所有程式碼


  def test_sel_codes(days,begin_count,end_count,names,file='codes.csv'):


  df=pd.read_csv(file)


  codes=df['ts_code'].values


  names=df['name'].values


  print(codes)


  all_test(codes,names=names,days=days,begin_count=begin_count,end_count=end_count)


  #測試指定程式碼是否符合條件


  def sel_current_code(ts_codes,name='no_name'):


  df=load_data(ts_codes)


  i=-1


  begin_count=-3


  end_count=-1


  date=df['trade_date'].values[begin_count:]


  close=df['close'].values[begin_count:]


  price=15


  if len(close)<2:


  return False,'1999'


  if close[-1]<price:


  return False,'1999'


  change=df['change'].values[begin_count:]


  pct_change=df['pct_chg'].values[begin_count:]


  if len(close)<2:


  return False,'1999'


  conditionOne=pct_change<i><9.5


  conditionTwo=pct_change[i+1]<9.5


  if conditionOne and conditionTwo:#pct_change[i+1]<9.5表示下一個交易日漲停


  buys,sells,ret=check_days_money(ts_codes,name,date<i>)


  if ret:


  print(ts_codes,name,buys,sells,ret,'%.1f'%(buys/sells*100))


  return True,date<i>


  #if close<i>>ma5<i>and close[i-1]<ma5[i-1]and pct_change<i><9.5:#pct_change<i><9.5當日交易漲停


  return False,date<i>


  #測試所有程式碼是否符合條件


  def all_sel_current_code(codes,names,writefileName='sel_codes.csv',writefile=True):


  path='./選擇/'


  dir=Path(path)


  if not dir.exists():


  os.mkdir(dir)


  begin_count=-200


  end_count=-1


  days=20


  name_array=np.array([])


  code_array=np.array([])


  date_array=np.array([])


  for i in range(len(codes)):


  code=codes<i>


  name=names<i>


  #判斷條件選擇


  ret,date=sel_current_code(code,name)


  if ret:


  name_array=np.append(name_array,name)


  code_array=np.append(code_array,code)


  date_array=np.append(date_array,date)


  earnings,suc,fail,index_array,pct_array,hold_days=test(code,name=name,days=days,begin_count=begin_count,end_count=end_count)


  if suc==-1:


  continue


  name=str.replace(name,'*','')


  if earnings>0:


  file_dir=path+'Curent_AAA_%s_%s%d%d++%.1f++%.1f.png'%(code,name,suc,fail,suc*100/(fail+suc+1),earnings)


  else:


  file_dir=path+'Curent_BBB_%s_%s%d%d——%.1f——%.1f.png'%(code,name,suc,fail,suc*100/(fail+suc+1),earnings)


  plot_pct(code,index_array,pct_array,begin_count=begin_count,end_count=end_count,writefilename=file_dir)


  print("codes%s%s earnings%.2f"%(code,name,earnings))


  data=[code_array,name_array,date_array]


  data=np.transpose(data)#矩陣轉置


  ser2=pd.DataFrame(data,columns=['ts_code','name','date'])


  if writefile:


  ser2.to_csv(writefileName,encoding="utf_8_sig")


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69956839/viewspace-2936754/,如需轉載,請註明出處,否則將追究法律責任。

相關文章