This repository has been archived by the owner on Apr 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.js
268 lines (251 loc) · 7.51 KB
/
sim.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
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
$.extend({
sprintf: function (format)
{
// 第2引数以降を順に処理
for (var i = 1; i < arguments.length; i++)
{
// 正規表現でプレイスホルダと対応する引数の値を置換処理
var reg = new RegExp('\\{' + (i - 1) + '\\}', 'g');
format = format.replace(reg, arguments[i]);
}
return format;
},
toFixed: function (value)
{
return $.isNumeric(value) ? parseFloat(value).toFixed(2) : value;
}
});
var COMMAND =
{
TRADE: "Trade",
CLOSE: "Close"
};
var TRADE_TYPE =
{
BUY: "Buy",
SELL: "Sell"
};
var TRADE_OPTION =
{
NO_LIMIT: "N/L",
STOP: "Stop",
LIMIT: "Limit",
};
var Order =
function (order, type, price, lot, takeProfit, option)
{
this.order = order;
this.type = type;
this.price = price;
this.lot = lot;
this.takeProfit = takeProfit ? takeProfit : "*****";
this.option = option ? option : "";
this.fixedPrice = function () { return $.toFixed(this.price); };
this.fixedTP = function () { return $.toFixed(this.takeProfit); };
this.toPosition = function () { return new Position(this.type, this.price, this.lot, this.takeProfit); };
this.toPreOrder = function () { return new PreOrder(this.type, this.option, this.price, this.lot, this.takeProfit); };
};
var Position =
function (type, price, lot, takeProfit)
{
this.type = type;
this.price = price;
this.lot = lot;
this.fluctuated = price;
this.sign = type == TRADE_TYPE.BUY ? 1 : -1;
this.takeProfit = takeProfit ? takeProfit : "*****";
this.fixedPrice = function () { return $.toFixed(this.price); };
this.fixedTP = function () { return $.toFixed(this.takeProfit); };
this.fixedFL = function () { return $.toFixed(this.fluctuated); };
this.isContract = function ()
{
return this.type == TRADE_TYPE.BUY ?
this.fluctuated >= this.takeProfit : this.fluctuated <= this.takeProfit;
};
this.gain = function ()
{
return Math.floor((this.fluctuated - this.price) * this.lot * unit * this.sign);
};
this.gainTP = function ()
{
return Math.floor((this.takeProfit - this.price) * this.lot * unit * this.sign);
};
};
var PreOrder =
function (type, option, price, lot, takeProfit)
{
this.type = type;
this.price = price;
this.lot = lot;
this.option = option;
this.takeProfit = takeProfit ? takeProfit : "*****";
this.fixedPrice = function () { return $.toFixed(this.price); };
this.fixedTP = function () { return $.toFixed(this.takeProfit); };
this.isTrade = function (fluctuated)
{
return (this.type == TRADE_TYPE.BUY && this.option == TRADE_OPTION.STOP) || (this.type == TRADE_TYPE.SELL && this.option == TRADE_OPTION.LIMIT) ?
fluctuated >= this.price : fluctuated <= this.price;
};
this.toPosition = function () { return new Position(this.type, this.price, this.lot, this.takeProfit); };
};
var initialRate = 165;
var exchangeGap = 0.25;
var unit = 1000;
var lotUnit = 0.01;
var lot = 0;
var currentRate = 165;
var totalProfits = 0;
var unrealizedGains = 0;
var martingale = 0;
var orders = [];
var positions = [];
var preorder = [];
/**
* 数値検証
*/
function isValue(value, min, max)
{
var mn = $.isNumeric(min) ? min : Number.NEGATIVE_INFINITY;
var mx = $.isNumeric(max) ? max : Number.POSITIVE_INFITINY;
return $.isNumeric(value) && value >= mn && value <= mx;
}
function connectField(id, init, set)
{
var q = $(id);
var change =
function ()
{
var v = q.val();
if (isValue(v, q.attr("min"), q.attr("max")))
{
set(parseFloat(v));
}
};
q.val(init)
q.change(change);
change();
return q;
}
function render(list, target, renderer)
{
var html = $(target);
html.empty();
$.each(list, function (i, o) { html.append(renderer(o)); });
}
function refreshStatus()
{
var rdr =
function (o)
{
var format = "<li class=\"{1}\">{0}: {1} {2} {3}, x{4}, T/P: {5}</li>";
return $.sprintf(format, o.order, o.type, o.option, o.fixedPrice(), o.lot, o.fixedTP());
};
render(orders, "#directions", rdr);
var rpt =
function (o)
{
var format = "<li class=\"{0}\">{0}: {1} -> {2} ({3}), x{4}, T/P: {5}</li>";
return $.sprintf(format, o.type, o.fixedPrice(), o.fixedFL(), o.gain(), o.lot, o.fixedTP());
};
render(positions, "#positions", rpt);
var rpo =
function (o)
{
var format = "<li class=\"{0}\">{0} {1} {2}, x{3}, T/P: {4}</li>";
return $.sprintf(format, o.type, o.option, o.fixedPrice(), o.lot, o.fixedTP());
};
render(preorders, "#preorders", rpo);
$("#profit").text(totalProfits);
$("#ugain").text(unrealizedGains);
}
function setCurrentRate(value)
{
$("#ft").val((currentRate = value).toFixed(2));
}
function moveCurrentRate(value)
{
setCurrentRate(currentRate + value);
}
function setLot()
{
$("#lt").val(lot = unit * lotUnit);
}
function setDirection(command, type, price, lot, takeProfit, option)
{
var order = new Order(command, type, price, lot, takeProfit, option);
orders.push(order);
switch (command)
{
case COMMAND.TRADE:
if (option == TRADE_OPTION.NO_LIMIT)
{
positions.push(order.toPosition());
}
else
{
preorders.push(order.toPreOrder());
}
break;
case COMMAND.CLOSE:
break;
}
}
function setPreOrder()
{
setDirection(COMMAND.TRADE, TRADE_TYPE.BUY, currentRate + exchangeGap, lotUnit, currentRate + exchangeGap * 2, TRADE_OPTION.STOP);
setDirection(COMMAND.TRADE, TRADE_TYPE.SELL, currentRate + exchangeGap, lotUnit, currentRate, TRADE_OPTION.LIMIT);
setDirection(COMMAND.TRADE, TRADE_TYPE.BUY, currentRate - exchangeGap, lotUnit, currentRate, TRADE_OPTION.LIMIT);
setDirection(COMMAND.TRADE, TRADE_TYPE.SELL, currentRate - exchangeGap, lotUnit, currentRate - exchangeGap * 2, TRADE_OPTION.STOP);
}
function reset()
{
setCurrentRate(initialRate);
setLot();
orders = [];
positions = [];
preorders = [];
totalProfits = 0;
unrealizedGains = 0;
martingale = 0;
setDirection(COMMAND.TRADE, TRADE_TYPE.BUY, currentRate, lotUnit, currentRate + exchangeGap, TRADE_OPTION.NO_LIMIT);
setDirection(COMMAND.TRADE, TRADE_TYPE.SELL, currentRate, lotUnit, currentRate - exchangeGap, TRADE_OPTION.NO_LIMIT);
setPreOrder();
refreshStatus();
}
function commit()
{
if (currentRate == initialRate)
{
return;
}
orders = [];
$.each(positions, function (i, o) {
o.fluctuated = currentRate;
});
var contracted = $.grep(positions, function (o) { return o.isContract(); });
positions = $.grep(positions, function (o) { return !o.isContract(); });
$.each(contracted, function (i, o) {
setDirection(COMMAND.CLOSE, o.type, o.price, o.lot, o.takeProfit);
totalProfits += o.gainTP();
});
unrealizedGains = 0;
$.each(positions, function (i, o) { unrealizedGains += o.gain(); });
var traded = $.grep(preorders, function (o) { return o.isTrade(currentRate); });
preorders = $.grep(preorders, function (o) { return !o.isTrade(currentRate); });
$.each(traded, function (i, o) { positions.push(o.toPosition()); });
setPreOrder();
refreshStatus();
}
function init()
{
connectField("#ir", initialRate, function (v) { initialRate = v; reset(); });
connectField("#eg", exchangeGap, function (v) { exchangeGap = v; reset(); });
connectField("#un", unit, function (v) { unit = v; reset(); });
connectField("#lu", lotUnit, function (v) { lotUnit = v; reset(); });
$("#up").click(function () { moveCurrentRate(exchangeGap) });
$("#dn").click(function () { moveCurrentRate(-exchangeGap) });
$("#reset").click(reset);
$("#commit").click(commit);
reset();
}
$(init);