-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtable.ajax.php
247 lines (193 loc) · 8.9 KB
/
table.ajax.php
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
<?php
header('Access-Control-Allow-Methods: GET');
require('depedencies.inc.php');
setlocale(LC_MONETARY, 'fr_FR.UTF-8');
// Query Data
if(isset($_GET['debug']) && $_GET['debug'] > 0) { $debug = (int) $_GET['debug']; }
else $debug = 0;
if(isset($_GET['limit']) && $_GET['limit'] > 0) { $limit = (int) $_GET['limit']; }
else $limit = 2;
if(isset($_GET['status']) && $_GET['status'] != '') { $status = $_GET['status']; }
else $status = 'closed';
// Google Table on Ledger
$Ledger = new Ledger();
/*if($status == 'open')
$Ledger->selectOpen($limit);
else
$Ledger->selectClosed($limit);*/
$Ledger->select($limit);
if($debug)
krumo($Ledger->List);
// First INVERT SORT for Gain / Loss calculation
$Ledger->List = array_reverse($Ledger->List, true);
if($debug) {
echo "SORT";
krumo($Ledger->List);
}
if(isset($Ledger->List) && is_array($Ledger->List) && count($Ledger->List) > 0) {
$i=0; $googleTableCols[$i] = new stdClass();
$googleTableCols[$i]->id = 'id';
$googleTableCols[$i]->label = 'id';
$googleTableCols[$i]->type = 'number';
$i++; $googleTableCols[$i] = new stdClass();
$googleTableCols[$i]->id = 'date';
$googleTableCols[$i]->label = 'Date';
$googleTableCols[$i]->type = 'datetime';
$i++; $googleTableCols[$i] = new stdClass();
$googleTableCols[$i]->id = 'status';
$googleTableCols[$i]->label = 'Status';
$googleTableCols[$i]->type = 'string';
$i++; $googleTableCols[$i] = new stdClass();
$googleTableCols[$i]->id = 'action';
$googleTableCols[$i]->label = 'Action';
$googleTableCols[$i]->type = 'string';
$i++; $googleTableCols[$i] = new stdClass();
$googleTableCols[$i]->id = 'volume';
$googleTableCols[$i]->label = 'Vol';
$googleTableCols[$i]->type = 'number';
$i++; $googleTableCols[$i] = new stdClass();
$googleTableCols[$i]->id = 'price';
$googleTableCols[$i]->label = 'Price';
$googleTableCols[$i]->type = 'number';
$i++; $googleTableCols[$i] = new stdClass();
$googleTableCols[$i]->id = 'total';
$googleTableCols[$i]->label = 'Total';
$googleTableCols[$i]->type = 'number';
$i++; $googleTableCols[$i] = new stdClass();
$googleTableCols[$i]->id = 'gain';
$googleTableCols[$i]->label = 'Gain';
$googleTableCols[$i]->type = 'string';
$i++; $googleTableCols[$i] = new stdClass();
$googleTableCols[$i]->id = 'reference';
$googleTableCols[$i]->label = 'Reference';
$googleTableCols[$i]->type = 'string';
$i++; $googleTableCols[$i] = new stdClass();
$googleTableCols[$i]->id = 'info';
$googleTableCols[$i]->label = 'Info';
$googleTableCols[$i]->type = 'string';
$i=0;
$prev = new stdClass();
$prev->cost = 0;
$prev->fee = 0;
foreach($Ledger->List as $data) {
// ID
$j=0; $googleTableRows[$i]->c[$j] = new stdClass();
$googleTableRows[$i]->c[$j]->v = $data->id;
// Date
$j++; $googleTableRows[$i]->c[$j] = new stdClass();
if($data->status == 'open') {
$googleTableRows[$i]->c[$j]->v = "Date(".date('Y,n,d,H,i,s', strtotime($data->addDate)).")";
$googleTableRows[$i]->c[$j]->f = date('d/m H:i:s', strtotime($data->addDate));
}
else {
$googleTableRows[$i]->c[$j]->v = "Date(".date('Y,n,d,H,i,s', strtotime($data->closeDate)).")";
$googleTableRows[$i]->c[$j]->f = date('d/m H:i:s', strtotime($data->closeDate));
}
// Status
$j++; $googleTableRows[$i]->c[$j] = new stdClass(); $googleTableRows[$i]->c[$j]->v = $data->status;
// Action
$j++; $googleTableRows[$i]->c[$j] = new stdClass(); $googleTableRows[$i]->c[$j]->v = $data->orderAction;
// Prices
if($data->status == 'closed') {
// Volume
$j++; $googleTableRows[$i]->c[$j] = new stdClass(); $googleTableRows[$i]->c[$j]->v = $data->volume_executed;
// Price
$j++; $googleTableRows[$i]->c[$j] = new stdClass();
$googleTableRows[$i]->c[$j]->v = $data->price_executed;
$googleTableRows[$i]->c[$j]->f = money_format('%(#1n', $data->price_executed);
// Total
$j++; $googleTableRows[$i]->c[$j] = new stdClass();
$googleTableRows[$i]->c[$j]->v = $data->cost;
$googleTableRows[$i]->c[$j]->f = money_format('%(#1n', $data->cost);
// Gain if limit / market
if($data->type != 'position') {
if($prev->cost && $data->orderAction == 'sell' && $prev->action == 'buy') {
$j++; $googleTableRows[$i]->c[$j] = new stdClass();
$gain = $data->cost - $prev->cost - $data->fee - $prev->fee;
$googleTableRows[$i]->c[$j]->v = $gain;
$googleTableRows[$i]->c[$j]->f = money_format('%(#1n', $gain);
$prev->cost = 0;
$prev->fee = 0;
}
elseif($data->orderAction == 'sell' && $prev->action == 'sell') {
$previ = $i-1;
$j++; $googleTableRows[$i]->c[$j] = new stdClass();
$gain = $data->cost - $prev->cost - $data->fee - $prev->fee + $googleTableRows[$previ]->c[$j]->v;
$googleTableRows[$i]->c[$j]->v = $gain;
$googleTableRows[$i]->c[$j]->f = money_format('%(#1n', $gain);
$prev->cost = 0;
$prev->fee = 0;
// reset prev gain
$googleTableRows[$previ]->c[$j]->v = '';
$googleTableRows[$previ]->c[$j]->f = '';
}
elseif($data->orderAction == 'buy') {
$j++; $googleTableRows[$i]->c[$j] = new stdClass(); $googleTableRows[$i]->c[$j]->v = '';
$prev->cost += $data->cost;
$prev->fee += $data->fee;
}
else {
$j++; $googleTableRows[$i]->c[$j] = new stdClass(); $googleTableRows[$i]->c[$j]->v = '';
}
}
else {
$j++; $googleTableRows[$i]->c[$j] = new stdClass(); $googleTableRows[$i]->c[$j]->v = '';
}
}
else {
// Volume
$j++; $googleTableRows[$i]->c[$j] = new stdClass(); $googleTableRows[$i]->c[$j]->v = $data->volume;
// Price
$j++; $googleTableRows[$i]->c[$j] = new stdClass();
$googleTableRows[$i]->c[$j]->v = $data->price;
$googleTableRows[$i]->c[$j]->f = money_format('%(#1n', $data->price);
// Total
$j++; $googleTableRows[$i]->c[$j] = new stdClass();
$googleTableRows[$i]->c[$j]->v = $data->total;
$googleTableRows[$i]->c[$j]->f = money_format('%(#1n', $data->total);
// Gain / Cancel
$j++; $googleTableRows[$i]->c[$j] = new stdClass();
if($data->status == 'open') {
if(@$data->reference) {
$googleTableRows[$i]->c[$j]->v = "<a href='index.php?cancel=$data->reference&id=$data->id' class='btn btn-danger btn-xs' role='button'><span class='glyphicon glyphicon-remove'></span> Cancel</a>";
}
else {
$googleTableRows[$i]->c[$j]->v = "<a href='index.php?cancel=new&id=$data->id' class='btn btn-danger btn-xs' role='button'><span class='glyphicon glyphicon-remove'></span> Cancel</a>";
}
}
else {
$googleTableRows[$i]->c[$j]->v = '';
}
}
// Reference
$j++; $googleTableRows[$i]->c[$j] = new stdClass(); $googleTableRows[$i]->c[$j]->v = $data->reference;
// Info
if($data->status != 'canceled') {
$j++; $googleTableRows[$i]->c[$j] = new stdClass();
if($data->scalp == 'pending') {
$googleTableRows[$i]->c[$j]->v = "<a href='index.php?cancelScalp=$data->reference&id=$data->id'>";
if($data->stopLoss)
$googleTableRows[$i]->c[$j]->v .= "stop-loss:".money_format('%(#1n', $data->stopLoss);
if($data->takeProfit)
$googleTableRows[$i]->c[$j]->v .= " take-profit:".money_format('%(#1n', $data->takeProfit);
$googleTableRows[$i]->c[$j]->v .= "</a>";
}
}
// Gain calculation
if($data->type != 'position') {
$prev->status = $data->status;
$prev->action = $data->orderAction;
$prev->id = $data->id;
}
$i++;
}
if($debug)
krumo($googleTableRows);
// Second INVERT SORT for Gain / Loss calculation
$googleTableRows = array_reverse($googleTableRows);
$googleTable = array('cols' => $googleTableCols, 'rows' => $googleTableRows);
if($debug)
krumo($googleTable);
echo json_encode($googleTable, JSON_UNESCAPED_SLASHES); //JSON_PRETTY_PRINT JSON_UNESCAPED_SLASHES
}
?>