· 6 years ago · Sep 04, 2019, 09:58 AM
1<?php
2
3namespace App\Http\Controllers;
4
5use App\BuyMatchedOrder;
6use App\BuyOrdersRankByPriceNb;
7use App\CashSILOwnership;
8use App\FinalBuyMatchedOrder;
9use App\FinalSellMatchedOrder;
10use App\LastMatchedPriceQuantity;
11use App\ListAllOrders;
12use App\ListNonActiveOrders;
13use App\NewOrderEnters;
14use App\OrderBookBuy;
15use App\OrderBookSell;
16use App\Orders;
17use App\OrginalListOrders;
18use App\SellMatchedOrder;
19use App\SellOrdersRankByPriceNb;
20use App\StopOrderList;
21use App\TempListOrders;
22use App\TemporaryOrderBookBuy;
23use App\TemporaryOrderBookSell;
24use App\Variables;
25use Carbon\Carbon;
26use Illuminate\Http\Request;
27use Illuminate\Support\Facades\Auth;
28use Illuminate\Support\Facades\DB;
29use Illuminate\Support\Facades\Validator;
30use League\Flysystem\Exception;
31use phpDocumentor\Reflection\Types\Array_;
32
33
34//SELECT x.bid_price,y.ask_price
35//FROM
36//(SELECT * from order_book_buy) as x, (SELECT * FROM order_book_sell) as y
37
38class OrderController extends Controller {
39
40 // Private Form field variables
41 private $currency;
42 private $buy_sell;
43 private $desired_sil;
44 private $price;
45 private $order_type;
46 private $trigger_price_activating_stop_order;
47 private $duration;
48 private $status;
49 private $split_id;
50 private $levelling_ratio;
51 private $LMM_percentage;
52 private $entry_date;
53 private $entry_time;
54
55 private $extreme_price_cancellation;
56 private $price_mark_down;
57 private $price_mark_up;
58
59 public function __construct( ) {
60 $variables = Variables::first();
61
62 if ( $variables->exists() ) {
63 $this->extreme_price_cancellation = $variables->extreme_price_cancellation;
64 $this->price_mark_down = $variables->price_mark_down;
65 $this->price_mark_up = $variables->price_mark_up;
66 }
67 }
68 public function trading() {
69 return view( 'items/trading' );
70 }
71
72 public function get_order_book() {
73 $mark_up = $this->price_mark_up;
74 $mark_down = $this->price_mark_down;
75 $buy_order_book_obj = OrderBookBuy::orderby('bid_price','DESC')->get();
76 $sell_order_book_obj = SellMatchedOrder::orderby('bid_price','ASC')->get();
77 dd($sell_order_book_obj);
78 }
79
80 public function create_order() {
81 return view( 'items/create_order' );
82 }
83
84 private function macro_update_user_liabilities_for_specific_user(
85 $user_id,
86 $list_of_orders_obj,
87 $list_of_stop_orders_obj
88 ) {
89 // purpose of the macro is to update the table CashSILOwnership for ONE SPECIFIC USER= $user_id, fields:
90 // cash_to_be_paid_order_matched, representing the cash to be paid should all the buy orders of that user be matched
91 // sil_to_be_given_order_matched, representing the SIL to be given away should all the sell orders of that user be
92 // matched and sil_to_be_obtained_order_matched, representing the SIL to be obtained should all the buy orders of
93 // that user be matched
94
95 print( "<br> running macro_update_user_liabilities_for_specific_user for user : " . $user_id . "<br>" );
96 $cash_to_be_paid_order_matched_user = 0;
97 $sil_to_be_given_order_matched_user = 0;
98 $sil_to_be_obtained_order_matched_user = 0;
99 print ( " looking at list of all orders " );
100
101// $list_of_order_table = ListAllOrders::where( 'order_number', '>', 0 );
102// $list_of_orders_obj = null;
103
104 if ( $list_of_orders_obj != null ) {
105 //$list_of_orders_obj = $list_of_order_table->get();
106
107 foreach ( $list_of_orders_obj as $list_of_order_obj ) {
108 $user_id_in_list_orders = $list_of_order_obj->user_id;
109 $number_sil_user = $list_of_order_obj->desired_sil;
110 $buy_sell = $list_of_order_obj->buy_sell;
111 $order_number_user = $list_of_order_obj->order_number;
112
113 if ( $user_id_in_list_orders == $user_id ) {
114
115 If ( $buy_sell == 'B' ) {
116 print ( ' am in if buy_sell == B ' );
117 print( "<br> <h3> user_id: " . $user_id . "</h3><br>" );
118 print( "<br> order_number_user: " . $order_number_user . "<br>" );
119 print( "<br> number_sil_user: " . $number_sil_user . "<br>" );
120 $price_user_original = floatval( $list_of_order_obj->price / $this->price_mark_down );
121 $price_user_original = round( $price_user_original, 2 );
122
123// print('<br><b>Original Price Beofre = </b>'.$price_user_original."<br>");
124
125 // order from user is a buy order
126 // we update cash to be paid should the order be matched by looking at the price of that order in table
127// $original_list_of_orders_obj = OrginalListOrders::where( 'order_number',
128// $order_number_user )->first();
129//
130// if ( $original_list_of_orders_obj != null ) {
131// $price_user_original = $original_list_of_orders_obj->price;
132// print('<br><b>Original Price After = </b>'.$price_user_original."<br>");
133// } //end for if ( $original_list_of_orders_obj != null )
134// dd('Chal bay');
135 print( "<br> price_user_original in original list of order" . $price_user_original . "<br>" );
136 $cash_to_be_paid_order_matched_user_spcific_trade = $price_user_original * $number_sil_user;
137 print( "<br> cash_to_be_paid_order_matched_user_spcific_trade: " . $cash_to_be_paid_order_matched_user_spcific_trade . "<br>" );
138
139 $cash_to_be_paid_order_matched_user = $cash_to_be_paid_order_matched_user + $cash_to_be_paid_order_matched_user_spcific_trade;
140
141 print( "<br> cash_to_be_paid_order_matched_user: " . $cash_to_be_paid_order_matched_user . "<br>" );
142
143 // SIL update
144 $sil_to_be_obtained_order_matched_user = $sil_to_be_obtained_order_matched_user + $number_sil_user;
145 print( "<br> sil_to_be_obtained_order_matched_user: " . $sil_to_be_obtained_order_matched_user . "<br>" );
146
147 } elseif ( $buy_sell == "S" ) {
148 // order from user is a sell order
149
150 print ( " am in if buy_sell == S " );
151 print( "<br> order_number_user: " . $order_number_user . "<br>" );
152 print( "<br> number_sil_user: " . $number_sil_user . "<br>" );
153
154 $sil_to_be_given_order_matched_user = $sil_to_be_given_order_matched_user + $number_sil_user;
155 print( "<br> sil_to_be_given_order_matched_user: " . $sil_to_be_given_order_matched_user . "<br>" );
156 } // end of elseif ( $buy_sell == "S" )
157 } // end of if ($user_id_in_list_orders == $user_id
158 } // end of for each
159 } // end of if ( $list_of_order_table ->exists()
160
161 print ( "<br> looking at Stop order list <br>" );
162// $list_of_stop_order_table = StopOrderList::where( 'order_number', '>', 0 );
163// $list_of_stop_orders_obj = null;
164
165 if ( $list_of_stop_orders_obj != null ) {
166 //$list_of_stop_orders_obj = $list_of_stop_order_table->get();
167
168 foreach ( $list_of_stop_orders_obj as $list_of_stop_order_obj ) {
169 $user_id_in_list_orders = $list_of_stop_order_obj->user_id;
170 $number_sil_user = $list_of_stop_order_obj->desired_sil;
171 $buy_sell = $list_of_stop_order_obj->buy_sell;
172 $order_number_user = $list_of_stop_order_obj->order_number;
173
174 if ( $user_id_in_list_orders == $user_id ) {
175 if ( $buy_sell == 'B' ) {
176 print ( " am in if buy_sell == B case Stop order list " );
177 print( "<br> user_id " . $user_id . "<br>" );
178 print( "<br> order_number_user: " . $order_number_user . "<br>" );
179 print( "<br> number_sil_user: " . $number_sil_user . "<br>" );
180
181 $price_user_original = floatval( $list_of_stop_order_obj->price / $this->price_mark_down );
182 $price_user_original = round( $price_user_original, 2 );
183
184 // order from user is a buy order
185 // we update cash to be paid should the order be matched by looking at the price of that order in table
186
187// $original_list_of_orders_obj = OrginalListOrders::where( 'order_number',
188// $order_number_user )->first();
189//
190// if ( $original_list_of_orders_obj != null ) {
191// $price_user_original = $original_list_of_orders_obj->price;
192// }
193
194 print( "<br> price in Original List Of Order: " . $price_user_original . "<br>" );
195 $cash_to_be_paid_order_matched_user_spcific_trade = $price_user_original * $number_sil_user;
196
197 print( "<br> cash_to_be_paid_order_matched_user_spcific_trade: " . $cash_to_be_paid_order_matched_user_spcific_trade . "<br>" );
198
199 $cash_to_be_paid_order_matched_user = $cash_to_be_paid_order_matched_user + $cash_to_be_paid_order_matched_user_spcific_trade;
200 print( "<br> cash_to_be_paid_order_matched_user " . $cash_to_be_paid_order_matched_user . "<br>" );
201
202 // SIL update
203 $sil_to_be_obtained_order_matched_user = $sil_to_be_obtained_order_matched_user + $number_sil_user;
204 print( "<br> sil_to_be_obtained_order_matched_user: " . $sil_to_be_obtained_order_matched_user . "<br>" );
205 } elseif ( $buy_sell == 'S' ) {
206 // order from user is a sell order
207 print ( " am in if buy_sell == S case Stop order list " );
208 print( "<br> order_number_user: " . $order_number_user . "<br>" );
209 print( "<br> number_sil_user: " . $number_sil_user . "<br>" );
210
211 $sil_to_be_given_order_matched_user = $sil_to_be_given_order_matched_user + $number_sil_user;
212 print( "<br> sil_to_be_given_order_matched_user: " . $sil_to_be_given_order_matched_user . "<br>" );
213 } // end of elseif ( $buy_sell == "S" )
214 } // end of if ($user_id_in_list_orders == $user_id
215 } // end of for each
216 } // end of if ( $list_of_stop_order_table ->exists()
217
218 // update table CashSILOwnership for user_id
219 $cash_sil_Ownership_table = CashSILOwnership::where( 'user_id', $user_id );
220
221
222 if ( $cash_sil_Ownership_table->exists() ) {
223
224 $cash_sil_Ownership_table->update( [
225 'cash_to_be_paid_order_matched' => $cash_to_be_paid_order_matched_user,
226 'sil_to_be_given_order_matched' => $sil_to_be_given_order_matched_user,
227 'sil_to_be_obtained_order_matched' => $sil_to_be_obtained_order_matched_user,
228 ] );
229
230 print( '<br><b>Printing value after the update for the user = </b> ' . $user_id . "<br>" );
231
232 print( "<br> cash_to_be_paid_order_matched_user inserted in CashSILOwnership " . $cash_to_be_paid_order_matched_user . "<br>" );
233 print( "<br> sil_to_be_given_order_matched_user inserted in CashSILOwnership " . $sil_to_be_given_order_matched_user . "<br>" );
234 print( "<br> sil_to_be_obtained_order_matched_user inserted in CashSILOwnership " . $sil_to_be_obtained_order_matched_user . "<br>" );
235 }
236 } //End of Macro
237
238 private function macro_update_user_liabilities() {
239 print( '<br> <b>Calling macro_update_user_liabilities </b> <br>' );
240 // purpose of the macro is to update the table CashSILOwnership for the users where status is different than zero
241 // for those there has been a change in their ownership of SIL
242 // the macro will update the following fields in the table CashSILOwnership:
243 // cash_to_be_paid_order_matched, representing the cash to be paid should all the buy orders of a given user be //matched
244 // sil_to_be_given_order_matched, representing the SIL to be given away should all the sell orders of a given user //be
245 // matched and sil_to_be_obtained_order_matched, representing the SIL to be obtained should all the buy orders of a
246 // user be matched
247
248 $cash_sil_Ownerships_obj = null;
249 $cash_sil_Ownership_table = CashSILOwnership::where( 'id', '>', 0 );
250
251 if ( $cash_sil_Ownership_table->exists() ) {
252 $cash_sil_Ownerships_obj = $cash_sil_Ownership_table->get();
253 $list_orders_array = [];
254
255 $list_of_order_table = ListAllOrders::where( 'order_number', '>', 0 );
256 $list_of_stop_order_table = StopOrderList::where( 'order_number', '>', 0 );
257
258 foreach ( $cash_sil_Ownerships_obj as $cash_sil_Ownership_obj ) {
259 $user_id = $cash_sil_Ownership_obj->user_id;
260 print( "<br> user_id : " . $user_id . "<br>" );
261 $this->macro_update_user_liabilities_for_specific_user( $user_id, $list_of_order_table->get(),
262 $list_of_stop_order_table->get() );
263 } // end of for each
264
265 } // end of if ( $cash_sil_Ownership_table->exists()
266 } // end of macro
267
268
269 private function macro_cash_SIL_ownership() {
270 // purpose of the macro is to update the table CashSILOwnership showing the cash and SIL ownership for each users
271 // including Safe Silver
272 $order_matching_process_buy = null;
273 $buy_matched_order_obj = null;
274 $buyer_user_id = null;
275 $order_matching_process_buy = null;
276 $new_order_id_buy = null;
277 $seller_user_id = null;
278 $order_matching_process_sell = null;
279 $new_order_id_sell = null;
280
281 $sell_matched_order_obj_temp = SellMatchedOrder::all();
282 $buy_matched_order_obj_temp = BuyMatchedOrder::all();
283
284 print( ' <br> <b > Copy of SellMatchedOrder </b > <br > ' );
285
286// foreach ( $sell_matched_order_obj_temp as $record ) {
287// print "<br> <code>";
288// print "<br>User ID = " . $record->sell_user_id . "<br>";
289// print "<br>Sell_quantity = " . $record->sell_quantity . "<br>";
290// print "<br>Sell_Order ID = " . $record->sell_order_id . "<br>";
291// print "<br> </code>";
292// }
293
294 print( ' <br > <b > Copy of BuyMatchedOrder </b > <br > ' );
295
296// foreach ( $buy_matched_order_obj_temp as $record ) {
297// print "<br> <code>";
298// print "<br>User ID = " . $record->buy_user_id . "<br>";
299// print "<br>buy_quantity = " . $record->buy_quantity . "<br>";
300// print "<br>buy_Order ID = " . $record->buy_order_id . "<br>";
301// print "<br> </code>";
302// }
303
304
305 print( '<br > <h2 > Calling macro_cash_SIL_ownership </h2 ><br > ' );
306 // fetch data from table buy_matched_order
307 $buy_matched_order_table = BuyMatchedOrder::orderBy( 'id', 'asc' )->limit( 1 );
308 $buy_matched_order_obj = null;
309 $sell_matched_order_obj = null;
310
311 if ( $buy_matched_order_table->exists() ) {
312 $buy_matched_order_obj = $buy_matched_order_table->first();
313 $buyer_user_id = $buy_matched_order_obj->buy_user_id;
314 $order_matching_process_buy = $buy_matched_order_obj->matching_process;
315 $new_order_id_buy = $buy_matched_order_obj->buy_order_id;
316
317 print( "<br> order_matching_process_buy: " . $order_matching_process_buy . "<br>" );
318 print( "<br> new_order_id_buy: " . $new_order_id_buy . "<br>" );
319 print( "<br> buyer_user_id: " . $buyer_user_id . "<br>" );
320 }
321
322 // fetch data from table Sell_matched_order
323 $sell_matched_order_table = SellMatchedOrder::orderBy( 'id', 'asc' )->limit( 1 );
324
325 if ( $sell_matched_order_table->exists() ) {
326 $sell_matched_order_obj = $sell_matched_order_table->first();
327 $seller_user_id = $sell_matched_order_obj->sell_user_id;
328 $order_matching_process_sell = $sell_matched_order_obj->matching_process;
329 $new_order_id_sell = $sell_matched_order_obj->sell_order_id;
330 print( "<br> order_matching_process_buy: " . $order_matching_process_sell . "<br>" );
331 print( "<br> new_order_id_sell: " . $new_order_id_sell . "<br>" );
332 print( "<br> seller_user_id: " . $seller_user_id . "<br>" );
333
334 }
335
336 // check if new order is buy or sell ?
337 if ( $order_matching_process_buy == "TOP_ORDER_BUY" ||
338 $order_matching_process_buy == "LMM_BUY" ||
339 $order_matching_process_buy == "FIFO_FIRST_PASS_BUY" ||
340 $order_matching_process_buy == "PRO_RATA_BUY" ||
341 $order_matching_process_buy == "FIFO_FINAL_PASS_BUY"
342 ) {
343 // new order is a buy order
344 $order_type = "B";
345 $new_order_id = $new_order_id_buy;
346
347 } else {
348 $order_type = "S";
349 $new_order_id = $new_order_id_sell;
350
351 }
352
353 print( "<br> order_type: " . $order_type . "<br>" );
354 print( "<br> new_order_id: " . $new_order_id . "<br>" );
355 $quantity_matched_new_order = 0;
356
357 # Rest Cash SIL Status
358 //( new CashSILOwnership() )->update( [ 'status' => 0 ] );
359 $affected = DB::table( 'cash_sil_ownership' )->update( array( 'status' => 0 ) );
360
361 $tmp_cash_sil = CashSILOwnership::all();
362 print( ' <br> PRINTING STATUS <br>' );
363
364// foreach ( $tmp_cash_sil as $item ) {
365//
366// print( ' <br>' . $item->user_id . ' ' . $item->status . ' <br>' );
367// }
368
369 //whether new order is B or S we should always update table CashSILOwnership starting with matched sell orders
370 //(where Safe Silver is the buyer and pays to the seller)
371 // CASE WHEN NEW ORDER IS SELL *********************************
372
373 if ( $order_type == "S" ) {
374 // new order is sell order and in that case we aggregate the “desired_sil” in table sell matched orders.
375 // The aggregated value is $quantity_matched_new_order. In this case the new order is a sell order and in table
376 // SellMatchedOrder there is only one order with same price but different desired_sil.
377 // We should have $quantity_matched_new_order = sum of desired_sil in table sell matched orders
378
379 print ( " <br> <b>CASE When Order Type is SELL</b> <br>" );
380 $sell_matched_order_table = SellMatchedOrder::groupBy( 'ask_price' )
381 ->select( 'sell_price as ask_price',
382 DB::raw( 'sum( sell_quantity ) as quantity_matched_new_order' ) );
383
384
385 if ( $sell_matched_order_table->exists() ) {
386 $sell_matched_order_obj = $sell_matched_order_table->first();
387 $quantity_matched_new_order = $sell_matched_order_obj->quantity_matched_new_order;
388
389 $cash_SIL_Ownership_table = CashSILOwnership::where( 'user_id', $seller_user_id );
390
391 if ( $cash_SIL_Ownership_table->exists() ) {
392
393 $sell_matched_order_obj = $cash_SIL_Ownership_table->first();
394 $current_SIL_available_seller = $sell_matched_order_obj->number_of_sil_owned;
395 $current_status = $sell_matched_order_obj->status;
396
397 print( "<br> <font color='red'> current_SIL_available_seller: " . $current_SIL_available_seller . "</font><br>" );
398
399 // then calculated the revised number of SIL after selling
400 $new_SIL_available_seller = intval( $current_SIL_available_seller - $quantity_matched_new_order );
401
402 $new_status = $current_status - $quantity_matched_new_order;
403 print( '<br> NEW STATUS INSIDE = ' . $new_status . "<br>" );
404
405 // then update SIL owned in table CashSILOwnership for user_id
406 $cash_SIL_Ownership_table->update( [
407 'number_of_sil_owned' => $new_SIL_available_seller,
408 'status' => $new_status
409 ] );
410
411 print( ' <br> STATUS UPDATED for seller ID = ' . $seller_user_id . "<br>" );
412
413 print( "<br> new_SIL_available_seller: " . $new_SIL_available_seller . "<br>" );
414 }
415 }
416
417 // record more SIL to Safe Silver (whose user_id=1) as intermediary buyer
418 // first fetch data from Safe Silver we need the number of SIL recorded in table CashSILOwnership for Safe Silver
419
420 //$cash_SIL_Ownership_obj = CashSILOwnership::where( 'user_id', 1 )->first();
421
422// if ( $cash_SIL_Ownership_obj != null ) {
423// $current_Safe_Silver_available_SIL = $cash_SIL_Ownership_obj->number_of_sil_owned;
424// print( "<br> current_Safe_Silver_available_SIL: " . $current_Safe_Silver_available_SIL . "<br>" );
425//
426// // then calculate the revised number of SIL after Safe Silver is buying from Seller
427// $new_Safe_Silver_available_SIL = $current_Safe_Silver_available_SIL + $quantity_matched_new_order;
428// // then update SIL owned in table CashSILOwnership for Safe Silver
429//
430// // TO DO: Make sure to update the status field to find out which user is updated
431//
432// $cash_SIL_Ownership_obj->update( [
433// 'number_of_sil_owned' => $new_Safe_Silver_available_SIL,
434//// 'status' => 1
435// ] );
436// }
437//
438// print( "<br> new_Safe_Silver_available_SIL: " . $new_Safe_Silver_available_SIL . "<br>" );
439
440 //At THIS STAGE LAUNCH PROCESS TO UPDATE SILL OWNERSHIP ON BLOCKCHAIN FOR SELLER $seller_user_id (REDUCE HIS NUMBER
441 //OF SIL BY $quantity_matched_new_order) AND SAFE SILVER (INCREASE NUMBER OF SIL BY $quantity_matched_new_order).
442 // EXCLUDE UPDATE ON BLOCKCHAIN IF SELLER IS SAFE SILEVR ITSELF ($seller_user_id = Safe Silver).
443
444 // update Cash ownership in table CashSILOwnership
445 // first find original sell price in table OrginalListOrders
446
447 $original_list_of_orders_obj = OrginalListOrders::where( 'order_number',
448 $new_order_id )->first();
449 $original_sell_price = 0;
450
451 if ( $original_list_of_orders_obj != null ) {
452 $original_sell_price = $original_list_of_orders_obj->price;
453 }
454 //$cash_sell_transaction is the cash the seller will get:
455 $cash_sell_transaction = $original_sell_price * $quantity_matched_new_order;
456 print( "<br> original_sell_price: " . $original_sell_price . "<br>" );
457 print( "<br> cash_sell_transaction: " . $cash_sell_transaction . "<br>" );
458
459 // record more cash to selling user
460 // first fetch data from seller we need the amount of cash recorded in table CashSILOwnership for user_id
461 $cash_SIL_Ownership_table = CashSILOwnership::where( 'user_id', $seller_user_id );
462
463 if ( $cash_SIL_Ownership_table->exists() ) {
464
465 $cash_SIL_Ownership_obj = $cash_SIL_Ownership_table->first();
466 $current_Cash_available_seller = $cash_SIL_Ownership_obj->cash_available_on_the_account;
467 print( "<br> current_Cash_available_seller: " . $current_Cash_available_seller . "<br>" );
468
469 // then calculate the increased amount of cash after selling
470 $new_Cash_available_seller = $current_Cash_available_seller + $cash_sell_transaction;
471
472 // then update cash owned in table CashSILOwnership for user_id
473 $cash_SIL_Ownership_table->update( [
474 'cash_available_on_the_account' => $new_Cash_available_seller
475 ] );
476
477 print( "<br> new_Cash_available_seller: " . $new_Cash_available_seller . "<br>" );
478 }
479
480
481 // record less cash to Safe Silver as intermediary buyer
482 // first fetch data from Safe Silver (whose user_id=1) we need the amount of cash recorded in table CashSILOwnership for Safe Silver
483 $cash_SIL_Ownership_table = CashSILOwnership::where( 'user_id', 1 );
484
485
486 if ( $cash_SIL_Ownership_table->exists() ) {
487 $cash_SIL_Ownership_obj = $cash_SIL_Ownership_table->first();
488 $current_Safe_Silver_available_Cash = $cash_SIL_Ownership_obj->cash_available_on_the_account;
489 print( "<br> current_Safe_Silver_available_Cash: " . $current_Safe_Silver_available_Cash . "<br>" );
490 // then calculated the reduced amount of cash after buying from seller
491 $new_Safe_Silver_available_Cash = $current_Safe_Silver_available_Cash - $cash_sell_transaction;
492 // then update cash owned in table CashSILOwnership for Safe Silver
493 $cash_SIL_Ownership_table->update( [
494 'cash_available_on_the_account' => $new_Safe_Silver_available_Cash,
495 ] );
496 }
497 print( "<br> new_Safe_Silver_available_Cash: " . $new_Safe_Silver_available_Cash . "<br>" );
498 // Then we need to update each corresponding buyer one by one from table BuyMatchedOrder
499 $buy_matched_order_table = BuyMatchedOrder::where( 'buy_order_id', '>', 0 );
500 $buy_matched_orders_obj = null;
501
502 if ( $buy_matched_order_table->exists() ) {
503 $buy_matched_orders_obj = $buy_matched_order_table->get();
504 print( '<br> Accesing Buy Matched Order Details <br>' );
505
506 foreach ( $buy_matched_orders_obj as $buy_matched_order_obj ) {
507 $buyer_order_number = $buy_matched_order_obj->buy_order_id;
508 $buyer_user_id = $buy_matched_order_obj->buy_user_id;
509 $quantity_matched_buyer = $buy_matched_order_obj->buy_quantity;
510
511 print( "<br> buyer_order_number " . $buyer_order_number . "<br>" );
512 print( "<br> buyer_user_id " . $buyer_user_id . "<br>" );
513 print( "<br> quantity_matched_buyer " . $quantity_matched_buyer . "<br>" );
514 // update sill ownership
515 // record more SIL to buying user
516 // first fetch data from buyer we need the number of SIL recorded in table CashSILOwnership for $buyer_user_id
517 $cash_SIL_Ownership_table = CashSILOwnership::where( 'user_id', $buyer_user_id );
518
519
520 if ( $cash_SIL_Ownership_obj != null ) {
521 $cash_SIL_Ownership_obj = $cash_SIL_Ownership_table->first();
522 $current_SIL_available_buyer = $cash_SIL_Ownership_obj->number_of_sil_owned;
523 $current_status = $cash_SIL_Ownership_obj->status;
524
525 print( "<br> current_SIL_available_buyer: " . $current_SIL_available_buyer . "<br>" );
526 // then calculated the revised number of SIL after buying
527
528 $new_SIL_available_buyer = $current_SIL_available_buyer + $quantity_matched_buyer;
529
530 $new_status = $current_status + $quantity_matched_buyer;
531
532 // then update SIL owned in table CashSILOwnership for $buyer_user_id
533 $cash_SIL_Ownership_table->update( [
534 'number_of_sil_owned' => $new_SIL_available_buyer,
535 'status' => $new_status
536 ] );
537
538 print( ' <br> STATUS UPDATED for BUYER ID = ' . $buyer_user_id . "<br>" );
539
540 print( "<br> new_SIL_available_buyer with new Status: " . $new_SIL_available_buyer . "<br>" );
541
542 print( ' <br>The Updated User Id = ' . $buyer_user_id . "<br>" );
543 }
544
545 // record less SIL to Safe Silver as intermediary seller
546 // first fetch data from Safe Silver (whose user_id=1) we need the number of SIL recorded in table CashSILOwnership for Safe Silver
547// $cash_SIL_Ownership_obj = CashSILOwnership::where( 'user_id', 1 )->first();
548//
549// if ( $cash_SIL_Ownership_obj != null ) {
550//
551// $current_Safe_Silver_available_SIL = intval( $cash_SIL_Ownership_obj->number_of_sil_owned );
552// print( "<br> current_Safe_Silver_available_SIL: " . $current_Safe_Silver_available_SIL . "<br>" );
553// // then calculate the revised number of SIL after Safe Silver is selling to buyer
554// $new_Safe_Silver_available_SIL = $current_Safe_Silver_available_SIL - $quantity_matched_buyer;
555// // then update SIL owned in table CashSILOwnership for Safe Silver
556// $cash_SIL_Ownership_obj->update( [
557// 'number_of_sil_owned' => $new_Safe_Silver_available_SIL,
558// ] );
559// }
560// print( "<br> new_Safe_Silver_available_SIL: " . $new_Safe_Silver_available_SIL . "<br>" );
561 //At THIS STAGE LAUNCH PROCESS TO UPDATE SILL OWNERSHIP ON BLOCKCHAIN FOR BUYER $buyer_user_id (INCREASE HIS NUMBER
562 //OF SIL BY $quantity_matched_buyer) AND SAFE SILVER (DECREASE NUMBER OF SIL BY $quantity_matched_buyer).
563 //EXCLUDE UPDATE ON BLOCKCHAIN IF BUYER IS SAFE SILEVR ITSELF ($buyer_user_id= Safe Silver)
564
565
566 // update Cash ownership in table CashSILOwnership
567 // first find original buy price in table OrginalListOrders
568 $original_buy_price = 0;
569 $original_list_of_orders_obj = OrginalListOrders::where( 'order_number',
570 $buyer_order_number )->first();
571
572 if ( $original_list_of_orders_obj != null ) {
573 $original_buy_price = $original_list_of_orders_obj->price;
574 }
575
576 // this is the cash the buyer will pay:
577 $cash_buy_transaction = $original_buy_price * $quantity_matched_buyer;
578 print( "<br> original_buy_price: " . $original_buy_price . "<br>" );
579 print( "<br> cash_buy_transaction: " . $cash_buy_transaction . "<br>" );
580 // record less cash to buying user
581 // first fetch data from buyer we need the amount of cash recorded in table CashSILOwnership for user_id
582 $cash_SIL_Ownership_table = CashSILOwnership::where( 'user_id', $buyer_user_id );
583
584 if ( $cash_SIL_Ownership_obj != null ) {
585 $cash_SIL_Ownership_obj = $cash_SIL_Ownership_table->first();
586 $current_Cash_available_buyer = $cash_SIL_Ownership_obj->cash_available_on_the_account;
587 print( "<br> current_Cash_available_buyer: " . $current_Cash_available_buyer . "<br>" );
588
589 // then calculate the decreased amount of cash after buying
590 $new_Cash_available_buyer = $current_Cash_available_buyer - $cash_buy_transaction;
591 // then update cash owned in table CashSILOwnership for buyer_user_id
592 $cash_SIL_Ownership_table->update( [
593 'cash_available_on_the_account' => $new_Cash_available_buyer,
594 ] );
595 }
596 print( "<br> new_Cash_available_buyer: " . $new_Cash_available_buyer . "<br>" );
597 // record more cash to Safe Silver as intermediary seller
598 // first fetch data from Safe Silver (whose user_id=1) we need the amount of cash recorded in table CashSILOwnership for Safe Silver
599 $cash_SIL_Ownership_table = CashSILOwnership::where( 'user_id', 1 );
600
601 if ( $cash_SIL_Ownership_table->exists() ) {
602 $cash_SIL_Ownership_obj = $cash_SIL_Ownership_table->first();
603 $current_Safe_Silver_available_Cash = $cash_SIL_Ownership_obj->cash_available_on_the_account;
604 print( "<br> current_Safe_Silver_available_Cash: " . $current_Safe_Silver_available_Cash . "<br>" );
605 // then calculate the increased amount of cash after selling to buyer acting as intermediary
606 $new_Safe_Silver_available_Cash = $current_Safe_Silver_available_Cash + $cash_buy_transaction;
607 // then update cash owned in table CashSILOwnership for Safe Silver
608
609 $cash_SIL_Ownership_table->update( [
610 'cash_available_on_the_account' => $new_Safe_Silver_available_Cash,
611 ] );
612 }
613 print( "<br> new_Safe_Silver_available_Cash: " . $new_Safe_Silver_available_Cash . "<br>" );
614 } // end for each $buy_matched_orders_obj
615 print( "<br> END OF CASE WHEN NEW ORDER IS SELL <br>" );
616 } // end of if ( $buy_matched_order_table -> exists()
617 } elseif ( $order_type == 'B' ) {
618
619 print( "<br>CASE WHEN NEW ORDER IS BUY<br>" );
620
621 // CASE WHEN NEW ORDER IS BUY *********************************
622 // we must first look at all the sell matched trades in table SellMatchedOrder.
623 $sell_matched_order_table = SellMatchedOrder::where( 'sell_order_id', '>', 0 );
624 $sell_matched_orders_obj = null;
625
626 if ( $sell_matched_order_table->exists() ) {
627 $sell_matched_orders_obj = $sell_matched_order_table->get();
628
629 foreach ( $sell_matched_orders_obj as $sell_matched_order_obj ) {
630 $seller_order_number = $sell_matched_order_obj->sell_order_id;
631 $seller_user_id = $sell_matched_order_obj->sell_user_id;
632 $quantity_matched_seller = $sell_matched_order_obj->sell_quantity;
633
634 print( "<br> seller_order_number: " . $seller_order_number . "<br>" );
635 print( "<br> seller_user_id: " . $seller_user_id . "<br>" );
636 print( "<br> quantity_matched_seller " . $quantity_matched_seller . "<br>" );
637 // update SIL ownership
638 // record less SIL to selling user
639 // first fetch data from seller we need the number of SIL recorded in table CashSILOwnership for $seller_user_id
640 $cash_SIL_Ownership_table = CashSILOwnership::where( 'user_id', $seller_user_id );
641 print( "<br> quantity_matched_new_order " . $quantity_matched_new_order . "<br>" );
642
643 if ( $cash_SIL_Ownership_table->exists() ) {
644 $cash_SIL_Ownership_obj = $cash_SIL_Ownership_table->first();
645 $current_SIL_available_seller = $cash_SIL_Ownership_obj->number_of_sil_owned;
646 $current_status = $cash_SIL_Ownership_obj->status;
647
648 print( "<br> current_SIL_available_seller: " . $current_SIL_available_seller . "<br>" );
649
650 // then calculate the revised number of SIL after selling
651 $new_SIL_available_seller = $current_SIL_available_seller - $quantity_matched_seller;
652 $new_status = $current_status - $quantity_matched_seller;
653 // then update SIL owned in table CashSILOwnership for $seller_user_id
654
655 $cash_SIL_Ownership_table->update( [
656 'number_of_sil_owned' => $new_SIL_available_seller,
657 'status' => $new_status
658 ] );
659
660 print( "<br> new_SIL_available_seller with status = 1: " . $new_SIL_available_seller . "<br>" );
661 print( ' <br>The USER ID = ' . $seller_user_id . "<br>" );
662 }
663
664 // record more SIL to Safe Silver as intermediary buyer
665 // first fetch data from Safe Silver (whose user_id=1) we need the number of SIL recorded in table CashSILOwnership for Safe Silver
666// $cash_SIL_Ownership_obj = CashSILOwnership::where( 'user_id', 1 )->first();
667//
668// if ( $cash_SIL_Ownership_obj != null ) {
669// $current_Safe_Silver_available_SIL = $cash_SIL_Ownership_obj->number_of_sil_owned;
670// print( "<br> current_Safe_Silver_available_SIL: " . $current_Safe_Silver_available_SIL . "<br>" );
671//
672// print( ' <br>Quantity Matched Order( in BUY):- ' . $quantity_matched_seller . "<br>" );
673// // then calculate the revised number of SIL after Safe Silver is buying from seller
674//
675// $new_Safe_Silver_available_SIL = $current_Safe_Silver_available_SIL + $quantity_matched_seller;
676// // then update SIL owned in table CashSILOwnership for Safe Silver
677// $cash_SIL_Ownership_obj->update( [
678// 'number_of_sil_owned' => $new_Safe_Silver_available_SIL,
679// ] );
680// }
681// print( "<br> new_Safe_Silver_available_SIL: " . $new_Safe_Silver_available_SIL . "<br>" );
682
683
684 //At THIS STAGE LAUNCH PROCESS TO UPDATE SILL OWNERSHIP ON BLOCKCHAIN FOR SELLER $seller_user_id (DECREASE HIS
685 //NUMBER OF SIL BY $quantity_matched_seller) AND SAFE SILVER (INCREASE NUMBER OF SIL BY $quantity_matched_seller).
686 //EXCLUDE UPDATE ON BLOCKCHAIN IF SELLER IS SAFE SILEVR ITSELF ($seller_user_id= Safe Silver)
687
688 // update Cash ownership in table CashSILOwnership
689 // first find original sell price in table OrginalListOrders
690 $original_list_of_orders_obj = OrginalListOrders::where( 'order_number',
691 $seller_order_number )->first();
692
693 $original_sell_price = 0;
694
695 if ( $original_list_of_orders_obj != null ) {
696 $original_sell_price = $original_list_of_orders_obj->price;
697 }
698 // this is the cash the buyer will pay
699 $cash_sell_transaction = $original_sell_price * $quantity_matched_seller;
700
701 print( "<br> original_sell_price: " . $original_sell_price . "<br>" );
702 print( "<br> cash_sell_transaction: " . $cash_sell_transaction . "<br>" );
703
704 // record more cash to selling user
705 // first fetch data from seller we need the amount of cash recorded in table CashSILOwnership for seller user_id
706 $cash_SIL_Ownership_table = CashSILOwnership::where( 'user_id', $seller_user_id );
707
708
709 if ( $cash_SIL_Ownership_obj != null ) {
710 $cash_SIL_Ownership_obj = $cash_SIL_Ownership_table->first();
711 $current_Cash_available_seller = $cash_SIL_Ownership_obj->cash_available_on_the_account;
712 print( "<br> current_Cash_available_seller: " . $current_Cash_available_seller . "<br>" );
713 // then calculated the increased amount of cash after selling
714 $new_Cash_available_seller = $current_Cash_available_seller + $cash_sell_transaction;
715 // then update cash owned in table CashSILOwnership for seller_user_id
716
717 $cash_SIL_Ownership_table->update( [
718 'cash_available_on_the_account' => $new_Cash_available_seller,
719 ] );
720
721 print( "<br> new_Cash_available_seller: " . $new_Cash_available_seller . "<br>" );
722
723 }
724
725
726 // record less cash to Safe Silver as intermediary buyer
727 // first fetch data from Safe Silver (whose user_id=1) we need the amount of cash recorded in table CashSILOwnership for Safe Silver
728 $cash_SIL_Ownership_table = CashSILOwnership::where( 'user_id', 1 );
729
730
731 if ( $cash_SIL_Ownership_obj != null ) {
732 $cash_SIL_Ownership_obj = $cash_SIL_Ownership_table->first();
733 $current_Safe_Silver_available_Cash = $cash_SIL_Ownership_obj->cash_available_on_the_account;
734 print( "<br> current_Safe_Silver_available_Cash: " . $current_Safe_Silver_available_Cash . "<br>" );
735 // then calculate the decreased amount of cash after buying from seller acting as intermediary
736 $new_Safe_Silver_available_Cash = $current_Safe_Silver_available_Cash - $cash_sell_transaction;
737
738 // then update cash owned in table CashSILOwnership for Safe Silver
739 $cash_SIL_Ownership_table->update( [
740 'cash_available_on_the_account' => $new_Safe_Silver_available_Cash,
741 ] );
742 }
743 print( "<br> new_Safe_Silver_available_Cash: " . $new_Safe_Silver_available_Cash . "<br>" );
744 print( "<br>END OF CASE WHEN NEW ORDER IS BUY<br>" );
745
746 } // end for each $sell_matched_orders_obj
747 } // end of if ( $sell_matched_order_table -> exists()
748
749 // Now we take care of the buy order
750 // Then since new order is buy order in that case we aggregate the quantity of SIL in table buy matched orders
751 // we aggregate the “desired_sil” in table buy matched orders.
752 // The aggregated value is $quantity_matched_new_order. In this case the new order is a buy order and in table
753 // BuyMatchedOrder there is only one order with same price but different desired_sil.
754 // We should have $quantity_matched_new_order = sum of desired_sil
755
756 $buy_matched_order_table = BuyMatchedOrder::groupBy( 'bid_price' )
757 ->select( 'buy_price as bid_price',
758 DB::raw( 'sum( buy_quantity ) as quantity_matched_new_order' )
759 );
760 if ( $buy_matched_order_table->exists() ) {
761 $buy_matched_order_obj = $buy_matched_order_table->first();
762 $quantity_matched_new_order = $buy_matched_order_obj->quantity_matched_new_order;
763
764 print( ' <br> Sum of quantity_matched_new_order = ' . $quantity_matched_new_order . "<br>" );
765
766 // update sill ownership
767 // record more SIL to buying user
768 // first fetch data from buyer we need the number of SIL recorded in table CashSILOwnership for user_id
769 $cash_SIL_Ownership_table = CashSILOwnership::where( 'user_id', $buyer_user_id );
770 $current_status = 0;
771
772 if ( $cash_SIL_Ownership_table->exists() ) {
773
774 $cash_SIL_Ownership_obj = $cash_SIL_Ownership_table->first();
775 $current_SIL_available_buyer = $cash_SIL_Ownership_obj->number_of_sil_owned;
776 $current_status = $cash_SIL_Ownership_obj->status;
777
778 print( ' <br> CURRENT STATUS = ' . $current_status . "<br>" );
779
780 print( "<br><font color='red'> current_SIL_available_buyer in BUY: " . $current_SIL_available_buyer . "</font><br>" );
781 $new_status = intval( $current_status + $quantity_matched_new_order );
782
783 print( ' <br> NEW STATUS = ' . $new_status . " AND USER ID = $buyer_user_id<br>" );
784 // then calculate the revised number of SIL after buying
785 $new_SIL_available_buyer = intval( $current_SIL_available_buyer + $quantity_matched_new_order );
786
787
788 // then update SIL owned in table CashSILOwnership for user_id
789 $cash_SIL_Ownership_table->update( [
790 'number_of_sil_owned' => $new_SIL_available_buyer,
791 'status' => $new_status
792 ] );
793 print( "<br><font color='green'> new_SIL_available_buyer: " . $new_SIL_available_buyer . "</font><br>" );
794 print( ' <br> BUYER USER ID = ' . $buyer_user_id . "<br>" );
795 }
796 }
797
798
799 // record less SIL to Safe Silver who acts as intermediary seller
800 // first fetch data from Safe Silver (whose user_id=1) we need the number of SIL recorded in table CashSILOwnership for Safe Silver
801 //$cash_SIL_Ownership_obj = CashSILOwnership::where( 'user_id', 1 )->first();
802
803
804// if ( $cash_SIL_Ownership_obj != null ) {
805// $current_Safe_Silver_available_SIL = intval( $cash_SIL_Ownership_obj->number_of_sil_owned );
806// print( "<br> current_Safe_Silver_available_SIL: " . $current_Safe_Silver_available_SIL . "<br>" );
807// // then calculate the revised number of SIL after Safe Silver is selling to buyer
808// $new_Safe_Silver_available_SIL = $current_Safe_Silver_available_SIL - $quantity_matched_new_order;
809// // then update SIL owned in table CashSILOwnership for Safe Silver
810// $cash_SIL_Ownership_obj->update( [
811// 'number_of_sil_owned' => $new_Safe_Silver_available_SIL,
812// ] );
813// }
814// print( "<br> new_Safe_Silver_available_SIL: " . $new_Safe_Silver_available_SIL . "<br>" );
815
816 //At THIS STAGE LAUNCH PROCESS TO UPDATE SILL OWNERSHIP ON BLOCKCHAIN FOR BUYER $buyer_user_id (INCREASE HIS NUMBER
817 //OF SIL BY $quantity_matched_new_order AND SAFE SILVER (DECREASE NUMBER OF SIL BY $quantity_matched_new_order).
818 // EXCLUDE UPDATE ON BLOCKCHAIN IF BUYER IS SAFE SILEVR ITSELF ($buyer_user_id = Safe Silver).
819 // update Cash ownership in table CashSILOwnership
820 // first find original buy price in table OrginalListOrders
821 $original_list_of_orders_obj = OrginalListOrders::where( 'order_number',
822 $new_order_id )->first();
823
824 if ( $original_list_of_orders_obj != null ) {
825
826 $original_buy_price = $original_list_of_orders_obj->price;
827 }
828 // this is the cash the buyer will pay:
829 $cash_buy_transaction = $original_buy_price * $quantity_matched_new_order;
830 print( "<br> original_buy_price: " . $original_buy_price . "<br>" );
831 print( "<br> cash_buy_transaction: " . $cash_buy_transaction . "<br>" );
832 // record less cash to buying user
833 // first fetch data from buyer we need the amount of cash recorded in table CashSILOwnership for user_id
834 $cash_SIL_Ownership_table = CashSILOwnership::where( 'user_id', $buyer_user_id );
835
836 if ( $cash_SIL_Ownership_table->exists() ) {
837
838 $cash_SIL_Ownership_obj = $cash_SIL_Ownership_table->first();
839 $current_Cash_available_buyer = $cash_SIL_Ownership_obj->cash_available_on_the_account;
840
841 print( "<br> current_Cash_available_buyer: " . $current_Cash_available_buyer . "<br>" );
842
843 // then calculated the decreased amount of cash after buying
844 $new_Cash_available_buyer = $current_Cash_available_buyer - $cash_buy_transaction;
845 // then update cash owned in table CashSILOwnership for user_id
846 $cash_SIL_Ownership_table->update( [
847 'cash_available_on_the_account' => $new_Cash_available_buyer,
848 ] );
849 }
850 print( "<br> new_Cash_available_buyer: " . $new_Cash_available_buyer . "<br>" );
851 // record more cash to Safe Silver as intermediary seller
852 // first fetch data from Safe Silver (whose user_id=1) we need the amount of cash recorded in table CashSILOwnership for Safe Silver
853 $cash_SIL_Ownership_table = CashSILOwnership::where( 'user_id', 1 );
854
855
856 if ( $cash_SIL_Ownership_table->exists() ) {
857 $cash_SIL_Ownership_obj = $cash_SIL_Ownership_table->first();
858
859 $current_Safe_Silver_available_Cash = $cash_SIL_Ownership_obj->cash_available_on_the_account;
860 print( "<br> current_Safe_Silver_available_Cash: " . $current_Safe_Silver_available_Cash . "<br>" );
861 // then calculate the increased amount of cash after selling to buyer
862 $new_Safe_Silver_available_Cash = $current_Safe_Silver_available_Cash + $cash_buy_transaction;
863 // then update cash owned in table CashSILOwnership for Safe Silver
864 $cash_SIL_Ownership_table->update( [
865 'cash_available_on_the_account' => $new_Safe_Silver_available_Cash,
866 ] );
867 }
868 print( "<br> new_Safe_Silver_available_Cash: " . $new_Safe_Silver_available_Cash . "<br>" );
869
870 } // end of elseif ( $order_type == "B" )
871 } // end of macro_cash_SIL_ownership
872
873
874 private function createSellOrderBook() {
875 $ask_prize = [ 0, 0, 0 ];
876 $ask_size = [ 0, 0, 0 ];
877 $ask_count = [ 0, 0, 0 ];
878 $sell_rank_by_price = [];
879
880 //print( ' <br> Calling createSellOrderBook <br>' );
881
882 try {
883
884 // Select count(desired_sil),price from sell_orders_rank_by_price_nb group by price order by price
885 $sell_orders_rank_by_price = SellOrdersRankByPriceNb::orderBy( 'price', 'asc' )->groupBy( 'price' )
886 ->select( 'price as ask_price',
887 DB::raw( 'count( desired_sil ) as num_ask_order' ),
888 DB::raw( 'sum( desired_sil ) as ask_size' )
889 )->get();
890 if ( count( $sell_orders_rank_by_price ) > 0 ) {
891 // Insert ranked and aggregated records in Temp OrderBook Sell
892 foreach ( $sell_orders_rank_by_price as $sell_order_rank_by_price ) {
893 $now = Carbon::now()->toDateTimeString();
894
895 $sell_rank_by_price[] = [
896 'ask_size' => $sell_order_rank_by_price->ask_size,
897 'ask_price' => $sell_order_rank_by_price->ask_price,
898 'num_ask_order' => $sell_order_rank_by_price->num_ask_order,
899 'created_at' => $now,
900 'updated_at' => $now,
901 ];
902
903// OrderBookSell::create(
904// [
905// 'ask_size' => $sell_order_rank_by_price->ask_size,
906// 'ask_price' => $sell_order_rank_by_price->ask_price,
907// 'num_ask_order' => $sell_order_rank_by_price->num_ask_order,
908// ]
909// );
910
911 }
912 OrderBookSell::insert( $sell_rank_by_price );
913 }
914
915 } catch ( Exception $e ) {
916 print( ' <br>Exception in createSellOrderBook <br>' );
917 print( ' <br>Line # ' . $e->getLine() . "<br>" );
918 print( '<br>Message # ' . $e->getLine() . "<br>" );
919 }
920 }
921
922 private function createBuyOrderBook() {
923 $bid_price = [ 0, 0, 0 ];
924 $bid_size = [ 0, 0, 0 ];
925 $bid_count = [ 0, 0, 0 ];
926 $buy_rank_by_price = [];
927
928
929// print( 'Calling createBuyOrderBook' );
930// print_r( $bid_price );
931// print " <br>";
932// print_r( $bid_size );
933// print " <br>";
934// print_r( $bid_count );
935// print " <br>";
936
937 // Select count(desired_sil),price from sell_orders_rank_by_price_nb group by price order by price
938 try {
939
940 $buy_orders_rank_by_price = BuyOrdersRankByPriceNb::orderBy( 'price', 'DESC' )->groupBy( 'price' )
941 ->select( 'price as bid_price',
942 DB::raw( 'count(desired_sil) as num_bid_order' ),
943 DB::raw( 'sum(desired_sil) as bid_size' )
944 )->get();
945 if ( count( $buy_orders_rank_by_price ) > 0 ) {
946 // Insert ranked and aggregated records in Temp OrderBook Sell
947 foreach ( $buy_orders_rank_by_price as $buy_order_rank_by_price ) {
948 $now = Carbon::now()->toDateTimeString();
949
950 $buy_rank_by_price[] = [
951 'bid_size' => $buy_order_rank_by_price->bid_size,
952 'bid_price' => $buy_order_rank_by_price->bid_price,
953 'num_bid_order' => $buy_order_rank_by_price->num_bid_order,
954 'created_at' => $now,
955 'updated_at' => $now,
956
957 ];
958
959// OrderBookBuy::create(
960// [
961// 'bid_size' => $buy_order_rank_by_price->bid_size,
962// 'bid_price' => $buy_order_rank_by_price->bid_price,
963// 'num_bid_order' => $buy_order_rank_by_price->num_bid_order,
964// ]
965// );
966 }
967 OrderBookBuy::insert( $buy_rank_by_price );
968 }
969
970 } catch ( Exception $e ) {
971 print( '<br>Exception in createBuyOrderBook <br>' );
972 print( '<br>Line # ' . $e->getLine() . "<br>" );
973 print( '<br>Message # ' . $e->getLine() . "<br>" );
974 }
975 }
976
977 private function Macro10() {
978 print( '<br> <strong> Calling Macro10 </strong> <br>' );
979
980 //initialisation of variables
981 $temporary_Buy_orders = 0;
982 $temporary_Sell_orders = 0;
983 $increment = 0;
984 $user_id = 0;
985
986 try {
987 //Emptyng Rank BY Price Tables first
988 BuyOrdersRankByPriceNb::truncate();
989 SellOrdersRankByPriceNb::truncate();
990 NewOrderEnters::truncate();
991 $buy_rank_by_price = [];
992 $sell_rank_by_price = [];
993
994 //Select all orders having status is OPEN
995 $temp_orders = ListAllOrders::where( [ 'status' => 'open' ] )->get();
996
997 foreach ( $temp_orders as $temp_order ) {
998 $order_number = $temp_order->order_number;
999 $currency = $temp_order->currency;
1000 $buy_sell = $temp_order->buy_sell;
1001 $desired_sil = $temp_order->desired_sil;
1002 $price = $temp_order->price;
1003 $order_type = $temp_order->order_type;
1004 $trigger_price_activating_stop_order = $temp_order->trigger_price_activating_stop_order;
1005 $duration = ( $temp_order->duration == null ? '1970-01-01' : $temp_order->duration );
1006 $split_id = $temp_order->split_id;
1007 $levelling_ratio = $temp_order->levelling_ratio;
1008 $LMM_percentage = $temp_order->LMM_percentage;
1009 $trigger_price_activating_stop_order = $temp_order->trigger_price_activating_stop_order;
1010 $entry_date = $temp_order->entry_date;
1011 $entry_time = $temp_order->entry_time;
1012 $status = $temp_order->status;
1013 $user_id = $temp_order->user_id;
1014 $now = Carbon::now()->toDateTimeString();
1015
1016 if ( $buy_sell == 'B' ) {
1017 $buy_rank_by_price[] = [
1018 'order_number' => $order_number,
1019 'currency' => $currency,
1020 'buy_sell' => $buy_sell,
1021 'entry_date' => $entry_date,
1022 'entry_time' => $entry_time,
1023 'desired_sil' => $desired_sil,
1024 'price' => $price,
1025 'order_type' => $order_type,
1026 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
1027 'duration' => $duration,
1028 'status' => $status,
1029 'split_id' => $split_id,
1030 'levelling_ratio' => $levelling_ratio,
1031 'LMM_percentage' => $LMM_percentage,
1032 'user_id' => $user_id,
1033 'created_at' => $now,
1034 'updated_at' => $now,
1035 ];
1036
1037// $buy_order_rank_price_nb = BuyOrdersRankByPriceNb::create(
1038// [
1039// 'order_number' => $order_number,
1040// 'currency' => $currency,
1041// 'buy_sell' => $buy_sell,
1042// 'entry_date' => $entry_date,
1043// 'entry_time' => $entry_time,
1044// 'desired_sil' => $desired_sil,
1045// 'price' => $price,
1046// 'order_type' => $order_type,
1047// 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
1048// 'duration' => $duration,
1049// 'status' => $status,
1050// 'split_id' => $split_id,
1051// 'levelling_ratio' => $levelling_ratio,
1052// 'LMM_percentage' => $LMM_percentage,
1053// 'user_id' => $user_id,
1054//
1055// ]
1056// );
1057
1058 } elseif ( $buy_sell == 'S' ) {
1059 $sell_rank_by_price[] = [
1060 'order_number' => $order_number,
1061 'currency' => $currency,
1062 'buy_sell' => $buy_sell,
1063 'entry_date' => $entry_date,
1064 'entry_time' => $entry_time,
1065 'desired_sil' => $desired_sil,
1066 'price' => $price,
1067 'order_type' => $order_type,
1068 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
1069 'duration' => $duration,
1070 'status' => $status,
1071 'split_id' => $split_id,
1072 'levelling_ratio' => $levelling_ratio,
1073 'LMM_percentage' => $LMM_percentage,
1074 'user_id' => $user_id,
1075 'created_at' => $now,
1076 'updated_at' => $now,
1077 ];
1078
1079// $buy_order_rank_price_nb = SellOrdersRankByPriceNb::create(
1080// [
1081// 'order_number' => $order_number,
1082// 'buy_sell' => $buy_sell,
1083// 'entry_date' => $entry_date,
1084// 'entry_time' => $entry_time,
1085// 'desired_sil' => $desired_sil,
1086// 'price' => $price,
1087// 'order_type' => $order_type,
1088// 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
1089// 'duration' => $duration,
1090// 'status' => $status,
1091// 'split_id' => $split_id,
1092// 'levelling_ratio' => $levelling_ratio,
1093// 'LMM_percentage' => $LMM_percentage,
1094// 'user_id' => $user_id,
1095// ]
1096// );
1097 }
1098
1099 /*
1100 * We are ranking Orders both Sell and Buy by Price
1101 */
1102
1103// // Ranking Buy
1104// $ranked_buy_orders_by_price = BuyOrdersRankByPriceNb::orderBy( 'price',
1105// 'desc' )->orderBy( 'order_number',
1106// 'asc' )->get();
1107//
1108// // Ranking Sell
1109// $ranked_sell_orders_by_price = SellOrdersRankByPriceNb::orderBy( 'price',
1110// 'asc' )->orderBy( 'order_number',
1111// 'asc' )->get();
1112
1113
1114 }
1115
1116
1117 BuyOrdersRankByPriceNb::insert( $buy_rank_by_price );
1118 SellOrdersRankByPriceNb::insert( $sell_rank_by_price );
1119
1120 // Make sure Temp Order Book Buy and Sell are Empty by Truncating them.
1121 OrderBookBuy::truncate();
1122 OrderBookSell::truncate();
1123
1124 $this->createSellOrderBook();
1125 $this->createBuyOrderBook();
1126
1127
1128 } catch ( Exception $e ) {
1129 print( '<br>Exception in Macro12 <br>' );
1130 print( '<br>Line # ' . $e->getLine() . "<br>" );
1131 print( '<br>Message # ' . $e->getLine() . "<br>" );
1132 }
1133 }
1134
1135 private function Macro4() {
1136
1137 print( '<br> Callin Macro4 <br>' );
1138 $list_orders_obj = null;
1139 $list_orders_temp = [];
1140 $temp_orders = [];
1141
1142 //copy of orders from “list of all orders” with status different than open into table //list of non active order
1143 //then delete orders from “list of all orders” with status different than open or pending
1144
1145 // Fetch data from list of active order
1146
1147 try {
1148 //$list_orders_table = ListAllOrders::where( 'order_number', '>', 0 )->where( 'status', '!=', 'open' );
1149 $list_orders_obj = ListAllOrders::where( 'order_number', '>', 0 )->where( 'status', '!=', 'open' )->get();
1150
1151 //dd($list_orders_obj->isEmpty());
1152
1153 if ( ! $list_orders_obj->isEmpty() ) {
1154 //if ( $list_orders_table->exists() ) {
1155 //$list_orders_obj = $list_orders_table->get();
1156
1157 foreach ( $list_orders_obj as $list_order_obj ) {
1158 $now = Carbon::now()->toDateTimeString();
1159 $temp_orders[] = $list_order_obj->order_number; //for deleting purpose
1160
1161 $order_number = $list_order_obj->order_number;
1162 $order_status = $list_order_obj->status;
1163 $order_currency = $list_order_obj->currency;
1164 $order_type = $list_order_obj->order_type;
1165 $order_entry_date = $list_order_obj->entry_date;
1166 $order_entry_time = $list_order_obj->entry_time;
1167 $order_user_id = $list_order_obj->user_id;
1168 $order_buy_sell = $list_order_obj->buy_sell;
1169 $order_price = $list_order_obj->price;
1170 $order_desired_sil = $list_order_obj->desired_sil;
1171 $order_duration = $list_order_obj->duration;
1172 $order_split_id = $list_order_obj->split_id;
1173 $order_levelling_ratio = $list_order_obj->levelling_ratio;
1174 $order_LMM_percentage = $list_order_obj->LMM_percentage;
1175 $order_trigger_price = $list_order_obj->trigger_price;
1176
1177 $list_orders_temp[] = [
1178 'order_number' => $order_number,
1179 'currency' => $order_currency,
1180 'buy_sell' => $order_buy_sell,
1181 'entry_date' => $order_entry_date,
1182 'entry_time' => $order_entry_time,
1183 'desired_sil' => $order_desired_sil,
1184 'price' => $order_price,
1185 'order_type' => $order_type,
1186 //'trigger_price' => $order_trigger_price,
1187 'trigger_price_activating_stop_order' => $order_trigger_price,
1188 'duration' => $order_duration,
1189 'status' => $order_status,
1190 'split_id' => $order_split_id,
1191 'levelling_ratio' => $order_levelling_ratio,
1192 'LMM_percentage' => $order_LMM_percentage,
1193 'user_id' => $order_user_id,
1194 'created_at' => $now,
1195 'updated_at' => $now,
1196 ];
1197 } // end for each temp_list_orders_obj
1198
1199 ListNonActiveOrders::insert( $list_orders_temp );//Bulk Insert
1200 ListAllOrders::whereIn( 'order_number', $temp_orders )->delete();
1201 } // end of if ( $temp_list_orders_table -> exists()
1202
1203 } catch ( Exception $e ) {
1204 print( '<br>Exception in Macro12 <br>' );
1205 print( '<br>Line # ' . $e->getLine() . "<br>" );
1206 print( '<br>Message # ' . $e->getLine() . "<br>" );
1207 }
1208
1209 }
1210
1211 private function Macro7() {
1212 print( '<br> Calling Macro7 <br>' );
1213
1214
1215 try {
1216 //cancellation of orders with extreme price difference in table “list of all orders”
1217 // Fetch both last buy and sell matched price
1218 $last_matched_order = LastMatchedPriceQuantity::orderBy( 'id',
1219 'DESC' )->first();
1220 $last_buy_price_matched = floatval( $last_matched_order->buy_price );
1221 $last_sell_price_matched = floatval( $last_matched_order->sell_price );
1222 $price_difference_automatic_order_cancellation = $this->extreme_price_cancellation;
1223
1224// print( '<br> $last_buy_price_matched = ' . $last_buy_price_matched . "<br>" );
1225// print( '<br> $last_sell_price_matched = ' . $last_sell_price_matched . "<br>" );
1226
1227 // Fetch data from list of active order
1228 $list_orders_table = ListAllOrders::where( 'order_number', '>', 0 );
1229 $list_orders_obj = null;
1230
1231 if ( $list_orders_table->count() ) {
1232 $list_orders_obj = $list_orders_table->get();
1233
1234
1235 foreach ( $list_orders_obj as $list_order_obj ) {
1236 $order_buy_sell = $list_order_obj->buy_sell;
1237 $order_price = $list_order_obj->price;
1238 $diff_buy = abs( $order_price - $last_buy_price_matched ) / $last_buy_price_matched;
1239 $diff_sell = abs( $order_price - $last_sell_price_matched ) / $last_sell_price_matched;
1240
1241// print( '<br>$diff_buy = ' . $diff_buy . "<br>" );
1242// print( '<br>$diff_sell = ' . $diff_sell . "<br>" );
1243//
1244// print( '<br>$last_buy_price_matched = ' . $last_buy_price_matched . "<br>" );
1245// print( '<br>$last_sell_price_matched = ' . $last_sell_price_matched . "<br>" );
1246//
1247// print( '<br>$price_difference_automatic_order_cancellation = ' . $price_difference_automatic_order_cancellation . "<br>" );
1248
1249 if ( $order_buy_sell == 'B' && $diff_buy > $price_difference_automatic_order_cancellation ) {
1250 // buy order with extreme price its status need to be updated into 'cancelled_price_extreme'
1251 //print( 'Order Buy_SELL is B and status is CANCELLED <br>' );
1252
1253 $list_order_obj->where( 'order_number', $list_order_obj->order_number )->update( [
1254 'status' => 'cancelled_price_extreme',
1255 ] );
1256
1257 } // end of if ( $order_type == 'B' …..)
1258
1259 if ( $order_buy_sell == 'S' && $diff_sell > $price_difference_automatic_order_cancellation ) {
1260 // sell order with extreme price its status need to be updated into 'cancelled_price_extreme'
1261 print( 'Order Buy_SELL is S and status is CANCELLED <br>' );
1262
1263 $list_order_obj->where( 'order_number', $list_order_obj->order_number )->update( [
1264 'status' => 'cancelled_price_extreme',
1265 ] );
1266
1267 } // end of if ( $order_type == 'S' …..)
1268 } // end for each temp_list_orders_obj
1269
1270 } // end of if ( $temp_list_orders_table -> exists()
1271 } catch ( Exception $e ) {
1272 print( '<br>Exception in Macro7 <br>' );
1273 print( '<br>Line # ' . $e->getLine() . "<br>" );
1274 print( '<br>Message # ' . $e->getLine() . "<br>" );
1275 }
1276 }
1277
1278 private function Macro16() {
1279 print( '<br><b> Calling Macro16</b> <br>' );
1280 $final_buy_matched_orders = [];
1281 $final_sell_matched_orders = [];
1282
1283 try {
1284 //Empty list of all orders
1285 ListAllOrders::truncate();
1286 // copy all orders from table “temporary list of active order” into “list of active order”
1287 //$temp_list_orders_table = TempListOrders::where( 'order_number', '>', 0 );
1288 $temp_select = TempListOrders::select( [
1289 'order_number',
1290 'currency',
1291 'buy_sell',
1292 'entry_date',
1293 'entry_time',
1294 'desired_sil',
1295 'price',
1296 'order_type',
1297 'trigger_price_activating_stop_order',
1298 'duration',
1299 'status',
1300 'split_id',
1301 'levelling_ratio',
1302 'LMM_percentage',
1303 'user_id',
1304 'updated_at',
1305 'created_at'
1306 ] );
1307
1308 $insertQuery = 'insert into `list_all_orders` (`order_number`, `currency`, `buy_sell`, `entry_date`, `entry_time`, `desired_sil`, `price`, `order_type`, `trigger_price_activating_stop_order`, `duration`, `status`, `split_id`, `levelling_ratio`, `LMM_percentage`, `user_id`, `updated_at`, `created_at`)'
1309 . $temp_select->toSql();
1310
1311 DB::insert( $insertQuery );
1312
1313 // this is macro2 in VBA code which will be directly included in macro 16 in PHP code
1314 /// no need to create a separate macro2
1315 // 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”
1316 //$temp_buy_matched_order_table = BuyMatchedOrder::where( 'buy_order_id', '>', 0 );
1317 $temp_buy_matched_orders_obj = BuyMatchedOrder::join( 'sell_matched_order as so',
1318 'buy_matched_order.matching_id', '=',
1319 'so.matching_id' )->select(
1320 'buy_matched_order.buy_order_id',
1321 'buy_matched_order.buy_split_id',
1322 'buy_matched_order.buy_price',
1323 'buy_matched_order.buy_user_id',
1324 'buy_matched_order.matching_date',
1325 'buy_matched_order.matching_time',
1326 'buy_matched_order.matching_process',
1327 'buy_matched_order.buy_quantity',
1328 'so.sell_order_id',
1329 'so.sell_split_id'
1330 )->get();
1331
1332 if ( count($temp_buy_matched_orders_obj) > 0 ) {
1333 //$temp_buy_matched_orders_obj = $temp_buy_matched_order_table->get();
1334
1335 foreach ( $temp_buy_matched_orders_obj as $temp_buy_matched_order_obj ) {
1336 $tmp_sell_order_id = 0;
1337 $tmp_sell_split_id = '';
1338
1339 //Pull the sell order id and split id when it matches the MATCH ID
1340 //print( '<br> MATCHING ID = ' . $temp_buy_matched_order_obj->matching_id . "<br>" );
1341
1342// $sell_matched_order_sell_order_split_id_obj = SellMatchedOrder:: where( 'matching_id',
1343// $temp_buy_matched_order_obj->matching_id )->first();
1344
1345 $tmp_sell_order_id = $temp_buy_matched_order_obj->sell_order_id;
1346 $tmp_sell_split_id = $temp_buy_matched_order_obj->sell_split_id;
1347
1348 $buy_order_id = $temp_buy_matched_order_obj->buy_order_id;
1349 $buy_order_matching_date = $temp_buy_matched_order_obj->matching_date;
1350 $buy_order_matching_time = $temp_buy_matched_order_obj->matching_time;
1351 $buy_order_matching_id = $temp_buy_matched_order_obj->matching_id;
1352 $buy_order_matching_process = $temp_buy_matched_order_obj->matching_process;
1353 $buy_order_user_id = $temp_buy_matched_order_obj->buy_user_id;
1354 $buy_order_split_id = $temp_buy_matched_order_obj->buy_split_id;
1355 $buy_order_price = $temp_buy_matched_order_obj->buy_price;
1356 $buy_order_quantity = $temp_buy_matched_order_obj->buy_quantity;
1357
1358 // we need to add the values to the table FinalBuyMatchedOrder those are additional rows
1359 //to be ADDED to the existing ones
1360
1361 $final_buy_matched_orders[] = [
1362 'buy_order_id' => $buy_order_id,
1363 'matching_date' => $buy_order_matching_date,
1364 'matching_time' => $buy_order_matching_time,
1365 'matching_process' => $buy_order_matching_process,
1366 'buy_user_id' => $buy_order_user_id,
1367 'buy_price' => $buy_order_price,
1368 'buy_quantity' => $buy_order_quantity,
1369 'buy_split_id' => $buy_order_split_id,
1370 'sell_order_id' => $tmp_sell_order_id,
1371 'sell_split_id' => $tmp_sell_split_id,
1372 'created_at' => Carbon::now()->toDateTimeString(),
1373 'updated_at' => Carbon::now()->toDateTimeString(),
1374
1375 ];
1376
1377// $list_final_buy_matched_orders_temp = FinalBuyMatchedOrder::create( [
1378// 'buy_order_id' => $buy_order_id,
1379// 'matching_date' => $buy_order_matching_date,
1380// 'matching_time' => $buy_order_matching_time,
1381// 'matching_id' => $buy_order_matching_id,
1382// 'matching_process' => $buy_order_matching_process,
1383// 'buy_user_id' => $buy_order_user_id,
1384// 'buy_price' => $buy_order_price,
1385// 'buy_quantity' => $buy_order_quantity,
1386// 'buy_split_id' => $buy_order_split_id,
1387// 'sell_order_id' => $tmp_sell_order_id,
1388// 'sell_split_id' => $tmp_sell_split_id,
1389//
1390// ] );
1391
1392 print( '<br>FinalBuyMatchedOrder Created <br>' );
1393
1394 } // end for each temp_buy_matched_order_obj
1395
1396 FinalBuyMatchedOrder::insert( $final_buy_matched_orders );
1397 } // end of If( $temp_buy_matched_order_table->exists()
1398
1399 // 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”
1400 //$temp_sell_matched_order_table = SellMatchedOrder::where( 'sell_order_id', '>', 0 );
1401 $temp_sell_matched_orders_obj = SellMatchedOrder::join( 'buy_matched_order as bo',
1402 'sell_matched_order.matching_id', '=',
1403 'bo.matching_id' )->select(
1404
1405 'sell_matched_order.sell_order_id',
1406 'sell_matched_order.sell_split_id',
1407 'sell_matched_order.sell_price',
1408 'sell_matched_order.sell_user_id',
1409 'sell_matched_order.matching_date',
1410 'sell_matched_order.matching_time',
1411 'sell_matched_order.matching_process',
1412 'sell_matched_order.sell_quantity',
1413 'bo.buy_order_id',
1414 'bo.buy_split_id'
1415 )->get();
1416
1417
1418 if ( count($temp_sell_matched_orders_obj) > 0 ) {
1419 //$temp_sell_matched_orders_obj = $temp_sell_matched_order_table->get();
1420
1421 foreach ( $temp_sell_matched_orders_obj as $temp_sell_matched_order_obj ) {
1422 $tmp_buy_order_id = 0;
1423 $tmp_buy_split_id = '';
1424
1425 //Pull the sell order id and split id when it matches the MATCH ID
1426// $buy_matched_order_buy_order_split_id_obj = BuyMatchedOrder:: where( 'matching_id',
1427// $temp_sell_matched_order_obj->matching_id )->first();
1428
1429 $tmp_buy_order_id = $temp_sell_matched_order_obj->buy_order_id;
1430 $tmp_buy_split_id = $temp_sell_matched_order_obj->buy_split_id;
1431
1432 $sell_order_id = $temp_sell_matched_order_obj->sell_order_id;
1433 $sell_order_matching_date = $temp_sell_matched_order_obj->matching_date;
1434 $sell_order_matching_time = $temp_sell_matched_order_obj->matching_time;
1435 $sell_order_matching_id = $temp_sell_matched_order_obj->matching_id;
1436 $sell_order_matching_process = $temp_sell_matched_order_obj->matching_process;
1437 $sell_order_user_id = $temp_sell_matched_order_obj->sell_user_id;
1438 $sell_order_price = $temp_sell_matched_order_obj->sell_price;
1439 $sell_order_quantity = $temp_sell_matched_order_obj->sell_quantity;
1440 $sell_order_split_id = $temp_sell_matched_order_obj->sell_split_id;
1441
1442 // we need to add the values to the table FinalSellMatchedOrder those are additional rows
1443 //to be ADDED to the existing ones
1444 //$list_final_sell_matched_orders_temp = FinalSel
1445
1446 $final_sell_matched_orders[] = [
1447 'sell_order_id' => $sell_order_id,
1448 'matching_date' => $sell_order_matching_date,
1449 'matching_time' => $sell_order_matching_time,
1450 'matching_process' => $sell_order_matching_process,
1451 'sell_user_id' => $sell_order_user_id,
1452 'sell_price' => $sell_order_price,
1453 'sell_quantity' => $sell_order_quantity,
1454 'sell_split_id' => $sell_order_split_id,
1455 'buy_order_id' => $tmp_buy_order_id,
1456 'buy_split_id' => $tmp_buy_split_id,
1457 'created_at' => Carbon::now()->toDateTimeString(),
1458 'updated_at' => Carbon::now()->toDateTimeString(),
1459 ];
1460
1461// $list_final_sell_matched_orders_temp = FinalSellMatchedOrder::create( [
1462// 'sell_order_id' => $sell_order_id,
1463// 'matching_date' => $sell_order_matching_date,
1464// 'matching_time' => $sell_order_matching_time,
1465// 'matching_id' => $sell_order_matching_id,
1466// 'matching_process' => $sell_order_matching_process,
1467// 'sell_user_id' => $sell_order_user_id,
1468// 'sell_price' => $sell_order_price,
1469// 'sell_quantity' => $sell_order_quantity,
1470// 'sell_split_id' => $sell_order_split_id,
1471// 'buy_order_id' => $tmp_buy_order_id,
1472// 'buy_split_id' => $tmp_buy_split_id,
1473// ] );
1474 } // end for each temp_sell_matched_order_obj
1475
1476 FinalSellMatchedOrder::insert( $final_sell_matched_orders );
1477
1478 } // end of If( $temp_sell_matched_order_table->exists()
1479
1480 // this is end of macro2 in VBA code which will is directly included in macro 16 in PHP code
1481 // update of table “Last_matched_price_and_quantity” with values from last traded order
1482
1483 // fetch data from table buy_matched_order, we should fetch the last value entered
1484 $buy_matched_order_table = BuyMatchedOrder::orderBy( 'id', 'DESC' )->limit( 1 );
1485
1486 if ( $buy_matched_order_table->exists() ) {
1487 $buy_matched_order_obj = $buy_matched_order_table->first();
1488 $matched_buy_price = $buy_matched_order_obj->buy_price;
1489 $matched_quantity = $buy_matched_order_obj->buy_quantity;
1490 $matched_date = $buy_matched_order_obj->matching_date;
1491 $matched_time = $buy_matched_order_obj->matching_time;
1492
1493// print( "<br> matched_buy_price: " . $matched_buy_price . "<br>" );
1494// print( "<br> matched_quantity: " . $matched_quantity . "<br>" );
1495 }
1496
1497 // fetch last sell price from table sell_matched_order, we should fetch the last value entered
1498 $sell_matched_order_table = SellMatchedOrder::orderBy( 'id', 'DESC' )->limit( 1 );
1499
1500 if ( $sell_matched_order_table->exists() ) {
1501 $sell_matched_order_obj = $sell_matched_order_table->first();
1502 $matched_sell_price = $sell_matched_order_obj->sell_price;
1503 print( "<br> matched_sell_price: " . $matched_sell_price . "<br>" );
1504 }
1505
1506 // update of table “Last_matched_price_and_quantity” with values from last traded order
1507 LastMatchedPriceQuantity::create( [
1508 'buy_price' => $matched_buy_price,
1509 'sell_price' => $matched_sell_price,
1510 'average' => ( $matched_buy_price + $matched_sell_price ) / 2,
1511 'buy_quantity' => $matched_quantity,
1512 'matching_date' => $matched_date,
1513 'matching_time' => $matched_time,
1514 ] );
1515
1516 $this->macro_cash_SIL_ownership();
1517 $this->Macro7();
1518 $this->Macro4();
1519 $this->Macro6();
1520 $this->Macro10();
1521 $this->Macro11();
1522
1523 print( '<br>Called macro_cash_SIL_ownership from Macro 16 <br>' );
1524
1525 } catch ( Exception $e ) {
1526 print( '<br>Exception in Macro16 <br>' );
1527 print( '<br>Line # ' . $e->getLine() . "<br>" );
1528 print( '<br>Message # ' . $e->getLine() . "<br>" );
1529 }
1530
1531
1532 } //End Macro 16
1533
1534 private
1535 function update_matching_order(
1536 $single_buy_order
1537 ) {
1538 print( 'Calling update_matching_order <br>' );
1539 print( '$single_buy_order = ' . $single_buy_order . ' <br>' );
1540 $temporary_sell_orders_id = 0;
1541 $temporary_sell_split_id = '';
1542 $temporary_sell_quantity = 0;
1543
1544 $temporary_buy_orders_id = 0;
1545 $temporary_buy_split_id = '';
1546 $temporary_buy_quantity = 0;
1547
1548
1549 try {
1550 if ( $single_buy_order == 1 ) { //Begin $single_buy_order == 1
1551 $sell_orders_rank_by_price_Temporary_table = SellOrdersRankByPriceNb:: where( 'split_id', '!=', 1 );
1552
1553 if ( $sell_orders_rank_by_price_Temporary_table->exists() ) {
1554 $sell_orders_rank_by_price_Temporary_obj = $sell_orders_rank_by_price_Temporary_table->get();
1555
1556
1557 foreach ( $sell_orders_rank_by_price_Temporary_obj as $sell_orders_rank_by_price_Temporary_obj_item ) {
1558 $temporary_sell_orders_id = $sell_orders_rank_by_price_Temporary_obj_item->order_id;
1559 $temporary_sell_split_id = $sell_orders_rank_by_price_Temporary_obj_item->split_id;
1560 $temporary_sell_quantity = $sell_orders_rank_by_price_Temporary_obj_item->desired_sil;
1561 print( ' <br>$temporary_sell_orders_id = ' . $temporary_sell_orders_id . "<br>" );
1562 print( ' <br>$temporary_sell_split_id = ' . $temporary_sell_split_id . "<br>" );
1563 print( ' <br>$temporary_sell_quantity = ' . $temporary_sell_quantity . "<br>" );
1564
1565 TempListOrders::where( 'order_number', $temporary_sell_orders_id )->update(
1566 [
1567 'split_id' => $temporary_buy_split_id,
1568 'desired_sil' => $temporary_buy_quantity
1569 ]
1570 );
1571 print( ' <br> Temp List Order updated In SELL for Order # ' . $temporary_buy_orders_id . "<br>" );
1572 }
1573 }
1574
1575
1576 } //end $single_buy_order == 1
1577 else {
1578 $buy_orders_rank_by_price_Temporary_table = BuyOrdersRankByPriceNb:: where( 'split_id', '!=', '1' );
1579
1580 if ( $buy_orders_rank_by_price_Temporary_table->exists() ) {
1581 $buy_orders_rank_by_price_Temporary_obj = $buy_orders_rank_by_price_Temporary_table->get();
1582
1583 foreach ( $buy_orders_rank_by_price_Temporary_obj as $buy_orders_rank_by_price_Temporary_obj_item ) {
1584 $temporary_buy_orders_id = $buy_orders_rank_by_price_Temporary_obj_item->order_number;
1585 $temporary_buy_split_id = $buy_orders_rank_by_price_Temporary_obj_item->split_id;
1586 $temporary_buy_quantity = $buy_orders_rank_by_price_Temporary_obj_item->desired_sil;
1587 print( '<br>$temporary_buy_orders_id = ' . $temporary_buy_orders_id . "<br>" );
1588 print( '<br>$temporary_buy_split_id = ' . $temporary_buy_split_id . "<br>" );
1589 print( '<br>$temporary_buy_quantity = ' . $temporary_buy_quantity . "<br>" );
1590
1591 TempListOrders::where( 'order_number', $temporary_buy_orders_id )->update(
1592 [
1593 'split_id' => $temporary_buy_split_id,
1594 'desired_sil' => $temporary_buy_quantity
1595 ]
1596 );
1597 print( '<br> Temp List Order updated for Order # ' . $temporary_buy_orders_id . "<br>" );
1598 }
1599
1600 }
1601 }
1602
1603 } catch ( Exception $e ) {
1604 print( '<br>Exception in update_matching_order <br>' );
1605 print( '<br>Line # ' . $e->getLine() . "<br>" );
1606 print( '<br>Message # ' . $e->getLine() . "<br>" );
1607 }
1608
1609 return 1;
1610 }
1611
1612 private
1613 function Macro18() {
1614 print "<br> Calling Marco 18 here <br>";
1615 // no matching, test to see if new order is "fill or kill" or "immediate or cancel" type
1616 // if yes move to non active list and change status
1617
1618 try {
1619 $temp_orders_table = TempListOrders::orderby( 'order_number', 'DESC' );
1620 $temp_orders_obj = null;
1621
1622 if ( $temp_orders_table->exists() ) {
1623 $temp_order_obj = $temp_orders_table->limit( 1 )->first();
1624
1625 if ( $temp_order_obj != null ) {
1626 $order_number = $temp_order_obj->order_number;
1627 $order_status = $temp_order_obj->status;
1628 $order_currency = $temp_order_obj->currency;
1629 $order_type = $temp_order_obj->order_type;
1630 $order_entry_date = $temp_order_obj->entry_date;
1631 $order_entry_time = $temp_order_obj->entry_time;
1632 $order_user_id = $temp_order_obj->user_id;
1633 $order_buy_sell = $temp_order_obj->buy_sell;
1634 $order_price = $temp_order_obj->price;
1635 $order_desired_sil = $temp_order_obj->desired_sil;
1636 $order_duration = $temp_order_obj->duration;
1637 $order_split_id = $temp_order_obj->split_id;
1638 $order_levelling_ratio = $temp_order_obj->levelling_ratio;
1639 $order_LMM_percentage = $temp_order_obj->LMM_percentage;
1640 $order_trigger_price = $temp_order_obj->trigger_price;
1641
1642 print( "<br> analysed last order in macro18: " . $order_number . "<br>" );
1643
1644 print( "<br> order type of last order in macro18: " . $order_type . "<br>" );
1645
1646
1647 //if ( $this->order_type == 'fill_or_kill' || $this->order_type == 'immediate_or_cancel' ) {
1648 if ( $order_type == 'fill_or_kill' || $order_type == 'immediate_or_cancel' ) {
1649 print( "test fill kill or immediate cancel activated" );
1650 //Change the status accordingly
1651 if ( $this->order_type == 'fill_or_kill' ) {
1652 $updated_status = 'fill_kill_cancelled_not_fully_matched';
1653 print( "updated status is fill kill canceled" );
1654 } elseif ( $this->order_type == 'immediate_or_cancel' ) {
1655 print( "updated status is unmatched part canceled" );
1656 $updated_status = 'unmatched_immediate_cancel_cancel';
1657 }
1658
1659 // update status of unmatched order
1660 $temp_orders_table->update( [
1661 'status' => $updated_status,
1662 ] );
1663
1664 // move order to list of non active order
1665 ListNonActiveOrders::create( [
1666 'order_number' => $order_number,
1667 'status' => $updated_status,
1668 'order_type' => $order_type,
1669 'entry_date' => $order_entry_date,
1670 'entry_time' => $order_entry_time,
1671 'user_id' => $order_user_id,
1672 'buy_sell' => $order_buy_sell,
1673 'price' => $order_price,
1674 'desired_sil' => $order_desired_sil,
1675 'duration' => $order_duration,
1676 'split_id' => $order_split_id,
1677 'levelling_ratio' => $order_levelling_ratio,
1678 'LMM_percentage' => $order_LMM_percentage,
1679 'trigger_price' => $order_trigger_price,
1680
1681 ] );
1682
1683 $this->Macro6();
1684 $this->Macro11();
1685 } else { // else of end of If ($order_type == 'fill_ot_kill' || $order_type == 'immediate or cancel' )
1686 $this->Macro13();
1687 $this->Macro6B();
1688 $this->Macro11();
1689 }
1690
1691 }
1692 }
1693
1694 } catch ( Exception $e ) {
1695 print( '<br>Exception in Macro18 <br>' );
1696 print( '<br>Line # ' . $e->getLine() . "<br>" );
1697 print( '<br>Message # ' . $e->getLine() . "<br>" );
1698 }
1699
1700 }
1701
1702 private function macro_FIFO_Sell_order(
1703 $allocated_top_order_quantity,
1704 $sell_quantity_q0,
1705 $matching_date,
1706 $matching_time,
1707 $matching_process_type
1708 ) { //Begin of Function
1709
1710 try {
1711 print( '<br>Calling macro_FIFO_Sell_order <br>' );
1712 //$allocated_top_order_quantity = 11; // WARNING.. Temp to remove.
1713 $buy_orders_rank_by_price_and_nb_17_table = BuyOrdersRankByPriceNb::orderBy( 'price',
1714 'DESC' )->orderBy( 'order_number', 'ASC' )->limit( 1 );
1715 $buy_orders_rank_by_price_and_nb_17_obj = $buy_orders_rank_by_price_and_nb_17_table->get();
1716
1717 if ( count( $buy_orders_rank_by_price_and_nb_17_obj ) > 0 ) { //Begin $buy_orders_rank_by_price_and_nb_17_obj Cond
1718 $buy_order_id = $buy_orders_rank_by_price_and_nb_17_obj[0]->order_number;
1719 $buy_split_id = $buy_orders_rank_by_price_and_nb_17_obj[0]->split_id;
1720 $buy_quantity = $buy_orders_rank_by_price_and_nb_17_obj[0]->desired_sil;
1721 $buy_price = $buy_orders_rank_by_price_and_nb_17_obj[0]->price;
1722 $buy_user_id = $buy_orders_rank_by_price_and_nb_17_obj[0]->user_id;
1723// print( "<br> BUY ORDER ID = " . $buy_order_id . "<br>" );
1724// print( "<br> BUY SPLIT ID = " . $buy_split_id . "<br>" );
1725// print( "<br> BUY QUANTITY = " . $buy_quantity . "<br>" );
1726// print( "<br> BUY PRICE = " . $buy_price . "<br>" );
1727// print( "<br> BUY USER_ID = " . $buy_user_id . "<br>" );
1728
1729 /**
1730 * Fetching Sell Order Data
1731 */
1732
1733 // We are doing ASC because we want the first row of both tables.
1734 $sell_orders_rank_by_price_and_nb_17_table = SellOrdersRankByPriceNb::orderBy( 'price',
1735 'asc' )->orderBy( 'order_number', 'ASC' )->limit( 1 );
1736
1737 $sell_orders_rank_by_price_and_nb_17_obj = $sell_orders_rank_by_price_and_nb_17_table->get();
1738
1739
1740 if ( count( $sell_orders_rank_by_price_and_nb_17_obj ) > 0 ) { // BEGIN $sell_orders_rank_by_price_and_nb_17_obj
1741 $sell_price = $sell_orders_rank_by_price_and_nb_17_obj[0]->price;
1742 $curent_sell_quantity = $sell_orders_rank_by_price_and_nb_17_obj[0]->desired_sil;
1743 $sell_split_id = $sell_orders_rank_by_price_and_nb_17_obj[0]->split_id;
1744 $sell_user_id = $sell_orders_rank_by_price_and_nb_17_obj[0]->user_id;
1745 $sell_order_id = $sell_orders_rank_by_price_and_nb_17_obj[0]->order_number;
1746
1747 print( "<br><br>" );
1748// print( "<br> SELL PRICE = " . $sell_price . "<br>" );
1749// print( "<br> SELL DESIRED SIL = " . $curent_sell_quantity . "<br>" );
1750// print( "<br> SELL ORDER ID = " . $sell_order_id . "<br>" );
1751// print( "<br> SELL SPLIT ID = " . $sell_split_id . "<br>" );
1752// print( "<br> SELL USER_ID = " . $sell_user_id . "<br>" );
1753 } // END $sell_orders_rank_by_price_and_nb_17_obj
1754
1755 // Check whether Sell Matched Order is Empty
1756 $sell_matched_order_obj = SellMatchedOrder::orderBy( 'id', 'DESC' )->limit( 1 )->first();
1757 print ( "<br> Sell Matched Order Table Related <br>" );
1758
1759 //$previous_sell_matching_id = 0; // REMOVE HARD CODED
1760
1761 // Pascal suggested to disable and have to test
1762 if ( $sell_matched_order_obj == null ) { // begin count( $sell_matched_order_obj ) == 0 )
1763 $previous_sell_matching_id = 0;
1764 print( "WE ARE NOT INSIDE ELSE <br>" );
1765 } else {
1766 $previous_sell_matching_id = $sell_matched_order_obj->matching_id;
1767 print ( 'SELL MATCHING ID = ' . $previous_sell_matching_id . "<br>" );
1768 } //end count( $sell_matched_order_obj ) == 0 )
1769
1770
1771 // Check whether Buy Matched Order is Empty
1772 $buy_matched_order_obj = BuyMatchedOrder::orderBy( 'id', 'DESC' )->limit( 1 )->first();
1773 print ( "<br> Buy Matched Order Table Related <br>" );
1774 $previous_buy_matching_id = 0; //REMOVE
1775 //test if there is no sell matching order yet
1776
1777 if ( $buy_matched_order_obj == null ) {
1778 $previous_buy_matching_id = 0;
1779 } else {
1780 $previous_buy_matching_id = $buy_matched_order_obj->matching_id;
1781 print ( '<br> BUY MATCHING ID = ' . $previous_buy_matching_id . "<br>" );
1782 }
1783
1784
1785 // Initialize variables with data coming from table Temporary_list_of_all_orders
1786 $temp_list_orders_obj = TempListOrders::where( [ 'order_number' => $buy_order_id ] )->first();
1787 $current_split_id = '';
1788 $current_quantity = 0;
1789 if ( $temp_list_orders_obj ) {
1790 $current_split_id = $temp_list_orders_obj->split_id;
1791 $current_quantity = $temp_list_orders_obj->desired_sil;
1792 }
1793 /**
1794 * test to see if Sell_price > Buy_price if yes by convention FIFO_Sell_order = -1 and
1795 * matching stops.
1796 * test to see if buy_price is empty it means that there is no more sell order available
1797 */
1798 if ( $sell_price > $buy_price || $buy_price == 0 ) { //Begin if ( $sell_price > $buy_price || $buy_price == 0
1799 $m_FIFO_Sell_order = - 1;
1800 //Select Records from Temp List Of Orders and update Desired SIL and SPLIT
1801 $temp_list_orders_buy_obj = TempListOrders::where( [ 'order_number' => $sell_order_id ] );
1802 if ( $temp_list_orders_buy_obj->exists() ) { //Begin $temp_list_orders_buy_obj->exists() )
1803
1804 $temp_list_orders_buy_obj->update( [
1805 'desired_sil' => $curent_sell_quantity,
1806 'split_id' => $sell_split_id,
1807 ] );
1808
1809 return $m_FIFO_Sell_order;
1810
1811 } // END $temp_list_orders_buy_obj->exists() )
1812
1813 } // END if ( $sell_price > $buy_price || $buy_price == 0
1814
1815 print( 'ALLOCATED QUANITYT =' . $allocated_top_order_quantity . "<br>" );
1816 print( 'CURRENT QUANITYT =' . $curent_sell_quantity . "<br>" );
1817 print( 'MATCHING PROCESS =' . $matching_process_type . "<br>" );
1818
1819 // test to see if top order large enough to absorb Allocated_top_order_quantity
1820 if ( $allocated_top_order_quantity >= $buy_quantity ) { //Begin if ( $allocated_top_order_quantity > $buy_quantity )
1821
1822 /**
1823 * top buyl will be fully matched and closed (sell order allocated >= buy order)
1824 * copy of fully matched buy order to table "Buy_matched_orders"
1825 */
1826 print( '<br> BUY MATCHED ORDER CREATED <br>' );
1827 BuyMatchedOrder::create( [
1828 'buy_order_id' => $buy_order_id,
1829 'buy_split_id' => $buy_split_id,
1830 'buy_price' => $buy_price,
1831 'buy_user_id' => $buy_user_id,
1832 'buy_quantity' => $buy_quantity,
1833 'matching_date' => $matching_date,
1834 'matching_time' => $matching_time,
1835 'matching_id' => $previous_buy_matching_id + 1,
1836 'matching_process' => $matching_process_type
1837
1838 ] );
1839 /**
1840 * update of status and split_id in the table "Temporary list of all orders"
1841 */
1842 print( '<br> Update Temp List orders for Order # ' . $buy_order_id . "<br><br>" );
1843 $temp_list_orders_buy_obj = TempListOrders::where( [ 'order_number' => $buy_order_id ] );
1844
1845 if ( $temp_list_orders_buy_obj->exists() ) { //Begin if ( $temp_list_orders_buy_obj->exists() )
1846 $temp_list_orders_buy_obj->update( [
1847 'status' => 'fully_matched',
1848 'split_id' => $buy_split_id,
1849 ] );
1850 } //End if ( $temp_list_orders_buy_obj->exists() )
1851
1852 //Delete from BuyOrderRankbyNbPrice for the Order# we found above
1853 BuyOrdersRankByPriceNb::where( 'order_number', $buy_order_id )->delete();
1854 print( "<br> Order # " . $buy_order_id . " removed from BuyOrdersRankByPriceNb <br>" );
1855
1856 /**
1857 * adjustemnt of sell order in Table "Sell orders rank by price & nb"
1858 * remaining unmatched buy quantities
1859 */
1860
1861 print( 'sell_quantity_q0 =' . $sell_quantity_q0 );
1862 $sell_quantity_q1 = intval( $sell_quantity_q0 - $buy_quantity );
1863 print( '<br> Remaining Quantity =' . $sell_quantity_q1 );
1864
1865 //$sell_quantity_q1 = 0; // WARNING: THIS NEEDS TO BE REMOVED.
1866 // COND1
1867 if ( $sell_quantity_q1 == 0 ) { //Begin if ( $sell_quantity_q1 == 0
1868 // if remaining quantity=0 sell order fully matched stop matching
1869 // update of quantity in Table "Sell orders rank by price & nb"
1870 $sell_orders_rank_by_price_and_nb_17_table->update( [ 'desired_sil' => 0 ] );
1871 print( '<br>Updated SellOrderRankByPrice for Order #' . $sell_order_id . "<br>" );
1872
1873 //update status of sell order in Table "Temporary_list_of_all_order"
1874 $temp_list_all_orders = TempListOrders::where( [ 'order_number' => $sell_order_id ] );
1875
1876 if ( $temp_list_all_orders->exists() ) { //Begin if ( $temp_list_all_orders->exists() )
1877 $temp_list_all_orders->update( [
1878 'status' => 'fully_matched',
1879 'split_id' => $sell_split_id,
1880 ] );
1881 print( '<br>Updated TempListOrders for Order #' . $sell_order_id . "<br>" );
1882
1883 } //End if ( $temp_list_all_orders->exists() )
1884
1885 } //End if ( $sell_quantity_q1 == 0 )
1886 else { //Else Cond1
1887 print( '<br> IN ELSE CONDITION 1 <br>' );
1888 /**
1889 * if remaining quantity <> 0 sell order NOT fully matched matching should continue
1890 * update quantity in Table "Sell orders rank by price & nb"
1891 */
1892 $sell_orders_rank_by_price_sell_order_table = SellOrdersRankByPriceNb::where( 'order_number',
1893 $sell_order_id );
1894
1895 if ( $sell_orders_rank_by_price_sell_order_table->exists() ) { //Begin if ( $sell_orders_rank_by_price_sell_order_table->exists() )
1896 $sell_orders_rank_by_price_sell_order_table->update( [
1897 'desired_sil' => $sell_quantity_q1,
1898 'split_id' => $sell_split_id . '_1'
1899 ] );
1900 print( 'SellOrderRankByPrice updated for remaining desired SIL <br>' );
1901
1902 } //End if ( $sell_orders_rank_by_price_sell_order_table->exists() )
1903 }
1904
1905 SellMatchedOrder::create( [
1906 'sell_order_id' => $sell_order_id,
1907 'sell_split_id' => $sell_split_id,
1908 'sell_price' => $sell_price,
1909 'sell_user_id' => $sell_user_id,
1910 'sell_quantity' => $buy_quantity, // Pascal, is this correct or current_buy_quantity?
1911 'matching_date' => $matching_date,
1912 'matching_time' => $matching_time,
1913 'matching_id' => $previous_sell_matching_id + 1,
1914 'matching_process' => $matching_process_type
1915 ] );
1916 print( '<br>Inserted Record in SellMatchedOrder for Order #' . $sell_order_id . "<br>" );
1917
1918 // END OF ELSE COND1
1919
1920 } //End if ( $allocated_top_order_quantity > $buy_quantity )
1921 else {
1922
1923 /**
1924 * \\ top buy will not be fully matched but sell order could be fully matched if
1925 * alloc =100% (sell order allocated < buyl order
1926 */
1927 print( '<br> SELL QUANTITY < BUY ORDER <br>' );
1928 /**
1929 * copy of partially matched buy order to table "matched orders"
1930 */
1931
1932 $allocated_buy_quantity = $allocated_top_order_quantity;
1933 //update of Sell order quantity and split id
1934 $updated_quantity = $buy_quantity - $allocated_buy_quantity;
1935 print( '<br> UPDATED QUANTITY = ' . $updated_quantity . "<br>" );
1936 $buy_order_rankby_price_table = BuyOrdersRankByPriceNb::where( 'order_number', $buy_order_id );
1937
1938 if ( $buy_order_rankby_price_table->exists() ) { //Begin if ( $buy_order_rankby_price_table->exists() )
1939 $buy_order_rankby_price_table->update( [
1940 'desired_sil' => $updated_quantity,
1941 'split_id' => $buy_split_id . '_1'
1942 ] );
1943 print( 'BuyOrdersRankByPriceNb updated with updated split id' );
1944 BuyMatchedOrder::create( [
1945 'buy_order_id' => $buy_order_id,
1946 'buy_split_id' => $buy_split_id,
1947 'buy_price' => $buy_price,
1948 'buy_user_id' => $buy_user_id,
1949 'buy_quantity' => $allocated_buy_quantity,
1950 'matching_date' => $matching_date,
1951 'matching_time' => $matching_time,
1952 'matching_id' => $previous_buy_matching_id + 1,
1953 'matching_process' => $matching_process_type
1954
1955 ] );
1956
1957 print( 'Inserted new record in BuyMatchedOrder or Buy Order Id #' . $buy_order_id . "<br>" );
1958 // remaining unmatched buy quantities
1959 $sell_quantity_q1 = $sell_quantity_q0 - $allocated_top_order_quantity;
1960 print( '<br><br>$buy_quantity_q1 = ' . $sell_quantity_q1 . "<br>" );
1961 //$sell_quantity_q1 = 0; //WARNING TEMP
1962 //START OF COND2
1963 if ( $sell_quantity_q1 == 0 ) { //Begin if ( $sell_quantity_q1 == 0 )
1964 $sell_orders_rank_by_price_and_nb_17_table->update( [ 'desired_sil' => 0 ] );
1965 print( '<br>Updated SellOrderRankByPrice for Order #' . $sell_order_id . "<br>" );
1966 //update status of sell order in Table "Temporary_list_of_all_order"
1967 $temp_list_all_orders = TempListOrders::where( [ 'order_number' => $sell_order_id ] );
1968
1969 if ( $temp_list_all_orders->exists() ) { // Begin if ( $temp_list_all_orders->exists() )
1970
1971 $temp_list_all_orders->update( [
1972 'status' => 'fully_matched',
1973 'split_id' => $sell_split_id,
1974 ] );
1975 print( '<br>Updated TempListOrders for Order #' . $sell_order_id . "<br>" );
1976 } //End if ( $temp_list_all_orders->exists() )
1977
1978 //update split_id and quantity of buy order in Table "Temporary_list_of_all_order"
1979 $temp_list_all_orders = TempListOrders::where( [ 'order_number' => $buy_order_id ] );
1980
1981 if ( $temp_list_all_orders->exists() ) { //Begin if ( $temp_list_all_orders->exists() )
1982
1983 $temp_list_all_orders->update( [
1984 'desired_sil' => intval( $current_quantity - $allocated_top_order_quantity ),
1985 'split_id' => $current_split_id . '_1',
1986 ] );
1987 print( '<br>Updated TempListOrders for Order #' . $buy_order_id . "<br>" );
1988
1989
1990 } //End if ( $temp_list_all_orders->exists() )
1991
1992
1993 }//End if ( $sell_quantity_q1 == 0 )
1994
1995 else {
1996
1997 //ELSE COND2
1998 print( '<br> IN ELSE CONDITION # 2 <br>' );
1999 /**
2000 * if remaining quantity <> 0 sell order NOT fully matched matching should continue
2001 * update quantity in Table "Sell orders rank by price & nb"
2002 */
2003 $sell_orders_rank_by_price_buy_order_table = SellOrdersRankByPriceNb::where( 'order_number',
2004 $sell_order_id );
2005 if ( $sell_orders_rank_by_price_buy_order_table->exists() ) { // Begin if ( $sell_orders_rank_by_price_buy_order_table->exists() )
2006 $sell_orders_rank_by_price_buy_order_table->update( [
2007 'desired_sil' => $sell_quantity_q1,
2008 'split_id' => $sell_split_id . '_1'
2009 ] );
2010 print( 'SellOrderRankByPrice updated for remaining desired SIL <br>' );
2011
2012 } // End if ( $sell_orders_rank_by_price_buy_order_table->exists() )
2013 } //Else conditions 2 ends here
2014
2015 SellMatchedOrder::create( [
2016 'sell_order_id' => $sell_order_id,
2017 'sell_split_id' => $sell_split_id,
2018 'sell_price' => $sell_price,
2019 'sell_user_id' => $sell_user_id,
2020 'sell_quantity' => $allocated_buy_quantity,
2021 'matching_date' => $matching_date,
2022 'matching_time' => $matching_time,
2023 'matching_id' => $previous_sell_matching_id + 1,
2024 'matching_process' => $matching_process_type
2025
2026 ] );
2027 print( '<br>2nd time...Inserted Record in BuyMatchedOrder for Order #' . $buy_order_id . "<br>" );
2028
2029 } //End if ( $buy_order_rankby_price_table->exists() )
2030 } //Else Condition 2 ends here
2031
2032 } //Begin $buy_orders_rank_by_price_and_nb_17_obj Cond
2033
2034 $m_FIFO_Sell_order = $sell_quantity_q1;
2035
2036 } catch ( Exception $e ) {
2037 print( '<br>Exception in macro_FIFO_Sell_order <br>' );
2038 print( '<br>Line # ' . $e->getLine() . "<br>" );
2039 print( '<br>Message # ' . $e->getLine() . "<br>" );
2040 }
2041
2042
2043 return $m_FIFO_Sell_order;
2044
2045 } //End of function
2046
2047 /**
2048 * @param $allocated_top_order_quantity
2049 * @param $buy_quantity_q0
2050 * @param $matching_date
2051 * @param $matching_time
2052 * @param $matching_process_type
2053 */
2054 private
2055 function macro_FIFO_Buy_order(
2056 $allocated_top_order_quantity,
2057 $buy_quantity_q0,
2058 $matching_date,
2059 $matching_time,
2060 $matching_process_type
2061 ) {
2062
2063 $sell_order_id = 0;
2064 $sell_price = 0;
2065 $buy_price = 0;
2066
2067 try {
2068 /**
2069 * Fetching Buy Order Data
2070 */
2071 $buy_orders_rank_by_price_and_nb_17_table = BuyOrdersRankByPriceNb::orderBy( 'price',
2072 'DESC' )->orderBy( 'order_number', 'ASC' )->limit( 1 );
2073
2074 $buy_orders_rank_by_price_and_nb_17_obj = $buy_orders_rank_by_price_and_nb_17_table->get();
2075
2076 if ( count( $buy_orders_rank_by_price_and_nb_17_obj ) > 0 ) {
2077 $buy_order_id = $buy_orders_rank_by_price_and_nb_17_obj[0]->order_number;
2078
2079
2080 $buy_split_id = $buy_orders_rank_by_price_and_nb_17_obj[0]->split_id;
2081 $buy_quantity = $buy_orders_rank_by_price_and_nb_17_obj[0]->desired_sil;
2082 $buy_price = $buy_orders_rank_by_price_and_nb_17_obj[0]->price;
2083 $buy_user_id = $buy_orders_rank_by_price_and_nb_17_obj[0]->user_id;
2084
2085// print( "<br> BUY ORDER ID = " . $buy_order_id . "<br>" );
2086// print( "<br> BUY SPLIT ID = " . $buy_split_id . "<br>" );
2087// print( "<br> BUY QUANTITY = " . $buy_quantity . "<br>" );
2088// print( "<br> BUY PRICE = " . $buy_price . "<br>" );
2089// print( "<br> BUY USER_ID = " . $buy_user_id . "<br>" );
2090 }
2091
2092 /**
2093 * Fetching Sell Order Data
2094 */
2095
2096 // We are doing ASC because we want the first row of both tables.
2097 $sell_orders_rank_by_price_and_nb_17_table = SellOrdersRankByPriceNb::orderBy( 'price',
2098 'asc' )->orderBy( 'order_number', 'ASC' )->limit( 1 );
2099
2100 if ( $sell_orders_rank_by_price_and_nb_17_table->exists() ) {
2101 $sell_orders_rank_by_price_and_nb_17_obj = $sell_orders_rank_by_price_and_nb_17_table->get();
2102 $sell_price = $sell_orders_rank_by_price_and_nb_17_obj[0]->price;
2103 $curent_sell_quantity = $sell_orders_rank_by_price_and_nb_17_obj[0]->desired_sil;
2104
2105 $sell_split_id = $sell_orders_rank_by_price_and_nb_17_obj[0]->split_id;
2106 $sell_user_id = $sell_orders_rank_by_price_and_nb_17_obj[0]->user_id;
2107
2108 $sell_order_id = $sell_orders_rank_by_price_and_nb_17_obj[0]->order_number;
2109
2110 print( "<br><br>" );
2111// print( "<br> SELL PRICE = " . $sell_price . "<br>" );
2112// print( "<br> SELL DESIRED SIL = " . $curent_sell_quantity . "<br>" );
2113// print( "<br> SELL ORDER ID = " . $sell_order_id . "<br>" );
2114// print( "<br> SELL SPLIT ID = " . $sell_split_id . "<br>" );
2115// print( "<br> SELL USER_ID = " . $sell_user_id . "<br>" );
2116 }
2117
2118 // Check whether Sell Matched Order is Empty
2119 $sell_matched_order_obj = SellMatchedOrder::orderBy( 'id', 'DESC' )->limit( 1 )->first();
2120
2121 //test if there is no sell matching order yet
2122 if ( $sell_matched_order_obj == null ) {
2123 $previous_sell_matching_id = 0;
2124
2125 } else {
2126 $previous_sell_matching_id = $sell_matched_order_obj->matching_id;
2127 print ( 'MATCHING ID = ' . $previous_sell_matching_id . "<br>" );
2128
2129 }
2130 // Check whether Buy Matched Order is Empty
2131 $buy_matched_order_obj = BuyMatchedOrder::orderBy( 'id', 'DESC' )->limit( 1 )->first();
2132 print ( "<br> Buy Matched Order Table Related <br>" );
2133
2134 //test if there is no sell matching order yet
2135 if ( $buy_matched_order_obj == null ) {
2136 $previous_buy_matching_id = 0;
2137 } else {
2138 $previous_buy_matching_id = $buy_matched_order_obj->matching_id;
2139 print ( '<br> BUY MATCHING ID = ' . $previous_buy_matching_id . "<br>" );
2140 }
2141
2142 // Initialize variables with data coming from table Temporary_list_of_all_orders
2143 $temp_list_orders_obj = TempListOrders::where( [ 'order_number' => $sell_order_id ] )->first();
2144
2145 $current_split_id = '';
2146 $current_quantity = 0;
2147
2148 if ( $temp_list_orders_obj ) {
2149 $current_split_id = $temp_list_orders_obj->split_id;
2150 $current_quantity = $temp_list_orders_obj->desired_sil;
2151 }
2152
2153 /**
2154 * test to see if Sell_price > Buy_price if yes by convention FIFO_Buy_order = -1 and matching stops.
2155 * test to see if Sell_price is empty it means that there is no more sell order available
2156 */
2157
2158 if ( $sell_price > $buy_price || $sell_price == 0 ) {
2159 $m_FIFO_Buy_order = - 1;
2160
2161 /**
2162 * Select Records from Temp List Of Orders and update Desired SIL and SPLIT ID
2163 */
2164
2165 $temp_list_orders_buy_obj = TempListOrders::where( [ 'order_number' => $buy_order_id ] );
2166
2167 if ( $temp_list_orders_buy_obj->exists() ) {
2168 print( 'IT DOES EXIST' . "<br>" );
2169 $temp_list_orders_buy_obj->update( [
2170 'desired_sil' => $buy_quantity,
2171 'split_id' => $buy_split_id,
2172 ] );
2173
2174 return $m_FIFO_Buy_order;
2175 }
2176 }
2177
2178 print( 'ALLOCATED QUANITYT =' . $allocated_top_order_quantity . "<br>" );
2179 print( 'CURRENT QUANITYT =' . $curent_sell_quantity . "<br>" );
2180 print( 'MATCHING PROCESS =' . $matching_process_type . "<br>" );
2181
2182 // test to see if top order large enough to absorb Allocated_top_order_quantity
2183 if ( $allocated_top_order_quantity >= $curent_sell_quantity ) {
2184
2185 /**
2186 * top sell will be fully matched and closed (buy order allocated >= sell order)
2187 * copy of fully matched sell order to table "Sell_matched_orders"
2188 */
2189
2190 print( '<br> SELL MATCHED ORDER CREATED <br>' );
2191
2192 SellMatchedOrder::create(
2193 [
2194 'sell_order_id' => $sell_order_id,
2195 'sell_split_id' => $sell_split_id,
2196 'sell_price' => $sell_price,
2197 'sell_user_id' => $sell_user_id,
2198 'sell_quantity' => $curent_sell_quantity,
2199 'matching_date' => $matching_date,
2200 'matching_time' => $matching_time,
2201 'matching_id' => $previous_sell_matching_id + 1,
2202 'matching_process' => $matching_process_type
2203 ]
2204 );
2205
2206 /**
2207 * update of status and split_id in the table "Temporary list of all orders"
2208 */
2209 print( '<br> Update Temp List orders for Order # ' . $sell_order_id . "<br><br>" );
2210
2211 $temp_list_orders_sell_obj = TempListOrders::where( [ 'order_number' => $sell_order_id ] );
2212
2213 if ( $temp_list_orders_sell_obj->exists() ) {
2214
2215
2216 $temp_list_orders_sell_obj->update( [
2217 'status' => 'fully_matched',
2218 'split_id' => $sell_split_id,
2219 ] );
2220 }
2221
2222 //Delete from SellOrderRankbyNbPrice for the Order# we found above
2223 SellOrdersRankByPriceNb::where( 'order_number', $sell_order_id )->delete();
2224 print( "<br> Order # " . $sell_order_id . " removed from SellOrdersRankByPriceNb <br>" );
2225
2226 /**
2227 * adjustemnt of buy order in Table "Buy orders rank by price & nb"
2228 * remaining unmatched buy quantities
2229 */
2230
2231 print( 'Buy_quantity_q0 =' . $buy_quantity_q0 );
2232 $buy_quantity_q1 = intval( $buy_quantity_q0 - $curent_sell_quantity );
2233 print( '<br> Remaining Quantity =' . $buy_quantity_q1 );
2234
2235 //$buy_quantity_q1 = 0; // WARNING: THIS NEEDS TO BE REMOVED.
2236
2237 //COND
2238 if ( $buy_quantity_q1 == 0 ) {
2239
2240 // if remaining quantity=0 buy order fully matched stop matching
2241 // update of quantity in Table "Buy orders rank by price & nb"
2242 $buy_orders_rank_by_price_and_nb_17_table->update( [ 'desired_sil' => 0 ] );
2243
2244
2245 print( '<br>Updated BuyOrderRankByPrice for Order #' . $buy_order_id . "<br>" );
2246
2247 //update status of buy order in Table "Temporary_list_of_all_order"
2248
2249 $temp_list_all_orders = TempListOrders::where( [ 'order_number' => $buy_order_id ] );
2250
2251 if ( $temp_list_all_orders->exists() ) {
2252
2253 $temp_list_all_orders->update( [
2254 'status' => 'fully_matched',
2255 'split_id' => $buy_split_id,
2256 ] );
2257 print( '<br>Updated TempListOrders for Order #' . $buy_order_id . "<br>" );
2258
2259 }
2260
2261 } else {
2262 //ELSE COND
2263 print( '<br> IN ELSE CONDITION <br>' );
2264 /**
2265 * if remaining quantity <> 0 buy order NOT fully matched matching should continue
2266 * update quantity in Table "Buy orders rank by price & nb"
2267 */
2268
2269 $buy_orders_rank_by_price_buy_order_table = BuyOrdersRankByPriceNb::where( 'order_number',
2270 $buy_order_id );
2271
2272 if ( $buy_orders_rank_by_price_buy_order_table->exists() ) {
2273
2274 $buy_orders_rank_by_price_buy_order_table->update( [
2275 'desired_sil' => $buy_quantity_q1,
2276 'split_id' => $buy_split_id . '_1'
2277 ] );
2278
2279 print( 'BuyOrderRankByPrice updated for remaining desired SIL <br>' );
2280 }
2281 }
2282
2283 BuyMatchedOrder::create(
2284 [
2285 'buy_order_id' => $buy_order_id,
2286 'buy_split_id' => $buy_split_id,
2287 'buy_price' => $buy_price,
2288 'buy_user_id' => $buy_user_id,
2289 'buy_quantity' => $curent_sell_quantity,
2290 'matching_date' => $matching_date,
2291 'matching_time' => $matching_time,
2292 'matching_id' => $previous_buy_matching_id + 1,
2293 'matching_process' => $matching_process_type
2294 ]
2295 );
2296 print( '<br>Inserted Record in BuyMatchedOrder for Order #' . $buy_order_id . "<br>" );
2297 // END of ELSE COND1
2298 } else {
2299 /**
2300 * \\ top sell will not be fully matched but buy order could be fully matched if
2301 * alloc =100% (buy orderallocated < sell order
2302 */
2303 print( '<br> DESIRED SIL < SELL QUANTITY <br>' );
2304
2305 /**
2306 * copy of partially matched sell order to array "matched orders"
2307 */
2308 $sell_quantity = $allocated_top_order_quantity;
2309 //update of Sell order quantity and split id
2310 $updated_quantity = $curent_sell_quantity - $sell_quantity;
2311
2312 print( '<br> UPDATED QUANTITY = ' . $updated_quantity . "<br>" );
2313 $sell_order_rankby_price_table = SellOrdersRankByPriceNb::where( 'order_number', $sell_order_id );
2314
2315 if ( $sell_order_rankby_price_table->exists() ) {
2316
2317
2318 $sell_order_rankby_price_table->update( [
2319 'desired_sil' => $updated_quantity,
2320 'split_id' => $sell_split_id . '_1'
2321 ] );
2322 }
2323
2324 print( 'SellOrdersRankByPriceNb updated with updated split id' );
2325
2326
2327 SellMatchedOrder::create(
2328 [
2329 'sell_order_id' => $sell_order_id,
2330 'sell_split_id' => $sell_split_id,
2331 'sell_price' => $sell_price,
2332 'sell_user_id' => $sell_user_id,
2333 'sell_quantity' => $sell_quantity,
2334 'matching_date' => $matching_date,
2335 'matching_time' => $matching_time,
2336 'matching_id' => $previous_sell_matching_id + 1,
2337 'matching_process' => $matching_process_type
2338 ]
2339 );
2340
2341 print( 'Inserted new record in SellMatchedOrder or Sell Order Id #' . $sell_order_id . "<br>" );
2342
2343 /**
2344 * adjustemnt of buy order in Table "Buy orders rank by price & nb"
2345 */
2346
2347 // remaining unmatched buy quantities
2348 $buy_quantity_q1 = $buy_quantity_q0 - $allocated_top_order_quantity;
2349 print( '<br><br>$buy_quantity_q1 = ' . $buy_quantity_q1 . "<br>" );
2350
2351 //$buy_quantity_q1 = 0; //WARNING TEMP
2352
2353
2354 //Start of COND2
2355 if ( $buy_quantity_q1 == 0 ) {
2356
2357 $buy_orders_rank_by_price_and_nb_17_table->update( [ 'desired_sil' => 0 ] );
2358 print( '<br>Updated BuyOrderRankByPrice for Order #' . $buy_order_id . "<br>" );
2359
2360 //update status of buy order in Table "Temporary_list_of_all_order"
2361
2362 $temp_list_all_orders = TempListOrders::where( [ 'order_number' => $buy_order_id ] );
2363
2364
2365 if ( $temp_list_all_orders->exists() ) {
2366 $temp_list_all_orders->update( [
2367 'status' => 'fully_matched',
2368 'split_id' => $buy_split_id,
2369 ] );
2370 print( '<br>Updated TempListOrders for Order #' . $buy_order_id . "<br>" );
2371 }
2372
2373 //update split_id and quantity of sell order in Table "Temporary_list_of_all_order"
2374 $temp_list_all_orders = TempListOrders::where( [ 'order_number' => $sell_order_id ] );
2375
2376
2377 if ( $temp_list_all_orders->exists() ) {
2378 $temp_list_all_orders->update( [
2379 'desired_sil' => intval( $current_quantity - $allocated_top_order_quantity ),
2380 'split_id' => $current_split_id . '_1',
2381 ] );
2382 print( '<br>Updated TempListOrders for Order #' . $sell_order_id . "<br>" );
2383 }
2384
2385
2386 } else {
2387
2388 //ELSE COND2
2389
2390 print( '<br> IN ELSE CONDITION # 2 <br>' );
2391
2392 /**
2393 * if remaining quantity <> 0 buy order NOT fully matched matching should continue
2394 * update quantity in Table "Buy orders rank by price & nb"
2395 */
2396
2397 $buy_orders_rank_by_price_buy_order_table = BuyOrdersRankByPriceNb::where( 'order_number',
2398 $buy_order_id );
2399
2400 if ( $buy_orders_rank_by_price_buy_order_table->exists() ) {
2401
2402 $buy_orders_rank_by_price_buy_order_table->update( [
2403 'desired_sil' => $buy_quantity_q1,
2404 'split_id' => $buy_split_id . '_1'
2405 ] );
2406
2407 print( 'BuyOrderRankByPrice updated for remaining desired SIL <br>' );
2408 }
2409 } //Else conditions ends here
2410
2411 BuyMatchedOrder::create(
2412 [
2413 'buy_order_id' => $buy_order_id,
2414 'buy_split_id' => $buy_split_id,
2415 'buy_price' => $buy_price,
2416 'buy_user_id' => $buy_user_id,
2417 //'buy_quantity' => $curent_sell_quantity,
2418 'buy_quantity' => $sell_quantity,
2419 'matching_date' => $matching_date,
2420 'matching_time' => $matching_time,
2421 'matching_id' => $previous_buy_matching_id + 1,
2422 'matching_process' => $matching_process_type
2423 ]
2424 );
2425 print( '<br>2nd time...Inserted Record in BuyMatchedOrder for Order #' . $buy_order_id . "<br>" );
2426 //End Else Cond 2
2427 }
2428 $m_FIFO_Buy_order = $buy_quantity_q1;
2429
2430 } catch ( Exception $e ) {
2431 print( '<br>Exception in macro_FIFO_Buy_order <br>' );
2432 print( '<br>Line # ' . $e->getLine() . "<br>" );
2433 print( '<br>Message # ' . $e->getLine() . "<br>" );
2434 }
2435
2436
2437 return $m_FIFO_Buy_order;
2438 }
2439
2440 private
2441 function Macro14() {
2442 print "<br> Calling Marco 14 <br>";
2443
2444 //Make sure matching tables of BUy and Sell are fully empty
2445 BuyMatchedOrder::truncate();
2446 SellMatchedOrder::truncate();
2447
2448 $loop_stop = 0;
2449 $a_single_buy_order = 0;
2450 $a_single_sell_order = 0;
2451
2452 $first_buy_bid_price = 0;
2453 $first_sell_ask_price = 0;
2454
2455 $first_num_bid_order = 0;
2456 $first_num_ask_order = 0;
2457
2458 $first_num_bid_size = 0;
2459 $first_num_ask_size = 0;
2460 $quantity_to_be_bought_per_FIFO_final_pass = 50;
2461
2462
2463 $first_temp_order_book_buy = TemporaryOrderBookBuy::orderBy( 'id', 'asc' )->limit( 1 )->get();
2464 $first_temp_order_book_sell = TemporaryOrderBookSell::orderBy( 'id', 'asc' )->limit( 1 )->get();
2465
2466 if ( count( $first_temp_order_book_buy ) > 0 ) {
2467 $first_num_bid_order = $first_temp_order_book_buy[0]->num_bid_order;
2468 $first_num_bid_size = $first_temp_order_book_buy[0]->bid_size;
2469// print( "<br> FIRST NUM BID ORDER: " . $first_num_bid_order . "<br>" );
2470// print( "<br> FIRST NUM BID SIZE: " . $first_num_bid_size . "<br>" );
2471
2472 }
2473
2474 if ( count( $first_temp_order_book_sell ) > 0 ) {
2475 $first_num_ask_order = $first_temp_order_book_sell[0]->num_ask_order;
2476 //print( "<br> FIRST NUM ASK ORDER: " . $first_num_ask_order . "<br>" );
2477
2478 $first_num_ask_size = $first_temp_order_book_sell[0]->ask_size;
2479
2480 //print( "<br> FIRST NUM ASK SIZE: " . $first_num_ask_size . "<br>" );
2481 }
2482
2483 // ' A single Buy order to be matched with sell order(s)
2484// print( '<br>first_num_bid_order = ' . $first_num_bid_order . "<br>" );
2485// print( '<br>first_num_ask_order = ' . $first_num_ask_order . "<br>" );
2486
2487 if ( $first_num_bid_order == 1 && $first_num_ask_order != 1 ) {
2488 $a_single_buy_order = 1;
2489 }
2490
2491 if ( $first_num_bid_order != 1 && $first_num_ask_order == 1 ) {
2492 $a_single_sell_order = 1;
2493 }
2494
2495 if ( $first_num_bid_order == 1 && $first_num_ask_order == 1 ) {
2496
2497 if ( $first_num_ask_size < $first_num_bid_size ) {
2498 $a_single_buy_order = 1;
2499 } else {
2500 $a_single_sell_order = 1;
2501 }
2502 }
2503
2504 // If it happens, somthing goes wrong with order book and it should exit
2505 if ( $first_num_bid_order > 1 && $first_num_ask_order > 1 ) {
2506 $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';
2507
2508 return $msg;
2509 }
2510
2511 /**
2512 * this is the beginning of matching technique step 4 , step 1, 2 and 3 are NOT in prototype version 2
2513 * initialization of 2 variables in bold normally performed in step 1
2514 */
2515 $matching_date = date( 'Y-m-d' );
2516 $matching_time = date( 'H:i:s' );
2517
2518
2519 if ( $a_single_buy_order == 1 ) {
2520 /**
2521 * ‘\\ we are going to match a buy order
2522 * ‘\\ start with initialization of 7 variables (in bold):
2523 * ‘\\ the first variable should normally come from matching technique step 3 which is not implement in prototype version 2
2524 */
2525// $remaining_unmatched_Buy_quantities_after_step_3 = $first_temp_order_book_buy = TemporaryOrderBookBuy::orderBy( 'id',
2526// 'asc' )->limit( 1 )->get();
2527
2528 $remaining_unmatched_Buy_quantities_after_step_3 = TemporaryOrderBookBuy::orderBy( 'id',
2529 'asc' )->limit( 1 )->get();
2530
2531 if ( count( $remaining_unmatched_Buy_quantities_after_step_3 ) > 0 ) {
2532
2533 $remaining_unmatched_Buy_quantities_after_step_3 = intval( $remaining_unmatched_Buy_quantities_after_step_3[0]->bid_size );
2534 }
2535
2536
2537 $matching_process_type = "FIFO_FINAL_PASS_BUY";
2538 $allocated_buy_order_FIFO_final_pass = $remaining_unmatched_Buy_quantities_after_step_3;
2539 $quantity_to_be_bought_per_FIFO_final_pass = $allocated_buy_order_FIFO_final_pass;
2540 $total_quantity_still_to_buy_before_matching_FIFO_final_pass = $allocated_buy_order_FIFO_final_pass;
2541 $remaining_buy_quantity_after_FIFO_final_pass = $total_quantity_still_to_buy_before_matching_FIFO_final_pass;
2542 //$FIFO_final_pass_Buy_order_final_quantity = 0;
2543
2544 $idx = 0;
2545 $no_of_times = 300;
2546
2547
2548 while ( $remaining_buy_quantity_after_FIFO_final_pass > 0 ) {
2549 $idx ++;
2550 print( "Step# " . $idx . "<br>" );
2551
2552 // We have to kill the loop if it takes long in matching, means kill after 200 iterations.
2553 if ( $idx >= $no_of_times ) {
2554 break;
2555 }
2556
2557 //Calling FII BUY order
2558 $remaining_buy_quantity_after_FIFO_final_pass = $this->macro_FIFO_Buy_order( $quantity_to_be_bought_per_FIFO_final_pass,
2559 $total_quantity_still_to_buy_before_matching_FIFO_final_pass, $matching_date, $matching_time,
2560 $matching_process_type );
2561 /**
2562 * 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
2563 * buy price nothing could be matched anymore
2564 * matching stops and remaing buy quantity should be in list of all orders
2565 */
2566
2567 if ( $remaining_buy_quantity_after_FIFO_final_pass == - 1 ) { //Begin if ( $remaining_buy_quantity_after_FIFO_final_pass == - 1 )
2568 //Fetching Buy order Rank BY Price data
2569 $Buy_orders_rank_by_price_and_nb_temporary_table = BuyOrdersRankByPriceNb::orderBy( 'price',
2570 'DESC' )->orderBy( 'order_number', 'ASC' )->limit( 1 );
2571 $Buy_orders_rank_by_price_and_nb_temporary_obj = $Buy_orders_rank_by_price_and_nb_temporary_table->get();
2572
2573 if ( count( $Buy_orders_rank_by_price_and_nb_temporary_obj ) > 0 ) { //Begin if ( count( $Buy_orders_rank_by_price_and_nb_temporary_obj )
2574 $Final_Buy_quantity_remaining_after_step_4 = $Buy_orders_rank_by_price_and_nb_temporary_obj[0]->desired_sil;
2575 $Final_split_id_after_step_4 = $Buy_orders_rank_by_price_and_nb_temporary_obj[0]->split_id;
2576 $buy_order_id = $Buy_orders_rank_by_price_and_nb_temporary_obj[0]->order_number;
2577 } ////End if ( count( $Buy_orders_rank_by_price_and_nb_temporary_obj )
2578
2579 /**
2580 * update the remaining quantity of Buy order in the list of all order and put status of Buy order as fully matched if needed
2581 */
2582 $temp_list_orders_buy_obj = TempListOrders::where( [ 'order_number' => $buy_order_id ] );
2583
2584 if ( $temp_list_orders_buy_obj->exists() ) {
2585
2586 $temp_list_orders_buy_obj->update( [
2587 'split_id' => $Final_split_id_after_step_4,
2588 ] );
2589
2590 if ( $Final_Buy_quantity_remaining_after_step_4 == 0 ) { //Begin if($Final_Buy_quantity_remaining_after_step_4 == 0)
2591 $temp_list_orders_buy_obj->update( [
2592 'status' => 'fully_matched'
2593 ] );
2594 } //End if($Final_Buy_quantity_remaining_after_step_4 == 0)
2595 else {
2596
2597 if ( $temp_list_orders_buy_obj->exists() ) {
2598 $temp_list_orders_buy_obj->update( [
2599 'desired_sil' => $Final_Buy_quantity_remaining_after_step_4
2600 ] );
2601 }
2602 }
2603// //Label:- Go to Update Matching Order
2604// //$a_single_buy_order = 0; // WARNING : TO REMOVE
2605// $loop_stop = $this->update_matching_order( $a_single_buy_order );
2606//
2607// if($loop_stop == 1) {
2608// break;
2609// }
2610 }
2611 //Label:- Go to Update Matching Order
2612 //$a_single_buy_order = 0; // WARNING : TO REMOVE
2613 $loop_stop = $this->update_matching_order( $a_single_buy_order );
2614
2615 if ( $loop_stop == 1 ) {
2616 break;
2617 }
2618 } //End if ( $remaining_buy_quantity_after_FIFO_final_pass == - 1 )
2619
2620
2621 $quantity_bought = intval( $total_quantity_still_to_buy_before_matching_FIFO_final_pass - $remaining_buy_quantity_after_FIFO_final_pass );
2622
2623 // This the intialisation for next pass:
2624 $quantity_to_be_bought_per_FIFO_final_pass = intval( $quantity_to_be_bought_per_FIFO_final_pass - $quantity_bought );
2625 print( '$quantity_to_be_bought_per_FIFO_final_pass = ' . $quantity_to_be_bought_per_FIFO_final_pass . "<br>" );
2626
2627 $total_quantity_still_to_buy_before_matching_FIFO_final_pass = $remaining_buy_quantity_after_FIFO_final_pass;
2628 } // end of loop buy
2629 } //End $a_single_buyoder= 1
2630 else { //Begin Else of $a_single_buyoder= 1
2631
2632 /**
2633 * we are going to match a sell order.
2634 * start with initialization of 7 variables
2635 * the first variable should normally come from matching technique step 3 which is not implement in prototype version 2
2636 */
2637
2638 $remaining_unmatched_Sell_quantities_after_step_3 = TemporaryOrderBookSell::orderBy( 'id',
2639 'asc' )->limit( 1 )->get();
2640
2641 if ( count( $remaining_unmatched_Sell_quantities_after_step_3 ) > 0 ) {
2642
2643 $remaining_unmatched_Sell_quantities_after_step_3 = intval( $remaining_unmatched_Sell_quantities_after_step_3[0]->ask_size );
2644 }
2645
2646 $matching_process_type = "FIFO_FINAL_PASS_SELL";
2647 $allocated_sell_order_FIFO_final_pass = $remaining_unmatched_Sell_quantities_after_step_3;
2648 $quantity_to_be_sold_per_FIFO_final_pass = $allocated_sell_order_FIFO_final_pass;
2649 $total_quantity_still_to_sell_before_matching_FIFO_final_pass = $allocated_sell_order_FIFO_final_pass;
2650 $remaining_Sell_quantity_after_FIFO_final_pass = $total_quantity_still_to_sell_before_matching_FIFO_final_pass;
2651 $idx = 0;
2652 $no_of_times = 300;
2653
2654 //Starting of Sell Loop
2655 print( '<br>Inside Sell Loop <br>' );
2656
2657 while ( $remaining_Sell_quantity_after_FIFO_final_pass > 0 ) { //Begin Sell Loop
2658 $idx ++;
2659 print( "Step# " . $idx . "<br>" );
2660 // We have to kill the loop if it takes long in matching, means kill after 200 iterations.
2661 if ( $idx >= $no_of_times ) {
2662 break;
2663 }
2664
2665 //Calling FIFO Sell Order method
2666 $remaining_Sell_quantity_after_FIFO_final_pass = $this->macro_FIFO_Sell_order( $quantity_to_be_sold_per_FIFO_final_pass,
2667 $total_quantity_still_to_sell_before_matching_FIFO_final_pass, $matching_date, $matching_time,
2668 $matching_process_type );
2669
2670 print( '$remaining_Sell_quantity_after_FIFO_final_pass after macro_FIFO_Sell_order = ' . $remaining_Sell_quantity_after_FIFO_final_pass . '<br>' );
2671 /**
2672 * 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
2673 * matching stops and remaining sell quantity should be updated in list of all order
2674 */
2675 if ( $remaining_Sell_quantity_after_FIFO_final_pass == - 1 ) { //Begin $remaining_Sell_quantity_after_FIFO_final_pass
2676
2677 // Fetching Sell order Rank BY Price data
2678 $sell_orders_rank_by_price_and_nb_temporary_table = SellOrdersRankByPriceNb::orderBy( 'price',
2679 'DESC' )->orderBy( 'order_number', 'ASC' )->limit( 1 );
2680
2681 $sell_orders_rank_by_price_and_nb_temporary_table_obj = $sell_orders_rank_by_price_and_nb_temporary_table->get();
2682
2683 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
2684
2685 $Final_Sell_quantity_remaining_after_step_4 = $sell_orders_rank_by_price_and_nb_temporary_table_obj[0]->desired_sil;
2686 $Final_split_id_after_step_4 = $sell_orders_rank_by_price_and_nb_temporary_table_obj[0]->split_id;
2687 $sell_order_id = $sell_orders_rank_by_price_and_nb_temporary_table_obj[0]->order_number;
2688
2689 } //Ends count( $sell_orders_rank_by_price_and_nb_temporary_table_obj ) > 0
2690
2691 /**
2692 * update the remaining quantity of Sell order in the list of all order and put status of Sell order as fully matched if needed
2693 */
2694
2695 $temp_list_orders_sell_obj = TempListOrders::where( [ 'order_number' => $sell_order_id ] );
2696
2697 if ( $temp_list_orders_sell_obj->exists() ) { //Begin $temp_list_orders_sell_obj->exists()
2698
2699 $temp_list_orders_sell_obj->update( [
2700 'split_id' => $Final_split_id_after_step_4,
2701 ] );
2702
2703 if ( $Final_Sell_quantity_remaining_after_step_4 == 0 ) { //Begin if($Final_Sell_quantity_remaining_after_step_4 == 0)
2704
2705 $temp_list_orders_sell_obj->update( [
2706 'status' => 'fully_matched'
2707 ] );
2708
2709 } //Ends //Begin if($Final_Sell_quantity_remaining_after_step_4 == 0)
2710 else {
2711
2712 if ( $temp_list_orders_sell_obj->exists() ) {
2713 $temp_list_orders_sell_obj->update( [
2714 'desired_sil' => $Final_Sell_quantity_remaining_after_step_4
2715 ] );
2716 }
2717 }
2718// $loop_stop = $this->update_matching_order( $a_single_buy_order );
2719//
2720// if($loop_stop == 1) {
2721// break;
2722// }
2723
2724
2725 } //Ends $temp_list_orders_sell_obj->exists()
2726
2727 /**
2728 * Moved from above
2729 */
2730 $loop_stop = $this->update_matching_order( $a_single_buy_order );
2731
2732 if ( $loop_stop == 1 ) {
2733 break;
2734 }
2735
2736
2737 } //End $remaining_Sell_quantity_after_FIFO_final_pass = -1
2738
2739 $quantity_sold = intval( $total_quantity_still_to_sell_before_matching_FIFO_final_pass - $remaining_Sell_quantity_after_FIFO_final_pass );
2740 // This the intialisation for next pass:
2741 $quantity_to_be_sold_per_FIFO_final_pass = intval( $quantity_to_be_sold_per_FIFO_final_pass - $quantity_sold );
2742 print( '$quantity_to_be_sold_per_FIFO_final_pass =' . $quantity_to_be_sold_per_FIFO_final_pass . "<br>" );
2743 $total_quantity_still_to_sell_before_matching_FIFO_final_pass = $remaining_Sell_quantity_after_FIFO_final_pass;
2744
2745 } //End Sell Loop
2746 } //End Else of $a_single_buyoder= 1
2747 $this->update_matching_order( $a_single_buy_order );
2748 }
2749
2750 private function Macro15() {
2751 print "<b>Calling Marco 15</b>";
2752
2753 // fetch data from table buy_matched_order
2754 $buy_matched_order_table = BuyMatchedOrder::orderBy( 'id', 'asc' )->limit( 1 );
2755
2756 if ( $buy_matched_order_table->exists() ) {
2757 $buy_matched_order_obj = $buy_matched_order_table->get();
2758 $order_matching_process_buy = $buy_matched_order_obj[0]->matching_process;
2759 $new_order_id_buy = $buy_matched_order_obj[0]->buy_order_id;
2760 print( "<br> order_matching_process_buy: " . $order_matching_process_buy . "<br>" );
2761 print( "<br> new_order_id_buy: " . $new_order_id_buy . "<br>" );
2762 }
2763
2764 // fetch data from table buy_matched_order
2765 $sell_matched_order_table = SellMatchedOrder::orderBy( 'id', 'asc' )->limit( 1 );
2766
2767 if ( $sell_matched_order_table->exists() ) {
2768 $sell_matched_order_obj = $sell_matched_order_table->get();
2769
2770 $order_matching_process_sell = $sell_matched_order_obj[0]->matching_process;
2771 $new_order_id_sell = $sell_matched_order_obj[0]->sell_order_id;
2772 print( "<br> order_matching_process_buy: " . $order_matching_process_sell . "<br>" );
2773 print( "<br> new_order_id_sell: " . $new_order_id_sell . "<br>" );
2774 }
2775
2776 // check if order is buy or sell ?
2777 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" ) {
2778 // new order is a buy order
2779 $new_order_id = $new_order_id_buy;
2780
2781 } else {
2782 $new_order_id = $new_order_id_sell;
2783 }
2784 print( "<br> new_order_id: " . $new_order_id . "<br>" );
2785
2786 // check whether the order is a “Fill or kill” or “immediate or cancel”
2787 $temp_list_orders_table = TempListOrders::where( [ 'order_number' => $new_order_id ] );
2788 $temp_list_orders_obj = null;
2789
2790 if ( $temp_list_orders_table->exists() ) {
2791 $temp_list_orders_obj = $temp_list_orders_table->first();
2792
2793 $new_order_status = $temp_list_orders_obj->status;
2794 $new_order_type = $temp_list_orders_obj->order_type;
2795 $new_order_entry_date = $temp_list_orders_obj->entry_date;
2796 $new_order_entry_time = $temp_list_orders_obj->entry_time;
2797 $new_order_user_id = $temp_list_orders_obj->user_id;
2798 $new_order_buy_sell = $temp_list_orders_obj->buy_sell;
2799 $new_order_price = $temp_list_orders_obj->price;
2800 $new_order_desired_sil = $temp_list_orders_obj->desired_sil;
2801 $new_order_duration = $temp_list_orders_obj->duration;
2802 $new_order_split_id = $temp_list_orders_obj->split_id;
2803 $new_order_levelling_ratio = $temp_list_orders_obj->levelling_ratio;
2804 $new_order_LMM_percentage = $temp_list_orders_obj->LMM_percentage;
2805 $new_order_trigger_price = $temp_list_orders_obj->trigger_price;
2806 print( "<br> new_order_status here: " . $new_order_status . "<br>" );
2807 print( "<br> new_order_type: " . $new_order_type . "<br>" );
2808 }
2809
2810 If ( $new_order_type == "fill_or_kill" ) {
2811 // it is a “fill or kill” type we need to check whether it is fully matched or not
2812
2813 If ( $new_order_status == "fully_matched" ) {
2814 // it is a “fill or kill” type, fully matched we need to run macro16
2815 $this->Macro16();
2816
2817 } else {
2818 // else of If ( $new_order_status == “fully_matched”)
2819 // it is a “fill or kill” type but NOT fully matched , the official list of order will remain unchanged
2820 // change status of order in table TempListOrders
2821
2822 $updated_status = 'fill_kill_cancelled_not_fully_matched';
2823
2824 if ( $temp_list_orders_table != null ) {
2825
2826 $temp_list_orders_table->update( [
2827 'status' => $updated_status,
2828 ] );
2829
2830 print( 'Temp List Status Updated <br>' );
2831
2832 // move fill kill order to table list_of_non_active_orders
2833 ListNonActiveOrders::create(
2834 [
2835 'order_number' => $new_order_id,
2836 'status' => $updated_status,
2837 'order_type' => $new_order_type,
2838 'entry_date' => $new_order_entry_date,
2839 'entry_time' => $new_order_entry_time,
2840 'user_id' => $new_order_user_id,
2841 'buy_sell' => $new_order_buy_sell,
2842 'price' => $new_order_price,
2843 'desired_sil' => $new_order_desired_sil,
2844 'duration' => $new_order_duration,
2845 'split_id' => $new_order_split_id,
2846 'levelling_ratio' => $new_order_levelling_ratio,
2847 'LMM_percentage' => $new_order_LMM_percentage,
2848 'trigger_price' => $new_order_trigger_price,
2849
2850 ]
2851 );
2852 print( '<br> Macro15: Order #' . $new_order_id . ' is moved to ListNonActiveOrders <br>' );
2853 }
2854
2855 } //end of else If ( $new_order_status == "fully_matched" )
2856 } else { //Begin Else If ( $new_order_type == “fill_ot_kill”)
2857
2858 // else of test If ( $new_order_type == “fill_ot_kill”)
2859 // it is not a test “fill_ot_kill”, test to see if new order is “immediate or cancel”
2860 print( 'ORDER TYPE = ' . $new_order_type . "<br>" );
2861 If ( $new_order_type == "immediate_or_cancel" ) {
2862 // it is a “immediate or cancel” type we need to check whether it is fully matched or not
2863
2864 if ( $new_order_status == 'open' ) { //Begin if($new_order_status == 'open')
2865 // there is an unmatched part which needs to be canceled
2866 $updated_status = 'unmatched part of immediate or cancel order has been canceled';
2867 $updated_status = 'unmatched_immediate_cancel_cancel';
2868 $updated_status = 'cancel_unmatched_immediate_canel';
2869 $updated_status = 'cancel_unmatched_part_order';
2870
2871 if ( $temp_list_orders_table != null ) {
2872
2873 $temp_list_orders_table->update( [
2874 'status' => $updated_status,
2875 ] );
2876 }
2877
2878 } //End if($new_order_status == 'open')
2879 $this->Macro16();
2880 } //Ends If ( $new_order_type == "immediate_cancel" )
2881 else {
2882 $this->Macro16();
2883 }
2884 }
2885
2886 }
2887
2888//Test if matching is possible
2889 private function Macro17() {
2890
2891 print( '<br> <b>Calling Marco 17 </b>' );
2892
2893 $buy_bid_price = 0;
2894 $sell_ask_price = 0;
2895
2896 try {
2897
2898 } catch ( Exception $e ) {
2899
2900 }
2901
2902 $first_temp_order_book_buy = TemporaryOrderBookBuy::orderBy( 'bid_price', 'desc' )->limit( 1 )->first();
2903 $first_temp_order_book_sell = TemporaryOrderBookSell::orderBy( 'ask_price', 'asc' )->limit( 1 )->first();
2904
2905
2906 if ( $first_temp_order_book_sell != null ) {
2907 $sell_ask_price = $first_temp_order_book_sell->ask_price;
2908 }
2909
2910 if ( $first_temp_order_book_buy != null ) {
2911 $buy_bid_price = $first_temp_order_book_buy->bid_price;
2912 }
2913
2914
2915 print( '$sell_ask_price = ' . $sell_ask_price . " <br>" );
2916 print " <br>";
2917 print( '$buy_bid_price = ' . $buy_bid_price . " <br>" );
2918
2919 print " <br>";
2920
2921
2922 // Check whether it is matchable or not.
2923 if ( $buy_bid_price < $sell_ask_price || $buy_bid_price == 0 || $sell_ask_price == 0 ) {
2924 $this->Macro18();
2925 } else {
2926 //Matchable
2927 $this->Macro14();
2928
2929 $this->Macro15();
2930 }
2931
2932 }
2933
2934 private function createTempBuyOrderBook() {
2935 $bid_price = [ 0, 0, 0 ];
2936 $bid_size = [ 0, 0, 0 ];
2937 $bid_count = [ 0, 0, 0 ];
2938 $temp_order_book_buy = [];
2939
2940// print( 'Calling createTempBuyOrderBook' );
2941// print_r( $bid_price );
2942// print " <br>";
2943// print_r( $bid_size );
2944// print " <br>";
2945// print_r( $bid_count );
2946// print " <br>";
2947
2948
2949 $buy_orders_rank_by_price = BuyOrdersRankByPriceNb::orderBy( 'price', 'DESC' )->groupBy( 'price' )
2950 ->select( 'price as bid_price',
2951 DB::raw( 'count(desired_sil) as num_bid_order' ),
2952 DB::raw( 'sum(desired_sil) as bid_size' )
2953 )
2954 ->get();
2955
2956 if ( count( $buy_orders_rank_by_price ) > 0 ) {
2957 // Insert ranked and aggregated records in Temp OrderBook Sell
2958 foreach ( $buy_orders_rank_by_price as $buy_order_rank_by_price ) {
2959 $now = Carbon::now()->toDateTimeString();
2960 $temp_order_book_buy[] = [
2961 'bid_size' => $buy_order_rank_by_price->bid_size,
2962 'bid_price' => $buy_order_rank_by_price->bid_price,
2963 'num_bid_order' => $buy_order_rank_by_price->num_bid_order,
2964 'created_at' => $now,
2965 'updated_at' => $now,
2966 ];
2967
2968// TemporaryOrderBookBuy::create(
2969// [
2970// 'bid_size' => $buy_order_rank_by_price->bid_size,
2971// 'bid_price' => $buy_order_rank_by_price->bid_price,
2972// 'num_bid_order' => $buy_order_rank_by_price->num_bid_order,
2973// ]
2974// );
2975 }
2976
2977 TemporaryOrderBookBuy::insert( $temp_order_book_buy );
2978 }
2979 }
2980
2981 /**
2982 * Creation of Aggregated Sell Order Book
2983 */
2984
2985 private function createTempSellOrderBook() {
2986 $ask_prize = [ 0, 0, 0 ];
2987 $ask_size = [ 0, 0, 0 ];
2988 $ask_count = [ 0, 0, 0 ];
2989 $temp_order_book_sell = [];
2990
2991 print( ' <br> Calling createTempSellOrderBook <br>' );
2992
2993 // Select count(desired_sil),price from sell_orders_rank_by_price_nb group by price order by price
2994
2995 $sell_orders_rank_by_price = SellOrdersRankByPriceNb::orderBy( 'price', 'asc' )->groupBy( 'price' )
2996 ->select( 'price as ask_price',
2997 DB::raw( 'count(desired_sil) as num_ask_order' ),
2998 DB::raw( 'sum(desired_sil) as ask_size' )
2999 )
3000 ->get();
3001
3002 if ( count( $sell_orders_rank_by_price ) > 0 ) {
3003 // Insert ranked and aggregated records in Temp OrderBook Sell
3004 foreach ( $sell_orders_rank_by_price as $sell_order_rank_by_price ) {
3005 $now = Carbon::now()->toDateTimeString();
3006 $temp_order_book_sell[] = [
3007 'ask_size' => $sell_order_rank_by_price->ask_size,
3008 'ask_price' => $sell_order_rank_by_price->ask_price,
3009 'num_ask_order' => $sell_order_rank_by_price->num_ask_order,
3010 'created_at' => $now,
3011 'updated_at' => $now,
3012 ];
3013
3014// TemporaryOrderBookSell::create(
3015// [
3016// 'ask_size' => $sell_order_rank_by_price->ask_size,
3017// 'ask_price' => $sell_order_rank_by_price->ask_price,
3018// 'num_ask_order' => $sell_order_rank_by_price->num_ask_order,
3019// ]
3020// );
3021 }
3022 TemporaryOrderBookSell::insert( $temp_order_book_sell );
3023 }
3024 }
3025
3026 /*
3027 * Creation of Temporary arrays: Buy and Sell_orders_rank_by_price_and_nb and Temporary_Order_book_Buy and Sell
3028 */
3029 private function Macro9() {
3030
3031 print( 'Calling MACRO9 <br>' );
3032 //initialisation of variables
3033 $temporary_Buy_orders = 0;
3034 $temporary_Sell_orders = 0;
3035 $increment = 0;
3036 $user_id = 0;
3037 $buy_rank_by_price = [];
3038 $sell_rank_by_price = [];
3039
3040 //Select all Temp orders having status is OPEN and of Type "B"
3041 $temp_orders = TempListOrders::where( [ 'status' => 'open' ] )->get();
3042
3043 foreach ( $temp_orders as $temp_order ) {
3044 $user_id = $temp_order->user_id;
3045 $order_number = $temp_order->order_number;
3046 $currency = $temp_order->currency;
3047 $buy_sell = $temp_order->buy_sell;
3048 $desired_sil = $temp_order->desired_sil;
3049 $price = $temp_order->price;
3050 $order_type = $temp_order->order_type;
3051 $trigger_price_activating_stop_order = $temp_order->trigger_price_activating_stop_order;
3052 $duration = ( $temp_order->duration == null ? '1970-01-01' : $temp_order->duration );
3053 $split_id = $temp_order->split_id;
3054 $levelling_ratio = $temp_order->levelling_ratio;
3055 $LMM_percentage = $temp_order->LMM_percentage;
3056 $trigger_price_activating_stop_order = $temp_order->trigger_price_activating_stop_order;
3057 $entry_date = $temp_order->entry_date;
3058 $entry_time = $temp_order->entry_time;
3059 $status = $temp_order->status;
3060 $now = Carbon::now()->toDateTimeString();
3061
3062 if ( $buy_sell == 'B' ) {
3063 $buy_rank_by_price[] = [
3064 'order_number' => $order_number,
3065 'currency' => $currency,
3066 'buy_sell' => $buy_sell,
3067 'entry_date' => $entry_date,
3068 'entry_time' => $entry_time,
3069 'desired_sil' => $desired_sil,
3070 'price' => $price,
3071 'order_type' => $order_type,
3072 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
3073 'duration' => $duration,
3074 'status' => $status,
3075 'split_id' => $split_id,
3076 'levelling_ratio' => $levelling_ratio,
3077 'LMM_percentage' => $LMM_percentage,
3078 'user_id' => $user_id,
3079 'created_at' => $now,
3080 'updated_at' => $now,
3081 ];
3082
3083// $buy_order_rank_price_nb = BuyOrdersRankByPriceNb::create(
3084// [
3085// 'order_number' => $order_number,
3086// 'currency' => $currency,
3087// 'buy_sell' => $buy_sell,
3088// 'entry_date' => $entry_date,
3089// 'entry_time' => $entry_time,
3090// 'desired_sil' => $desired_sil,
3091// 'price' => $price,
3092// 'order_type' => $order_type,
3093// 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
3094// 'duration' => $duration,
3095// 'status' => $status,
3096// 'split_id' => $split_id,
3097// 'levelling_ratio' => $levelling_ratio,
3098// 'LMM_percentage' => $LMM_percentage,
3099// 'user_id' => $user_id,
3100// ]
3101// );
3102
3103 } elseif ( $buy_sell == 'S' ) {
3104 $sell_rank_by_price[] = [
3105 'order_number' => $order_number,
3106 'currency' => $currency,
3107 'buy_sell' => $buy_sell,
3108 'entry_date' => $entry_date,
3109 'entry_time' => $entry_time,
3110 'desired_sil' => $desired_sil,
3111 'price' => $price,
3112 'order_type' => $order_type,
3113 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
3114 'duration' => $duration,
3115 'status' => $status,
3116 'split_id' => $split_id,
3117 'levelling_ratio' => $levelling_ratio,
3118 'LMM_percentage' => $LMM_percentage,
3119 'user_id' => $user_id,
3120 'created_at' => $now,
3121 'updated_at' => $now,
3122 ];
3123// $buy_order_rank_price_nb = SellOrdersRankByPriceNb::create(
3124// [
3125// 'order_number' => $order_number,
3126// 'buy_sell' => $buy_sell,
3127// 'entry_date' => $entry_date,
3128// 'entry_time' => $entry_time,
3129// 'desired_sil' => $desired_sil,
3130// 'price' => $price,
3131// 'order_type' => $order_type,
3132// 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
3133// 'duration' => $duration,
3134// 'status' => $status,
3135// 'split_id' => $split_id,
3136// 'levelling_ratio' => $levelling_ratio,
3137// 'LMM_percentage' => $LMM_percentage,
3138// 'user_id' => $user_id,
3139// ]
3140// );
3141 }
3142 }
3143 BuyOrdersRankByPriceNb::insert( $buy_rank_by_price );
3144 SellOrdersRankByPriceNb::insert( $sell_rank_by_price );
3145
3146 /*
3147 * We are ranking Orders both Sell and Buy by Price
3148 */
3149
3150 // Ranking Buy
3151
3152// $ranked_buy_orders_by_price = BuyOrdersRankByPriceNb::orderBy( 'price', 'desc' )->orderBy( 'order_number',
3153// 'asc' )->get();
3154// $ranked_sell_orders_by_price = SellOrdersRankByPriceNb::orderBy( 'price', 'asc' )->orderBy( 'order_number',
3155// 'asc' )->get();
3156
3157 //dd($ranked_buy_orders_by_price);
3158 // Emptyng Rank BY Price Tables first
3159 //BuyOrdersRankByPriceNb::truncate();
3160 //SellOrdersRankByPriceNb::truncate();
3161
3162// foreach ( $ranked_buy_orders_by_price as $item ) {
3163// print(' '. $item->order_number.' - '.$item->price." <br>");
3164//// $buy_order_rank_price_nb = BuyOrdersRankByPriceNb::create(
3165//// [
3166//// 'order_number' => $item->order_number,
3167//// 'currency' => $item->currency,
3168//// 'buy_sell' => $item->buy_sell,
3169//// 'entry_date' => $item->entry_date,
3170//// 'entry_time' => $item->entry_time,
3171//// 'desired_sil' => $item->desired_sil,
3172//// 'price' => $item->price,
3173//// 'order_type' => $item->order_type,
3174//// 'trigger_price_activating_stop_order' => $item->trigger_price_activating_stop_order,
3175//// 'duration' => $item->duration,
3176//// 'status' => $item->status,
3177//// 'split_id' => $item->split_id,
3178//// 'levelling_ratio' => $item->levelling_ratio,
3179//// 'LMM_percentage' => $item->LMM_percentage,
3180//// 'user_id' => $logged_in_id,
3181//// ]
3182//// );
3183// }
3184
3185 // Make sure Temp Order Book Buy and Sell are Empty by Truncating them.
3186 TemporaryOrderBookBuy::truncate();
3187 //TemporaryOrderBookSell::truncate();
3188
3189 // Check whether records exist
3190// if ( count( $ranked_buy_orders_by_price ) > 0 ) {
3191// $temporary_Buy_orders = 1;
3192// }
3193//
3194// if ( count( $ranked_sell_orders_by_price ) > 0 ) {
3195// $temporary_Sell_orders = 1;
3196// }
3197//
3198// if ( $temporary_Sell_orders == 0 && $temporary_Buy_orders == 0 ) {
3199//
3200// }
3201
3202 $this->createTempSellOrderBook();
3203 $this->createTempBuyOrderBook();
3204
3205 }
3206
3207// Creation of Temp Order List
3208 private function Macro8() {
3209
3210 print( '<br> Calling Macro8' );
3211 // get all the data from List of All Orders
3212 //$list_all_orders = ListAllOrders::all();
3213 $select = ListAllOrders::select( [
3214 'order_number',
3215 'currency',
3216 'buy_sell',
3217 'entry_date',
3218 'entry_time',
3219 'desired_sil',
3220 'price',
3221 'order_type',
3222 'trigger_price_activating_stop_order',
3223 'duration',
3224 'status',
3225 'split_id',
3226 'levelling_ratio',
3227 'LMM_percentage',
3228 'user_id',
3229 'updated_at',
3230 'created_at'
3231 ] );
3232
3233 $insertQuery = 'insert into `temporary_list_of_all_orders` (`order_number`, `currency`, `buy_sell`, `entry_date`, `entry_time`, `desired_sil`, `price`, `order_type`, `trigger_price_activating_stop_order`, `duration`, `status`, `split_id`, `levelling_ratio`, `LMM_percentage`, `user_id`, `updated_at`, `created_at`)'
3234 . $select->toSql();
3235
3236 DB::insert( $insertQuery );
3237// dd( $insertQuery );
3238// foreach ( $list_all_orders as $list_all_order ) {
3239//
3240// $user_id = $list_all_order->user_id;
3241// $order_number = $list_all_order->order_number;
3242// $currency = $list_all_order->currency;
3243// $buy_sell = $list_all_order->buy_sell;
3244// $desired_sil = $list_all_order->desired_sil;
3245// $price = $list_all_order->price;
3246// $order_type = $list_all_order->order_type;
3247// $trigger_price_activating_stop_order = $list_all_order->trigger_price_activating_stop_order;
3248// $duration = ( $list_all_order->duration == null ? '1970-01-01' : $list_all_order->duration );
3249// $split_id = $list_all_order->split_id;
3250// $levelling_ratio = $list_all_order->levelling_ratio;
3251// $LMM_percentage = $list_all_order->LMM_percentage;
3252// $trigger_price_activating_stop_order = $list_all_order->trigger_price_activating_stop_order;
3253// $entry_date = $list_all_order->entry_date;
3254// $entry_time = $list_all_order->entry_time;
3255// $status = $list_all_order->status;
3256//
3257// $temp_order = TempListOrders::create(
3258// [
3259// 'order_number' => $order_number,
3260// 'currency' => $currency,
3261// 'buy_sell' => $buy_sell,
3262// 'entry_date' => $entry_date,
3263// 'entry_time' => $entry_time,
3264// 'desired_sil' => $desired_sil,
3265// 'price' => $price,
3266// 'order_type' => $order_type,
3267// 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
3268// 'duration' => $duration,
3269// 'status' => $status,
3270// 'split_id' => $split_id,
3271// 'levelling_ratio' => $levelling_ratio,
3272// 'LMM_percentage' => $LMM_percentage,
3273// 'user_id' => $user_id,
3274// ]
3275// );
3276// //print( '<br> Order #<b>' . $order_number . ' </b> inserted in TempOrderList in macro8 <br>' );
3277// }
3278
3279 // Get the new order from new Order Enters
3280 $new_orders_enter = NewOrderEnters::first();
3281
3282 if ( $new_orders_enter->exists() ) {
3283
3284 $new_order_number = $new_orders_enter->order_number;
3285 $new_user_id = $new_orders_enter->user_id;
3286 $new_currency = $new_orders_enter->currency;
3287 $new_buy_sell = $new_orders_enter->buy_sell;
3288 $new_desired_sil = $new_orders_enter->desired_sil;
3289 $new_price = $new_orders_enter->price;
3290 $new_order_type = $new_orders_enter->order_type;
3291 $new_trigger_price_activating_stop_order = $new_orders_enter->trigger_price_activating_stop_order;
3292 $new_duration = ( $new_orders_enter->duration == null ? '1970-01-01' : $new_orders_enter->duration );
3293 $new_split_id = $new_orders_enter->split_id;
3294 $new_levelling_ratio = $new_orders_enter->levelling_ratio;
3295 $new_LMM_percentage = $new_orders_enter->LMM_percentage;
3296 $new_status = $new_orders_enter->status;
3297 $new_trigger_price_activating_stop_order = $new_orders_enter->trigger_price_activating_stop_order;
3298 $new_entry_date = $new_orders_enter->entry_date;
3299 $new_entry_time = $new_orders_enter->entry_time;
3300 print( '<br> now we add new order to temporary list ' . $new_order_number . "<br>" );
3301
3302 $new_order = TempListOrders::create(
3303 [
3304 'order_number' => $new_order_number,
3305 'currency' => $new_currency,
3306 'buy_sell' => $new_buy_sell,
3307 'entry_date' => $new_entry_date,
3308 'entry_time' => $new_entry_time,
3309 'desired_sil' => $new_desired_sil,
3310 'price' => $new_price,
3311 'order_type' => $new_order_type,
3312 'trigger_price_activating_stop_order' => $new_trigger_price_activating_stop_order,
3313 'duration' => $new_duration,
3314 'status' => $new_status,
3315 'split_id' => $new_split_id,
3316 'levelling_ratio' => $new_levelling_ratio,
3317 'LMM_percentage' => $new_LMM_percentage,
3318 'user_id' => $new_user_id,
3319 ]
3320 );
3321 }
3322
3323 $result = $new_order->id;
3324 print( " <br> New Order Appended <br>" );
3325 }
3326
3327 private function Macro11() {
3328 print( " <br> Called Macro 11" );
3329 $current_order = 0;
3330 $order_ids = [];
3331 // check whether the last traded price will activate a stop order in table stop_order_list
3332 // the system will look at the oldest order first and if the price is triggered, the order will be copied in table
3333 // new_orders_enter whre its status will become open and the order will then be removed from table stop_order_list
3334
3335 // when reaching macro 11 table new order enters need to be empty, to be sure we empty it again
3336 NewOrderEnters::truncate();
3337 print( '<br> NewOrderEnters Truncated <br>' );
3338
3339 // Fetch both last buy and sell matched price
3340 $last_matched_order = LastMatchedPriceQuantity::orderBy( 'id', 'DESC' )->first();
3341 $last_buy_price_matched = floatval( $last_matched_order->buy_price );
3342 $last_sell_price_matched = floatval( $last_matched_order->sell_price );
3343
3344 print( "<br> last_buy_price_matched = " . $last_buy_price_matched . "<br>" );
3345 print( "<br> last_sell_price_matched = " . $last_sell_price_matched . "<br>" );
3346
3347 // ranking table stop_order_list by ascending order_number; we need to look at the oldest number first (smallest
3348 // order_number) check whether it can be activated and if not look at the next one in the list and so on
3349
3350 $stop_orders_table = StopOrderList::orderBy( 'order_number', 'asc' );
3351 $stop_orders_obj = null;
3352
3353 if ( $stop_orders_table->exists() ) {
3354 $stop_orders_obj = $stop_orders_table->get();
3355
3356 foreach ( $stop_orders_obj as $stop_order_obj ) {
3357
3358 $order_number = $stop_order_obj->order_number;
3359 print( '<br> Processing Stop Order# ' . $order_number . ' at the moment inside Macro11 <br>' );
3360 $order_status = $stop_order_obj->status;
3361 $order_currency = $stop_order_obj->currency;
3362 $order_type = $stop_order_obj->order_type;
3363 $order_entry_date = $stop_order_obj->entry_date;
3364 $order_entry_time = $stop_order_obj->entry_time;
3365 $order_user_id = $stop_order_obj->user_id;
3366 $order_buy_sell = $stop_order_obj->buy_sell;
3367 $order_price = $stop_order_obj->price;
3368 $order_desired_sil = $stop_order_obj->desired_sil;
3369 $order_duration = $stop_order_obj->duration;
3370 $order_split_id = $stop_order_obj->split_id;
3371 $order_levelling_ratio = $stop_order_obj->levelling_ratio;
3372 $order_LMM_percentage = $stop_order_obj->LMM_percentage;
3373 $order_trigger_price = $stop_order_obj->trigger_price_activating_stop_order;
3374
3375 print( '<br> $last_sell_price_matched = ' . $last_sell_price_matched . "<br>" );
3376 print( '<br> Order Trigger Price = ' . $order_trigger_price . "<br>" );
3377
3378 // test to see if trigger price activated:
3379 // for buy stop order trigger criteria is $last_buy_price_matched <= $order_trigger_price
3380 // for sell stop order trigger criteria is $last_sell_price_matched >= $order_trigger_price
3381
3382 If ( ( $order_buy_sell == 'B' && $last_buy_price_matched <= $order_trigger_price ) || ( $order_buy_sell == 'S' && $last_sell_price_matched >= $order_trigger_price ) ) {
3383 // stop order has been activated and needs to be moved into table new order enters with status open
3384 // table new order enters need to be empty
3385
3386 print( "<br> stop order activated " );
3387 print( "<br> order type = " . $order_buy_sell . "<br>" );
3388 print( "<br> order trigger price = " . $order_trigger_price . "<br>" );
3389
3390 print( "<br> stop order activated and going to macro12 is number = " . $order_number . "<br>" );
3391 $order_ids[] = $order_number;
3392
3393 //$ne = NewOrderEnters::where( 'order_number', $order_number );
3394 //if ( ! $ne->exists() )
3395 {
3396 print( '<br> Macro11::Did not find ' . $order_number . ' hence adding <br>' );
3397
3398 NewOrderEnters::truncate();
3399
3400 $new_orders_temp = NewOrderEnters::create( [
3401 'order_number' => $order_number,
3402 'currency' => $order_currency,
3403 'buy_sell' => $order_buy_sell,
3404 'entry_date' => $order_entry_date,
3405 'entry_time' => $order_entry_time,
3406 'desired_sil' => $order_desired_sil,
3407 'price' => $order_price,
3408 'order_type' => $order_type,
3409 'trigger_price' => $order_trigger_price,
3410 'duration' => $order_duration,
3411 'status' => 'open',
3412 'split_id' => $order_split_id,
3413 'levelling_ratio' => $order_levelling_ratio,
3414 'LMM_percentage' => $order_LMM_percentage,
3415 'user_id' => $order_user_id,
3416
3417
3418 ] );
3419 }
3420
3421 // remove activated order from table StopOrders
3422// StopOrderList::where( 'order_number', $order_number )->delete();
3423// print( '<br> <b> Order # ' . $order_number . ' removed from STOP ORDER </b> <br>' );
3424 $current_order = $order_number;
3425 break;
3426 }
3427 }
3428 StopOrderList::whereIn( 'order_number', $order_ids )->delete();
3429 // launch macro 12 with that newly activated order
3430 $this->Macro12();
3431 print( '<br><b>BACK FROM MACRO12</b><br>' );
3432 }
3433
3434 print( 'am in Macro11 and will launch MACRO UPDATE LIABILITIES' );
3435
3436
3437 return 1;
3438 }
3439
3440 private function Macro13() {
3441 print( "<br> Calling Macro 13 <br>" );
3442 $order_ids = [];
3443 $temp_order_book_buy = [];
3444 $temp_order_book_sell = [];
3445
3446 // copy content of table TemporaryOrderBookBuy into table OrderBookBuy
3447 // copy content of table TemporaryOrderBookSell into table OrderBookSell
3448 // copy content of table TempListOrders into table ListOrders
3449
3450 try {
3451 //empty table OrderBookBuy
3452 OrderBookBuy::truncate();
3453 //empty table OrderBookSell
3454 OrderBookSell::truncate();
3455 //empty table ListOrders
3456 ListAllOrders::truncate();
3457
3458 // copy content of table TemporaryOrderBookBuy into table OrderBookBuy
3459
3460 $temp_orders_buy = TemporaryOrderBookBuy::where( 'id', '>', 0 )->get();
3461
3462 foreach ( $temp_orders_buy as $temp_order_buy ) {
3463 $bid_price = $temp_order_buy->bid_price;
3464 $bid_size = $temp_order_buy->bid_size;
3465 $num_bid_order = $temp_order_buy->num_bid_order;
3466 $now = Carbon::now()->toDateTimeString();
3467
3468 $temp_order_book_buy[] = [
3469 'bid_price' => $bid_price,
3470 'bid_size' => $bid_size,
3471 'num_bid_order' => $num_bid_order,
3472 'created_at' => $now,
3473 'updated_at' => $now,
3474
3475 ];
3476 }
3477
3478 OrderBookBuy::insert( $temp_order_book_buy );
3479
3480 // copy content of table TemporaryOrderBookSell into table OrderBookSell
3481 $temp_orders_Sell = TemporaryOrderBookSell::where( 'id', '>', 0 )->get();
3482
3483 foreach ( $temp_orders_Sell as $temp_order_Sell ) {
3484 $ask_price = $temp_order_Sell->ask_price;
3485 $ask_size = $temp_order_Sell->ask_size;
3486 $num_ask_order = $temp_order_Sell->num_ask_order;
3487
3488 $temp_order_book_sell = [
3489 'ask_price' => $ask_price,
3490 'ask_size' => $ask_size,
3491 'num_ask_order' => $num_ask_order,
3492 'created_at' => $now,
3493 'updated_at' => $now,
3494 ];
3495 }
3496
3497 OrderBookSell::insert( $temp_order_book_sell );
3498
3499 // copy content of table TempListOrders into table ListOrders
3500
3501 //$temp_orders = TempListOrders::where( 'order_number', '>', 0 )->get();
3502
3503 $select = TempListOrders::select( [
3504 'order_number',
3505 'currency',
3506 'buy_sell',
3507 'entry_date',
3508 'entry_time',
3509 'desired_sil',
3510 'price',
3511 'order_type',
3512 'trigger_price_activating_stop_order',
3513 'duration',
3514 'status',
3515 'split_id',
3516 'levelling_ratio',
3517 'LMM_percentage',
3518 'user_id',
3519 'updated_at',
3520 'created_at'
3521 ] );
3522
3523 $temp_orders = $select->get();
3524
3525 foreach ( $temp_orders as $temp_order ) {
3526 $order_ids[] = $temp_order->order_number;
3527 }
3528
3529 $insertQuery = 'INSERT INTO `list_all_orders` (`order_number`, `currency`, `buy_sell`, `entry_date`, `entry_time`, `desired_sil`, `price`, `order_type`, `trigger_price_activating_stop_order`, `duration`, `status`, `split_id`, `levelling_ratio`, `LMM_percentage`, `user_id`, `updated_at`, `created_at`)'
3530 . $select->toSql();
3531
3532 ListAllOrders::whereIn( 'order_number', $order_ids )->delete();
3533 DB::insert( $insertQuery );
3534
3535
3536// foreach ( $temp_orders as $temp_order ) {
3537// $order_number = $temp_order->order_number;
3538// $currency = $temp_order->currency;
3539// $buy_sell = $temp_order->buy_sell;
3540// $desired_sil = $temp_order->desired_sil;
3541// $price = $temp_order->price;
3542// $order_type = $temp_order->order_type;
3543// $trigger_price_activating_stop_order = $temp_order->trigger_price_activating_stop_order;
3544// $duration = ( $temp_order->duration == null ? '1970-01-01' : $temp_order->duration );
3545// $split_id = $temp_order->split_id;
3546// $levelling_ratio = $temp_order->levelling_ratio;
3547// $LMM_percentage = $temp_order->LMM_percentage;
3548// $trigger_price_activating_stop_order = $temp_order->trigger_price_activating_stop_order;
3549// $entry_date = $temp_order->entry_date;
3550// $entry_time = $temp_order->entry_time;
3551// $status = $temp_order->status;
3552// $user_id = $temp_order->user_id;
3553//
3554// //print( '<br>Macro13::Order# ' . $order_number . ' copied in ListAllOrders <br>' );
3555//
3556// ListAllOrders::where( 'order_number', $order_number )->delete();
3557// $list_order = ListAllOrders::create( [
3558// 'order_number' => $order_number,
3559// 'currency' => $currency,
3560// 'buy_sell' => $buy_sell,
3561// 'entry_date' => $entry_date,
3562// 'entry_time' => $entry_time,
3563// 'desired_sil' => $desired_sil,
3564// 'price' => $price,
3565// 'order_type' => $order_type,
3566// 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
3567// 'duration' => $duration,
3568// 'status' => $status,
3569// 'split_id' => $split_id,
3570// 'levelling_ratio' => $levelling_ratio,
3571// 'LMM_percentage' => $LMM_percentage,
3572// 'user_id' => $user_id,
3573//
3574// ] );
3575// }
3576
3577
3578 //dd( 'ABAY BHAi' );
3579 //empty table temporary_list_of_all_orders
3580 TempListOrders::truncate();
3581 //empty table new_order_enter
3582 NewOrderEnters::truncate();
3583 } catch ( Exception $e ) {
3584 print( '<br>Exception in Macro13 <br>' );
3585 print( '<br>Line # ' . $e->getLine() . "<br>" );
3586 print( '<br>Message # ' . $e->getLine() . "<br>" );
3587 }
3588
3589
3590 } // end of macro13
3591
3592
3593 private function Macro6B() {
3594 print( 'Calling macro6B...cleaning Tables' );
3595 TempListOrders::truncate();
3596 SellMatchedOrder::truncate();
3597 TemporaryOrderBookSell::truncate();
3598
3599 BuyMatchedOrder::truncate();
3600 TemporaryOrderBookBuy::truncate();
3601 }
3602
3603 private function Macro6() {
3604 print( " <br> Called Macro 6. ..Cleaning Table" );
3605
3606 BuyOrdersRankByPriceNb::truncate();
3607 SellOrdersRankByPriceNb::truncate();
3608
3609 TempListOrders::truncate();
3610
3611 SellMatchedOrder::truncate();
3612 TemporaryOrderBookSell::truncate();
3613
3614 BuyMatchedOrder::truncate();
3615 TemporaryOrderBookBuy::truncate();
3616
3617 }
3618
3619 private function Macro12(
3620// $input_price,
3621// $order_type,
3622// $buy_sell,
3623// $form_data,
3624// $status = 'open'
3625 ) {
3626
3627// $input_price = $this->price;
3628// $order_type = $this->order_type;
3629// $buy_sell = $this->buy_sell;
3630 $status = 'open';
3631 $macro_11_return_value = 0;
3632
3633 /**
3634 * Fetch the values from New Order Enters
3635 */
3636 try {
3637 $new_orders_enter = NewOrderEnters::first();
3638
3639 if ( $new_orders_enter != null ) {
3640 if ( $new_orders_enter->exists() ) {
3641
3642 print( '<br> New Order Exist in Macro12 <br>' );
3643
3644 $order_number = $new_orders_enter->order_number;
3645 print( "<br> macro12 is treating order number = " . $order_number . "<br>" );
3646 $user_id = $new_orders_enter->user_id;
3647 $currency = $new_orders_enter->currency;
3648 $buy_sell = $new_orders_enter->buy_sell;
3649 $desired_sil = $new_orders_enter->desired_sil;
3650 $price = $new_orders_enter->price;
3651 $order_type = $new_orders_enter->order_type;
3652 $trigger_price_activating_stop_order = $new_orders_enter->trigger_price_activating_stop_order;
3653 $duration = ( $new_orders_enter->duration == null ? '1970-01-01' : $new_orders_enter->duration );
3654 $split_id = $new_orders_enter->split_id;
3655 $levelling_ratio = $new_orders_enter->levelling_ratio;
3656 $LMM_percentage = $new_orders_enter->LMM_percentage;
3657 $status = $new_orders_enter->status;
3658 $trigger_price_activating_stop_order = $new_orders_enter->trigger_price_activating_stop_order;
3659 $entry_date = $new_orders_enter->entry_date;
3660 $entry_time = $new_orders_enter->entry_time;
3661
3662
3663 if ( $order_type == 'stop_order' && $status == 'pending' ) {
3664
3665 print( '<br> Entering into Stop Order List <br>' );
3666
3667 //StopOrderList::truncate();
3668
3669 //Store it into Stop Order List
3670 //foreach ( $new_orders_enter as $new_order_enter ) {
3671 StopOrderList::create(
3672 [
3673 'order_number' => $order_number,
3674 'currency' => $currency,
3675 'buy_sell' => $buy_sell,
3676 'entry_date' => $entry_date,
3677 'entry_time' => $entry_time,
3678 'desired_sil' => $desired_sil,
3679 'price' => $price,
3680 'order_type' => $order_type,
3681 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
3682 'duration' => $duration,
3683 'status' => $status,
3684 'split_id' => $split_id,
3685 'levelling_ratio' => $levelling_ratio,
3686 'LMM_percentage' => $LMM_percentage,
3687 'user_id' => $user_id,
3688 ]
3689 );
3690 print( '<br> Order # ' . $order_number . ' entered again in STOP ORDER <br>' );
3691 //}
3692
3693 //Truncate New Order Enter
3694 NewOrderEnters::truncate();
3695 //Calling MAcro11
3696 $macro_11_return_value = $this->Macro11();
3697
3698
3699 } else {
3700 // LIMIT ORDER BEGINS
3701 //Check whether it is extreme price
3702 $last_matched_order = LastMatchedPriceQuantity::orderBy( 'id', 'desc' )->first();
3703
3704 // Fetch both Buy and sell price
3705 $last_buy_price_matched = floatval( $last_matched_order->buy_price );
3706 $last_sell_price_matched = floatval( $last_matched_order->sell_price );
3707 $price_difference_automatic_order_cancellation = $this->extreme_price_cancellation;
3708
3709 //Difference between new order price and price retrieved
3710
3711 // Check whether it is a Buy Order
3712 if ( $buy_sell == 'B' ) {
3713
3714 $diff = abs( $price - $last_buy_price_matched ) / $last_buy_price_matched;
3715
3716 // Check whether the difference > order cancellation
3717 if ( $diff > $price_difference_automatic_order_cancellation ) {
3718 $status = 'cancelled_price_extreme';
3719
3720 //Now we have to enter in LIST of Non active order
3721 #Add new Order
3722
3723 $non_active_order = ListNonActiveOrders::create(
3724 [
3725 'order_number' => $order_number,
3726 'currency' => $currency,
3727 'buy_sell' => $buy_sell,
3728 'entry_date' => $entry_date,
3729 'entry_time' => $entry_time,
3730 'desired_sil' => $desired_sil,
3731 'price' => $price,
3732 'order_type' => $order_type,
3733 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
3734 'duration' => $duration,
3735 'status' => $status,
3736 'split_id' => $split_id,
3737 'levelling_ratio' => $levelling_ratio,
3738 'LMM_percentage' => $LMM_percentage,
3739 'user_id' => $user_id,
3740 ]
3741 );
3742
3743 $macro_11_return_value = $this->Macro11();
3744 }
3745
3746 } elseif ( $buy_sell == 'S' ) {
3747
3748 $diff = abs( $price - $last_sell_price_matched ) / $last_sell_price_matched;
3749
3750 // Check whether the difference > order cancellation
3751 if ( $diff > $price_difference_automatic_order_cancellation ) {
3752 $status = 'cancelled_price_extreme';
3753
3754 //Now we have to enter in LIST of Non active order
3755 #Add new Order
3756 $non_active_order = ListNonActiveOrders::create(
3757 [
3758 'order_number' => $order_number,
3759 'currency' => $currency,
3760 'buy_sell' => $buy_sell,
3761 'entry_date' => $entry_date,
3762 'entry_time' => $entry_time,
3763 'desired_sil' => $desired_sil,
3764 'price' => $price,
3765 'order_type' => $order_type,
3766 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
3767 'duration' => $duration,
3768 'status' => $status,
3769 'split_id' => $split_id,
3770 'levelling_ratio' => $levelling_ratio,
3771 'LMM_percentage' => $LMM_percentage,
3772 'user_id' => $user_id,
3773 ]
3774 );
3775
3776 // After inserting into Non Active , now call Macro11
3777 $macro_11_return_value = $this->Macro11();
3778 }
3779 }
3780 }
3781
3782 if ( $macro_11_return_value == 0 ) {
3783 //Cleaning Process
3784 $this->Macro6();
3785 //Inserting Temp Order List
3786 $this->Macro8();
3787
3788 //Inserting Temp Order Book
3789 $this->Macro9();
3790
3791 // The routine check whether matching is possible
3792
3793 $this->Macro17();
3794 }
3795
3796 }
3797 }
3798
3799 //$this->macro_update_user_liabilities();
3800 } catch ( Exception $e ) {
3801 print( '<br>Exception in Macro12 <br>' );
3802 print( '<br>Line # ' . $e->getLine() . "<br>" );
3803 print( '<br>Message # ' . $e->getLine() . "<br>" );
3804 }
3805 }
3806
3807 public function submitBid(
3808 Request $request
3809 ) {
3810 $logged_in_id = 0;
3811
3812 if ( ! Auth::check() ) {
3813 return redirect( '/login' );
3814 exit();
3815 }
3816
3817 $validator = Validator::make( $request->all(), [
3818 'trigger_price_activating_stop_order' => "required_if:order_type,==,stop_order",
3819 ] );
3820
3821 if ( $validator->fails() ) {
3822 return redirect()->route( 'create_order' )
3823 ->withErrors( $validator )
3824 ->withInput();
3825 }
3826 $logged_in_id = Auth::user()->id;
3827 $this->currency = 'USD';
3828 $this->buy_sell = $request->get( 'buy_sell' );
3829 $this->desired_sil = $request->get( 'desired_sil' );
3830 $this->price = $request->get( 'price' );
3831 $this->order_type = $request->get( 'order_type' );
3832 $this->trigger_price_activating_stop_order = $request->get( 'trigger_price_activating_stop_order' );
3833 $this->duration = ( $request->get( 'duration' ) == null ? '1970-01-01' : $request->get( 'duration' ) );
3834
3835 if ( $this->order_type != 'stop_order' ) {
3836 $this->status = 'open';
3837 } else {
3838 $this->status = 'pending';
3839 }
3840 $this->split_id = '1';
3841 $this->levelling_ratio = 1;
3842 $this->LMM_percentage = 0.9;
3843 $this->entry_date = date( 'Y-m-d' );
3844 $this->entry_time = date( 'h:i:s' );
3845 print " <pre>";
3846 print_r( $request->all() );
3847 print " </pre > ";
3848
3849
3850 DB::transaction( function () {
3851 /**
3852 * Enter the newly inserted Order into Original List Of Orders
3853 */
3854
3855 $logged_in_id = Auth::user()->id;
3856 $currency = $this->currency;
3857 $buy_sell = $this->buy_sell;
3858 $desired_sil = $this->desired_sil;
3859 $price = $this->price;
3860 $order_type = $this->order_type;
3861 $trigger_price_activating_stop_order = $this->trigger_price_activating_stop_order;
3862 $duration = ( $this->duration == null ? '1970-01-01' : $this->duration );
3863 $split_id = '1';
3864 $levelling_ratio = 1;
3865 $LMM_percentage = 0.9;
3866 $status = $this->status;
3867 $entry_date = date( 'Y-m-d' );
3868 $entry_time = date( 'h:i:s' );
3869
3870 /**
3871 * We have to find the max Order number in List of All Order, List of Non Active and Stop Order
3872 */
3873 $max_order_values = [];
3874 $new_order_number = - 1;
3875
3876 $max_list_all_orders = ListAllOrders::max( 'order_number' );
3877 $max_list_non_active_orders = ListNonActiveOrders::max( 'order_number' );
3878 $max_list_stop_orders = StopOrderList::max( 'order_number' );
3879
3880 $max_order_values[] = $max_list_all_orders;
3881 $max_order_values[] = ( $max_list_non_active_orders == null ? 0 : $max_list_non_active_orders );
3882 $max_order_values[] = ( $max_list_stop_orders == null ? 0 : $max_list_stop_orders );
3883 $last_order_number = max( $max_order_values );
3884 $new_order_number = intval( $last_order_number + 1 );
3885
3886 OrginalListOrders::create(
3887 [
3888 'order_number' => $new_order_number,
3889 'currency' => $currency,
3890 'buy_sell' => $buy_sell,
3891 'entry_date' => $entry_date,
3892 'entry_time' => $entry_time,
3893 'desired_sil' => $desired_sil,
3894 'price' => $price,
3895 'order_type' => $order_type,
3896 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
3897 'duration' => $duration,
3898 'status' => $status,
3899 'split_id' => $split_id,
3900 'levelling_ratio' => $levelling_ratio,
3901 'LMM_percentage' => $LMM_percentage,
3902 'user_id' => $logged_in_id,
3903 ]
3904 );
3905 $extreme_price_cancellation = 0;
3906 $price_mark_down = 0;
3907 $price_mark_up = 0;
3908
3909// $variables = Variables::first();
3910//
3911// if ( $variables->exists() ) {
3912// $this->extreme_price_cancellation = $variables->extreme_price_cancellation;
3913// $this->price_mark_down = $variables->price_mark_down;
3914// $this->price_mark_up = $variables->price_mark_up;
3915// }
3916
3917
3918 if ( $buy_sell == 'B' ) {
3919 $price = floatval( $price * $this->price_mark_down );
3920 } elseif ( $buy_sell == 'S' ) {
3921 $price = floatval( $price * $this->price_mark_up );
3922 }
3923
3924 NewOrderEnters::truncate();
3925
3926 NewOrderEnters::create(
3927 [
3928 'order_number' => $new_order_number,
3929 'currency' => $currency,
3930 'buy_sell' => $buy_sell,
3931 'entry_date' => $entry_date,
3932 'entry_time' => $entry_time,
3933 'desired_sil' => $desired_sil,
3934 'price' => $price,
3935 'order_type' => $order_type,
3936 'trigger_price_activating_stop_order' => $trigger_price_activating_stop_order,
3937 'duration' => $duration,
3938 'status' => $status,
3939 'split_id' => $split_id,
3940 'levelling_ratio' => $levelling_ratio,
3941 'LMM_percentage' => $LMM_percentage,
3942 'user_id' => $logged_in_id,
3943 ]
3944 );
3945
3946 print( '<br> New Order Entered with ORDER ID#' . $new_order_number . ' <br>' );
3947 $this->Macro12();
3948 $this->macro_update_user_liabilities();
3949 } );
3950
3951 }
3952}