【转载】https://blog.csdn.net/qq_26948675/article/details/54783543
使用方法:把上一篇的配置基础,写到一个python文件中,文件名保存为 OkcoinSpotAPI,然后把下面的代码, 写到一个新的文件中,保存好,就可以直接运行了 原理:根据盘口买卖量,深度行情挂单量,进行买卖决策。属于高频策略。 注:该策略做过实盘,有回撤。使用的话,建议进一步优化。
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 16 17:40:55 2017
@author: yunjinqi
E-mail:yunjinqi@qq.com
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)
|