forked from fmzquant/strategies
-
Notifications
You must be signed in to change notification settings - Fork 1
/
差价监控.js
158 lines (147 loc) · 4.45 KB
/
差价监控.js
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
/*
策略出处: https://www.botvs.com/strategy/1340
策略名称: 差价监控
策略作者: Zero
策略描述:
只支持两个交易所, 可自定义差价的类型, 支持2.77托管者的自定义图表功能
参数 默认值 描述
------------ ----- ----------------------
AType 0 主平台价格类型: 上次成交价|买一价|卖一价
BType 0 次平台价格类型: 上次成交价|买一价|卖一价
Interval 2000 出错重试间隔(毫秒)
TickInterval 2000 检测频率(毫秒)
EnableCR false 自定义汇率
USDCNY false USDCNY
NormalDiff 0.1 普通差价
HighDiff 0.3 较高差价
按钮 默认值 描述
---- ---------- ----
重置数据 __button__ @
*/
var __lastDiff = 0;
var __AType = ["Last", "Buy", "Sell"][AType];
var __BType = ["Last", "Buy", "Sell"][BType];
var cfg = {
tooltip: {xDateFormat: '%Y-%m-%d %H:%M:%S, %A'},
title : { text : '差价分析图'},
rangeSelector: {
buttons: [{type: 'hour',count: 1, text: '1h'}, {type: 'hour',count: 3, text: '3h'}, {type: 'hour', count: 8, text: '8h'}, {type: 'all',text: 'All'}],
selected: 0,
inputEnabled: false
},
xAxis: { type: 'datetime'},
yAxis : {
plotLines : [{
value : 0.0,
color : 'black',
dashStyle : 'shortdash',
width : 3,
}, {
value : NormalDiff,
color : 'green',
dashStyle : 'shortdash',
width : 1,
}, {
value : HighDiff,
color : 'red',
dashStyle : 'shortdash',
width : 1,
},{
value : -NormalDiff,
color : 'green',
dashStyle : 'shortdash',
width : 1,
}, {
value : -HighDiff,
color : 'red',
dashStyle : 'shortdash',
width : 1,
}]
},
series : [{
name : '价差',
data : [],
tooltip: {
valueDecimals: 2
}
}]
};
function _N(v, precision) {
if (typeof(precision) != 'number') {
precision = 4;
}
var d = parseFloat(v.toFixed(Math.max(10, precision+5)));
s = d.toString().split(".");
if (s.length < 2 || s[1].length <= precision) {
return d;
}
var b = Math.pow(10, precision);
return Math.floor(d*b)/b;
}
function GetTicker(e) {
if (typeof(e) == 'undefined') {
e = exchange;
}
var ticker;
while (!(ticker = e.GetTicker())) {
Sleep(Interval);
}
return ticker;
}
function onTick() {
var tickerA = GetTicker(exchanges[0]);
var tickerB = GetTicker(exchanges[1]);
var diff = _N(tickerA[__AType] - tickerB[__BType]);
LogStatus(exchanges[0].GetName(), _N(tickerA[__AType]), exchanges[1].GetName(), _N(tickerB[__BType]), "差价:", diff);
if (__lastDiff != 0) {
if (Math.abs(Math.abs(diff) - Math.abs(__lastDiff)) > 200) {
return;
}
}
if (diff != __lastDiff) {
// add添加数据到series, 参数格式为[series序号, 数据];
cfg.yAxis.plotLines[0].value=diff;
cfg.subtitle={text:'当前价差:' + diff};
__chart.update(cfg);
__chart.add([0, [new Date().getTime(), diff]]);
__lastDiff = diff;
}
}
function main() {
if (parseFloat(Version()) < 2.77) {
throw "只支持2.77或以上版本";
}
if (exchanges.length != 2) {
throw "只支持两个交易所对冲";
}
// 传给Chart函数的必须是一个与上下文无关的结构体(附合HighStocks规则, 详情参数HighStocks使用方法)
__chart = Chart(cfg);
// reset 清空所有图表之前的信息
// __chart.reset();
if (EnableCR) {
for (var i = 0; i < exchanges.length; i++) {
var rate = exchanges[i].GetRate();
if (rate != 1) {
exchanges[i].SetRate(USDCNY);
Log("更改", exchanges[i].GetName(), "汇率", rate, "为", USDCNY);
}
var eName = exchanges[i].GetName();
if (eName == "Futures_BitVC") {
exchanges[i].SetContractType("week");
}
}
}
Log(exchanges[0].GetName()+"."+__AType, "-", exchanges[1].GetName()+"."+__BType, '差价做为收益显示到图表');
TickInterval = Math.max(TickInterval, 50);
Interval = Math.max(Interval, 50);
while (true) {
onTick();
Sleep(TickInterval);
if (GetCommand() === '重置数据') {
LogReset();
LogProfitReset();
__chart.reset();
Log("数据重置成功");
}
}
}