-
Notifications
You must be signed in to change notification settings - Fork 7
/
网格.js
144 lines (142 loc) · 4.38 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
var holdOrders = {
arr:[], // arr`s elements members: price 、amount 、 coverPrice 、time
SumProfit: 0,
search: function(ticker){
var info = null;
for(var i = 0; i < this.arr.length; i++){
if(this.arr[i] === null){
continue;
}
if(this.arr[i].coverPrice <= ticker.Last ){
info = $.Sell(this.arr[i].amount);
if(info){
Log("cover order time:", getTimeByNormal(this.arr[i].time.getTime()),"price:",info.price,"amount:",info.amount );
//this.outArr(i);
this.SumProfit += (info.price - this.arr[i].price)*this.arr[i].amount;
LogProfit( (info.price - this.arr[i].price)*this.arr[i].amount,"为本次盈亏","总盈亏:",this.SumProfit );
this.outArr(i);
this.show(ticker);
count.close++;//ceshi
}
}
}
}, // if enough coverPrice ,do cover op. if overstep HoldOutTime and enough price do cover op
inArr: function(info){
var isFind = false;
var orders = this.createOrder(info);
for(var i = 0; i < this.arr.length; i++){
if(this.arr[i] === null){
this.arr[i] = orders;
isFind = true;
break;
}
}
if(!isFind){
this.arr[this.arr.length] = orders;
}
}, // open and comein arr.
outArr: function(index){
this.arr[index] = null;
}, // cover and out arr.
createOrder: function(info){
var orders = {
price: 0,
amount: 0,
coverPrice: 0,
time: null,
};
orders.price = info.price;
orders.amount = info.amount;
orders.coverPrice = (1 + targetProfit) * info.price;
orders.time = new Date();
return orders;
}, //create object
show: function(ticker){
var msg = "";
var profit = 0;
var sumProfit = 0;
for(var i = 0; i < this.arr.length; i++){
if(this.arr[i] === null){
continue;
}
profit = (ticker.Last - this.arr[i].price) * this.arr[i].amount ;
msg += ("-" + i + ":" + this.arr[i] + "持仓盈亏:" + profit + "\n");
sumProfit += profit;
}
Log("holdOrders:\n",msg,"持仓总浮动盈亏:",sumProfit,"arr:",this.arr );
}
};
var lastPrice = 0;
var initAccount = null;
var lastTime = 0;
var count = {
open: 0,
close: 0
};
//global parameter set on UI : range 、 opAmount 、 HoldOutTime_hours 、 HoldOutTime_minute 、
// HoldOutTime_second 、Interval 、 targetProfit 、waitOutTime(hours)
function getTimeByNormal(time){
var timeByNormal = new Date();
timeByNormal.setTime(time);
var strTime = timeByNormal.toString();
var showTimeArr = strTime.split(" ");
var showTime = showTimeArr[3]+"-"+showTimeArr[1]+"-"+showTimeArr[2]+"-"+showTimeArr[4];
return showTime;
}
//var i = 0; //ceshi
function scan(){
var ticker = exchange.GetTicker();
while(!ticker){
ticker = exchange.GetTicker();
Sleep(Interval);
}
if( (lastPrice - ticker.Last) / lastPrice > range){
var nowAccount = exchange.GetAccount();
while(!nowAccount){
nowAccount = exchange.GetAccount();
}
if(nowAccount.Balance < opAmount){
Log("not enough money! waiting...");
return;
}
var info = $.Buy(opAmount);
if(info){
holdOrders.inArr(info);
lastPrice = ticker.Last;
lastTime = (new Date()).getTime();
count.open++;//ceshi
}
}//下跌,开多仓
if( (new Date()).getTime() - lastTime > waitOutTime * 1000 * 60 * 60 ){//ceshi
lastPrice = ticker.Last;
lastTime = (new Date()).getTime();
}
holdOrders.search(ticker);// 根据市场行情 扫描持仓
}
function main(){
initAccount = exchange.GetAccount();
var nowAccount = initAccount;
var ticker = exchange.GetTicker();
while(!initAccount || !ticker){
initAccount = exchange.GetAccount();
ticker = exchange.GetTicker();
Sleep(Interval);
}
lastPrice = ticker.Last;
lastTime = (new Date()).getTime();
while(true){
scan();
nowAccount = exchange.GetAccount();
ticker = exchange.GetTicker();
LogStatus("初始账户:","钱:",initAccount.Balance,"币:",initAccount.Stocks,"现在账户:","钱:",nowAccount.Balance,"币:",nowAccount.Stocks,"open:",count.open,"close:",count.close ,"\n",holdOrders.arr );
Sleep(Interval);
}
}
//参数:
/*
0.0005
0.1
500
0.001
1
*/