forked from fmzquant/strategies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR-Breaker 交易策略.py
226 lines (200 loc) · 9.75 KB
/
R-Breaker 交易策略.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
'''
策略出处: https://www.botvs.com/strategy/23531
策略名称: R-Breaker 交易策略
策略作者: 太极
策略描述:
R-Breaker 交易策略
'''
#!/usr/local/bin/python
#-*- coding: UTF-8 -*-
#R-Breaker 交易策略
#策略提供者 @FJK QQ:171938416
#改进 @太极 QQ:7650371
def my_buy(): #开仓
try:
global buy_price,buy_qty
initAccount = ext.GetAccount() #交易模板的导出函数, 获得账户状态,保存策略运行前账户初始状态
opAmount=1
PositionRatio =1
#开仓之前判断有币没有没有先进行买入
if int(initAccount.Stocks)>1:
if buy_price<1:
buy_price=_C(exchange.GetTicker).Last
buy_qty=initAccount.Stocks
#Log('开仓信息1 仓内还有比:',initAccount.Stocks,'进行清空','--开仓详情:',initAccount)
return 1
if int(initAccount.Stocks)<1:
if int(str(initAccount.Stocks).replace('0.',''))>=3:
if buy_price<1:
buy_price=_C(exchange.GetTicker).Last
buy_qty=initAccount.Stocks
#Log('开仓信息2 仓内还有比:',initAccount.Stocks,'进行清空','--开仓详情:',initAccount)
return 1
#if int(str(initAccount.Stocks).replace('0.',''))==0:
opAmount = _N(initAccount.Balance*PositionRatio,3) #买入数量
Log("开仓没有币先进行 开仓买入%s元"%(str(opAmount))) #生成LOG日志
Dict = ext.Buy(opAmount) #买入ext.Buy
if(Dict):#确认开仓成功
buy_price=Dict['price'] #买入价格 #{'price': 4046.446, 'amount': 1.5}
buy_qty=Dict['amount'] #买入数量
#LogProfit(_N(gains,4),'开仓信息 钱:',initAccount.Balance,'--币:',initAccount.Stocks,'--开仓详情:',Dict)
print_log(1,initAccount)
return 1
return 0
except Exception,ex:
Log('except Exception my_buy:',ex)
return 0
import time
import datetime
def Caltime(date1,date2): #计算运行天数
try:
date1=time.strptime(date1,"%Y-%m-%d %H:%M:%S")
date2=time.strptime(date2,"%Y-%m-%d %H:%M:%S")
date1=datetime.datetime(date1[0],date1[1],date1[2],date1[3],date1[4],date1[5])
date2=datetime.datetime(date2[0],date2[1],date2[2],date2[3],date2[4],date2[5])
return date2-date1
except Exception,ex:
Log('except Exception Caltime:',ex)
return "except Exception"
start_timexx =time.localtime(time.time()) #time.clock()
start_time=time.strftime("%Y-%m-%d %H:%M:%S",start_timexx)
buy_price=0 #买入价格
buy_qty=0 #买入数量
gains=0 #盈利
beng_Account = ext.GetAccount() #初始化信息
beng_ticker = _C(exchange.GetTicker).Last#Ticker 市场行情 最后成交价
beng_Balance=(beng_Account.Stocks*beng_ticker)+beng_Account.Balance #初始化账户钱
def print_log(k_p,data=""): #输出
try:
name=""
if k_p:
name="开仓"
else:
name="平仓"
global beng_Account,beng_ticker,beng_Balance
global gains
end_Account = ext.GetAccount() #当前账户信息
end_ticker = _C(exchange.GetTicker).Last#Ticker 市场行情 最后成交价
#################################################
date1=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
msg_data0=("本次开始运行时间:%s已运行:%s\r\n"%(start_time,Caltime(start_time,date1)))
#################################################
msg_data1=("本次初始化状态:%s\r\n当前运行状态:%s\r\n"%(beng_Account,end_Account))
#################################################
end_Balance=(end_Account.Stocks*end_ticker)+end_Account.Balance #当前账面上钱数
msg_data2=("初始化钱:%s现在钱:%s盈亏:%s\r\n"%(str(beng_Balance),str(end_Balance),str(end_Balance-beng_Balance)))
#################################################
total = end_Account.Balance+end_Account.Stocks*_C(exchange.GetTicker).Last #账户总额
roi = ((total/beng_Balance) -1)*100
msg_data3=("当前状态:%s--钱:%s--币:%s--总值约:%.2f\r\n"%(str(name),str(end_Account.Balance),str(end_Account.Stocks),roi))
#################################################
income = total - beng_Account['Balance'] - beng_Account['Stocks']*beng_ticker #总盈亏
msg_data4=("本次盈亏:%s(RMB)\t总盈亏:%.2f(RMB) %.2f\r\n"%(str(gains),income,roi))
#################################################
#盈利计算方法
#盈利计算方法 浮动利润: 按 (现在币 - 初始币)x 现在的价格 + (现在的钱 - 初始的钱)
diff_stocks=end_Account.Stocks-beng_Account.Stocks #比的差值
diff_balance=end_Account.Balance-beng_Account.Balance #钱的差值
new_end_balance=diff_stocks*end_ticker+diff_balance #实现盈亏 #当前的盈利
#盈利计算方法 账面利润 : (现在币 x 现在价格+现在钱) - (初始币 x 初始价格 + 初始钱)
new_end_balance2=(end_Account.Stocks*end_ticker+end_Account.Balance)-(beng_Account.Stocks*beng_ticker+beng_Account.Balance)
msg_data5=("浮动利润:%s(RMB)\r\n账面利润:%s(RMB)\r\n"%(str(_N(new_end_balance,3)),str(_N(new_end_balance2,3))))
#################################################
LogStatus("初始化投入2016/9/24 投入0.2个币=等于行情800RMB\r\n",
msg_data0,msg_data1,msg_data2,msg_data3,msg_data4,msg_data5,
"更新时间:%s\r\n"%(date1),
"%s"%(data)
)
#################################################
#################################################
#################################################
except Exception,ex:
Log('except Exception print_log:',ex)
def my_sell(): #平仓
try:
global buy_price,buy_qty,gains,ExitPeriod
ExitPeriod = 0
nowAccount = ext.GetAccount() #交易模板的导出函数 获取账户信息
if nowAccount.Stocks<=0.002: #保证满足交易量
#Log('不满足最小交易量:',nowAccount.Stocks)
return 1
#history_Last=_N(Volume_averages(Ticker_list),2) #历史均价
#cur_last = _N(_C(exchange.GetTicker).Last,2)
#if _N(_C(exchange.GetTicker).Last,2)>buy_price+ExitPeriod : #当前价格一定要大于 开仓价格
if True:
#if _N(_C(exchange.GetTicker).Last,2)>buy_price+ExitPeriod and history_Last - cur_last >0 and history_Last - cur_last < 2 : #当前价格一定要大于 开仓价格
#Log('历史差价:',history_Last - cur_last)
Dict = ext.Sell(nowAccount.Stocks)
#Dict ={"price":_C(exchange.GetTicker).Last}
if(Dict):
#sell_count+=1
sell_gains=(Dict['price']-buy_price)*Dict['amount']
gains=gains+sell_gains
buy_price=0 #买入价格
buy_qty=0 #买入数量
LogProfit(_N(gains,4),'平仓信息 钱:',nowAccount.Balance,'--币:',nowAccount.Stocks,'--平仓详情:',Dict)#收益曲线
print_log(0,nowAccount)
return 1
else:
current_Last = _N(_C(exchange.GetTicker).Last,2) ##当前价格
data="不具备平仓条件:买入-当前=差价:%s-%s=%s"%(buy_price,current_Last,_N(buy_price-current_Last,2))
print_log(0,nowAccount,data)
return 0
except Exception,ex:
Log('except Exception my_sell:',ex)
return 0
########################################################
def onTick():
try:
records =exchange.GetRecords() #由你自己设定的周期数返回的数据
HH = records[-2]['High'] #最日最高
LC = records[-2]['Low'] #昨日最低
HC = records[-2]['Close'] #昨日收盘
LL = records[-2]['Low'] #昨日最低
Pivot = (HH+HC+LC)/3 #枢轴点
R1 = 2*Pivot-LC #阻力1
R2 = Pivot+(HH-LC) #阻力2
R3 = HH +2*(Pivot-LC) #阻力3
S1 = 2*Pivot-HH #支撑位1
S2 = Pivot - (HH-LC) #支撑位2
S3 = LC-2*(HH-Pivot) #支撑位3
#Log('r1',R1,"R2",R2,'R3',R3)
#Log('S1',S1,"S2",S2,'S3',S3)
To = records[-1]['Open'] #今日开盘价
Th = records[-1]['High'] #今日最高价
Tl = records[-1]['Low'] #今日最低价
current_price = _C(exchange.GetTicker).Last #当前价格
#当前价格>阻力3 开仓
if current_price > R3: #突破上轨开多
if my_buy(): #Log('多')
return
#当前价格<支撑位3 平仓
if current_price < S3: #突破下轨卖空
if my_sell(): #Log('空')
return
# 条件1 今日最高价>阻力2
# 条件2 今日最高价<阻力3
# 条件3 当前价格<阻力1
# 同时具备这3个条件平仓
if Th >R2 and Th <R3 and current_price <R1: #趋势反转卖出
if my_sell(): #Log('空')
return
# 条件1 今日最低价<支撑位2
# 条件2 今日最低价>支撑位3
# 条件3 当前价格<支撑位1
# 同时具备这3个条件开仓
if Tl <S2 and Tl >S3 and current_price <S1: #支撑位1
if my_buy(): #Log('多')
return
# Log(records[-1])#今日K
# Log(records[-2])#昨日K
# Log(exchange.GetTicker())#当前
except Exception,ex:
Log('except Exception onTick:',ex)
def main():
global outAccount
outAccount = ext.GetAccount() #初始化信息
Log("run ",outAccount) #输出初始账户信息
while True:
onTick()
Sleep(1000)