-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.cpp
148 lines (137 loc) · 4.55 KB
/
transaction.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
#include "lab.h"
void transaction(Url url, vector<Stock>& stock, Result &result)
{
int i = findCompany(stock, url.company);
if(url.action == "Buy")
{
Order buy;
buy.name = url.name;
buy.shares = stoi(url.shares);
if (url.price != "Market")
{
buy.price = stof(url.enterPrice);
}
result.price = buy.price;
//use market price if the buying price is high than the lowest selling price
//in the sellheap
if (url.price == "Market" && stock[i].getHeap("sell").isEmpty())
{
buy.price = stock[i].getLastSale();
result.priceAdjust = true;
result.price = buy.price;
}
else if(url.price == "Market" || (!stock[i].getHeap("sell").isEmpty() && buy.price > stock[i].getHeap("sell").peak().price))
{
buy.price = stock[i].getHeap("sell").peak().price;
result.priceAdjust = true;
result.price = buy.price;
}
stock[i].getHeap("buy").Insert(buy);
}
else if (url.action == "Sell")
{
Order sell;
sell.name = url.name;
sell.shares = stoi(url.shares);
if (url.price != "Market")
{
sell.price = stof(url.enterPrice);
}
result.price = sell.price;
if(url.price == "Market" && stock[i].getHeap("buy").isEmpty())
{
sell.price = stock[i].getLastSale();
result.priceAdjust = true;
result.price = sell.price;
}
else if(url.price == "Market" || (!stock[i].getHeap("buy").isEmpty() && sell.price > stock[i].getHeap("buy").peak().price))
{
sell.price = stock[i].getHeap("buy").peak().price;
result.priceAdjust = true;
result.price = sell.price;
}
stock[i].getHeap("sell").Insert(sell);
}
if((!stock[i].getHeap("buy").isEmpty() && !stock[i].getHeap("sell").isEmpty()) && stock[i].getHeap("buy").peak().price == stock[i].getHeap("sell").peak().price)
{
Order buy, sell;
//Remove both top nodes of buy and sell
stock[i].getHeap("buy").Remove(buy);
stock[i].getHeap("sell").Remove(sell);
//compare the share
//if buy has larger share, push the rest buy share back
//if sell has larger share, push the rest sell share back
//if equal, don't push back
if(buy.shares > sell.shares)
{
buy.shares -= sell.shares;
stock[i].getHeap("buy").Insert(buy);
stock[i].updateData(buy.price);
if (buy.name == url.name)
{
result.success = true;
result.partial = true;
if (url.action == "Buy")
{
result.shares = buy.shares;
}
else if (url.action == "Sell")
{
result.shares = 0;
}
}
else if (sell.name == url.name)
{
result.success = true;
result.partial = true;
if (url.action == "Buy")
{
result.shares = buy.shares;
}
else if (url.action == "Sell")
{
result.shares = 0;
}
}
}
else if(buy.shares < sell.shares)
{
sell.shares -= buy.shares;
stock[i].getHeap("sell").Insert(sell);
stock[i].updateData(sell.price);
result.partial = true;
if (buy.name == url.name)
{
result.success = true;
result.partial = true;
if (url.action == "Buy")
{
result.shares = 0;
}
else if (url.action == "Sell")
{
result.shares = sell.shares;
}
}
else if (sell.name == url.name)
{
result.success = true;
result.partial = true;
if (url.action == "Buy")
{
result.shares = 0;
}
else if (url.action == "Sell")
{
result.shares = sell.shares;
}
}
}
else
{
stock[i].updateData(buy.price);
result.success = true;
}
}
stock[i].updateCurrent();
}