forked from fmzquant/strategies
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHedge_BTC-ETH Demo.js
More file actions
157 lines (122 loc) · 4.58 KB
/
Hedge_BTC-ETH Demo.js
File metadata and controls
157 lines (122 loc) · 4.58 KB
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
/*
策略出处: https://www.botvs.com/strategy/48536
策略名称: Hedge_BTC-ETH Demo
策略作者: 小小梦
策略描述:
BTC 和 ETH 的 跨品种对冲 思路测试 DEMO
根据价格比 画出 图表,分析 价格比 变化,寻找套利空间。
策略可行性 未知, 有兴趣的 同学可以研究一下~
by littleDream
参数 默认值 描述
---- ----- ------------
Mode 0 模式: BOLL|SMA
*/
/*exchanges
A : BTC
B : ETH
*/
var RateUpDateTime = new Date().getTime()
var UpDateCyc = 60 * 60 * 1000
var SumInCyc = 0
var AddCounts = 1
var RateArray = []
var BTC_hold_amount = 0
var ETH_hold_amount = 0
var IDLE = 0
var PLUS = 1
var STATE = IDLE
var fee_btc = {
buy : 0.2, // 0.2 % , 千分之四
sell : 0.2
}
var fee_eth = {
buy : 0.2,
sell : 0.2
}
var ModeStr = ["BOLL", "SMA"][Mode]
function CalcPriceForNoFee(price, fee, type){
if(type == "buy"){
return price * (1 - fee / 100)
}else if(type == "sell"){
return price * (1 + fee / 100)
}
}
function loop(nowTime){
var depthA = exchanges[0].GetDepth()
var depthB = exchanges[1].GetDepth()
var sma120 = null
var sma10 = null
var boll = null
if(!depthA || !depthB || depthA.Asks.length == 0 || depthA.Bids.length == 0 || depthB.Asks.length == 0 || depthB.Bids.length == 0){
return
}
var Rate = CalcPriceForNoFee((depthA.Bids[0].Price + depthA.Asks[0].Price) / 2, 0.2, "buy") / CalcPriceForNoFee((depthB.Bids[0].Price + depthB.Asks[0].Price) / 2, 0.2, "sell")
if(nowTime - RateUpDateTime > UpDateCyc){
RateArray.push(Rate)
$.PlotLine("avgRate", RateArray[RateArray.length - 2], RateUpDateTime)
if(RateArray.length > 60){
if(ModeStr == "SMA"){
sma120 = talib.SMA(RateArray, 60)
sma10 = talib.SMA(RateArray, 10)
$.PlotLine("sma120", sma120[sma120.length - 2], RateUpDateTime)
$.PlotLine("sma10", sma10[sma10.length - 2], RateUpDateTime)
}else if(ModeStr == "BOLL"){
boll = TA.BOLL(RateArray, 20, 2.5)
$.PlotLine("up", boll[0][boll[0].length - 2], RateUpDateTime)
$.PlotLine("down", boll[2][boll[2].length - 2], RateUpDateTime)
}
}
RateUpDateTime += UpDateCyc
SumInCyc = 0
AddCounts = 1
}else{
SumInCyc += Rate
AddCounts++
RateArray[RateArray.length - 1] = (SumInCyc / AddCounts)
$.PlotLine("avgRate", RateArray[RateArray.length - 1], RateUpDateTime)
if(RateArray.length > 60){
if(ModeStr == "SMA"){
sma120 = talib.SMA(RateArray, 60)
sma10 = talib.SMA(RateArray, 10)
$.PlotLine("sma120", sma120[sma120.length - 1], RateUpDateTime)
$.PlotLine("sma10", sma10[sma10.length - 1], RateUpDateTime)
}else if(ModeStr == "BOLL"){
boll = TA.BOLL(RateArray, 20, 2.5)
$.PlotLine("up", boll[0][boll[0].length - 1], RateUpDateTime)
$.PlotLine("down", boll[2][boll[2].length - 1], RateUpDateTime)
}
}
}
if(ModeStr == "SMA"){
if(STATE == IDLE && (sma120 && sma10) && sma120[sma120.length - 2] > sma10[sma10.length - 2] && sma120[sma120.length - 1] < sma10[sma10.length - 1]){
// PLUS
var SellInfo = $.Sell(exchanges[1], 5)
var sumMoney = SellInfo.price * SellInfo.amount
var amount = _N(sumMoney / depthA.Asks[0].Price, 2)
var BuyInfo = $.Buy(exchanges[0], amount)
ETH_hold_amount = SellInfo.amount
BTC_hold_amount = amount
STATE = PLUS
// throw "stop" // ceshi
}
if(STATE == PLUS && (sma120 && sma10) && sma120[sma120.length - 2] < sma10[sma10.length - 2] && sma120[sma120.length - 1] > sma10[sma10.length - 1]){
// COVER
var BuyInfo = $.Buy(exchanges[1], ETH_hold_amount)
var SellInfo = $.Sell(exchanges[0], BTC_hold_amount)
ETH_hold_amount = 0
BTC_hold_amount = 0
STATE = IDLE
Log(exchanges[0].GetAccount(), exchanges[1].GetAccount())
}
}
}
function main() {
var AccountA = exchanges[0].GetAccount()
var AccountB = exchanges[1].GetAccount()
Log("AccountA:", AccountA, "AccountB:", AccountB)
while(true){
var beginTime = new Date().getTime()
loop(beginTime)
Sleep(500)
}
}