-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathguncangmoniqiV2.js
199 lines (158 loc) · 5.08 KB
/
guncangmoniqiV2.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
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
//滚仓模拟器
//滚仓原理,比如开多,每赚1%,就拿赚的这1%开仓合约
//或者开空,每跌1%,就拿这赚的1%继续开空
//网格基础类
class Heyue {
//构造函数
constructor(nCoinNumber, price, c_rate, openprice, blong) {
this.nCoinNumber = nCoinNumber;//每格开多少张的合约
this.price = price;
this.c_rate = c_rate;
this.openprice = openprice;
this.blong = blong;
}
AddPiao(nCoinNumber, price, blong = false) {
nCoinNumber = Number(nCoinNumber);
if (this.blong == blong) {
var allprice = this.price * this.nCoinNumber + nCoinNumber * price;
this.nCoinNumber = this.nCoinNumber + nCoinNumber;
this.price = allprice / this.nCoinNumber;
}
else {
var allprice = this.price * this.nCoinNumber - nCoinNumber * price;
this.nCoinNumber = this.nCoinNumber - nCoinNumber;
this.price = allprice / this.nCoinNumber;
}
}
Getwin(nowprice) {
if (this.blong == true) {
this.nLost = (this.price - nowprice) * this.nCoinNumber + this.nCoinNumber * this.price * this.c_rate;
return 0 - this.nLost;
}
else {
this.nLost = (nowprice - this.price) * this.nCoinNumber + this.nCoinNumber * this.price * this.c_rate;
return 0 - this.nLost;
}
}
}
heyuejisuan();
//开仓价格
//滚仓比例
//杠杆水平
//保证金
//手续费
//结束价格
//单张合约最小数量
//小数点位数
//等差还是等比
function heyuejisuan() {
openprice = 100.0;
guicangbili = 0.1;
leverage = 1;
usdnumber = 10000;
//useusdnumber = 10000;
c_rate = 2 / 10000;
stopprice = 0.001;
minCoinNumber = 1;
minxiaoshudian = 0;
// 0 等比,1等差
addtype = 0;
//以下参数不用填
guncangtype = 2;
guncangprice = 0;
heyuepiao = null
//1做多,2做空
if (openprice < stopprice) {
guncangtype = 1;
guncangprice = ((stopprice - openprice) * guicangbili)
heyuepiao = new Heyue(0, 0, c_rate, openprice, true);
}
else {
guncangtype = 2;
guncangprice = ((openprice - stopprice) * guicangbili)
heyuepiao = new Heyue(0, 0, c_rate, openprice, false);
}
var diyifenzijin = usdnumber
nowprice = openprice;
//开始滚仓
while (nowprice>0) {
//计算花了多少保证金
var baozhengjin = 0;
var win = heyuepiao.Getwin(nowprice);
var keshiyongzijin = (diyifenzijin + win) * leverage;
diyifenzijin = 0;
var nCoinNumber = (keshiyongzijin / nowprice);
if (nCoinNumber < minCoinNumber) {
console.log("保证金太少,无法开合约");
}
else {
nCoinNumber = nCoinNumber.toFixed(minxiaoshudian)
//开始计算创建合约
if (guncangtype == 1) {
heyuepiao.AddPiao(nCoinNumber, nowprice, true);
}
else {
heyuepiao.AddPiao(nCoinNumber, nowprice, false);
}
}
// 计算合约爆仓价
var totalzijin = usdnumber + win;
var baocangjia = calcjisuanjiage(heyuepiao, nowprice, guncangtype, totalzijin);
console.log("持仓价格:" + heyuepiao.price + " 当前持仓张数:" + heyuepiao.nCoinNumber + " 当前价格:" + nowprice + " 当前盈利:" + win + " 盈利比:" + (win * 100 / usdnumber).toFixed(2) + "%" + " 当前爆仓价:" + baocangjia);
if (guncangtype == 1) {
if (nowprice > stopprice) {
break;
}
else {
if (addtype == 0) {
nowprice = nowprice * (1 + guicangbili)
}
else {
nowprice = nowprice + guncangprice;
}
}
}
else {
if (nowprice < stopprice) {
break;
}
else {
if (addtype == 0) {
nowprice = nowprice * (1 - guicangbili)
}
else {
nowprice = nowprice - guncangprice;
}
}
}
}
}
function calcjisuanjiage(piao, openprice, gridtype, useusdnumber) {
//合约爆仓价
console.log("可以用保证金为:" + useusdnumber)
if (gridtype == 1) {
//做多合约
nowprice = openprice;
while (nowprice > 0) {
var win = piao.Getwin(nowprice)
var dangqianyingli = win + useusdnumber;
if (dangqianyingli < 0) {
return nowprice;
}
nowprice = nowprice - 0.01;
}
return 0;
}
else {
//开空合约
var nowprice = openprice;
while (nowprice > 0) {
var win = piao.Getwin(nowprice);
var dangqianyingli = win + useusdnumber;
if (dangqianyingli < 0) {
return nowprice;
}
nowprice = nowprice + 0.0001;
}
}
}