現貨期權交易所繫統(成熟原始碼)丨現貨期權交易所繫統開發(方案)

Tg_StPv888發表於2023-02-23

  The development technology of digital currency exchange is a decentralized consensus mechanism to maintain a complete,distributed and tamper-proof ledger database.It enables participants in the blockchain to achieve a unified ledger system without establishing trust relationships.

  As a new information and network technology,blockchain uses encryption technology,distributed network and consensus mechanism to ensure that the information recorded by each node in the network is true and effective.Blockchain is constantly penetrating into all walks of life and has shown a good development trend.

  huobipro=ccxt.huobipro({

  'apiKey':'',

  'secret':'',

  })

  先使用ccxt獲取交易所的例項,然後獲取歷史k線,得到的資料使用dataframe格式接受

  huobipro.fetch_ohlcv(symbol=symbol,limit=limit_num,timeframe=timeframe)

  然後利用pandas提供的函式計算MA,

  df['median_short']=df['close'].rolling(n_short,min_periods=1).mean()

  df['median_long']=df['close'].rolling(n_long,min_periods=1).mean()

  然後再找出買入賣出訊號,

  #找出買入訊號

  condition1=df['median_short']>df['median_long']#短均線上穿長均線

  condition2=df['median_short'].shift(1)<=df['median_long'].shift(1)

  df.loc[condition1&condition2,'signal']=1#產生買入訊號的k線標記為1

  #找出賣出訊號

  condition1=df['median_short']<df['median_long']#短均線上穿長均線

  condition2=df['median_short'].shift(1)>=df['median_long'].shift(1)

  df.loc[condition1&condition2,'signal']=0#產生賣出訊號的k線標記為0

  有了交易訊號,就可以獲取訊號,再判斷進行下單(huobipro.create_limit_buy/sell_order()了)

  第五步:其實第四步就可以交易了,第五步是回測,一般來說先回測再根據回測結果選用策略,最後才進行實盤

  回測分析的相關有很多種,在這方面我也不是很懂,目前我還是習慣用累計利潤來進行分析,

  #由signal計算出實際的每天持倉

  df['pos']=df['signal'].shift()

  df['pos'].fillna(method='ffill',inplace=True)

  df['pos'].fillna(value=0,inplace=True)

  到這裡持倉訊號就有了,就可以根據持倉和歷史k線的價格計算累計利潤了,

  df['change']=df['close'].pct_change(1)#根據收盤價計算漲跌幅

  df['by_at_open_change']=df['close']/df['open']-1#開盤買入到收盤的漲跌幅

  df['sell_next_open_change']=df['open'].shift(-1)/df['close']-1#這根收盤到下根開盤的漲跌幅

  df.at[len(df)-1,'sell_next_open_change']=0#補全空值df.at[4,'B']

  condition1=df['pos']==1

  condition2=df['pos']!=df['pos'].shift(1)

  open_pos_condition=condition1&condition2

  #選取平倉條件

  condition1=df['pos']==0

  condition2=df['pos']!=df['pos'].shift(1)

  close_pos_condition=condition1&condition2

  #對每次交易進行分組

  df.loc[open_pos_condition,'start_time']=df['open_time']

  df['start_time'].fillna(method='ffill',inplace=True)

  df.loc[df['pos']==0,'start_time']=pd.NaT

  init_cash=1000#初始資金

  #計算倉位變動

  #開倉時倉位

  df.loc[open_pos_condition,'position']=init_cash*(1+df['by_at_open_change'])

  group_num=len(df.groupby('start_time'))

  if group_num>1:

  temp=df.groupby('start_time').apply(lambda x:x['close']/x.iloc0*x.iloc0)

  temp=temp.reset_index(level=[0])

  df['position']=temp['close']

  df['position_max']=df['position']*df['high']/df['close']

  df['position_min']=df['position']*df['low']/df['close']

  ##平倉時的倉位

  #df.loc[close_pos_condition,'position']*=(1+df.loc[close_pos_condition,'sell_next_open_change'])

  #計算持倉利潤

  df['porfit']=(df['position']-init_cash)*df['pos']#持倉利潤或虧損

  #df.loc[df['pos']==1,'porfit_min']=(df['position_min']-init_cash)*df['pos']#最小持倉盈利或虧損

  #df.loc[df['pos']==0,'porfit_max']=(df['position_max']-init_cash)*df['pos']

  #計算實際資金量

  df['cash']=init_cash+df['porfit']#實際資金

  #計算資金曲線

  df['equity_change']=df['cash'].pct_change()

  #開倉日收益率

  df.loc[open_pos_condition,'equity_change']=df.loc[open_pos_condition,'cash']/init_cash-1

  df['equity_change'].fillna(value=0,inplace=True)

  df['equity_curve']=(1+df['equity_change']).cumprod()

  df['equity_curve']=df['equity_curve']*init_cash


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

相關文章