· 6 years ago · Jul 17, 2019, 05:24 PM
1<?php
2
3namespace App\Http\Controllers;
4
5use App\BuyMatchedOrder;
6use App\BuyOrdersRankByPriceNb;
7use App\FinalBuyMatchedOrder;
8use App\FinalSellMatchedOrder;
9use App\LastMatchedPriceQuantity;
10use App\ListAllOrders;
11use App\ListNonActiveOrders;
12use App\NewOrderEnters;
13use App\OrderBookBuy;
14use App\OrderBookSell;
15use App\Orders;
16use App\OrginalListOrders;
17use App\SellMatchedOrder;
18use App\SellOrdersRankByPriceNb;
19use App\StopOrderList;
20use App\TempListOrders;
21use App\TemporaryOrderBookBuy;
22use App\TemporaryOrderBookSell;
23use App\Variables;
24use Illuminate\Http\Request;
25use Illuminate\Support\Facades\Auth;
26use Illuminate\Support\Facades\DB;
27use League\Flysystem\Exception;
28
29class OrderController extends Controller {
30
31 // Private Form field variables
32 private $currency;
33 private $buy_sell;
34 private $desired_sil;
35 private $price;
36 private $order_type;
37 private $trigger_price_activating_stop_order;
38 private $duration;
39 private $status;
40 private $split_id;
41 private $levelling_ratio;
42 private $LMM_percentage;
43 private $entry_date;
44 private $entry_time;
45
46 private $extreme_price_cancellation;
47 private $price_mark_down;
48 private $price_mark_up;
49
50 public function trading() {
51 return view( 'items/trading' );
52 }
53
54 private function createSellOrderBook() {
55 $ask_prize = [ 0, 0, 0 ];
56 $ask_size = [ 0, 0, 0 ];
57 $ask_count = [ 0, 0, 0 ];
58
59 print( '<br> Calling createSellOrderBook <br>' );
60
61 try {
62
63 // Select count(desired_sil),price from sell_orders_rank_by_price_nb group by price order by price
64 $sell_orders_rank_by_price = SellOrdersRankByPriceNb::orderBy( 'price', 'asc' )->groupBy( 'price' )
65 ->select( 'price as ask_price',
66 DB::raw( 'count(desired_sil) as num_ask_order' ),
67 DB::raw( 'sum(desired_sil) as ask_size' )
68 )->get();
69 if ( count( $sell_orders_rank_by_price ) > 0 ) {
70 // Insert ranked and aggregated records in Temp OrderBook Sell
71 foreach ( $sell_orders_rank_by_price as $sell_order_rank_by_price ) {
72 OrderBookSell::create(
73 [
74 'ask_size' => $sell_order_rank_by_price->ask_size,
75 'ask_price' => $sell_order_rank_by_price->ask_price,
76 'num_ask_order' => $sell_order_rank_by_price->num_ask_order,
77 ]
78 );
79
80 }
81 }
82
83 } catch ( Exception $e ) {
84 print( '<br>Exception in createSellOrderBook <br>' );
85 print( '<br>Line # ' . $e->getLine() . "<br>" );
86 print( '<br>Message # ' . $e->getLine() . "<br>" );
87 }
88 }
89
90 private function createBuyOrderBook() {
91 $bid_price = [ 0, 0, 0 ];
92 $bid_size = [ 0, 0, 0 ];
93 $bid_count = [ 0, 0, 0 ];
94
95 print( 'Calling createBuyOrderBook' );
96 print_r( $bid_price );
97 print " <br>";
98 print_r( $bid_size );
99 print " <br>";
100 print_r( $bid_count );
101 print " <br>";
102
103 // Select count(desired_sil),price from sell_orders_rank_by_price_nb group by price order by price
104 try {
105
106 $buy_orders_rank_by_price = BuyOrdersRankByPriceNb::orderBy( 'price', 'DESC' )->groupBy( 'price' )
107 ->select( 'price as bid_price',
108 DB::raw( 'count(desired_sil) as num_bid_order' ),
109 DB::raw( 'sum(desired_sil) as bid_size' )
110 )->get();
111 if ( count( $buy_orders_rank_by_price ) > 0 ) {
112 // Insert ranked and aggregated records in Temp OrderBook Sell
113 foreach ( $buy_orders_rank_by_price as $buy_order_rank_by_price ) {
114 OrderBookBuy::create(
115 [
116 'bid_size' => $buy_order_rank_by_price->bid_size,
117 'bid_price' => $buy_order_rank_by_price->bid_price,
118 'num_bid_order' => $buy_order_rank_by_price->num_bid_order,
119 ]
120 );
121
122 }
123 }
124
125 } catch ( Exception $e ) {
126 print( '<br>Exception in createBuyOrderBook <br>' );
127 print( '<br>Line # ' . $e->getLine() . "<br>" );
128 print( '<br>Message # ' . $e->getLine() . "<br>" );
129 }
130 }
131
132 private function Macro10() {
133 print( '<br> <strong> Calling Macro10 </strong> <br>' );
134
135 //initialisation of variables
136 $temporary_Buy_orders = 0;
137 $temporary_Sell_orders = 0;
138 $increment = 0;
139 $user_id = 0;
140
141 try {
142 //Emptyng Rank BY Price Tables first
143 BuyOrdersRankByPriceNb::truncate();
144 SellOrdersRankByPriceNb::truncate();
145 NewOrderEnters::truncate();
146
147 //Select all orders having status is OPEN
148 $temp_orders = ListAllOrders::where( [ 'status' => 'open' ] )->get();
149
150 foreach ( $temp_orders as $temp_order ) {
151 $order_number = $temp_order->order_number;
152 $currency = $temp_order->currency;
153 $buy_sell = $temp_order->buy_sell;
154 $desired_sil = $temp_order->desired_sil;
155 $price = $temp_order->price;
156 $order_type = $temp_order->order_type;
157 $trigger_price_activating_stop_order = $temp_order->trigger_price_activating_stop_order;
158 $duration = ( $temp_order->duration == null ? '1970-01-01' : $temp_order->duration );
159 $split_id = $temp_order->split_id;
160 $levelling_ratio = $temp_order->levelling_ratio;
161 $LMM_percentage = $temp_order->LMM_percentage;
162 $trigger_price_activating_stop_order = $temp_order->trigger_price_activating_stop_order;
163 $entry_date = $temp_order->entry_date;
164 $entry_time = $temp_order->entry_time;
165 $status = $temp_order->status;
166 $user_id = $temp_order->user_id;
167
168 if ( $buy_sell == 'B' ) {
169 $buy_order_rank_price_nb = BuyOrdersRankByPriceNb::create(
170 [
171 'order_number' => $order_number,
172 'currency' => $currency,
173 'buy_sell' => $buy_sell,
174 'entry_date' => $entry_date,
175 'entry_time' => $entry_time,
176 'desired_sil' => $desired_sil,
177 'price' => $price,
178 'order_type' => $order_type,
179 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
180 'duration' => $duration,
181 'status' => $status,
182 'split_id' => $split_id,
183 'levelling_ratio' => $levelling_ratio,
184 'LMM_percentage' => $LMM_percentage,
185 'user_id' => $user_id,
186 ]
187 );
188
189 } elseif ( $buy_sell == 'S' ) {
190 $buy_order_rank_price_nb = SellOrdersRankByPriceNb::create(
191 [
192 'order_number' => $order_number,
193 'buy_sell' => $buy_sell,
194 'entry_date' => $entry_date,
195 'entry_time' => $entry_time,
196 'desired_sil' => $desired_sil,
197 'price' => $price,
198 'order_type' => $order_type,
199 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
200 'duration' => $duration,
201 'status' => $status,
202 'split_id' => $split_id,
203 'levelling_ratio' => $levelling_ratio,
204 'LMM_percentage' => $LMM_percentage,
205 'user_id' => $user_id,
206 ]
207 );
208 }
209
210 /*
211 * We are ranking Orders both Sell and Buy by Price
212 */
213
214 // Ranking Buy
215 $ranked_buy_orders_by_price = BuyOrdersRankByPriceNb::orderBy( 'price',
216 'desc' )->orderBy( 'order_number',
217 'asc' )->get();
218
219 // Ranking Sell
220 $ranked_sell_orders_by_price = SellOrdersRankByPriceNb::orderBy( 'price',
221 'asc' )->orderBy( 'order_number',
222 'asc' )->get();
223
224
225 }
226
227 // Make sure Temp Order Book Buy and Sell are Empty by Truncating them.
228 OrderBookBuy::truncate();
229 OrderBookSell::truncate();
230
231 $this->createSellOrderBook();
232 $this->createBuyOrderBook();
233
234
235 } catch ( Exception $e ) {
236 print( '<br>Exception in Macro12 <br>' );
237 print( '<br>Line # ' . $e->getLine() . "<br>" );
238 print( '<br>Message # ' . $e->getLine() . "<br>" );
239 }
240 }
241
242 private function Macro4() {
243
244 print( '<br> Callin Macro4 <br>' );
245
246 //copy of orders from “list of all orders” with status different than open into table //list of non active order
247 //then delete orders from “list of all orders” with status different than open or pending
248
249 // Fetch data from list of active order
250
251 try {
252
253 $list_orders_table = ListAllOrders::where( 'order_number', '>', 0 )->where( 'status', '!=', 'open' );
254 $list_orders_obj = null;
255
256 if ( $list_orders_table->exists() ) {
257 $list_orders_obj = $list_orders_table->get();
258
259 foreach ( $list_orders_obj as $list_order_obj ) {
260
261 $order_number = $list_order_obj->order_number;
262 $order_status = $list_order_obj->status;
263 $order_currency = $list_order_obj->currency;
264 $order_type = $list_order_obj->order_type;
265 $order_entry_date = $list_order_obj->entry_date;
266 $order_entry_time = $list_order_obj->entry_time;
267 $order_user_id = $list_order_obj->user_id;
268 $order_buy_sell = $list_order_obj->buy_sell;
269 $order_price = $list_order_obj->price;
270 $order_desired_sil = $list_order_obj->desired_sil;
271 $order_duration = $list_order_obj->duration;
272 $order_split_id = $list_order_obj->split_id;
273 $order_levelling_ratio = $list_order_obj->levelling_ratio;
274 $order_LMM_percentage = $list_order_obj->LMM_percentage;
275 $order_trigger_price = $list_order_obj->trigger_price;
276
277 $list_orders_temp = ListNonActiveOrders::create( [
278 'order_number' => $order_number,
279 'currency' => $order_currency,
280 'buy_sell' => $order_buy_sell,
281 'entry_date' => $order_entry_date,
282 'entry_time' => $order_entry_time,
283 'desired_sil' => $order_desired_sil,
284 'price' => $order_price,
285 'order_type' => $order_type,
286 'trigger_price' => $order_trigger_price,
287 'duration' => $order_duration,
288 'status' => $order_status,
289 'split_id' => $order_split_id,
290 'levelling_ratio' => $order_levelling_ratio,
291 'LMM_percentage' => $order_LMM_percentage,
292 'user_id' => $order_user_id,
293
294
295 ] );
296
297 // and deleted from list of active order
298 ListAllOrders::where( 'order_number', $order_number )->delete();
299 print( '<br> Order# <b>' . $order_number . '</b> is moved in ListNoActive <br>' );
300 } // end for each temp_list_orders_obj
301
302 } // end of if ( $temp_list_orders_table -> exists()
303
304 } catch ( Exception $e ) {
305 print( '<br>Exception in Macro12 <br>' );
306 print( '<br>Line # ' . $e->getLine() . "<br>" );
307 print( '<br>Message # ' . $e->getLine() . "<br>" );
308 }
309
310 }
311
312 private function Macro7() {
313 print( '<br> Calling Macro7 <br>' );
314
315
316 try {
317 //cancellation of orders with extreme price difference in table “list of all orders”
318 // Fetch both last buy and sell matched price
319 $last_matched_order = LastMatchedPriceQuantity::orderBy( 'id',
320 'DESC' )->first();
321 $last_buy_price_matched = floatval( $last_matched_order->buy_price );
322 $last_sell_price_matched = floatval( $last_matched_order->sell_price );
323 $price_difference_automatic_order_cancellation = $this->extreme_price_cancellation;
324
325 print( '<br> $last_buy_price_matched = ' . $last_buy_price_matched . "<br>" );
326 print( '<br> $last_sell_price_matched = ' . $last_sell_price_matched . "<br>" );
327
328 // Fetch data from list of active order
329 $list_orders_table = ListAllOrders::where( 'order_number', '>', 0 );
330 $list_orders_obj = null;
331
332 if ( $list_orders_table->count() ) {
333 $list_orders_obj = $list_orders_table->get();
334
335
336 foreach ( $list_orders_obj as $list_order_obj ) {
337 $order_buy_sell = $list_order_obj->buy_sell;
338 $order_price = $list_order_obj->price;
339 $diff_buy = abs( $order_price - $last_buy_price_matched ) / $last_buy_price_matched;
340 $diff_sell = abs( $order_price - $last_sell_price_matched ) / $last_sell_price_matched;
341
342 print( '<br>$diff_buy = ' . $diff_buy . "<br>" );
343 print( '<br>$diff_sell = ' . $diff_sell . "<br>" );
344
345 print( '<br>$last_buy_price_matched = ' . $last_buy_price_matched . "<br>" );
346 print( '<br>$last_sell_price_matched = ' . $last_sell_price_matched . "<br>" );
347
348 print( '<br>$price_difference_automatic_order_cancellation = ' . $price_difference_automatic_order_cancellation . "<br>" );
349
350 if ( $order_buy_sell == 'B' && $diff_buy > $price_difference_automatic_order_cancellation ) {
351 // buy order with extreme price its status need to be updated into 'cancelled_price_extreme'
352 print( 'Order Buy_SELL is B and status is CANCELLED <br>' );
353
354 $list_order_obj->where( 'order_number', $list_order_obj->order_number )->update( [
355 'status' => 'cancelled_price_extreme',
356 ] );
357
358 } // end of if ( $order_type == 'B' …..)
359
360 if ( $order_buy_sell == 'S' && $diff_sell > $price_difference_automatic_order_cancellation ) {
361 // sell order with extreme price its status need to be updated into 'cancelled_price_extreme'
362 print( 'Order Buy_SELL is S and status is CANCELLED <br>' );
363
364 $list_order_obj->where( 'order_number', $list_order_obj->order_number )->update( [
365 'status' => 'cancelled_price_extreme',
366 ] );
367
368 } // end of if ( $order_type == 'S' …..)
369 } // end for each temp_list_orders_obj
370
371 } // end of if ( $temp_list_orders_table -> exists()
372 } catch ( Exception $e ) {
373 print( '<br>Exception in Macro7 <br>' );
374 print( '<br>Line # ' . $e->getLine() . "<br>" );
375 print( '<br>Message # ' . $e->getLine() . "<br>" );
376 }
377 }
378
379 private function Macro16() {
380 print( '<br> Calling Macro16 <br>' );
381
382 try {
383 //Empty list of all orders
384 ListAllOrders::truncate();
385 // copy all orders from table “temporary list of active order” into “list of active order”
386 $temp_list_orders_table = TempListOrders::where( 'order_number', '>', 0 );
387 $temp_list_orders_obj = null;
388
389 if ( $temp_list_orders_table->exists() ) {
390 $temp_list_orders_obj = $temp_list_orders_table->get();
391
392 foreach ( $temp_list_orders_obj as $temp_list_order_obj ) {
393
394 $order_number = $temp_list_order_obj->order_number;
395 $order_status = $temp_list_order_obj->status;
396 $order_currency = $temp_list_order_obj->currency;
397 $order_type = $temp_list_order_obj->order_type;
398 $order_entry_date = $temp_list_order_obj->entry_date;
399 $order_entry_time = $temp_list_order_obj->entry_time;
400 $order_user_id = $temp_list_order_obj->user_id;
401 $order_buy_sell = $temp_list_order_obj->buy_sell;
402 $order_price = $temp_list_order_obj->price;
403 $order_desired_sil = $temp_list_order_obj->desired_sil;
404 $order_duration = $temp_list_order_obj->duration;
405 $order_split_id = $temp_list_order_obj->split_id;
406 $order_levelling_ratio = $temp_list_order_obj->levelling_ratio;
407 $order_LMM_percentage = $temp_list_order_obj->LMM_percentage;
408 $order_trigger_price = $temp_list_order_obj->trigger_price;
409
410 $list_orders_temp = ListAllOrders::create( [
411 'order_number' => $order_number,
412 'currency' => $order_currency,
413 'buy_sell' => $order_buy_sell,
414 'entry_date' => $order_entry_date,
415 'entry_time' => $order_entry_time,
416 'desired_sil' => $order_desired_sil,
417 'price' => $order_price,
418 'order_type' => $order_type,
419 'trigger_price' => $order_trigger_price,
420 'duration' => $order_duration,
421 'status' => $order_status,
422 'split_id' => $order_split_id,
423 'levelling_ratio' => $order_levelling_ratio,
424 'LMM_percentage' => $order_LMM_percentage,
425 'user_id' => $order_user_id,
426 ] );
427 print( 'Order# ' . $order_number . ' is copied to ListAllOrders <br>' );
428
429 } // end for each temp_list_orders_obj
430 } // end of if ( $temp_list_orders_table -> exists()
431
432 // this is macro2 in VBA code which will be directly included in macro 16 in PHP code
433 /// no need to create a separate macro2
434 // transfer of fully and/or partially matched orders from table “buy_matched_order” to official list of matched orders in table “final_buy_matched_order”
435 $temp_buy_matched_order_table = BuyMatchedOrder::where( 'buy_order_id', '>', 0 );
436 $temp_buy_matched_order_obj = null;
437
438 if ( $temp_buy_matched_order_table->exists() ) {
439 $temp_buy_matched_orders_obj = $temp_buy_matched_order_table->get();
440
441 foreach ( $temp_buy_matched_orders_obj as $temp_buy_matched_order_obj ) {
442 $tmp_sell_order_id = 0;
443 $tmp_sell_split_id = '';
444
445 //Pull the sell order id and split id when it matches the MATCH ID
446 $sell_matched_order_sell_order_split_id_obj = SellMatchedOrder:: where( 'matching_id',
447 $temp_buy_matched_order_obj->matching_id )->first();
448
449 if ( $sell_matched_order_sell_order_split_id_obj != null ) {
450 $tmp_sell_order_id = $sell_matched_order_sell_order_split_id_obj->sell_order_id;
451 $tmp_sell_split_id = $sell_matched_order_sell_order_split_id_obj->sell_split_id;
452
453 print( '<br>$tmp_sell_order_id =' . $tmp_sell_order_id . "<br>" );
454 print( '<br>$tmp_sell_split_id =' . $tmp_sell_split_id . "<br>" );
455 }
456
457 $buy_order_id = $temp_buy_matched_order_obj->buy_order_id;
458 $buy_order_matching_date = $temp_buy_matched_order_obj->matching_date;
459 $buy_order_matching_time = $temp_buy_matched_order_obj->matching_time;
460 $buy_order_matching_id = $temp_buy_matched_order_obj->matching_id;
461 $buy_order_matching_process = $temp_buy_matched_order_obj->matching_process;
462 $buy_order_user_id = $temp_buy_matched_order_obj->buy_user_id;
463 $buy_order_split_id = $temp_buy_matched_order_obj->buy_split_id;
464 $buy_order_price = $temp_buy_matched_order_obj->buy_price;
465 $buy_order_quantity = $temp_buy_matched_order_obj->buy_quantity;
466
467 // we need to add the values to the table FinalBuyMatchedOrder those are additional rows
468 //to be ADDED to the existing ones
469 $list_final_buy_matched_orders_temp = FinalBuyMatchedOrder::create( [
470 'buy_order_id' => $buy_order_id,
471 'matching_date' => $buy_order_matching_date,
472 'matching_time' => $buy_order_matching_time,
473 'matching_id' => $buy_order_matching_id,
474 'matching_process' => $buy_order_matching_process,
475 'buy_user_id' => $buy_order_user_id,
476 'buy_price' => $buy_order_price,
477 'buy_quantity' => $buy_order_quantity,
478 'buy_split_id' => $buy_order_split_id,
479 'sell_order_id' => $tmp_sell_order_id,
480 'sell_split_id' => $tmp_sell_split_id,
481
482 ] );
483
484 } // end for each temp_buy_matched_order_obj
485 } // end of If( $temp_buy_matched_order_table->exists()
486
487 // transfer of fully and/or partially matched orders from table “sell_matched_order” to official list of matched orders in table “final_sell_matched_order”
488 $temp_sell_matched_order_table = SellMatchedOrder::where( 'sell_order_id', '>', 0 );
489 $temp_sell_matched_order_obj = null;
490
491 if ( $temp_sell_matched_order_table->count() > 0 ) {
492 $temp_sell_matched_orders_obj = $temp_sell_matched_order_table->get();
493
494 foreach ( $temp_sell_matched_orders_obj as $temp_sell_matched_order_obj ) {
495 $tmp_buy_order_id = 0;
496 $tmp_buy_split_id = '';
497
498 //Pull the sell order id and split id when it matches the MATCH ID
499 $buy_matched_order_buy_order_split_id_obj = BuyMatchedOrder:: where( 'matching_id',
500 $temp_sell_matched_order_obj->matching_id )->first();
501
502 if ( $buy_matched_order_buy_order_split_id_obj != null ) {
503 $tmp_buy_order_id = $buy_matched_order_buy_order_split_id_obj->buy_order_id;
504 $tmp_buy_split_id = $buy_matched_order_buy_order_split_id_obj->buy_split_id;
505
506 print( '<br>$tmp_buy_order_id =' . $tmp_buy_order_id . "<br>" );
507 print( '<br>$tmp_buy_split_id =' . $tmp_buy_split_id . "<br>" );
508 }
509
510
511 $sell_order_id = $temp_sell_matched_order_obj->sell_order_id;
512 $sell_order_matching_date = $temp_sell_matched_order_obj->matching_date;
513 $sell_order_matching_time = $temp_sell_matched_order_obj->matching_time;
514 $sell_order_matching_id = $temp_sell_matched_order_obj->matching_id;
515 $sell_order_matching_process = $temp_sell_matched_order_obj->matching_process;
516 $sell_order_user_id = $temp_sell_matched_order_obj->sell_user_id;
517 $sell_order_price = $temp_sell_matched_order_obj->sell_price;
518 $sell_order_quantity = $temp_sell_matched_order_obj->sell_quantity;
519 $sell_order_split_id = $temp_sell_matched_order_obj->sell_split_id;
520
521 // we need to add the values to the table FinalSellMatchedOrder those are additional rows
522 //to be ADDED to the existing ones
523 //$list_final_sell_matched_orders_temp = FinalSel
524
525 $list_final_sell_matched_orders_temp = FinalSellMatchedOrder::create( [
526 'sell_order_id' => $sell_order_id,
527 'matching_date' => $sell_order_matching_date,
528 'matching_time' => $sell_order_matching_time,
529 'matching_id' => $sell_order_matching_id,
530 'matching_process' => $sell_order_matching_process,
531 'sell_user_id' => $sell_order_user_id,
532 'sell_price' => $sell_order_price,
533 'sell_quantity' => $sell_order_quantity,
534 'sell_split_id' => $sell_order_split_id,
535 'buy_order_id' => $tmp_buy_order_id,
536 'buy_split_id' => $tmp_buy_split_id,
537 ] );
538 } // end for each temp_sell_matched_order_obj
539
540 } // end of If( $temp_sell_matched_order_table->exists()
541
542 // this is end of macro2 in VBA code which will is directly included in macro 16 in PHP code
543 // update of table “Last_matched_price_and_quantity” with values from last traded order
544
545 // fetch data from table buy_matched_order, we should fetch the last value entered
546 $buy_matched_order_table = BuyMatchedOrder::orderBy( 'id', 'DESC' )->limit( 1 );
547
548 if ( $buy_matched_order_table->exists() ) {
549 $buy_matched_order_obj = $buy_matched_order_table->first();
550 $matched_buy_price = $buy_matched_order_obj->buy_price;
551 $matched_quantity = $buy_matched_order_obj->buy_quantity;
552 $matched_date = $buy_matched_order_obj->matching_date;
553 $matched_time = $buy_matched_order_obj->matching_time;
554
555 print( "<br> matched_buy_price: " . $matched_buy_price . "<br>" );
556 print( "<br> matched_quantity: " . $matched_quantity . "<br>" );
557 }
558
559 // fetch last sell price from table sell_matched_order, we should fetch the last value entered
560 $sell_matched_order_table = SellMatchedOrder::orderBy( 'id', 'DESC' )->limit( 1 );
561
562 if ( $sell_matched_order_table->exists() ) {
563 $sell_matched_order_obj = $sell_matched_order_table->first();
564 $matched_sell_price = $sell_matched_order_obj->sell_price;
565 print( "<br> matched_sell_price: " . $matched_sell_price . "<br>" );
566 }
567
568 // update of table “Last_matched_price_and_quantity” with values from last traded order
569 LastMatchedPriceQuantity::create( [
570 'buy_price' => $matched_buy_price,
571 'sell_price' => $matched_sell_price,
572 'average' => ( $matched_buy_price + $matched_sell_price ) / 2,
573 'buy_quantity' => $matched_quantity,
574 'matching_date' => $matched_date,
575 'matching_time' => $matched_time,
576 ] );
577
578 $this->Macro7();
579 $this->Macro4();
580 $this->Macro6();
581 $this->Macro10();
582 $this->Macro11();
583
584 } catch ( Exception $e ) {
585 print( '<br>Exception in Macro16 <br>' );
586 print( '<br>Line # ' . $e->getLine() . "<br>" );
587 print( '<br>Message # ' . $e->getLine() . "<br>" );
588 }
589
590
591 } //End Macro 16
592
593 private function update_matching_order( $single_buy_order ) {
594 print( 'Calling update_matching_order <br>' );
595 print( '$single_buy_order = ' . $single_buy_order . ' <br>' );
596 $temporary_sell_orders_id = 0;
597 $temporary_sell_split_id = '';
598 $temporary_sell_quantity = 0;
599
600 $temporary_buy_orders_id = 0;
601 $temporary_buy_split_id = '';
602 $temporary_buy_quantity = 0;
603
604
605 try {
606 if ( $single_buy_order == 1 ) { //Begin $single_buy_order == 1
607 $sell_orders_rank_by_price_Temporary_table = SellOrdersRankByPriceNb:: where( 'split_id', ' != ', 1 );
608
609 if ( $sell_orders_rank_by_price_Temporary_table->exists() ) {
610 $sell_orders_rank_by_price_Temporary_obj = $sell_orders_rank_by_price_Temporary_table->get();
611
612
613 foreach ( $sell_orders_rank_by_price_Temporary_obj as $sell_orders_rank_by_price_Temporary_obj_item ) {
614 $temporary_sell_orders_id = $sell_orders_rank_by_price_Temporary_obj_item->order_id;
615 $temporary_sell_split_id = $sell_orders_rank_by_price_Temporary_obj_item->split_id;
616 $temporary_sell_quantity = $sell_orders_rank_by_price_Temporary_obj_item->desired_sil;
617 print( ' <br>$temporary_sell_orders_id = ' . $temporary_sell_orders_id . "<br>" );
618 print( ' <br>$temporary_sell_split_id = ' . $temporary_sell_split_id . "<br>" );
619 print( ' <br>$temporary_sell_quantity = ' . $temporary_sell_quantity . "<br>" );
620
621 TempListOrders::where( 'order_number', $temporary_sell_orders_id )->update(
622 [
623 'split_id' => $temporary_buy_split_id,
624 'desired_sil' => $temporary_buy_quantity
625 ]
626 );
627 print( ' <br> Temp List Order updated In SELL for Order # ' . $temporary_buy_orders_id . "<br>" );
628 }
629 }
630
631
632 } //end $single_buy_order == 1
633 else {
634 $buy_orders_rank_by_price_Temporary_table = BuyOrdersRankByPriceNb:: where( 'split_id', '!=', '1' );
635
636 if ( $buy_orders_rank_by_price_Temporary_table->exists() ) {
637 $buy_orders_rank_by_price_Temporary_obj = $buy_orders_rank_by_price_Temporary_table->get();
638
639 foreach ( $buy_orders_rank_by_price_Temporary_obj as $buy_orders_rank_by_price_Temporary_obj_item ) {
640 $temporary_buy_orders_id = $buy_orders_rank_by_price_Temporary_obj_item->order_number;
641 $temporary_buy_split_id = $buy_orders_rank_by_price_Temporary_obj_item->split_id;
642 $temporary_buy_quantity = $buy_orders_rank_by_price_Temporary_obj_item->desired_sil;
643 print( '<br>$temporary_buy_orders_id = ' . $temporary_buy_orders_id . "<br>" );
644 print( '<br>$temporary_buy_split_id = ' . $temporary_buy_split_id . "<br>" );
645 print( '<br>$temporary_buy_quantity = ' . $temporary_buy_quantity . "<br>" );
646
647 TempListOrders::where( 'order_number', $temporary_buy_orders_id )->update(
648 [
649 'split_id' => $temporary_buy_split_id,
650 'desired_sil' => $temporary_buy_quantity
651 ]
652 );
653 print( '<br> Temp List Order updated for Order # ' . $temporary_buy_orders_id . "<br>" );
654 }
655
656 }
657 }
658
659 } catch ( Exception $e ) {
660 print( '<br>Exception in update_matching_order <br>' );
661 print( '<br>Line # ' . $e->getLine() . "<br>" );
662 print( '<br>Message # ' . $e->getLine() . "<br>" );
663 }
664
665 return 1;
666 }
667
668 private function Macro18() {
669 print "<br> Calling Marco 18 here <br>";
670 // no matching, test to see if new order is "fill or kill" or "immediate or cancel" type
671 // if yes move to non active list and change status
672
673 try {
674 $temp_orders_table = TempListOrders::orderby( 'order_number', 'DESC' );
675 $temp_orders_obj = null;
676
677 if ( $temp_orders_table->exists() ) {
678 $temp_order_obj = $temp_orders_table->limit( 1 )->first();
679
680 if ( $temp_order_obj != null ) {
681 $order_number = $temp_order_obj->order_number;
682 $order_status = $temp_order_obj->status;
683 $order_currency = $temp_order_obj->currency;
684 $order_type = $temp_order_obj->order_type;
685 $order_entry_date = $temp_order_obj->entry_date;
686 $order_entry_time = $temp_order_obj->entry_time;
687 $order_user_id = $temp_order_obj->user_id;
688 $order_buy_sell = $temp_order_obj->buy_sell;
689 $order_price = $temp_order_obj->price;
690 $order_desired_sil = $temp_order_obj->desired_sil;
691 $order_duration = $temp_order_obj->duration;
692 $order_split_id = $temp_order_obj->split_id;
693 $order_levelling_ratio = $temp_order_obj->levelling_ratio;
694 $order_LMM_percentage = $temp_order_obj->LMM_percentage;
695 $order_trigger_price = $temp_order_obj->trigger_price;
696
697
698 if ( $this->order_type == 'fill_or_kill' || $this->order_type == 'immediate_or_cancel' ) {
699 //Change the status accordingly
700 if ( $this->order_type == 'fill_or_kill' ) {
701 $updated_status = 'fill_kill_cancelled_not_fully_matched';
702 } elseif ( $this->order_type == 'immediate_or_cancel' ) {
703 $updated_status = 'unmatched_immediate_cancel_cancel';
704 }
705
706 // update status of unmatched order
707 $temp_orders_table->update( [
708 'status' => $updated_status,
709 ] );
710
711 // move order to list of non active order
712 ListNonActiveOrders::create( [
713 'order_number' => $order_number,
714 'status' => $updated_status,
715 'order_type' => $order_type,
716 'entry_date' => $order_entry_date,
717 'entry_time' => $order_entry_time,
718 'user_id' => $order_user_id,
719 'buy_sell' => $order_buy_sell,
720 'price' => $order_price,
721 'desired_sil' => $order_desired_sil,
722 'duration' => $order_duration,
723 'split_id' => $order_split_id,
724 'levelling_ratio' => $order_levelling_ratio,
725 'LMM_percentage' => $order_LMM_percentage,
726 'trigger_price' => $order_trigger_price,
727
728 ] );
729
730 $this->Macro6();
731 $this->Macro11();
732 } else { // else of end of If ($order_type == 'fill_ot_kill' || $order_type == 'immediate or cancel' )
733 $this->Macro13();
734 $this->Macro6B();
735 $this->Macro11();
736 }
737
738 }
739 }
740
741 } catch ( Exception $e ) {
742 print( '<br>Exception in Macro18 <br>' );
743 print( '<br>Line # ' . $e->getLine() . "<br>" );
744 print( '<br>Message # ' . $e->getLine() . "<br>" );
745 }
746
747 }
748
749 private function macro_FIFO_Sell_order(
750 $allocated_top_order_quantity,
751 $sell_quantity_q0,
752 $matching_date,
753 $matching_time,
754 $matching_process_type
755 ) { //Begin of Function
756
757 try {
758 //$allocated_top_order_quantity = 11; // WARNING.. Temp to remove.
759 $buy_orders_rank_by_price_and_nb_17_table = BuyOrdersRankByPriceNb::orderBy( 'price',
760 'DESC' )->orderBy( 'order_number', 'ASC' )->limit( 1 );
761 $buy_orders_rank_by_price_and_nb_17_obj = $buy_orders_rank_by_price_and_nb_17_table->get();
762
763 if ( count( $buy_orders_rank_by_price_and_nb_17_obj ) > 0 ) { //Begin $buy_orders_rank_by_price_and_nb_17_obj Cond
764 $buy_order_id = $buy_orders_rank_by_price_and_nb_17_obj[0]->order_number;
765 $buy_split_id = $buy_orders_rank_by_price_and_nb_17_obj[0]->split_id;
766 $buy_quantity = $buy_orders_rank_by_price_and_nb_17_obj[0]->desired_sil;
767 $buy_price = $buy_orders_rank_by_price_and_nb_17_obj[0]->price;
768 $buy_user_id = $buy_orders_rank_by_price_and_nb_17_obj[0]->user_id;
769 print( "<br> BUY ORDER ID = " . $buy_order_id . "<br>" );
770 print( "<br> BUY SPLIT ID = " . $buy_split_id . "<br>" );
771 print( "<br> BUY QUANTITY = " . $buy_quantity . "<br>" );
772 print( "<br> BUY PRICE = " . $buy_price . "<br>" );
773 print( "<br> BUY USER_ID = " . $buy_user_id . "<br>" );
774
775 /**
776 * Fetching Sell Order Data
777 */
778
779 // We are doing ASC because we want the first row of both tables.
780 $sell_orders_rank_by_price_and_nb_17_table = SellOrdersRankByPriceNb::orderBy( 'price',
781 'asc' )->orderBy( 'order_number', 'ASC' )->limit( 1 );
782
783 $sell_orders_rank_by_price_and_nb_17_obj = $sell_orders_rank_by_price_and_nb_17_table->get();
784
785
786 if ( count( $sell_orders_rank_by_price_and_nb_17_obj ) > 0 ) { // BEGIN $sell_orders_rank_by_price_and_nb_17_obj
787 $sell_price = $sell_orders_rank_by_price_and_nb_17_obj[0]->price;
788 $curent_sell_quantity = $sell_orders_rank_by_price_and_nb_17_obj[0]->desired_sil;
789 $sell_split_id = $sell_orders_rank_by_price_and_nb_17_obj[0]->split_id;
790 $sell_user_id = $sell_orders_rank_by_price_and_nb_17_obj[0]->user_id;
791 $sell_order_id = $sell_orders_rank_by_price_and_nb_17_obj[0]->order_number;
792
793 print( "<br><br>" );
794 print( "<br> SELL PRICE = " . $sell_price . "<br>" );
795 print( "<br> SELL DESIRED SIL = " . $curent_sell_quantity . "<br>" );
796 print( "<br> SELL ORDER ID = " . $sell_order_id . "<br>" );
797 print( "<br> SELL SPLIT ID = " . $sell_split_id . "<br>" );
798 print( "<br> SELL USER_ID = " . $sell_user_id . "<br>" );
799 } // END $sell_orders_rank_by_price_and_nb_17_obj
800
801 // Check whether Sell Matched Order is Empty
802 $sell_matched_order_obj = SellMatchedOrder::orderBy( 'id', 'DESC' )->limit( 1 )->first();
803 print ( "<br> Sell Matched Order Table Related <br>" );
804
805 //$previous_sell_matching_id = 0; // REMOVE HARD CODED
806
807 // Pascal suggested to disable and have to test
808 if ( $sell_matched_order_obj == null ) { // begin count( $sell_matched_order_obj ) == 0 )
809 $previous_sell_matching_id = 0;
810 print( "WE ARE NOT INSIDE ELSE <br>" );
811 } else {
812 $previous_sell_matching_id = $sell_matched_order_obj->matching_id;
813 print ( 'SELL MATCHING ID = ' . $previous_sell_matching_id . "<br>" );
814 } //end count( $sell_matched_order_obj ) == 0 )
815
816
817 // Check whether Buy Matched Order is Empty
818 $buy_matched_order_obj = BuyMatchedOrder::orderBy( 'id', 'DESC' )->limit( 1 )->first();
819 print ( "<br> Buy Matched Order Table Related <br>" );
820 $previous_buy_matching_id = 0; //REMOVE
821 //test if there is no sell matching order yet
822
823 if ( $buy_matched_order_obj == null ) {
824 $previous_buy_matching_id = 0;
825 } else {
826 $previous_buy_matching_id = $buy_matched_order_obj->matching_id;
827 print ( '<br> BUY MATCHING ID = ' . $previous_buy_matching_id . "<br>" );
828 }
829
830
831 // Initialize variables with data coming from table Temporary_list_of_all_orders
832 $temp_list_orders_obj = TempListOrders::where( [ 'order_number' => $buy_order_id ] )->first();
833 $current_split_id = '';
834 $current_quantity = 0;
835 if ( $temp_list_orders_obj ) {
836 $current_split_id = $temp_list_orders_obj->split_id;
837 $current_quantity = $temp_list_orders_obj->desired_sil;
838 }
839 /**
840 * test to see if Sell_price > Buy_price if yes by convention FIFO_Sell_order = -1 and
841 * matching stops.
842 * test to see if buy_price is empty it means that there is no more sell order available
843 */
844 if ( $sell_price > $buy_price || $buy_price == 0 ) { //Begin if ( $sell_price > $buy_price || $buy_price == 0
845 $m_FIFO_Sell_order = - 1;
846 //Select Records from Temp List Of Orders and update Desired SIL and SPLIT
847 $temp_list_orders_buy_obj = TempListOrders::where( [ 'order_number' => $sell_order_id ] );
848 if ( $temp_list_orders_buy_obj->exists() ) { //Begin $temp_list_orders_buy_obj->exists() )
849
850 $temp_list_orders_buy_obj->update( [
851 'desired_sil' => $curent_sell_quantity,
852 'split_id' => $sell_split_id,
853 ] );
854
855 return $m_FIFO_Sell_order;
856
857 } // END $temp_list_orders_buy_obj->exists() )
858
859 } // END if ( $sell_price > $buy_price || $buy_price == 0
860
861 print( 'ALLOCATED QUANITYT =' . $allocated_top_order_quantity . "<br>" );
862 print( 'CURRENT QUANITYT =' . $curent_sell_quantity . "<br>" );
863 print( 'MATCHING PROCESS =' . $matching_process_type . "<br>" );
864
865 // test to see if top order large enough to absorb Allocated_top_order_quantity
866 if ( $allocated_top_order_quantity > $buy_quantity ) { //Begin if ( $allocated_top_order_quantity > $buy_quantity )
867
868 /**
869 * top buyl will be fully matched and closed (sell order allocated >= buy order)
870 * copy of fully matched buy order to table "Buy_matched_orders"
871 */
872 print( '<br> BUY MATCHED ORDER CREATED <br>' );
873 BuyMatchedOrder::create( [
874 'buy_order_id' => $buy_order_id,
875 'buy_split_id' => $buy_split_id,
876 'buy_price' => $buy_price,
877 'buy_user_id' => $buy_user_id,
878 'buy_quantity' => $buy_quantity,
879 'matching_date' => $matching_date,
880 'matching_time' => $matching_time,
881 'matching_id' => $previous_buy_matching_id + 1,
882 'matching_process' => $matching_process_type
883
884 ] );
885 /**
886 * update of status and split_id in the table "Temporary list of all orders"
887 */
888 print( '<br> Update Temp List orders for Order # ' . $buy_order_id . "<br><br>" );
889 $temp_list_orders_buy_obj = TempListOrders::where( [ 'order_number' => $buy_order_id ] );
890
891 if ( $temp_list_orders_buy_obj->exists() ) { //Begin if ( $temp_list_orders_buy_obj->exists() )
892 $temp_list_orders_buy_obj->update( [
893 'status' => 'fully_matched',
894 'split_id' => $buy_split_id,
895 ] );
896 } //End if ( $temp_list_orders_buy_obj->exists() )
897
898 //Delete from BuyOrderRankbyNbPrice for the Order# we found above
899 BuyOrdersRankByPriceNb::where( 'order_number', $buy_order_id )->delete();
900 print( "<br> Order # " . $buy_order_id . " removed from BuyOrdersRankByPriceNb <br>" );
901
902 /**
903 * adjustemnt of sell order in Table "Sell orders rank by price & nb"
904 * remaining unmatched buy quantities
905 */
906
907 print( 'sell_quantity_q0 =' . $sell_quantity_q0 );
908 $sell_quantity_q1 = intval( $sell_quantity_q0 - $buy_quantity );
909 print( '<br> Remaining Quantity =' . $sell_quantity_q1 );
910
911 //$sell_quantity_q1 = 0; // WARNING: THIS NEEDS TO BE REMOVED.
912 // COND1
913 if ( $sell_quantity_q1 == 0 ) { //Begin if ( $sell_quantity_q1 == 0
914 // if remaining quantity=0 sell order fully matched stop matching
915 // update of quantity in Table "Sell orders rank by price & nb"
916 $sell_orders_rank_by_price_and_nb_17_table->update( [ 'desired_sil' => 0 ] );
917 print( '<br>Updated SellOrderRankByPrice for Order #' . $sell_order_id . "<br>" );
918
919 //update status of sell order in Table "Temporary_list_of_all_order"
920 $temp_list_all_orders = TempListOrders::where( [ 'order_number' => $sell_order_id ] );
921
922 if ( $temp_list_all_orders->exists() ) { //Begin if ( $temp_list_all_orders->exists() )
923 $temp_list_all_orders->update( [
924 'status' => 'fully_matched',
925 'split_id' => $sell_split_id,
926 ] );
927 print( '<br>Updated TempListOrders for Order #' . $sell_order_id . "<br>" );
928
929 } //End if ( $temp_list_all_orders->exists() )
930
931 } //End if ( $sell_quantity_q1 == 0 )
932 else { //Else Cond1
933 print( '<br> IN ELSE CONDITION 1 <br>' );
934 /**
935 * if remaining quantity <> 0 sell order NOT fully matched matching should continue
936 * update quantity in Table "Sell orders rank by price & nb"
937 */
938 $sell_orders_rank_by_price_sell_order_table = SellOrdersRankByPriceNb::where( 'order_number',
939 $sell_order_id );
940
941 if ( $sell_orders_rank_by_price_sell_order_table->exists() ) { //Begin if ( $sell_orders_rank_by_price_sell_order_table->exists() )
942 $sell_orders_rank_by_price_sell_order_table->update( [
943 'desired_sil' => $sell_quantity_q1,
944 'split_id' => $sell_split_id . '_1'
945 ] );
946 print( 'SellOrderRankByPrice updated for remaining desired SIL <br>' );
947
948 } //End if ( $sell_orders_rank_by_price_sell_order_table->exists() )
949 }
950
951 SellMatchedOrder::create( [
952 'sell_order_id' => $sell_order_id,
953 'sell_split_id' => $sell_split_id,
954 'sell_price' => $sell_price,
955 'sell_user_id' => $sell_user_id,
956 'sell_quantity' => $buy_quantity, // Pascal, is this correct or current_buy_quantity?
957 'matching_date' => $matching_date,
958 'matching_time' => $matching_time,
959 'matching_id' => $previous_sell_matching_id + 1,
960 'matching_process' => $matching_process_type
961 ] );
962 print( '<br>Inserted Record in SellMatchedOrder for Order #' . $sell_order_id . "<br>" );
963
964 // END OF ELSE COND1
965
966 } //End if ( $allocated_top_order_quantity > $buy_quantity )
967 else {
968
969 /**
970 * \\ top buy will not be fully matched but sell order could be fully matched if
971 * alloc =100% (sell order allocated < buyl order
972 */
973 print( '<br> SELL QUANTITY < BUY ORDER <br>' );
974 /**
975 * copy of partially matched buy order to table "matched orders"
976 */
977
978 $allocated_buy_quantity = $allocated_top_order_quantity;
979 //update of Sell order quantity and split id
980 $updated_quantity = $buy_quantity - $allocated_buy_quantity;
981 print( '<br> UPDATED QUANTITY = ' . $updated_quantity . "<br>" );
982 $buy_order_rankby_price_table = BuyOrdersRankByPriceNb::where( 'order_number', $buy_order_id );
983
984 if ( $buy_order_rankby_price_table->exists() ) { //Begin if ( $buy_order_rankby_price_table->exists() )
985 $buy_order_rankby_price_table->update( [
986 'desired_sil' => $updated_quantity,
987 'split_id' => $buy_split_id . '_1'
988 ] );
989 print( 'BuyOrdersRankByPriceNb updated with updated split id' );
990 BuyMatchedOrder::create( [
991 'buy_order_id' => $buy_order_id,
992 'buy_split_id' => $buy_split_id,
993 'buy_price' => $buy_price,
994 'buy_user_id' => $buy_user_id,
995 'buy_quantity' => $allocated_buy_quantity,
996 'matching_date' => $matching_date,
997 'matching_time' => $matching_time,
998 'matching_id' => $previous_buy_matching_id + 1,
999 'matching_process' => $matching_process_type
1000
1001 ] );
1002
1003 print( 'Inserted new record in BuyMatchedOrder or Buy Order Id #' . $buy_order_id . "<br>" );
1004 // remaining unmatched buy quantities
1005 $sell_quantity_q1 = $sell_quantity_q0 - $allocated_top_order_quantity;
1006 print( '<br><br>$buy_quantity_q1 = ' . $sell_quantity_q1 . "<br>" );
1007 //$sell_quantity_q1 = 0; //WARNING TEMP
1008 //START OF COND2
1009 if ( $sell_quantity_q1 == 0 ) { //Begin if ( $sell_quantity_q1 == 0 )
1010 $sell_orders_rank_by_price_and_nb_17_table->update( [ 'desired_sil' => 0 ] );
1011 print( '<br>Updated SellOrderRankByPrice for Order #' . $sell_order_id . "<br>" );
1012 //update status of sell order in Table "Temporary_list_of_all_order"
1013 $temp_list_all_orders = TempListOrders::where( [ 'order_number' => $sell_order_id ] );
1014
1015 if ( $temp_list_all_orders->exists() ) { // Begin if ( $temp_list_all_orders->exists() )
1016
1017 $temp_list_all_orders->update( [
1018 'status' => 'fully_matched',
1019 'split_id' => $sell_split_id,
1020 ] );
1021 print( '<br>Updated TempListOrders for Order #' . $sell_order_id . "<br>" );
1022 } //End if ( $temp_list_all_orders->exists() )
1023
1024 //update split_id and quantity of buy order in Table "Temporary_list_of_all_order"
1025 $temp_list_all_orders = TempListOrders::where( [ 'order_number' => $buy_order_id ] );
1026
1027 if ( $temp_list_all_orders->exists() ) { //Begin if ( $temp_list_all_orders->exists() )
1028
1029 $temp_list_all_orders->update( [
1030 'desired_sil' => intval( $current_quantity - $allocated_top_order_quantity ),
1031 'split_id' => $current_split_id . '_1',
1032 ] );
1033 print( '<br>Updated TempListOrders for Order #' . $buy_order_id . "<br>" );
1034
1035
1036 } //End if ( $temp_list_all_orders->exists() )
1037
1038
1039 }//End if ( $sell_quantity_q1 == 0 )
1040
1041 else {
1042
1043 //ELSE COND2
1044 print( '<br> IN ELSE CONDITION # 2 <br>' );
1045 /**
1046 * if remaining quantity <> 0 sell order NOT fully matched matching should continue
1047 * update quantity in Table "Sell orders rank by price & nb"
1048 */
1049 $sell_orders_rank_by_price_buy_order_table = SellOrdersRankByPriceNb::where( 'order_number',
1050 $sell_order_id );
1051 if ( $sell_orders_rank_by_price_buy_order_table->exists() ) { // Begin if ( $sell_orders_rank_by_price_buy_order_table->exists() )
1052 $sell_orders_rank_by_price_buy_order_table->update( [
1053 'desired_sil' => $sell_quantity_q1,
1054 'split_id' => $sell_split_id . '_1'
1055 ] );
1056 print( 'SellOrderRankByPrice updated for remaining desired SIL <br>' );
1057
1058 } // End if ( $sell_orders_rank_by_price_buy_order_table->exists() )
1059 } //Else conditions 2 ends here
1060
1061 SellMatchedOrder::create( [
1062 'sell_order_id' => $sell_order_id,
1063 'sell_split_id' => $sell_split_id,
1064 'sell_price' => $sell_price,
1065 'sell_user_id' => $sell_user_id,
1066 'sell_quantity' => $allocated_buy_quantity,
1067 'matching_date' => $matching_date,
1068 'matching_time' => $matching_time,
1069 'matching_id' => $previous_sell_matching_id + 1,
1070 'matching_process' => $matching_process_type
1071
1072 ] );
1073 print( '<br>2nd time...Inserted Record in BuyMatchedOrder for Order #' . $buy_order_id . "<br>" );
1074
1075 } //End if ( $buy_order_rankby_price_table->exists() )
1076 } //Else Condition 2 ends here
1077
1078 } //Begin $buy_orders_rank_by_price_and_nb_17_obj Cond
1079
1080 $m_FIFO_Sell_order = $sell_quantity_q1;
1081
1082 } catch ( Exception $e ) {
1083 print( '<br>Exception in macro_FIFO_Sell_order <br>' );
1084 print( '<br>Line # ' . $e->getLine() . "<br>" );
1085 print( '<br>Message # ' . $e->getLine() . "<br>" );
1086 }
1087
1088
1089 return $m_FIFO_Sell_order;
1090
1091 } //End of function
1092
1093 /**
1094 * @param $allocated_top_order_quantity
1095 * @param $buy_quantity_q0
1096 * @param $matching_date
1097 * @param $matching_time
1098 * @param $matching_process_type
1099 */
1100 private function macro_FIFO_Buy_order(
1101 $allocated_top_order_quantity,
1102 $buy_quantity_q0,
1103 $matching_date,
1104 $matching_time,
1105 $matching_process_type
1106 ) {
1107
1108 try {
1109 /**
1110 * Fetching Buy Order Data
1111 */
1112 $buy_orders_rank_by_price_and_nb_17_table = BuyOrdersRankByPriceNb::orderBy( 'price',
1113 'DESC' )->orderBy( 'order_number', 'ASC' )->limit( 1 );
1114
1115 $buy_orders_rank_by_price_and_nb_17_obj = $buy_orders_rank_by_price_and_nb_17_table->get();
1116
1117 if ( count( $buy_orders_rank_by_price_and_nb_17_obj ) > 0 ) {
1118 $buy_order_id = $buy_orders_rank_by_price_and_nb_17_obj[0]->order_number;
1119
1120
1121 $buy_split_id = $buy_orders_rank_by_price_and_nb_17_obj[0]->split_id;
1122 $buy_quantity = $buy_orders_rank_by_price_and_nb_17_obj[0]->desired_sil;
1123 $buy_price = $buy_orders_rank_by_price_and_nb_17_obj[0]->price;
1124 $buy_user_id = $buy_orders_rank_by_price_and_nb_17_obj[0]->user_id;
1125
1126 print( "<br> BUY ORDER ID = " . $buy_order_id . "<br>" );
1127 print( "<br> BUY SPLIT ID = " . $buy_split_id . "<br>" );
1128 print( "<br> BUY QUANTITY = " . $buy_quantity . "<br>" );
1129 print( "<br> BUY PRICE = " . $buy_price . "<br>" );
1130 print( "<br> BUY USER_ID = " . $buy_user_id . "<br>" );
1131 }
1132
1133 /**
1134 * Fetching Sell Order Data
1135 */
1136
1137 // We are doing ASC because we want the first row of both tables.
1138 $sell_orders_rank_by_price_and_nb_17_obj = SellOrdersRankByPriceNb::orderBy( 'price',
1139 'asc' )->orderBy( 'order_number', 'ASC' )->limit( 1 )->get();
1140
1141 if ( count( $sell_orders_rank_by_price_and_nb_17_obj ) > 0 ) {
1142
1143 $sell_price = $sell_orders_rank_by_price_and_nb_17_obj[0]->price;
1144 $curent_sell_quantity = $sell_orders_rank_by_price_and_nb_17_obj[0]->desired_sil;
1145
1146 $sell_split_id = $sell_orders_rank_by_price_and_nb_17_obj[0]->split_id;
1147 $sell_user_id = $sell_orders_rank_by_price_and_nb_17_obj[0]->user_id;
1148
1149 $sell_order_id = $sell_orders_rank_by_price_and_nb_17_obj[0]->order_number;
1150
1151 print( "<br><br>" );
1152 print( "<br> SELL PRICE = " . $sell_price . "<br>" );
1153 print( "<br> SELL DESIRED SIL = " . $curent_sell_quantity . "<br>" );
1154 print( "<br> SELL ORDER ID = " . $sell_order_id . "<br>" );
1155 print( "<br> SELL SPLIT ID = " . $sell_split_id . "<br>" );
1156 print( "<br> SELL USER_ID = " . $sell_user_id . "<br>" );
1157 }
1158
1159 // Check whether Sell Matched Order is Empty
1160 $sell_matched_order_obj = SellMatchedOrder::orderBy( 'id', 'DESC' )->limit( 1 )->first();
1161
1162 //test if there is no sell matching order yet
1163 if ( $sell_matched_order_obj == null ) {
1164 $previous_sell_matching_id = 0;
1165
1166 } else {
1167 $previous_sell_matching_id = $sell_matched_order_obj->matching_id;
1168 print ( 'MATCHING ID = ' . $previous_sell_matching_id . "<br>" );
1169
1170 }
1171 // Check whether Buy Matched Order is Empty
1172 $buy_matched_order_obj = BuyMatchedOrder::orderBy( 'id', 'DESC' )->limit( 1 )->first();
1173 print ( "<br> Buy Matched Order Table Related <br>" );
1174
1175 //test if there is no sell matching order yet
1176 if ( $buy_matched_order_obj == null ) {
1177 $previous_buy_matching_id = 0;
1178 } else {
1179 $previous_buy_matching_id = $buy_matched_order_obj->matching_id;
1180 print ( '<br> BUY MATCHING ID = ' . $previous_buy_matching_id . "<br>" );
1181 }
1182
1183 // Initialize variables with data coming from table Temporary_list_of_all_orders
1184 $temp_list_orders_obj = TempListOrders::where( [ 'order_number' => $sell_order_id ] )->first();
1185
1186 $current_split_id = '';
1187 $current_quantity = 0;
1188
1189 if ( $temp_list_orders_obj ) {
1190 $current_split_id = $temp_list_orders_obj->split_id;
1191 $current_quantity = $temp_list_orders_obj->desired_sil;
1192 }
1193
1194 /**
1195 * test to see if Sell_price > Buy_price if yes by convention FIFO_Buy_order = -1 and matching stops.
1196 * test to see if Sell_price is empty it means that there is no more sell order available
1197 */
1198
1199 if ( $sell_price > $buy_price || $sell_price == 0 ) {
1200 $m_FIFO_Buy_order = - 1;
1201
1202 /**
1203 * Select Records from Temp List Of Orders and update Desired SIL and SPLIT ID
1204 */
1205
1206 $temp_list_orders_buy_obj = TempListOrders::where( [ 'order_number' => $buy_order_id ] );
1207
1208 if ( $temp_list_orders_buy_obj->exists() ) {
1209 print( 'IT DOES EXIST' . "<br>" );
1210 $temp_list_orders_buy_obj->update( [
1211 'desired_sil' => $buy_quantity,
1212 'split_id' => $buy_split_id,
1213 ] );
1214
1215 return $m_FIFO_Buy_order;
1216 }
1217 }
1218
1219 print( 'ALLOCATED QUANITYT =' . $allocated_top_order_quantity . "<br>" );
1220 print( 'CURRENT QUANITYT =' . $curent_sell_quantity . "<br>" );
1221 print( 'MATCHING PROCESS =' . $matching_process_type . "<br>" );
1222
1223 // test to see if top order large enough to absorb Allocated_top_order_quantity
1224 if ( $allocated_top_order_quantity > $curent_sell_quantity ) {
1225
1226 /**
1227 * top sell will be fully matched and closed (buy order allocated >= sell order)
1228 * copy of fully matched sell order to table "Sell_matched_orders"
1229 */
1230
1231 print( '<br> SELL MATCHED ORDER CREATED <br>' );
1232
1233 SellMatchedOrder::create(
1234 [
1235 'sell_order_id' => $sell_order_id,
1236 'sell_split_id' => $sell_split_id,
1237 'sell_price' => $sell_price,
1238 'sell_user_id' => $sell_user_id,
1239 'sell_quantity' => $curent_sell_quantity,
1240 'matching_date' => $matching_date,
1241 'matching_time' => $matching_time,
1242 'matching_id' => $previous_sell_matching_id + 1,
1243 'matching_process' => $matching_process_type
1244 ]
1245 );
1246
1247 /**
1248 * update of status and split_id in the table "Temporary list of all orders"
1249 */
1250 print( '<br> Update Temp List orders for Order # ' . $sell_order_id . "<br><br>" );
1251
1252 $temp_list_orders_sell_obj = TempListOrders::where( [ 'order_number' => $sell_order_id ] );
1253
1254 if ( $temp_list_orders_sell_obj->exists() ) {
1255
1256
1257 $temp_list_orders_sell_obj->update( [
1258 'status' => 'fully_matched',
1259 'split_id' => $sell_split_id,
1260 ] );
1261 }
1262
1263 //Delete from SellOrderRankbyNbPrice for the Order# we found above
1264 SellOrdersRankByPriceNb::where( 'order_number', $sell_order_id )->delete();
1265 print( "<br> Order # " . $sell_order_id . " removed from SellOrdersRankByPriceNb <br>" );
1266
1267 /**
1268 * adjustemnt of buy order in Table "Buy orders rank by price & nb"
1269 * remaining unmatched buy quantities
1270 */
1271
1272 print( 'Buy_quantity_q0 =' . $buy_quantity_q0 );
1273 $buy_quantity_q1 = intval( $buy_quantity_q0 - $curent_sell_quantity );
1274 print( '<br> Remaining Quantity =' . $buy_quantity_q1 );
1275
1276 //$buy_quantity_q1 = 0; // WARNING: THIS NEEDS TO BE REMOVED.
1277
1278 //COND
1279 if ( $buy_quantity_q1 == 0 ) {
1280
1281 // if remaining quantity=0 buy order fully matched stop matching
1282 // update of quantity in Table "Buy orders rank by price & nb"
1283 $buy_orders_rank_by_price_and_nb_17_table->update( [ 'desired_sil' => 0 ] );
1284
1285
1286 print( '<br>Updated BuyOrderRankByPrice for Order #' . $buy_order_id . "<br>" );
1287
1288 //update status of buy order in Table "Temporary_list_of_all_order"
1289
1290 $temp_list_all_orders = TempListOrders::where( [ 'order_number' => $buy_order_id ] )->toSql();
1291
1292 if ( $temp_list_all_orders->exists() ) {
1293
1294 $temp_list_all_orders->update( [
1295 'status' => 'fully_matched',
1296 'split_id' => $buy_split_id,
1297 ] );
1298 print( '<br>Updated TempListOrders for Order #' . $buy_order_id . "<br>" );
1299
1300 }
1301
1302 } else {
1303 //ELSE COND
1304 print( '<br> IN ELSE CONDITION <br>' );
1305 /**
1306 * if remaining quantity <> 0 buy order NOT fully matched matching should continue
1307 * update quantity in Table "Buy orders rank by price & nb"
1308 */
1309
1310 $buy_orders_rank_by_price_buy_order_table = BuyOrdersRankByPriceNb::where( 'order_number',
1311 $buy_order_id );
1312
1313 if ( $buy_orders_rank_by_price_buy_order_table->exists() ) {
1314
1315 $buy_orders_rank_by_price_buy_order_table->update( [
1316 'desired_sil' => $buy_quantity_q1,
1317 'split_id' => $buy_split_id . '_1'
1318 ] );
1319
1320 print( 'BuyOrderRankByPrice updated for remaining desired SIL <br>' );
1321 }
1322 }
1323
1324 BuyMatchedOrder::create(
1325 [
1326 'buy_order_id' => $buy_order_id,
1327 'buy_split_id' => $buy_split_id,
1328 'buy_price' => $buy_price,
1329 'buy_user_id' => $buy_user_id,
1330 'buy_quantity' => $curent_sell_quantity,
1331 'matching_date' => $matching_date,
1332 'matching_time' => $matching_time,
1333 'matching_id' => $previous_buy_matching_id + 1,
1334 'matching_process' => $matching_process_type
1335 ]
1336 );
1337 print( '<br>Inserted Record in BuyMatchedOrder for Order #' . $buy_order_id . "<br>" );
1338 // END of ELSE COND1
1339 } else {
1340 /**
1341 * \\ top sell will not be fully matched but buy order could be fully matched if
1342 * alloc =100% (buy orderallocated < sell order
1343 */
1344 print( '<br> DESIRED SIL < SELL QUANTITY <br>' );
1345
1346 /**
1347 * copy of partially matched sell order to array "matched orders"
1348 */
1349 $sell_quantity = $allocated_top_order_quantity;
1350 //update of Sell order quantity and split id
1351 $updated_quantity = $curent_sell_quantity - $sell_quantity;
1352
1353 print( '<br> UPDATED QUANTITY = ' . $updated_quantity . "<br>" );
1354 $sell_order_rankby_price_table = SellOrdersRankByPriceNb::where( 'order_number', $sell_order_id );
1355
1356 if ( $sell_order_rankby_price_table->exists() ) {
1357
1358
1359 $sell_order_rankby_price_table->update( [
1360 'desired_sil' => $updated_quantity,
1361 'split_id' => $sell_split_id . '_1'
1362 ] );
1363 }
1364
1365 print( 'SellOrdersRankByPriceNb updated with updated split id' );
1366
1367
1368 SellMatchedOrder::create(
1369 [
1370 'sell_order_id' => $sell_order_id,
1371 'sell_split_id' => $sell_split_id,
1372 'sell_price' => $sell_price,
1373 'sell_user_id' => $sell_user_id,
1374 'sell_quantity' => $sell_quantity,
1375 'matching_date' => $matching_date,
1376 'matching_time' => $matching_time,
1377 'matching_id' => $previous_sell_matching_id + 1,
1378 'matching_process' => $matching_process_type
1379 ]
1380 );
1381
1382 print( 'Inserted new record in SellMatchedOrder or Sell Order Id #' . $sell_order_id . "<br>" );
1383
1384 /**
1385 * adjustemnt of buy order in Table "Buy orders rank by price & nb"
1386 */
1387
1388 // remaining unmatched buy quantities
1389 $buy_quantity_q1 = $buy_quantity_q0 - $allocated_top_order_quantity;
1390 print( '<br><br>$buy_quantity_q1 = ' . $buy_quantity_q1 . "<br>" );
1391
1392 //$buy_quantity_q1 = 0; //WARNING TEMP
1393
1394
1395 //Start of COND2
1396 if ( $buy_quantity_q1 == 0 ) {
1397
1398 $buy_orders_rank_by_price_and_nb_17_table->update( [ 'desired_sil' => 0 ] );
1399 print( '<br>Updated BuyOrderRankByPrice for Order #' . $buy_order_id . "<br>" );
1400
1401 //update status of buy order in Table "Temporary_list_of_all_order"
1402
1403 $temp_list_all_orders = TempListOrders::where( [ 'order_number' => $buy_order_id ] );
1404
1405
1406 if ( $temp_list_all_orders->exists() ) {
1407 $temp_list_all_orders->update( [
1408 'status' => 'fully_matched',
1409 'split_id' => $buy_split_id,
1410 ] );
1411 print( '<br>Updated TempListOrders for Order #' . $buy_order_id . "<br>" );
1412 }
1413
1414 //update split_id and quantity of sell order in Table "Temporary_list_of_all_order"
1415 $temp_list_all_orders = TempListOrders::where( [ 'order_number' => $sell_order_id ] );
1416
1417
1418 if ( $temp_list_all_orders->exists() ) {
1419 $temp_list_all_orders->update( [
1420 'desired_sil' => intval( $current_quantity - $allocated_top_order_quantity ),
1421 'split_id' => $current_split_id . '_1',
1422 ] );
1423 print( '<br>Updated TempListOrders for Order #' . $sell_order_id . "<br>" );
1424 }
1425
1426
1427 } else {
1428
1429 //ELSE COND2
1430
1431 print( '<br> IN ELSE CONDITION # 2 <br>' );
1432
1433 /**
1434 * if remaining quantity <> 0 buy order NOT fully matched matching should continue
1435 * update quantity in Table "Buy orders rank by price & nb"
1436 */
1437
1438 $buy_orders_rank_by_price_buy_order_table = BuyOrdersRankByPriceNb::where( 'order_number',
1439 $buy_order_id );
1440
1441 if ( $buy_orders_rank_by_price_buy_order_table->exists() ) {
1442
1443 $buy_orders_rank_by_price_buy_order_table->update( [
1444 'desired_sil' => $buy_quantity_q1,
1445 'split_id' => $buy_split_id . '_1'
1446 ] );
1447
1448 print( 'BuyOrderRankByPrice updated for remaining desired SIL <br>' );
1449 }
1450 } //Else conditions ends here
1451
1452 BuyMatchedOrder::create(
1453 [
1454 'buy_order_id' => $buy_order_id,
1455 'buy_split_id' => $buy_split_id,
1456 'buy_price' => $buy_price,
1457 'buy_user_id' => $buy_user_id,
1458 //'buy_quantity' => $curent_sell_quantity,
1459 'buy_quantity' => $sell_quantity,
1460 'matching_date' => $matching_date,
1461 'matching_time' => $matching_time,
1462 'matching_id' => $previous_buy_matching_id + 1,
1463 'matching_process' => $matching_process_type
1464 ]
1465 );
1466 print( '<br>2nd time...Inserted Record in BuyMatchedOrder for Order #' . $buy_order_id . "<br>" );
1467 //End Else Cond 2
1468 }
1469 $m_FIFO_Buy_order = $buy_quantity_q1;
1470
1471 } catch ( Exception $e ) {
1472 print( '<br>Exception in macro_FIFO_Buy_order <br>' );
1473 print( '<br>Line # ' . $e->getLine() . "<br>" );
1474 print( '<br>Message # ' . $e->getLine() . "<br>" );
1475 }
1476
1477
1478 return $m_FIFO_Buy_order;
1479 }
1480
1481 private function Macro14() {
1482 print "<br> Calling Marco 14 <br>";
1483
1484 //Make sure matching tables of BUy and Sell are fully empty
1485 BuyMatchedOrder::truncate();
1486 SellMatchedOrder::truncate();
1487
1488 $loop_stop = 0;
1489 $a_single_buy_order = 0;
1490 $a_single_sell_order = 0;
1491
1492 $first_buy_bid_price = 0;
1493 $first_sell_ask_price = 0;
1494
1495 $first_num_bid_order = 0;
1496 $first_num_ask_order = 0;
1497
1498 $first_num_bid_size = 0;
1499 $first_num_ask_size = 0;
1500 $quantity_to_be_bought_per_FIFO_final_pass = 50;
1501
1502
1503 $first_temp_order_book_buy = TemporaryOrderBookBuy::orderBy( 'id', 'asc' )->limit( 1 )->get();
1504 $first_temp_order_book_sell = TemporaryOrderBookSell::orderBy( 'id', 'asc' )->limit( 1 )->get();
1505
1506 if ( count( $first_temp_order_book_buy ) > 0 ) {
1507 $first_num_bid_order = $first_temp_order_book_buy[0]->num_bid_order;
1508 $first_num_bid_size = $first_temp_order_book_buy[0]->bid_size;
1509 print( "<br> FIRST NUM BID ORDER: " . $first_num_bid_order . "<br>" );
1510 print( "<br> FIRST NUM BID SIZE: " . $first_num_bid_size . "<br>" );
1511
1512 }
1513
1514 if ( count( $first_temp_order_book_sell ) > 0 ) {
1515 $first_num_ask_order = $first_temp_order_book_sell[0]->num_ask_order;
1516 print( "<br> FIRST NUM ASK ORDER: " . $first_num_ask_order . "<br>" );
1517
1518 $first_num_ask_size = $first_temp_order_book_sell[0]->ask_size;
1519
1520 print( "<br> FIRST NUM ASK SIZE: " . $first_num_ask_size . "<br>" );
1521 }
1522
1523 // ' A single Buy order to be matched with sell order(s)
1524 print( '<br>first_num_bid_order = ' . $first_num_bid_order . "<br>" );
1525 print( '<br>first_num_ask_order = ' . $first_num_ask_order . "<br>" );
1526
1527 if ( $first_num_bid_order == 1 && $first_num_ask_order != 1 ) {
1528 $a_single_buy_order = 1;
1529 }
1530
1531 if ( $first_num_bid_order != 1 && $first_num_ask_order == 1 ) {
1532 $a_single_sell_order = 1;
1533 }
1534
1535 if ( $first_num_bid_order == 1 && $first_num_ask_order == 1 ) {
1536
1537 if ( $first_num_ask_size < $first_num_bid_size ) {
1538 $a_single_buy_order = 1;
1539 } else {
1540 $a_single_sell_order = 1;
1541 }
1542 }
1543
1544 // If it happens, somthing goes wrong with order book and it should exit
1545 if ( $first_num_bid_order > 1 && $first_num_ask_order > 1 ) {
1546 $msg = 'error in order book we cannot have in bucket 1 more than one Sell order and at same time more than one buy order';
1547
1548 return $msg;
1549 }
1550
1551 /**
1552 * this is the beginning of matching technique step 4 , step 1, 2 and 3 are NOT in prototype version 2
1553 * initialization of 2 variables in bold normally performed in step 1
1554 */
1555 $matching_date = date( 'Y-m-d' );
1556 $matching_time = date( 'H:i:s' );
1557
1558
1559 if ( $a_single_buy_order == 1 ) {
1560 /**
1561 * ‘\\ we are going to match a buy order
1562 * ‘\\ start with initialization of 7 variables (in bold):
1563 * ‘\\ the first variable should normally come from matching technique step 3 which is not implement in prototype version 2
1564 */
1565// $remaining_unmatched_Buy_quantities_after_step_3 = $first_temp_order_book_buy = TemporaryOrderBookBuy::orderBy( 'id',
1566// 'asc' )->limit( 1 )->get();
1567
1568 $remaining_unmatched_Buy_quantities_after_step_3 = TemporaryOrderBookBuy::orderBy( 'id',
1569 'asc' )->limit( 1 )->get();
1570
1571 if ( count( $remaining_unmatched_Buy_quantities_after_step_3 ) > 0 ) {
1572
1573 $remaining_unmatched_Buy_quantities_after_step_3 = intval( $remaining_unmatched_Buy_quantities_after_step_3[0]->bid_size );
1574 }
1575
1576
1577 $matching_process_type = "FIFO_FINAL_PASS_BUY";
1578 $allocated_buy_order_FIFO_final_pass = $remaining_unmatched_Buy_quantities_after_step_3;
1579 $quantity_to_be_bought_per_FIFO_final_pass = $allocated_buy_order_FIFO_final_pass;
1580 $total_quantity_still_to_buy_before_matching_FIFO_final_pass = $allocated_buy_order_FIFO_final_pass;
1581 $remaining_buy_quantity_after_FIFO_final_pass = $total_quantity_still_to_buy_before_matching_FIFO_final_pass;
1582 //$FIFO_final_pass_Buy_order_final_quantity = 0;
1583
1584 $idx = 0;
1585 $no_of_times = 10;
1586
1587
1588 while ( $remaining_buy_quantity_after_FIFO_final_pass > 0 ) {
1589 $idx ++;
1590 print( "Step# " . $idx . "<br>" );
1591
1592 // We have to kill the loop if it takes long in matching, means kill after 200 iterations.
1593 if ( $idx >= $no_of_times ) {
1594 break;
1595 }
1596
1597 //Calling FII BUY order
1598 $remaining_buy_quantity_after_FIFO_final_pass = $this->macro_FIFO_Buy_order( $quantity_to_be_bought_per_FIFO_final_pass,
1599 $total_quantity_still_to_buy_before_matching_FIFO_final_pass, $matching_date, $matching_time,
1600 $matching_process_type );
1601 /**
1602 * if Remaining_Buy_quantity_after_FIFO_final_pass = -1 it means by convention that there was not * anymore any Sell order with a price lower than the
1603 * buy price nothing could be matched anymore
1604 * matching stops and remaing buy quantity should be in list of all orders
1605 */
1606
1607 if ( $remaining_buy_quantity_after_FIFO_final_pass == - 1 ) { //Begin if ( $remaining_buy_quantity_after_FIFO_final_pass == - 1 )
1608 //Fetching Buy order Rank BY Price data
1609 $Buy_orders_rank_by_price_and_nb_temporary_table = BuyOrdersRankByPriceNb::orderBy( 'price',
1610 'DESC' )->orderBy( 'order_number', 'ASC' )->limit( 1 );
1611 $Buy_orders_rank_by_price_and_nb_temporary_obj = $Buy_orders_rank_by_price_and_nb_temporary_table->get();
1612
1613 if ( count( $Buy_orders_rank_by_price_and_nb_temporary_obj ) > 0 ) { //Begin if ( count( $Buy_orders_rank_by_price_and_nb_temporary_obj )
1614 $Final_Buy_quantity_remaining_after_step_4 = $Buy_orders_rank_by_price_and_nb_temporary_obj[0]->desired_sil;
1615 $Final_split_id_after_step_4 = $Buy_orders_rank_by_price_and_nb_temporary_obj[0]->split_id;
1616 $buy_order_id = $Buy_orders_rank_by_price_and_nb_temporary_obj[0]->order_number;
1617 } ////End if ( count( $Buy_orders_rank_by_price_and_nb_temporary_obj )
1618
1619 /**
1620 * update the remaining quantity of Buy order in the list of all order and put status of Buy order as fully matched if needed
1621 */
1622 $temp_list_orders_buy_obj = TempListOrders::where( [ 'order_number' => $buy_order_id ] );
1623
1624 if ( $temp_list_orders_buy_obj->exists() ) {
1625
1626 $temp_list_orders_buy_obj->update( [
1627 'split_id' => $Final_split_id_after_step_4,
1628 ] );
1629
1630 if ( $Final_Buy_quantity_remaining_after_step_4 == 0 ) { //Begin if($Final_Buy_quantity_remaining_after_step_4 == 0)
1631 $temp_list_orders_buy_obj->update( [
1632 'status' => 'fully_matched'
1633 ] );
1634 } //End if($Final_Buy_quantity_remaining_after_step_4 == 0)
1635 else {
1636
1637 if ( $temp_list_orders_buy_obj->exists() ) {
1638 $temp_list_orders_buy_obj->update( [
1639 'desired_sil' => $Final_Buy_quantity_remaining_after_step_4
1640 ] );
1641 }
1642 }
1643// //Label:- Go to Update Matching Order
1644// //$a_single_buy_order = 0; // WARNING : TO REMOVE
1645// $loop_stop = $this->update_matching_order( $a_single_buy_order );
1646//
1647// if($loop_stop == 1) {
1648// break;
1649// }
1650 }
1651 //Label:- Go to Update Matching Order
1652 //$a_single_buy_order = 0; // WARNING : TO REMOVE
1653 $loop_stop = $this->update_matching_order( $a_single_buy_order );
1654
1655 if ( $loop_stop == 1 ) {
1656 break;
1657 }
1658 } //End if ( $remaining_buy_quantity_after_FIFO_final_pass == - 1 )
1659
1660
1661 $quantity_bought = intval( $total_quantity_still_to_buy_before_matching_FIFO_final_pass - $remaining_buy_quantity_after_FIFO_final_pass );
1662
1663 // This the intialisation for next pass:
1664 $quantity_to_be_bought_per_FIFO_final_pass = intval( $quantity_to_be_bought_per_FIFO_final_pass - $quantity_bought );
1665 print( '$quantity_to_be_bought_per_FIFO_final_pass = ' . $quantity_to_be_bought_per_FIFO_final_pass . "<br>" );
1666
1667 $total_quantity_still_to_buy_before_matching_FIFO_final_pass = $remaining_buy_quantity_after_FIFO_final_pass;
1668 } // end of loop buy
1669 } //End $a_single_buyoder= 1
1670 else { //Begin Else of $a_single_buyoder= 1
1671
1672 /**
1673 * we are going to match a sell order.
1674 * start with initialization of 7 variables
1675 * the first variable should normally come from matching technique step 3 which is not implement in prototype version 2
1676 */
1677
1678 $remaining_unmatched_Sell_quantities_after_step_3 = TemporaryOrderBookSell::orderBy( 'id',
1679 'asc' )->limit( 1 )->get();
1680
1681 if ( count( $remaining_unmatched_Sell_quantities_after_step_3 ) > 0 ) {
1682
1683 $remaining_unmatched_Sell_quantities_after_step_3 = intval( $remaining_unmatched_Sell_quantities_after_step_3[0]->ask_size );
1684 }
1685
1686 $matching_process_type = "FIFO_FINAL_PASS_SELL";
1687 $allocated_sell_order_FIFO_final_pass = $remaining_unmatched_Sell_quantities_after_step_3;
1688 $quantity_to_be_sold_per_FIFO_final_pass = $allocated_sell_order_FIFO_final_pass;
1689 $total_quantity_still_to_sell_before_matching_FIFO_final_pass = $allocated_sell_order_FIFO_final_pass;
1690 $remaining_Sell_quantity_after_FIFO_final_pass = $total_quantity_still_to_sell_before_matching_FIFO_final_pass;
1691 $idx = 0;
1692 $no_of_times = 10;
1693
1694 //Starting of Sell Loop
1695 print( '<br>Inside Sell Loop <br>' );
1696
1697 while ( $remaining_Sell_quantity_after_FIFO_final_pass > 0 ) { //Begin Sell Loop
1698 $idx ++;
1699 print( "Step# " . $idx . "<br>" );
1700 // We have to kill the loop if it takes long in matching, means kill after 200 iterations.
1701 if ( $idx >= $no_of_times ) {
1702 break;
1703 }
1704
1705 //Calling FIFO Sell Order method
1706 $remaining_Sell_quantity_after_FIFO_final_pass = $this->macro_FIFO_Sell_order( $quantity_to_be_sold_per_FIFO_final_pass,
1707 $total_quantity_still_to_sell_before_matching_FIFO_final_pass, $matching_date, $matching_time,
1708 $matching_process_type );
1709
1710 print( '$remaining_Sell_quantity_after_FIFO_final_pass after macro_FIFO_Sell_order = ' . $remaining_Sell_quantity_after_FIFO_final_pass . '<br>' );
1711 /**
1712 * if remaining_Sell_quantity_after_FIFO_final_pass = -1 it means by convention that there was not * anymore any buy order with a price higher than the sell price nothing could be matched anymore
1713 * matching stops and remaining sell quantity should be updated in list of all order
1714 */
1715 if ( $remaining_Sell_quantity_after_FIFO_final_pass == - 1 ) { //Begin $remaining_Sell_quantity_after_FIFO_final_pass
1716
1717 // Fetching Sell order Rank BY Price data
1718 $sell_orders_rank_by_price_and_nb_temporary_table = SellOrdersRankByPriceNb::orderBy( 'price',
1719 'DESC' )->orderBy( 'order_number', 'ASC' )->limit( 1 );
1720
1721 $sell_orders_rank_by_price_and_nb_temporary_table_obj = $sell_orders_rank_by_price_and_nb_temporary_table->get();
1722
1723 if ( count( $sell_orders_rank_by_price_and_nb_temporary_table_obj ) > 0 ) { //Begin count( $sell_orders_rank_by_price_and_nb_temporary_table_obj ) > 0
1724
1725 $Final_Sell_quantity_remaining_after_step_4 = $sell_orders_rank_by_price_and_nb_temporary_table_obj[0]->desired_sil;
1726 $Final_split_id_after_step_4 = $sell_orders_rank_by_price_and_nb_temporary_table_obj[0]->split_id;
1727 $sell_order_id = $sell_orders_rank_by_price_and_nb_temporary_table_obj[0]->order_number;
1728
1729 } //Ends count( $sell_orders_rank_by_price_and_nb_temporary_table_obj ) > 0
1730
1731 /**
1732 * update the remaining quantity of Sell order in the list of all order and put status of Sell order as fully matched if needed
1733 */
1734
1735 $temp_list_orders_sell_obj = TempListOrders::where( [ 'order_number' => $sell_order_id ] );
1736
1737 if ( $temp_list_orders_sell_obj->exists() ) { //Begin $temp_list_orders_sell_obj->exists()
1738
1739 $temp_list_orders_sell_obj->update( [
1740 'split_id' => $Final_split_id_after_step_4,
1741 ] );
1742
1743 if ( $Final_Sell_quantity_remaining_after_step_4 == 0 ) { //Begin if($Final_Sell_quantity_remaining_after_step_4 == 0)
1744
1745 $temp_list_orders_sell_obj->update( [
1746 'status' => 'fully_matched'
1747 ] );
1748
1749 } //Ends //Begin if($Final_Sell_quantity_remaining_after_step_4 == 0)
1750 else {
1751
1752 if ( $temp_list_orders_sell_obj->exists() ) {
1753 $temp_list_orders_sell_obj->update( [
1754 'desired_sil' => $Final_Sell_quantity_remaining_after_step_4
1755 ] );
1756 }
1757 }
1758// $loop_stop = $this->update_matching_order( $a_single_buy_order );
1759//
1760// if($loop_stop == 1) {
1761// break;
1762// }
1763
1764
1765 } //Ends $temp_list_orders_sell_obj->exists()
1766
1767 /**
1768 * Moved from above
1769 */
1770 $loop_stop = $this->update_matching_order( $a_single_buy_order );
1771
1772 if ( $loop_stop == 1 ) {
1773 break;
1774 }
1775
1776
1777 } //End $remaining_Sell_quantity_after_FIFO_final_pass = -1
1778
1779 $quantity_sold = intval( $total_quantity_still_to_sell_before_matching_FIFO_final_pass - $remaining_Sell_quantity_after_FIFO_final_pass );
1780 // This the intialisation for next pass:
1781 $quantity_to_be_sold_per_FIFO_final_pass = intval( $quantity_to_be_sold_per_FIFO_final_pass - $quantity_sold );
1782 print( '$quantity_to_be_sold_per_FIFO_final_pass =' . $quantity_to_be_sold_per_FIFO_final_pass . "<br>" );
1783 $total_quantity_still_to_sell_before_matching_FIFO_final_pass = $remaining_Sell_quantity_after_FIFO_final_pass;
1784
1785 } //End Sell Loop
1786 } //End Else of $a_single_buyoder= 1
1787 $this->update_matching_order( $a_single_buy_order );
1788 }
1789
1790 private function Macro15() {
1791 print "<b>Calling Marco 15</b>";
1792
1793 // fetch data from table buy_matched_order
1794 $buy_matched_order_table = BuyMatchedOrder::orderBy( 'id', 'asc' )->limit( 1 );
1795
1796 if ( $buy_matched_order_table->exists() ) {
1797 $buy_matched_order_obj = $buy_matched_order_table->get();
1798 $order_matching_process_buy = $buy_matched_order_obj[0]->matching_process;
1799 $new_order_id_buy = $buy_matched_order_obj[0]->buy_order_id;
1800 print( "<br> order_matching_process_buy: " . $order_matching_process_buy . "<br>" );
1801 print( "<br> new_order_id_buy: " . $new_order_id_buy . "<br>" );
1802 }
1803
1804 // fetch data from table buy_matched_order
1805 $sell_matched_order_table = SellMatchedOrder::orderBy( 'id', 'asc' )->limit( 1 );
1806
1807 if ( $sell_matched_order_table->exists() ) {
1808 $sell_matched_order_obj = $sell_matched_order_table->get();
1809
1810 $order_matching_process_sell = $sell_matched_order_obj[0]->matching_process;
1811 $new_order_id_sell = $sell_matched_order_obj[0]->sell_order_id;
1812 print( "<br> order_matching_process_buy: " . $order_matching_process_sell . "<br>" );
1813 print( "<br> new_order_id_sell: " . $new_order_id_sell . "<br>" );
1814 }
1815
1816 // check if order is buy or sell ?
1817 If ( $order_matching_process_buy == "TOP_ORDER_BUY" || $order_matching_process_buy == "LMM_BUY" || $order_matching_process_buy == "FIFO_FIRST_PASS_BUY" || $order_matching_process_buy == "PRO_RATA_BUY" || $order_matching_process_buy == "FIFO_FINAL_PASS_BUY" ) {
1818 // new order is a buy order
1819 $new_order_id = $new_order_id_buy;
1820
1821 } else {
1822 $new_order_id = $new_order_id_sell;
1823 }
1824 print( "<br> new_order_id: " . $new_order_id . "<br>" );
1825
1826 // check whether the order is a “Fill or kill” or “immediate or cancel”
1827 $temp_list_orders_table = TempListOrders::where( [ 'order_number' => $new_order_id ] );
1828 $temp_list_orders_obj = null;
1829
1830 if ( $temp_list_orders_table->exists() ) {
1831 $temp_list_orders_obj = $temp_list_orders_table->first();
1832
1833 $new_order_status = $temp_list_orders_obj->status;
1834 $new_order_type = $temp_list_orders_obj->order_type;
1835 $new_order_entry_date = $temp_list_orders_obj->entry_date;
1836 $new_order_entry_time = $temp_list_orders_obj->entry_time;
1837 $new_order_user_id = $temp_list_orders_obj->user_id;
1838 $new_order_buy_sell = $temp_list_orders_obj->buy_sell;
1839 $new_order_price = $temp_list_orders_obj->price;
1840 $new_order_desired_sil = $temp_list_orders_obj->desired_sil;
1841 $new_order_duration = $temp_list_orders_obj->duration;
1842 $new_order_split_id = $temp_list_orders_obj->split_id;
1843 $new_order_levelling_ratio = $temp_list_orders_obj->levelling_ratio;
1844 $new_order_LMM_percentage = $temp_list_orders_obj->LMM_percentage;
1845 $new_order_trigger_price = $temp_list_orders_obj->trigger_price;
1846 print( "<br> new_order_status here: " . $new_order_status . "<br>" );
1847 print( "<br> new_order_type: " . $new_order_type . "<br>" );
1848 }
1849
1850 If ( $new_order_type == "fill_or_kill" ) {
1851 // it is a “fill or kill” type we need to check whether it is fully matched or not
1852
1853 If ( $new_order_status == "fully_matched" ) {
1854 // it is a “fill or kill” type, fully matched we need to run macro16
1855 $this->Macro16();
1856
1857 } else {
1858 // else of If ( $new_order_status == “fully_matched”)
1859 // it is a “fill or kill” type but NOT fully matched , the official list of order will remain unchanged
1860 // change status of order in table TempListOrders
1861
1862 $updated_status = 'fill_kill_cancelled_not_fully_matched';
1863
1864 if ( $temp_list_orders_table != null ) {
1865
1866 $temp_list_orders_table->update( [
1867 'status' => $updated_status,
1868 ] );
1869
1870 print( 'Temp List Status Updated <br>' );
1871
1872 // move fill kill order to table list_of_non_active_orders
1873 ListNonActiveOrders::create(
1874 [
1875 'order_number' => $new_order_id,
1876 'status' => $updated_status,
1877 'order_type' => $new_order_type,
1878 'entry_date' => $new_order_entry_date,
1879 'entry_time' => $new_order_entry_time,
1880 'user_id' => $new_order_user_id,
1881 'buy_sell' => $new_order_buy_sell,
1882 'price' => $new_order_price,
1883 'desired_sil' => $new_order_desired_sil,
1884 'duration' => $new_order_duration,
1885 'split_id' => $new_order_split_id,
1886 'levelling_ratio' => $new_order_levelling_ratio,
1887 'LMM_percentage' => $new_order_LMM_percentage,
1888 'trigger_price' => $new_order_trigger_price,
1889
1890 ]
1891 );
1892 }
1893
1894 } //end of else If ( $new_order_status == "fully_matched" )
1895 } else { //Begin Else If ( $new_order_type == “fill_ot_kill”)
1896
1897 // else of test If ( $new_order_type == “fill_ot_kill”)
1898 // it is not a test “fill_ot_kill”, test to see if new order is “immediate or cancel”
1899 print( 'ORDER TYPE = ' . $new_order_type . "<br>" );
1900 If ( $new_order_type == "immediate_or_cancel" ) {
1901 // it is a “immediate or cancel” type we need to check whether it is fully matched or not
1902
1903 if ( $new_order_status == 'open' ) { //Begin if($new_order_status == 'open')
1904 // there is an unmatched part which needs to be canceled
1905 $updated_status = 'unmatched part of immediate or cancel order has been canceled';
1906 $updated_status = 'unmatched_immediate_cancel_cancel';
1907 $updated_status = 'cancel_unmatched_immediate_canel';
1908 $updated_status = 'cancel_unmatched_part_order';
1909
1910 if ( $temp_list_orders_table != null ) {
1911
1912 $temp_list_orders_table->update( [
1913 'status' => $updated_status,
1914 ] );
1915 }
1916
1917 } //End if($new_order_status == 'open')
1918 $this->Macro16();
1919 } //Ends If ( $new_order_type == "immediate_cancel" )
1920 else {
1921 $this->Macro16();
1922 }
1923 }
1924
1925 }
1926
1927//Test if matching is possible
1928 private function Macro17() {
1929
1930 print( '<br> Calling Marco 17' );
1931
1932 $buy_bid_price = 0;
1933 $sell_ask_price = 0;
1934
1935 try {
1936
1937 } catch ( Exception $e ) {
1938
1939 }
1940
1941 $first_temp_order_book_buy = TemporaryOrderBookBuy::orderBy( 'bid_price', 'desc' )->limit( 1 )->first();
1942 $first_temp_order_book_sell = TemporaryOrderBookSell::orderBy( 'ask_price', 'asc' )->limit( 1 )->first();
1943
1944
1945 if ( $first_temp_order_book_sell != null ) {
1946 $sell_ask_price = $first_temp_order_book_sell->ask_price;
1947 }
1948
1949 if ( $first_temp_order_book_buy != null ) {
1950 $buy_bid_price = $first_temp_order_book_buy->bid_price;
1951 }
1952
1953
1954 print( '$sell_ask_price = ' . $sell_ask_price . " <br>" );
1955 print " <br>";
1956 print( '$buy_bid_price = ' . $buy_bid_price . " <br>" );
1957
1958 print " <br>";
1959
1960
1961 // Check whether it is matchable or not.
1962 if ( $buy_bid_price < $sell_ask_price || $buy_bid_price == 0 || $sell_ask_price == 0 ) {
1963 $this->Macro18();
1964 } else {
1965 //Matchable
1966 $this->Macro14();
1967
1968 $this->Macro15();
1969 }
1970
1971 }
1972
1973 private function createTempBuyOrderBook() {
1974 $bid_price = [ 0, 0, 0 ];
1975 $bid_size = [ 0, 0, 0 ];
1976 $bid_count = [ 0, 0, 0 ];
1977
1978 print( 'Calling createTempBuyOrderBook' );
1979 print_r( $bid_price );
1980 print " <br>";
1981 print_r( $bid_size );
1982 print " <br>";
1983 print_r( $bid_count );
1984 print " <br>";
1985
1986 // Select count(desired_sil),price from sell_orders_rank_by_price_nb group by price order by price
1987
1988 $buy_orders_rank_by_price = BuyOrdersRankByPriceNb::orderBy( 'price', 'DESC' )->groupBy( 'price' )
1989 ->select( 'price as bid_price',
1990 DB::raw( 'count(desired_sil) as num_bid_order' ),
1991 DB::raw( 'sum(desired_sil) as bid_size' )
1992 )
1993 ->get();
1994
1995
1996 if ( count( $buy_orders_rank_by_price ) > 0 ) {
1997 // Insert ranked and aggregated records in Temp OrderBook Sell
1998 foreach ( $buy_orders_rank_by_price as $buy_order_rank_by_price ) {
1999 TemporaryOrderBookBuy::create(
2000 [
2001 'bid_size' => $buy_order_rank_by_price->bid_size,
2002 'bid_price' => $buy_order_rank_by_price->bid_price,
2003 'num_bid_order' => $buy_order_rank_by_price->num_bid_order,
2004 ]
2005 );
2006 }
2007 }
2008
2009
2010 }
2011
2012 /**
2013 * Creation of Aggregated Sell Order Book
2014 */
2015
2016 private function createTempSellOrderBook() {
2017 $ask_prize = [ 0, 0, 0 ];
2018 $ask_size = [ 0, 0, 0 ];
2019 $ask_count = [ 0, 0, 0 ];
2020
2021 print( ' <br> Calling createTempSellOrderBook <br>' );
2022
2023 // Select count(desired_sil),price from sell_orders_rank_by_price_nb group by price order by price
2024
2025 $sell_orders_rank_by_price = SellOrdersRankByPriceNb::orderBy( 'price', 'asc' )->groupBy( 'price' )
2026 ->select( 'price as ask_price',
2027 DB::raw( 'count(desired_sil) as num_ask_order' ),
2028 DB::raw( 'sum(desired_sil) as ask_size' )
2029 )
2030 ->get();
2031
2032 if ( count( $sell_orders_rank_by_price ) > 0 ) {
2033 // Insert ranked and aggregated records in Temp OrderBook Sell
2034 foreach ( $sell_orders_rank_by_price as $sell_order_rank_by_price ) {
2035 TemporaryOrderBookSell::create(
2036 [
2037 'ask_size' => $sell_order_rank_by_price->ask_size,
2038 'ask_price' => $sell_order_rank_by_price->ask_price,
2039 'num_ask_order' => $sell_order_rank_by_price->num_ask_order,
2040 ]
2041 );
2042 }
2043 }
2044 }
2045
2046 /*
2047 * Creation of Temporary arrays: Buy and Sell_orders_rank_by_price_and_nb and Temporary_Order_book_Buy and Sell
2048 */
2049 private function Macro9() {
2050
2051 print( 'Calling MACRO9 <br>' );
2052 //initialisation of variables
2053 $temporary_Buy_orders = 0;
2054 $temporary_Sell_orders = 0;
2055 $increment = 0;
2056 $user_id = 0;
2057
2058 //Select all Temp orders having status is OPEN and of Type "B"
2059 $temp_orders = TempListOrders::where( [ 'status' => 'open' ] )->get();
2060
2061 foreach ( $temp_orders as $temp_order ) {
2062 $user_id = $temp_order->user_id;
2063 $order_number = $temp_order->order_number;
2064 $currency = $temp_order->currency;
2065 $buy_sell = $temp_order->buy_sell;
2066 $desired_sil = $temp_order->desired_sil;
2067 $price = $temp_order->price;
2068 $order_type = $temp_order->order_type;
2069 $trigger_price_activating_stop_order = $temp_order->trigger_price_activating_stop_order;
2070 $duration = ( $temp_order->duration == null ? '1970-01-01' : $temp_order->duration );
2071 $split_id = $temp_order->split_id;
2072 $levelling_ratio = $temp_order->levelling_ratio;
2073 $LMM_percentage = $temp_order->LMM_percentage;
2074 $trigger_price_activating_stop_order = $temp_order->trigger_price_activating_stop_order;
2075 $entry_date = $temp_order->entry_date;
2076 $entry_time = $temp_order->entry_time;
2077 $status = $temp_order->status;
2078
2079 if ( $buy_sell == 'B' ) {
2080 $buy_order_rank_price_nb = BuyOrdersRankByPriceNb::create(
2081 [
2082 'order_number' => $order_number,
2083 'currency' => $currency,
2084 'buy_sell' => $buy_sell,
2085 'entry_date' => $entry_date,
2086 'entry_time' => $entry_time,
2087 'desired_sil' => $desired_sil,
2088 'price' => $price,
2089 'order_type' => $order_type,
2090 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
2091 'duration' => $duration,
2092 'status' => $status,
2093 'split_id' => $split_id,
2094 'levelling_ratio' => $levelling_ratio,
2095 'LMM_percentage' => $LMM_percentage,
2096 'user_id' => $user_id,
2097 ]
2098 );
2099
2100 } elseif ( $buy_sell == 'S' ) {
2101 $buy_order_rank_price_nb = SellOrdersRankByPriceNb::create(
2102 [
2103 'order_number' => $order_number,
2104 'buy_sell' => $buy_sell,
2105 'entry_date' => $entry_date,
2106 'entry_time' => $entry_time,
2107 'desired_sil' => $desired_sil,
2108 'price' => $price,
2109 'order_type' => $order_type,
2110 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
2111 'duration' => $duration,
2112 'status' => $status,
2113 'split_id' => $split_id,
2114 'levelling_ratio' => $levelling_ratio,
2115 'LMM_percentage' => $LMM_percentage,
2116 'user_id' => $user_id,
2117 ]
2118 );
2119 }
2120 }
2121
2122 /*
2123 * We are ranking Orders both Sell and Buy by Price
2124 */
2125
2126 // Ranking Buy
2127
2128 $ranked_buy_orders_by_price = BuyOrdersRankByPriceNb::orderBy( 'price', 'desc' )->orderBy( 'order_number',
2129 'asc' )->get();
2130 $ranked_sell_orders_by_price = SellOrdersRankByPriceNb::orderBy( 'price', 'asc' )->orderBy( 'order_number',
2131 'asc' )->get();
2132
2133 //dd($ranked_buy_orders_by_price);
2134 // Emptyng Rank BY Price Tables first
2135 //BuyOrdersRankByPriceNb::truncate();
2136 //SellOrdersRankByPriceNb::truncate();
2137
2138// foreach ( $ranked_buy_orders_by_price as $item ) {
2139// print(' '. $item->order_number.' - '.$item->price." <br>");
2140//// $buy_order_rank_price_nb = BuyOrdersRankByPriceNb::create(
2141//// [
2142//// 'order_number' => $item->order_number,
2143//// 'currency' => $item->currency,
2144//// 'buy_sell' => $item->buy_sell,
2145//// 'entry_date' => $item->entry_date,
2146//// 'entry_time' => $item->entry_time,
2147//// 'desired_sil' => $item->desired_sil,
2148//// 'price' => $item->price,
2149//// 'order_type' => $item->order_type,
2150//// 'trigger_price_activating_stop_order' => $item->trigger_price_activating_stop_order,
2151//// 'duration' => $item->duration,
2152//// 'status' => $item->status,
2153//// 'split_id' => $item->split_id,
2154//// 'levelling_ratio' => $item->levelling_ratio,
2155//// 'LMM_percentage' => $item->LMM_percentage,
2156//// 'user_id' => $logged_in_id,
2157//// ]
2158//// );
2159// }
2160
2161 // Make sure Temp Order Book Buy and Sell are Empty by Truncating them.
2162 TemporaryOrderBookBuy::truncate();
2163 //TemporaryOrderBookSell::truncate();
2164
2165 // Check whether records exist
2166 if ( count( $ranked_buy_orders_by_price ) > 0 ) {
2167 $temporary_Buy_orders = 1;
2168 }
2169
2170 if ( count( $ranked_sell_orders_by_price ) > 0 ) {
2171 $temporary_Sell_orders = 1;
2172 }
2173
2174 if ( $temporary_Sell_orders == 0 && $temporary_Buy_orders == 0 ) {
2175
2176 }
2177
2178 $this->createTempSellOrderBook();
2179 $this->createTempBuyOrderBook();
2180
2181 }
2182
2183// Creation of Temp Order List
2184 private function Macro8() {
2185
2186 print( '<br> Calling Macro8' );
2187 // get all the data from List of All Orders
2188 $list_all_orders = ListAllOrders::all();
2189
2190 foreach ( $list_all_orders as $list_all_order ) {
2191
2192 $user_id = $list_all_order->user_id;
2193 $order_number = $list_all_order->order_number;
2194 $currency = $list_all_order->currency;
2195 $buy_sell = $list_all_order->buy_sell;
2196 $desired_sil = $list_all_order->desired_sil;
2197 $price = $list_all_order->price;
2198 $order_type = $list_all_order->order_type;
2199 $trigger_price_activating_stop_order = $list_all_order->trigger_price_activating_stop_order;
2200 $duration = ( $list_all_order->duration == null ? '1970-01-01' : $list_all_order->duration );
2201 $split_id = $list_all_order->split_id;
2202 $levelling_ratio = $list_all_order->levelling_ratio;
2203 $LMM_percentage = $list_all_order->LMM_percentage;
2204 $trigger_price_activating_stop_order = $list_all_order->trigger_price_activating_stop_order;
2205 $entry_date = $list_all_order->entry_date;
2206 $entry_time = $list_all_order->entry_time;
2207 $status = $list_all_order->status;
2208
2209 $temp_order = TempListOrders::create(
2210 [
2211 'order_number' => $order_number,
2212 'currency' => $currency,
2213 'buy_sell' => $buy_sell,
2214 'entry_date' => $entry_date,
2215 'entry_time' => $entry_time,
2216 'desired_sil' => $desired_sil,
2217 'price' => $price,
2218 'order_type' => $order_type,
2219 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
2220 'duration' => $duration,
2221 'status' => $status,
2222 'split_id' => $split_id,
2223 'levelling_ratio' => $levelling_ratio,
2224 'LMM_percentage' => $LMM_percentage,
2225 'user_id' => $user_id,
2226 ]
2227 );
2228 }
2229
2230 // Get the new order from new Order Enters
2231 $new_orders_enter = NewOrderEnters::first();
2232
2233 if ( $new_orders_enter->exists() ) {
2234
2235 print( '<br> now we add new order to temporary list<br>' );
2236 $new_order_number = $new_orders_enter->order_number;
2237 $new_user_id = $new_orders_enter->user_id;
2238 $new_currency = $new_orders_enter->currency;
2239 $new_buy_sell = $new_orders_enter->buy_sell;
2240 $new_desired_sil = $new_orders_enter->desired_sil;
2241 $new_price = $new_orders_enter->price;
2242 $new_order_type = $new_orders_enter->order_type;
2243 $new_trigger_price_activating_stop_order = $new_orders_enter->trigger_price_activating_stop_order;
2244 $new_duration = ( $new_orders_enter->duration == null ? '1970-01-01' : $new_orders_enter->duration );
2245 $new_split_id = $new_orders_enter->split_id;
2246 $new_levelling_ratio = $new_orders_enter->levelling_ratio;
2247 $new_LMM_percentage = $new_orders_enter->LMM_percentage;
2248 $new_status = $new_orders_enter->status;
2249 $new_trigger_price_activating_stop_order = $new_orders_enter->trigger_price_activating_stop_order;
2250 $new_entry_date = $new_orders_enter->entry_date;
2251 $new_entry_time = $new_orders_enter->entry_time;
2252 print( '<br> now we add new order to temporary list ' . $new_order_number . "<br>" );
2253
2254 $new_order = TempListOrders::create(
2255 [
2256 'order_number' => $new_order_number,
2257 'currency' => $new_currency,
2258 'buy_sell' => $new_buy_sell,
2259 'entry_date' => $new_entry_date,
2260 'entry_time' => $new_entry_time,
2261 'desired_sil' => $new_desired_sil,
2262 'price' => $new_price,
2263 'order_type' => $new_order_type,
2264 'trigger_price_activating_stop_order' => $new_trigger_price_activating_stop_order,
2265 'duration' => $new_duration,
2266 'status' => $new_status,
2267 'split_id' => $new_split_id,
2268 'levelling_ratio' => $new_levelling_ratio,
2269 'LMM_percentage' => $new_LMM_percentage,
2270 'user_id' => $new_user_id,
2271 ]
2272 );
2273 }
2274
2275 $result = $new_order->id;
2276 print( " <br> New Order Appended <br>" );
2277 }
2278
2279 private function Macro11() {
2280 print( " <br> Called Macro 11" );
2281 // check whether the last traded price will activate a stop order in table stop_order_list
2282 // the system will look at the oldest order first and if the price is triggered, the order will be copied in table
2283 // new_orders_enter whre its status will become open and the order will then be removed from table stop_order_list
2284
2285 // when reaching macro 11 table new order enters need to be empty, to be sure we empty it again
2286 NewOrderEnters::truncate();
2287
2288 // Fetch both last buy and sell matched price
2289 $last_matched_order = LastMatchedPriceQuantity::orderBy( 'created_at', 'DESC' )->first();
2290 $last_buy_price_matched = floatval( $last_matched_order->buy_price );
2291 $last_sell_price_matched = floatval( $last_matched_order->sell_price );
2292
2293 print( "<br> last_buy_price_matched = " . $last_buy_price_matched . "<br>" );
2294 print( "<br> last_sell_price_matched = " . $last_sell_price_matched . "<br>" );
2295
2296 // ranking table stop_order_list by ascending order_number; we need to look at the oldest number first (smallest
2297 // order_number) check whether it can be activated and if not look at the next one in the list and so on
2298
2299 $stop_orders_table = StopOrderList::orderBy( 'order_number', 'asc' );
2300 $stop_orders_obj = null;
2301
2302 if ( $stop_orders_table->exists() ) {
2303 $stop_orders_obj = $stop_orders_table->get();
2304
2305 foreach ( $stop_orders_obj as $stop_order_obj ) {
2306
2307 $order_number = $stop_order_obj->order_number;
2308 $order_status = $stop_order_obj->status;
2309 $order_currency = $stop_order_obj->currency;
2310 $order_type = $stop_order_obj->order_type;
2311 $order_entry_date = $stop_order_obj->entry_date;
2312 $order_entry_time = $stop_order_obj->entry_time;
2313 $order_user_id = $stop_order_obj->user_id;
2314 $order_buy_sell = $stop_order_obj->buy_sell;
2315 $order_price = $stop_order_obj->price;
2316 $order_desired_sil = $stop_order_obj->desired_sil;
2317 $order_duration = $stop_order_obj->duration;
2318 $order_split_id = $stop_order_obj->split_id;
2319 $order_levelling_ratio = $stop_order_obj->levelling_ratio;
2320 $order_LMM_percentage = $stop_order_obj->LMM_percentage;
2321 $order_trigger_price = $stop_order_obj->trigger_price_activating_stop_order;
2322
2323 print( '<br> $last_sell_price_matched = ' . $last_sell_price_matched . "<br>" );
2324 print( '<br> Order Trigger Price = ' . $order_trigger_price . "<br>" );
2325
2326 // test to see if trigger price activated:
2327 // for buy stop order trigger criteria is $last_buy_price_matched <= $order_trigger_price
2328 // for sell stop order trigger criteria is $last_sell_price_matched >= $order_trigger_price
2329
2330 If ( ( $order_buy_sell == 'B' && $last_buy_price_matched <= $order_trigger_price ) || ( $order_buy_sell == 'S' && $last_sell_price_matched >= $order_trigger_price ) ) {
2331 // stop order has been activated and needs to be moved into table new order enters with status open
2332 // table new order enters need to be empty
2333
2334 print( "<br> stop order activated " );
2335 print( "<br> order type = " . $order_buy_sell . "<br>" );
2336 print( "<br> order trigger price = " . $order_trigger_price . "<br>" );
2337
2338 print( "<br> stop order activated and going to macro12 is number = " . $order_number . "<br>" );
2339
2340
2341 $new_orders_temp = NewOrderEnters::create( [
2342 'order_number' => $order_number,
2343 'currency' => $order_currency,
2344 'buy_sell' => $order_buy_sell,
2345 'entry_date' => $order_entry_date,
2346 'entry_time' => $order_entry_time,
2347 'desired_sil' => $order_desired_sil,
2348 'price' => $order_price,
2349 'order_type' => $order_type,
2350 'trigger_price' => $order_trigger_price,
2351 'duration' => $order_duration,
2352 'status' => 'open',
2353 'split_id' => $order_split_id,
2354 'levelling_ratio' => $order_levelling_ratio,
2355 'LMM_percentage' => $order_LMM_percentage,
2356 'user_id' => $order_user_id,
2357
2358
2359 ] );
2360
2361 // remove activated order from table StopOrders
2362 StopOrderList::where( 'order_number', $order_number )->delete();
2363 // launch macro 12 with that newly activated order
2364 $this->Macro12();
2365 }
2366 }
2367 }
2368
2369 return 1;
2370 }
2371
2372 private function Macro13() {
2373 print( "<br> Calling Macro 13 <br>" );
2374 // copy content of table TemporaryOrderBookBuy into table OrderBookBuy
2375 // copy content of table TemporaryOrderBookSell into table OrderBookSell
2376 // copy content of table TempListOrders into table ListOrders
2377
2378 try {
2379 //empty table OrderBookBuy
2380 OrderBookBuy::truncate();
2381 //empty table OrderBookSell
2382 OrderBookSell::truncate();
2383 //empty table ListOrders
2384 ListAllOrders::truncate();
2385
2386 // copy content of table TemporaryOrderBookBuy into table OrderBookBuy
2387
2388 $temp_orders_buy = TemporaryOrderBookBuy::where( 'id', '>', 0 )->get();
2389
2390 foreach ( $temp_orders_buy as $temp_order_buy ) {
2391 $bid_price = $temp_order_buy->bid_price;
2392 $bid_size = $temp_order_buy->bid_size;
2393 $num_bid_order = $temp_order_buy->num_bid_order;
2394
2395 $order_book_buy = OrderBookBuy::create( [
2396 'bid_price' => $bid_price,
2397 'bid_size' => $bid_size,
2398 'num_bid_order' => $num_bid_order,
2399
2400 ] );
2401
2402 }
2403
2404 // copy content of table TemporaryOrderBookSell into table OrderBookSell
2405 $temp_orders_Sell = TemporaryOrderBookSell::where( 'id', '>', 0 )->get();
2406
2407 foreach ( $temp_orders_Sell as $temp_order_Sell ) {
2408 $ask_price = $temp_order_Sell->ask_price;
2409 $ask_size = $temp_order_Sell->ask_size;
2410 $num_ask_order = $temp_order_Sell->num_ask_order;
2411
2412 $order_book_Sell = OrderBookSell::create( [
2413 'ask_price' => $ask_price,
2414 'ask_size' => $ask_size,
2415 'num_ask_order' => $num_ask_order,
2416
2417 ] );
2418 }
2419
2420 // copy content of table TempListOrders into table ListOrders
2421 $temp_orders = TempListOrders::where( 'order_number', '>', 0 )->get();
2422 foreach ( $temp_orders as $temp_order ) {
2423
2424 $order_number = $temp_order->order_number;
2425 $currency = $temp_order->currency;
2426 $buy_sell = $temp_order->buy_sell;
2427 $desired_sil = $temp_order->desired_sil;
2428 $price = $temp_order->price;
2429 $order_type = $temp_order->order_type;
2430 $trigger_price_activating_stop_order = $temp_order->trigger_price_activating_stop_order;
2431 $duration = ( $temp_order->duration == null ? '1970-01-01' : $temp_order->duration );
2432 $split_id = $temp_order->split_id;
2433 $levelling_ratio = $temp_order->levelling_ratio;
2434 $LMM_percentage = $temp_order->LMM_percentage;
2435 $trigger_price_activating_stop_order = $temp_order->trigger_price_activating_stop_order;
2436 $entry_date = $temp_order->entry_date;
2437 $entry_time = $temp_order->entry_time;
2438 $status = $temp_order->status;
2439 $user_id = $temp_order->user_id;
2440
2441 $list_order = ListAllOrders::create( [
2442 'order_number' => $order_number,
2443 'currency' => $currency,
2444 'buy_sell' => $buy_sell,
2445 'entry_date' => $entry_date,
2446 'entry_time' => $entry_time,
2447 'desired_sil' => $desired_sil,
2448 'price' => $price,
2449 'order_type' => $order_type,
2450 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
2451 'duration' => $duration,
2452 'status' => $status,
2453 'split_id' => $split_id,
2454 'levelling_ratio' => $levelling_ratio,
2455 'LMM_percentage' => $LMM_percentage,
2456 'user_id' => $user_id,
2457
2458 ] );
2459 }
2460
2461 //empty table temporary_list_of_all_orders
2462 TempListOrders::truncate();
2463 //empty table new_order_enter
2464 NewOrderEnters::truncate();
2465 } catch (Exception $e) {
2466 print( '<br>Exception in Macro13 <br>' );
2467 print( '<br>Line # ' . $e->getLine() . "<br>" );
2468 print( '<br>Message # ' . $e->getLine() . "<br>" );
2469 }
2470
2471
2472
2473 } // end of macro13
2474
2475
2476 private function Macro6B() {
2477 print( 'Calling macro6B...cleaning Tables' );
2478 TempListOrders::truncate();
2479 SellMatchedOrder::truncate();
2480 TemporaryOrderBookSell::truncate();
2481
2482 BuyMatchedOrder::truncate();
2483 TemporaryOrderBookBuy::truncate();
2484 }
2485
2486 private function Macro6() {
2487 print( " <br> Called Macro 6. ..Cleaning Table" );
2488
2489 BuyOrdersRankByPriceNb::truncate();
2490 SellOrdersRankByPriceNb::truncate();
2491
2492 TempListOrders::truncate();
2493
2494 SellMatchedOrder::truncate();
2495 TemporaryOrderBookSell::truncate();
2496
2497 BuyMatchedOrder::truncate();
2498 TemporaryOrderBookBuy::truncate();
2499
2500 }
2501
2502 private function Macro12(
2503// $input_price,
2504// $order_type,
2505// $buy_sell,
2506// $form_data,
2507// $status = 'open'
2508 ) {
2509
2510// $input_price = $this->price;
2511// $order_type = $this->order_type;
2512// $buy_sell = $this->buy_sell;
2513 $status = 'open';
2514 $macro_11_return_value = 0;
2515
2516 /**
2517 * Fetch the values from New Order Enters
2518 */
2519 try {
2520 $new_orders_enter = NewOrderEnters::first();
2521
2522 if ( $new_orders_enter->exists() ) {
2523
2524 print( '<br> New Order Exist in Macro12 <br>' );
2525
2526 $order_number = $new_orders_enter->order_number;
2527 print( "<br> macro12 is treating order number = " . $order_number . "<br>" );
2528 $user_id = $new_orders_enter->user_id;
2529 $currency = $new_orders_enter->currency;
2530 $buy_sell = $new_orders_enter->buy_sell;
2531 $desired_sil = $new_orders_enter->desired_sil;
2532 $price = $new_orders_enter->price;
2533 $order_type = $new_orders_enter->order_type;
2534 $trigger_price_activating_stop_order = $new_orders_enter->trigger_price_activating_stop_order;
2535 $duration = ( $new_orders_enter->duration == null ? '1970-01-01' : $new_orders_enter->duration );
2536 $split_id = $new_orders_enter->split_id;
2537 $levelling_ratio = $new_orders_enter->levelling_ratio;
2538 $LMM_percentage = $new_orders_enter->LMM_percentage;
2539 $status = $new_orders_enter->status;
2540 $trigger_price_activating_stop_order = $new_orders_enter->trigger_price_activating_stop_order;
2541 $entry_date = $new_orders_enter->entry_date;
2542 $entry_time = $new_orders_enter->entry_time;
2543
2544 if ( $order_type == 'stop_order' && $status == 'pending' ) {
2545
2546 print( '<br> Entering into Stop Order List <br>' );
2547
2548 //StopOrderList::truncate();
2549
2550 //Store it into Stop Order List
2551 //foreach ( $new_orders_enter as $new_order_enter ) {
2552 StopOrderList::create(
2553 [
2554 'order_number' => $order_number,
2555 'currency' => $currency,
2556 'buy_sell' => $buy_sell,
2557 'entry_date' => $entry_date,
2558 'entry_time' => $entry_time,
2559 'desired_sil' => $desired_sil,
2560 'price' => $price,
2561 'order_type' => $order_type,
2562 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
2563 'duration' => $duration,
2564 'status' => $status,
2565 'split_id' => $split_id,
2566 'levelling_ratio' => $levelling_ratio,
2567 'LMM_percentage' => $LMM_percentage,
2568 'user_id' => $user_id,
2569 ]
2570 );
2571 //}
2572
2573 //Truncate New Order Enter
2574 NewOrderEnters::truncate();
2575 //Calling MAcro11
2576 $macro_11_return_value = $this->Macro11();
2577
2578
2579 } else {
2580 // LIMIT ORDER BEGINS
2581 //Check whether it is extreme price
2582 $last_matched_order = LastMatchedPriceQuantity::orderBy( 'created_at', 'desc' )->first();
2583
2584 // Fetch both Buy and sell price
2585 $last_buy_price_matched = floatval( $last_matched_order->buy_price );
2586 $last_sell_price_matched = floatval( $last_matched_order->sell_price );
2587 $price_difference_automatic_order_cancellation = $this->extreme_price_cancellation;
2588
2589 //Difference between new order price and price retrieved
2590
2591 // Check whether it is a Buy Order
2592 if ( $buy_sell == 'B' ) {
2593
2594 $diff = abs( $price - $last_buy_price_matched ) / $last_buy_price_matched;
2595
2596 // Check whether the difference > order cancellation
2597 if ( $diff > $price_difference_automatic_order_cancellation ) {
2598 $status = 'cancelled_price_extreme';
2599
2600 //Now we have to enter in LIST of Non active order
2601 #Add new Order
2602
2603 $non_active_order = ListNonActiveOrders::create(
2604 [
2605 'currency' => $currency,
2606 'buy_sell' => $buy_sell,
2607 'entry_date' => $entry_date,
2608 'entry_time' => $entry_time,
2609 'desired_sil' => $desired_sil,
2610 'price' => $price,
2611 'order_type' => $order_type,
2612 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
2613 'duration' => $duration,
2614 'status' => $status,
2615 'split_id' => $split_id,
2616 'levelling_ratio' => $levelling_ratio,
2617 'LMM_percentage' => $LMM_percentage,
2618 'user_id' => $user_id,
2619 ]
2620 );
2621
2622 $macro_11_return_value = $this->Macro11();
2623 }
2624
2625 } elseif ( $buy_sell == 'S' ) {
2626
2627 $diff = abs( $price - $last_sell_price_matched ) / $last_sell_price_matched;
2628
2629 // Check whether the difference > order cancellation
2630 if ( $diff > $price_difference_automatic_order_cancellation ) {
2631 $status = 'cancelled_price_extreme';
2632
2633 //Now we have to enter in LIST of Non active order
2634 #Add new Order
2635 $non_active_order = ListNonActiveOrders::create(
2636 [
2637 'currency' => $currency,
2638 'buy_sell' => $buy_sell,
2639 'entry_date' => $entry_date,
2640 'entry_time' => $entry_time,
2641 'desired_sil' => $desired_sil,
2642 'price' => $price,
2643 'order_type' => $order_type,
2644 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
2645 'duration' => $duration,
2646 'status' => $status,
2647 'split_id' => $split_id,
2648 'levelling_ratio' => $levelling_ratio,
2649 'LMM_percentage' => $LMM_percentage,
2650 'user_id' => $user_id,
2651 ]
2652 );
2653
2654 // After inserting into Non Active , now call Macro11
2655 $macro_11_return_value = $this->Macro11();
2656 }
2657 }
2658 }
2659
2660 if ( $macro_11_return_value == 0 ) {
2661 //Cleaning Process
2662 $this->Macro6();
2663 //Inserting Temp Order List
2664 $this->Macro8();
2665
2666 //Inserting Temp Order Book
2667 $this->Macro9();
2668
2669 // The routine check whether matching is possible
2670
2671 $this->Macro17();
2672 }
2673
2674 }
2675
2676 } catch ( Exception $e ) {
2677 print( '<br>Exception in Macro12 <br>' );
2678 print( '<br>Line # ' . $e->getLine() . "<br>" );
2679 print( '<br>Message # ' . $e->getLine() . "<br>" );
2680 }
2681 }
2682
2683 public function submitBid( Request $request ) {
2684 $logged_in_id = 0;
2685 if ( ! Auth::check() ) {
2686 return redirect( '/login' );
2687 exit();
2688 }
2689 $logged_in_id = Auth::user()->id;
2690 $this->currency = $request->get( 'currency' );
2691 $this->buy_sell = $request->get( 'buy_sell' );
2692 $this->desired_sil = $request->get( 'desired_sil' );
2693 $this->price = $request->get( 'price' );
2694 $this->order_type = $request->get( 'order_type' );
2695 $this->trigger_price_activating_stop_order = $request->get( 'trigger_price_activating_stop_order' );
2696 $this->duration = ( $request->get( 'duration' ) == null ? '1970-01-01' : $request->get( 'duration' ) );
2697
2698 if ( $this->order_type != 'stop_order' ) {
2699 $this->status = 'open';
2700 } else {
2701 $this->status = 'pending';
2702 }
2703 $this->split_id = '1';
2704 $this->levelling_ratio = 1;
2705 $this->LMM_percentage = 0.9;
2706 $this->entry_date = date( 'Y-m-d' );
2707 $this->entry_time = date( 'h:i:s' );
2708 print " <pre>";
2709 print_r( $request->all() );
2710 print " </pre > ";
2711
2712
2713 //DB::transaction( function () {
2714 /**
2715 * Enter the newly inserted Order into Original List Of Orders
2716 */
2717
2718 $logged_in_id = Auth::user()->id;
2719 $currency = $this->currency;
2720 $buy_sell = $this->buy_sell;
2721 $desired_sil = $this->desired_sil;
2722 $price = $this->price;
2723 $order_type = $this->order_type;
2724 $trigger_price_activating_stop_order = $this->trigger_price_activating_stop_order;
2725 $duration = ( $this->duration == null ? '1970-01-01' : $this->duration );
2726 $split_id = '1';
2727 $levelling_ratio = 1;
2728 $LMM_percentage = 0.9;
2729 $status = $this->status;
2730 $entry_date = date( 'Y-m-d' );
2731 $entry_time = date( 'h:i:s' );
2732
2733 /**
2734 * We have to find the max Order number in List of All Order, List of Non Active and Stop Order
2735 */
2736 $max_order_values = [];
2737 $new_order_number = - 1;
2738
2739 $max_list_all_orders = ListAllOrders::max( 'order_number' );
2740 $max_list_non_active_orders = ListNonActiveOrders::max( 'order_number' );
2741 $max_list_stop_orders = StopOrderList::max( 'order_number' );
2742
2743 $max_order_values[] = $max_list_all_orders;
2744 $max_order_values[] = ( $max_list_non_active_orders == null ? 0 : $max_list_non_active_orders );
2745 $max_order_values[] = ( $max_list_stop_orders == null ? 0 : $max_list_stop_orders );
2746 $last_order_number = max( $max_order_values );
2747 $new_order_number = intval( $last_order_number + 1 );
2748
2749 OrginalListOrders::create(
2750 [
2751 'order_number' => $new_order_number,
2752 'currency' => $currency,
2753 'buy_sell' => $buy_sell,
2754 'entry_date' => $entry_date,
2755 'entry_time' => $entry_time,
2756 'desired_sil' => $desired_sil,
2757 'price' => $price,
2758 'order_type' => $order_type,
2759 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
2760 'duration' => $duration,
2761 'status' => $status,
2762 'split_id' => $split_id,
2763 'levelling_ratio' => $levelling_ratio,
2764 'LMM_percentage' => $LMM_percentage,
2765 'user_id' => $logged_in_id,
2766 ]
2767 );
2768 $extreme_price_cancellation = 0;
2769 $price_mark_down = 0;
2770 $price_mark_up = 0;
2771
2772 $variables = Variables::first();
2773
2774 if ( $variables->exists() ) {
2775 $this->extreme_price_cancellation = $variables->extreme_price_cancellation;
2776 $this->price_mark_down = $variables->price_mark_down;
2777 $this->price_mark_up = $variables->price_mark_up;
2778 }
2779
2780
2781 if ( $buy_sell == 'B' ) {
2782 $price = floatval( $price * $this->price_mark_down );
2783 } elseif ( $buy_sell == 'S' ) {
2784 $price = floatval( $price * $this->price_mark_up );
2785 }
2786
2787 NewOrderEnters::truncate();
2788
2789
2790 NewOrderEnters::create(
2791 [
2792 'order_number' => $new_order_number,
2793 'currency' => $currency,
2794 'buy_sell' => $buy_sell,
2795 'entry_date' => $entry_date,
2796 'entry_time' => $entry_time,
2797 'desired_sil' => $desired_sil,
2798 'price' => $price,
2799 'order_type' => $order_type,
2800 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
2801 'duration' => $duration,
2802 'status' => $status,
2803 'split_id' => $split_id,
2804 'levelling_ratio' => $levelling_ratio,
2805 'LMM_percentage' => $LMM_percentage,
2806 'user_id' => $logged_in_id,
2807 ]
2808 );
2809
2810 print( '<br> New Order Entered <br>' );
2811 $this->Macro12();
2812 //} );
2813
2814 }
2815}