1. 程式人生 > >用python做量化投資系列之比特幣---盤口高頻策略

用python做量化投資系列之比特幣---盤口高頻策略

使用方法:把上一篇的配置基礎,寫到一個python檔案中,檔名儲存為 OkcoinSpotAPI,然後把下面的程式碼,     寫到一個新的檔案中,儲存好,就可以直接運行了

原理:根據盤口買賣量,深度行情掛單量,進行買賣決策。屬於高頻策略。

注:該策略做過實盤,有回撤。使用的話,建議進一步優化。

        更一步交流,qq:1733505732

       驗證請註明:python ,比特幣量化


# -*- coding: utf-8 -*-
"""
Created on Mon Jan 16 17:40:55 2017

@author: yunjinqi

E-mail:
[email protected]
Differentiate yourself in the world from anyone else. """ ########################################################盤口模型 from OkcoinSpotAPI import * import pandas as pd import numpy as np import datetime import time ######################################################初始資料 #引入初始資訊 apikey = '9b93e53a-e803-4883-b8fc-64af2f3ccc57' secretkey = '14284B3C0B9CF0F932E83888388855C9' okcoinRESTURL = 'www.okcoin.cn' #請求注意:國內賬號需要 修改為 www.okcoin.cn okcoinSpot = OKCoinSpot(okcoinRESTURL,apikey,secretkey) okcoinFuture = OKCoinFuture(okcoinRESTURL,apikey,secretkey) #info=eval(okcoinSpot.userinfo())#賬戶資訊 #info #####################################獲取並整理資料 def cut(deep): deep['bid_price']='' deep['bid_volume']='' deep['ask_price']='' deep['ask_price']='' for i in range(len(deep)): deep.ix[i,'bid_price']=deep.ix[i,'bids'][0] deep.ix[i,'bid_volume']=deep.ix[i,'bids'][1] deep.ix[i,'ask_price']=deep.ix[i,'asks'][0] deep.ix[i,'ask_volume']=deep.ix[i,'asks'][1] del deep['asks'] del deep['bids'] deep['bid_price']=deep['bid_price'].astype('float64') deep['bid_volume']=deep['bid_volume'].astype('float64') deep['ask_price']=deep['ask_price'].astype('float64') deep['ask_price']=deep['ask_price'].astype('float64') return deep def bid_ask_vol_diff(deep): bidvol10=deep['bid_volume'][:10] askvol10=deep['ask_volume'][-10:] diff=bidvol10.sum()-askvol10.sum() return diff #diff>0是入場條件1 def bid_ask_price_diff(deep): bidprice10=deep['bid_price'][:10] askprice10=deep['ask_price'][-10:] bid_diff=bidprice10.max()-bidprice10.min() ask_diff=askprice10.max()-askprice10.min() diff=bid_diff-ask_diff #小於0是入場條件 return diff def bid_ask_bigvol(deep): bidvol10=deep['bid_volume'][:10] askvol10=deep['ask_volume'][-10:] diff=bidvol10.max()>askvol10.max()#大於0是入場條件 return diff i=0 while True: deep=pd.DataFrame(okcoinSpot.depth('btc_cny')) deep=cut(deep) deep if bid_ask_vol_diff(deep)>0 and bid_ask_price_diff(deep)<0 and bid_ask_bigvol(deep)>0: price_buy=str(deep['bid_price'][1]+0.01) buy=okcoinSpot.trade('btc_cny','buy',price_buy,'0.01') time.sleep(0.2) price_sell=str(deep['bid_price'][1]+0.50) sell=okcoinSpot.trade('btc_cny','sell',price_sell,'0.01') print(sell) i=i+1 try: buyid=str(eval(buy)['order_id']) sellid=str(eval(sell)['order_id']) except NameError: pass except KeyError: pass time.sleep(5) try: cancel_buy=okcoinSpot.cancelOrder('btc_cny',buyid) cance1_sell=okcoinSpot.cancelOrder('btc_cny',sellid) except NameError: pass except KeyError: pass info_btc_free=eval(eval(okcoinSpot.userinfo())['info']['funds']['free']['btc']) info_net=eval(eval(okcoinSpot.userinfo())['info']['funds']['asset']['net']) print(i,info_btc_free,info_net)