-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcalc.html
272 lines (241 loc) · 7.67 KB
/
calc.html
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>网格爆仓计算器</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<!-- 引入 echarts.js -->
<script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
<style>
li{ display:inline}
</style>
</head>
<body>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div id="main1" ><table id="main" border="1">
<tr>
<td><a>币的名称:</a><td>
<td><input id="tokenname" value="GMT"></input></td>
</tr>
<tr>
<td><a>开仓价格:</a><td>
<td><input id="htmlopenprice" value="2.29"></input></td>
</tr>
<tr>
<td><a>网格下限价格</a><td>
<td><input id="htmlpricelow" value="1.2"></input></td>
</tr>
<tr>
<td><a>网格上限价格</a><td>
<td><input id="htmlpricehigh" value="3.6"></input></td>
</tr>
<tr>
<td><a>网格数量</a><td>
<td><input id="htmlgridnumber" value="100"></input></td>
</tr>
<tr>
<td><a>杠杆倍数</a><td>
<td><input id="htmlleverage" value="20"></input></td>
</tr>
<tr>
<td><a>开仓投入U数量</a><td>
<td><input id="htmlusdnumber" value="10000"></input></td>
</tr>
<tr>
<td><a>保证金数量</a><td>
<td><input id="htmluseusdnumber" value="10000"></input></td>
</tr>
<tr>
<td><a>最小开仓张数</a><td>
<td><input id="htmlminCoinNumber" value="1"></input></td>
</tr>
<tr>
<td><a>最小开仓张数有几位小数</a><td>
<td><input id="htmlminxiaoshudian" value="0"></input></td>
</tr>
<tr>
<td><a>手续费</a><td>
<td><input id="htmlc_rate" value="0.0002"></input></td>
</tr>
</table ></div>
<button type="button" onclick=calc() style="width:100px;height:50px">提交</button>
<br>
<div id="log" width="600" height="600"></div>
<script type="text/javascript">
//网格类,用于计算该网格的在某个价格下亏损多少钱
class Grid {
//构造函数
constructor(nCoinNumber, price,c_rate, openprice) {
this.nCoinNumber = nCoinNumber;//类中变量
this.price = price;
this.c_rate = c_rate;
this.openprice = openprice;
if(price<openprice)
{
this.blong = true;
}
else
{
this.blong = false;
}
}
//类中函数
Getlost(nowprice) {
if(this.blong == true)
{
if(nowprice>this.price)
{
this.nLost =0;
return 0;
}
this.nLost = (this.price -nowprice)*this.nCoinNumber + this.nCoinNumber*this.price*this.c_rate;
return this.nLost;
}
else
{
if(nowprice<this.price)
{
this.nLost =0;
return 0;
}
this.nLost = (nowprice-this.price )*this.nCoinNumber + this.nCoinNumber*this.price*this.c_rate;
return this.nLost;
}
}
}
/*参数
*开仓价格 openprice
*网格上限 pricelow
*网格下限 pricehigh
*网格数量 gridnumber
*杠杆倍数 leverage
*开仓投入U数量 usdnumber
*保证金数量 useusdnumber 可以决定补仓,比如初始投入1万U,我后面快爆了又投入1万U,这个时候就有2万U的保证金
*等差等比网格 暂时只支持等差网格
*最小开仓张数 minCoinNumber
*最小开仓张数有几位小数 minxiaoshudian
*手续费 c_rate
*/
/*
openprice = 2.29;
pricelow = 1.2;
pricehigh = 3.6;
gridnumber = 100;
leverage = 20;
usdnumber =10000;
useusdnumber = 20000;
c_rate = 2/10000;
minCoinNumber = 1;
minxiaoshudian = 0;*/
function calc()
{
openprice = Number(htmlopenprice.value);
pricelow = Number(htmlpricelow.value);
pricehigh = Number(htmlpricehigh.value);
gridnumber = Number(htmlgridnumber.value);
leverage = Number(htmlleverage.value);
usdnumber = Number(htmlusdnumber.value);
useusdnumber = Number(htmluseusdnumber.value);
c_rate = Number(htmlc_rate.value);
minCoinNumber = Number(htmlminCoinNumber.value);
minxiaoshudian = Number(htmlminxiaoshudian.value);
CreateGrid(openprice,pricelow,pricehigh,gridnumber,leverage,usdnumber,useusdnumber,c_rate,minCoinNumber,minxiaoshudian);
}
//这里暂时用的小币种,最小开一张合约这种。如果比特币之类的要调整
//实际计算代码
function CreateGrid(openprice,pricelow,pricehigh,gridnumber,leverage,usdnumber,useusdnumber,c_rate,minCoinNumber,minxiaoshudian)
{
//debugger;
var GridList= []
var nprice = (pricehigh -pricelow)/gridnumber;
nprice = nprice.toFixed(4)
var nCoinNumber = (leverage*usdnumber/(gridnumber*openprice));
if(nCoinNumber<minCoinNumber)
{
console.log("保证金太少,无法开网格");
}
nCoinNumber = nCoinNumber.toFixed(minxiaoshudian)
//开仓的时候靠近网格的一格不开
var nmid = parseInt((openprice - pricelow)/nprice);
var lowmid = openprice - pricelow - nmid*nprice;
var highmid =pricelow + (nmid+1)*nprice - openprice;
var missmid = nmid;
if(highmid < lowmid)
{
missmid = nmid+1;
}
for(i=0;i<gridnumber+1;i++)
{
if(i== missmid)
{
continue;
}
var grid =new Grid(nCoinNumber,pricelow + i*nprice,c_rate,openprice);
GridList.push(grid);
}
console.log("------------------------------------------------------");
var nowprice = openprice;
while(nowprice>0)
{
var los = 0;
for(i=0;i<gridnumber;i++)
{
var grid = GridList[i];
los =los + grid.Getlost(nowprice);
}
if(los > useusdnumber)
{
console.log("开仓信息:")
console.log("开仓价格:" + openprice);
console.log("网格最低价:" + pricelow);
console.log("网格最高价:" + pricehigh);
console.log("网格数量:" + gridnumber);
console.log("网格杠杆:" + leverage + "x");
console.log("网格开仓金额" + usdnumber +"usd");
console.log("网格保证金金额" + useusdnumber + "usd");
console.log("当价格低于" + nowprice+"时,爆仓");
break;
}
nowprice = nowprice - 0.0001;
}
console.log("------------------------------------------------------");
nowprice = openprice;
while(nowprice>0)
{
var los = 0;
for(i=0;i<gridnumber;i++)
{
var grid = GridList[i];
los =los + grid.Getlost(nowprice);
}
if(los > useusdnumber)
{
console.log("开仓信息:")
console.log("开仓价格:" + openprice);
console.log("网格最低价:" + pricelow);
console.log("网格最高价:" + pricehigh);
console.log("网格数量:" + gridnumber);
console.log("网格杠杆:" + leverage+ "x");
console.log("网格开仓金额" + usdnumber +"usd");
console.log("网格保证金金额" + useusdnumber + "usd");
console.log("当价格大于" + nowprice+"时,爆仓");
break;
}
nowprice = nowprice + 0.0001;
}
console.log("------------------------------------------------------");
}
(function () {
var old = console.log;
var logger = document.getElementById('log');
console.log = function (message) {
if (typeof message == 'object') {
logger.innerHTML += (JSON && JSON.stringify ? JSON.stringify(message) : message) + '<br />';
} else {
logger.innerHTML += message + '<br />';
}
}
})();
</script>
</body>
</html>