转摘自 http://blog.csdn.net/qq_26948675/article/details/54783533
使用方法:把上一篇的配置基础,写到一个python文件中,文件名保存为 OkcoinSpotAPI。
然后把下面的代码,写到一个新的文件中,保存好,就可以直接运行了
原理:当价格突破布林带上轨的时候买入,跌破均线的时候卖出。
注:这个策略仅仅是最初始的调试版本,很不完善哦。
- "font-size:24px;">
-
-
-
-
-
-
-
-
-
-
-
- from OkcoinSpotAPI import *
- import pandas as pd
- import numpy as np
- import datetime
- import time
-
- okcoinRESTURL = 'www.okcoin.cn'
- apikey='a3c363bd-28c7-4f6d-a04d-ac4a58e929b9'
- secretkey='83782FBD5E365EFC511EC739C0B54103'
- okcoinSpot = OKCoinSpot(okcoinRESTURL,apikey,secretkey)
-
- def bbands_MA(M,N):
- m=str(M)
- n=str(N)
- try:
- kline=pd.DataFrame(okcoinSpot.getKline('1min',m,'0'))
- except ValueError as e:
- print('json错误')
- mid=kline.iloc[::,4].mean()
- std=kline.iloc[::,4].std()
- upper=mid+N*std
- lower=mid-N*std
- ma=kline.iloc[::,4].mean()
- result=[upper,mid,lower]
- return result
-
- try:
- ref_boll=bbands(94,2)
- ref_upper=boll[0]
- ref_lower=boll[2]
- ref_close=okcoinSpot.getKline('1min','1','0')[0][4]
- ref_ma34=pd.DataFrame(okcoinSpot.getKline('1min','34','0')).iloc[::,4].mean()
- except:
- print('json错误')
-
- time.sleep(58)
- while True:
- try:
- boll=bbands(94,2)
- upper=boll[0]
- lower=boll[2]
- close=okcoinSpot.getKline('1min','1','0')[0][4]
- ma34=pd.DataFrame(okcoinSpot.getKline('1min','34','0')).iloc[::,4].mean()
- except:
- print('json错误')
- continue
- if close>upper and ref_close
- print('买入信号',okcoinSpot.trade('btc_cny','buy','7500','0.01'))
- if closeand ref_close>ref_ma34:
- print('卖出信号',okcoinSpot.trade('btc_cny','sell','1','0.01'))
-
- try:
- ref_boll=bbands(94,2)
- ref_upper=boll[0]
- ref_lower=boll[2]
- ref_close=okcoinSpot.getKline('1min','1','0')[0][4]
- ref_ma34=pd.DataFrame(okcoinSpot.getKline('1min','34','0')).iloc[::,4].mean()
- except:
- print('json错误')
- continue
- time.sleep(58)
- now=datetime.datetime.now()
- now=now.strftime('%Y-%m-%d %H:%M:%S')
- i=i+1
- print(now,i)