-
Notifications
You must be signed in to change notification settings - Fork 1
/
limitOrder.cpp
159 lines (121 loc) · 5.44 KB
/
limitOrder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include<bits/stdc++.h>
#define ld long double
#define ll long long
using namespace std;
/*-----------------------------------------------------------------------------------------------------------------
#1 Function totalQuantity() is for both ASK and BID data to calculate the total quantity of orders individaually.
-------------------------------------------------------------------------------------------------------------------*/
//To check and return the sum of the order quantity.
ld totalQuantity(ld Q[], int limit)
{
ld total = 0;
//This loop will calculate the total quantity of the order.
for(int i = 0; i < limit; ++i)
{
total += Q[i];
}
//Returning the total number of orders to the function.
return total;
}
/*---------------------------------------------------------------------------------------
#2 Function calculateOrder() is to calculate the total value the orders and difference.
-----------------------------------------------------------------------------------------*/
//To calculate the total value and difference between ASK and BID orders.
void calculateOrder(ld AQ[], ld AP[], ld BQ[], ld BP[], int Alimit, int Blimit)
{
ld totalA = 0, totalB = 0;
//Calculating total value of ASK orders.
for(int i = 0; i < Alimit; ++i)
totalA += AQ[i] * AP[i];
//Calculating total value of BID orders.
for(int i = 0; i < Alimit; ++i)
totalB += BQ[i] * BP[i];
cout << "Total ASK Value: " << totalA << "\nTotal BID Value: " << totalB << "\n";
cout << "Difference: " << abs(totalA - totalB);
}
/*---------------------------------------------------------------------------------
#3 Function printChart() is to print the price and quantity chart of ASK and BID.
-----------------------------------------------------------------------------------*/
void printChart(ld AQ[], ld AP[], ld BQ[], ld BP[], int Alimit, int Blimit)
{
cout << left << setw(15) << setfill(' ') << "ASK Price" << setw(15) << setfill(' ') << "ASK Quantity";
cout << "\n";
for(int i = 0; i < Alimit; ++i)
cout << left << setw(15) << setfill(' ') << AP[i] << setw(15) << setfill(' ') << AQ[i] << "\n";
cout << "------------------------------------------------\n";
cout << left << setw(15) << setfill(' ') << "BID Price" << setw(15) << setfill(' ') << "BID Quantity";
cout << "\n";
for(int i = 0; i < Blimit; ++i)
cout << left << setw(15) << setfill(' ') << BP[i] << setw(15) << setfill(' ') << BQ[i] << "\n";
}
/*------------------------------------------
#4 Function is the main() driver function.
--------------------------------------------*/
int main()
{
//Asking for the maximum supply. Because, crossing supply limit by the orders is illegal.
ld supply;
cout << "Put the maximum supply of your stock: ";
cin >> supply;
cout << "------------------------------------------------\n------------------------------------------------\n";
//Assigning ASK and BID limits for the trade and will ask the user to put the order limit.
int Alimit = 0, Blimit = 0;
//Asking for the ASK order limit. And later we'll input the order details.
cout << "Put the ASK order limit: ";
cin >> Alimit;
cout << "------------------------------------------------\n";
ld AQ[Alimit]; ld AP[Alimit];
for(int i = 0; i < Alimit; ++i)
{
cout << "ASK Quantity(" << i + 1 << "): ";
cin >> AQ[i];
cout << "ASK Price(" << i + 1 << "): ";
cin >> AP[i];
} cout << "------------------------------------------------\n";
//Asking for the BID order limit. And later we'll input the order details.
cout << "Put the BID order limit: ";
cin >> Blimit;
cout << "------------------------------------------------\n";
ld BQ[Blimit]; ld BP[Blimit];
for(int i = 0; i < Blimit; ++i)
{
cout << "BID Quantity(" << i + 1 << "): ";
cin >> BQ[i];
cout << "BID Price(" << i + 1 << "): ";
cin >> BP[i];
} cout << "------------------------------------------------\n";
//Checking if the ASK/BID order limits crossing the maximum supply or not.
//If order limits are less than or equal to total supply.
if(totalQuantity(AQ, Alimit) <= supply && totalQuantity(BQ, Blimit) <= supply)
calculateOrder(AQ, AP, BQ, BP, Alimit, Blimit);
else
{
cout << "INVALID ORDER LIMITS!\n";
cout << "Your ASK order is " << abs(Alimit - supply) << " more/less than supply limit.\n";
cout << "Your BID order is " << abs(Blimit - supply) << " more/less than supply limit.\n";
}
//Here the function printChart() is called to print all the orders available for ASK/BID.
cout << "\n------------------------------------------------\n";
printChart(AQ, AP, BQ, BP, Alimit, Blimit);
cout << "------------------------------------------------\n";
ld OQ = 0, OP = 0;
cout << "Enter your limit order quantity: "; cin >> OQ;
cout << "Enter your limit order price: "; cin >> OP;
cout << "------------------------------------------------\n";
cout << left << setw(15) << setfill(' ') << "BID Quantity" << setw(15) << setfill(' ') << "BID Price";
cout << "\n";
for(int i = 0; i < Blimit; ++i)
{
if(OQ == BQ[i] && OP == BP[i])
{
cout << "[" << OQ << " orders are placed for " << OP << "].\n";
continue;
}
else
{
cout << left << setw(15) << setfill(' ') << BQ[i] << setw(15) << setfill(' ') << BP[i] << "\n";
continue;
}
}
return 0;
}