Repository navigation
Expand file tree
/
Copy pathprocessor.php
More file actions
292 lines (209 loc) · 11.5 KB
/
Copy pathprocessor.php
File metadata and controls
292 lines (209 loc) · 11.5 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
require_once('dbConnect.php');
require_once ('OrderBook.php');
require_once ('Order.php');
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
function processTransactions ()
{
// if the process isn't running
if (processorIsRunning()==1)
{
//try to lock it
if (lockProcessor() == 1)
{
$orderBook = new OrderBook();
$orderBook->moveToActive(true, 10);
$orderBook->setActiveIndex(0);
//$orders = $orderBook->getActiveOrdersToMatch();
$unfilledOrders = $orderBook->getActiveOrdersToMatch();
//$unfilledOrders=array();
//for ( $i=0; $i < $orderBook->getActiveIndex(); $i++){
//$unfilledOrders[$i]=$orders[$i];
}
for( $j=$orderBook->getActiveIndex(); $j< count($unfilledOrders); $j++){
$newOrderInProcess=$unfilledOrders[$j];
$tradeprice=0;
$tradeShares=0;
$buyer=-1;
$seller=-1;
$tradeStock= $newOrderInProcess->getStock();
$index=0;
// main algorithm
//get matching list
$matchingList = array();
if($newOrderInProcess->getBS()=="S"){
$tradeprice= $newOrderInProcess->getPrice();
$seller= $newOrderInProcess->getId();
for($m=0;$m<count($unfilledOrders); $m++){
if($unfilledOrders[$m]->getBS()=="B" && $unfilledOrders[$m]->getStock()==$tradeStock &&
$unfilledOrders[$m]->getPrice() >= $tradeprice)
array_push ($matchingList, $m);
}
//$unfilledList= get records from database such that:
//recods.bs="b" & records.stock= $stock & records.price>=$neworder->price;
}else{
$buyer= $newOrderInProcess->getId();
//$list= get records from database such that:
//recods.bs="b" & recods.stock= $stock & records.price<=$neworder->price;
for($m=0;$m<$j; $m++){ ///////
if($unfilledOrders[$m]->getBS()=="S" && $unfilledOrders[$m]->getStock()==$tradeStock &&
$unfilledOrders[$m]->getPrice() <= $tradeprice)
array_push ($matchingList, $m);
}
}
print_r($matchingList);
if(count($matchingList)==0){
/////
// echo "<br/> BREAK ". $j . " *******";
continue;
}
//echo "<br> Unfilled orders before while <br/>";
//forEach($unfilledOrders as $var){
// echo $var->toString()."--".$var->getId();
// echo "<br/>";
//}
//echo "<br/> Cur ORDER before loop--------------------------<br/>";
// echo $newOrderInProcess->toString()."--".$newOrderInProcess->getId();
//echo "<br/>**************<br/>";
$residualOrder = newResidual($newOrderInProcess);
//echo "<br> Unfilled orders before while <br/>";
// forEach($unfilledOrders as $var){
// echo $var->toString()."--".$var->getId();
// echo "<br/>";
while(($match=getmatch($unfilledOrders ,$matchingList))!=-1 && $residualOrder->getShares()>0){
//echo "<br/>**********loop****<br/>";
//echo count($unfilledOrders)."<br/>";
//forEach($unfilledOrders as $var){
// echo $var->toString()."+++++";
// echo "<br/>";
//}
$matchResidual= newResidual($unfilledOrders[$match]);
//echo "Matching ORDER before set bs<br/>";
//echo $unfilledOrders[$match]->toString()."--".$unfilledOrders[$match]->getId();
//echo "<br/>**************<br/>";
$newOrderInProcess->setBS('f');
$unfilledOrders[$match]->setBS('f');
//echo "Matching ORDER<br/>";
//echo $unfilledOrders[$match]->toString()."--".$unfilledOrders[$match]->getId();
//echo "<br/>**************<br/>";
$tradeShares = min($unfilledOrders[$match]->getShares(), $residualOrder->getShares() );
$matchResidual->setShares( $unfilledOrders[$match]->getShares() - $tradeShares);
if($matchResidual->getBS()=="S")
$seller=($unfilledOrders[$match]->getParent()==0 )?$unfilledOrders[$match]->getId():$unfilledOrders[$match]->getParent();
if($matchResidual->getBS()=="B")
$buyer=($unfilledOrders[$match]->getParent()==0 )?$unfilledOrders[$match]->getId():$unfilledOrders[$match]->getParent();
$residualOrder->setShares($residualOrder->getShares() - $tradeShares);
$matchResidual->setShares( $unfilledOrders[$match]->getShares() - $tradeShares);
if($tradeShares!=$unfilledOrders[$match]->getShares()){
//add($matchResidual )to unfilled;
array_push($unfilledOrders, $matchResidual);
// echo "Res Matching ORDER<br/>";
// echo $matchResidual->toString()."--".$matchResidual->getId();
// echo "<br/>**************<br/>";
}
// echo "RESIDUAL ORDER<br/>";
// echo $residualOrder->toString()."--".$residualOrder->getId();
// echo "<br/>**************<br/>";
//echo "ORDER done<br/>";
//echo $newOrderInProcess->toString()."--".$newOrderInProcess->getId();
//echo "<br/>*******END*******<br/>";
//addrecord($matchNumber,$tradeStock, $tradeShares, $tradeprice, $buyer,$seller, current server time);
$query="INSERT INTO trade_book (`timestamp`,`stock`,`buy_ref`, `sell_ref`, `price`, `amount`)
values (NOW(),'$tradeStock','$buyer', '$seller', '$tradeprice', '$tradeShares' );";
//echo "<br/>!!!!".$buyer." ".$seller." ".$tradeprice." ".$tradeShares." ".$tradeStock."!!!<br/>";
$response=mysql_query($query);
if($response){
// echo "went trough";
}else{
//echo "did not go through<br/>";
}
//construct new tradeRecord object + add it to tradebook
}
if ( $residualOrder->getShares()>0){
array_push($unfilledOrders, $residualOrder);
}
//if($newOrderInProcess->getBS()!='f'){
// array_push($unfilledOrders, $residualOrder);
//}
//echo "<br> Unfilled ordera after while <br/>";
//forEach($unfilledOrders as $var){
// echo $var->toString()."--".$var->getId();
//echo "<br/>";
//}
}
$query2="TRUNCATE TABLE order_book_active;";
$response2=mysql_query($query2);
//if($response2){
// echo "Freshness";
//}else{
// echo "Problem<br/>";
//}
foreach ($unfilledOrders as $var){
if($var->getBS()=='f'){
$var->insertArchive();
}
else {
// echo "<br/>**********".$var->toString();
$var->insertActive();
}
}
releaseProcessor();
}
//}
}
function newResidual($newOrderInProcess){ // TEST OK 05:11
$residualOrder= new Order($newOrderInProcess->getFrom(), $newOrderInProcess->getBS(), $newOrderInProcess->getShares(),
$newOrderInProcess->getStock(), $newOrderInProcess->getPrice(), $newOrderInProcess->getTwilio(), $newOrderInProcess->getState());
$residualOrder->setParent($newOrderInProcess->getId());
$residualOrder->setId( $newOrderInProcess->getId()+1);
return $residualOrder;
}
//this function returns 0 if the first argument has a better price (depending on whether
// he is a seller or buyer) than order 2; 1 otherwise
function bestPrice($order1,$order2){
if($order1->getBS() == "B")
return ( ($order1->getPrice() > $order2->getPrice()) ? 0 : 1);
else
return ( ($order1->getPrice() < $order2->getPrice()) ? 0 : 1);
}
// this function gets the matching order with highest priority
function getmatch($unfilledList ,$matchingList){
$match=-1;
$i=0;
for($i=0; $i<count($matchingList); $i++){
//echo "<br/>Time stamp**<br/> ";
//echo "Temp ".$unfilledList[$matchingList[$i]]->getTimestamp()."**Match ".$unfilledList[1]->getTimestamp()."<br/>";
if($unfilledList[$matchingList[$i]]->getBS()!='f')
if($match==-1 || bestPrice($unfilledList[$match],$unfilledList[$matchingList[$i]] ) ||
($unfilledList[$match]->getPrice()==$unfilledList[$matchingList[$i]]->getPrice()
&& ($unfilledList[$match]->getTimestamp() > $unfilledList[$matchingList[$i]]->getTimestamp())))
$match = $matchingList[$i];
// time attribute shouls hold parent time
}
return $match;
}
function lockProcessor()
{
$query = "SELECT GET_LOCK('Singleton',5)";
$result = mysql_query($query);
$check = mysql_fetch_row($result);
return $check[0];
}
function releaseProcessor()
{
$query = "SELECT RELEASE_LOCK('Singleton')";
$result = mysql_query($query);
$check = mysql_fetch_row($result);
return $check[0];
}
function processorIsRunning()
{
$query = "SELECT IS_FREE_LOCK('Singleton')";
$result = mysql_query($query);
$check = mysql_fetch_row($result);
return $check[0];
}
?>