-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAgent.cpp
More file actions
174 lines (161 loc) · 4.29 KB
/
Copy pathAgent.cpp
File metadata and controls
174 lines (161 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "Market.h"
#include "Agent.h"
#include "Order.h"
#include "OrderBook.h"
#include <iostream>
#include <cstdlib>
Agent::Agent(Market *a_market, AgentType a_Type, int a_favouriteStockId)
{
m_linkToMarket = a_market;
m_Type = a_Type ;
m_cashPosition = 0;
m_favouriteStockId = a_favouriteStockId;
m_fees = 0.0;
}
Agent::~Agent()
{
}
int Agent::getFavouriteStock()
{
return m_favouriteStockId;
}
AgentType Agent::getAgentType()const
{
return m_Type ;
}
void Agent::setIdentifier(int a_identifier)
{
m_identifier = a_identifier;
}
void Agent::submitOrder(int a_asset , double a_time ,
int a_volume , OrderType a_type, int a_price )
{
if(a_volume!=0){
int l_orderIdentifier = m_linkToMarket->getOrderIdentifier();
Order l_order(a_asset , m_identifier ,a_time , a_price,
a_volume ,a_type ,
l_orderIdentifier);
m_passedOrders[l_orderIdentifier] = l_order;
m_pendingOrders[l_orderIdentifier] = l_order;
m_linkToMarket->pushOrder(l_order);
}
}
void Agent::submitCancellation(int a_OrderBookId,int a_orderIdentifier, double a_time)
{
m_linkToMarket->getOrderBook(a_OrderBookId)->processCancellation(m_identifier, a_orderIdentifier, a_time);
}
void Agent::notifyExecution(int a_orderIdentifier,double a_time,int a_price)
{
Order *l_order = &m_passedOrders[a_orderIdentifier];
l_order->m_state = EXECUTED;
// Remove from pending orders list
std::map<int,Order>::iterator iter = m_pendingOrders.find(a_orderIdentifier) ;
if(iter==m_pendingOrders.end()){
std::cout << "a_orderIdentifier not found in Agent::notifyExecution." << std::endl ;
exit(1) ;
}
m_pendingOrders.erase(iter) ;
l_order->m_executionHistory.push_back(ExecutionHistory(a_time,l_order->m_volume,a_price));
updateStockNumber(l_order,l_order->m_volume);
processCashPosition(a_time,l_order->m_volume,a_price,l_order->m_type);
l_order->m_volume = 0;
}
void Agent::notifyPartialExecution(int a_orderIdentifier,double a_time,int a_volume,int a_price)
{
Order *l_order = &m_passedOrders[a_orderIdentifier];
l_order->m_state = PARTIALLY_EXECUTED;
l_order->m_executionHistory.push_back(ExecutionHistory(a_time,a_volume,a_price));
l_order->m_volume -= a_volume;
updateStockNumber(l_order,a_volume);
processCashPosition(a_time,a_volume,a_price,l_order->m_type);
}
void Agent::notifyCancellation(int a_orderIdentifier,double a_time)
{
Order *l_order = &m_passedOrders[a_orderIdentifier];
l_order->m_state = CANCELED;
// Remove from pending orders list
std::map<int,Order>::iterator iter = m_pendingOrders.find(a_orderIdentifier) ;
if(iter==m_pendingOrders.end()){
std::cout << "a_orderIdentifier not found in Agent::notifyCancellation." << std::endl ;
exit(1) ;
}
m_pendingOrders.erase(iter) ;
l_order->m_executionHistory.push_back(ExecutionHistory(a_time,-1,-1));
}
void Agent::processCashPosition(double a_time,int a_volume,int a_price,const OrderType &a_type)
{
int l_cashFlow = a_volume*a_price;
int l_type;
switch (a_type)
{
case LIMIT_SELL:
case MARKET_SELL:
l_type = 1;
break;
case LIMIT_BUY:
case MARKET_BUY:
l_type =-1;
break;
default:// Nothing to do with other order types
break;
}
m_cashPosition += (l_type*l_cashFlow);
m_cashPositionHistory.push_back(std::make_pair(a_time,m_cashPosition));
}
void Agent::notifyAgentOfMarketEvent()
{
processInformation();
}
void Agent::initAssetQuantity(int a_stockIdentifier)
{
m_stockQuantity[a_stockIdentifier] = 0;
}
int Agent::getStockQuantity(int a_stockIdentifier)
{
return m_stockQuantity[a_stockIdentifier];
}
void Agent::updateStockNumber(Order *a_order,int a_executedVolume)
{
switch(a_order->m_type)
{
case LIMIT_SELL:
case MARKET_SELL:
m_stockQuantity[a_order->m_asset] -= a_executedVolume;
break;
case LIMIT_BUY:
case MARKET_BUY:
m_stockQuantity[a_order->m_asset] += a_executedVolume;
break;
default:// Nothing to do with other order types
break;
}
}
bool Agent::isMine(Order a_order)const
{
bool l_mine = false;
if (a_order.m_owner == m_identifier)
{
l_mine = true;
}
return l_mine;
}
void Agent::chargeMarketFees(double a_fee)
{
m_fees += a_fee;
}
double Agent::getNetCashPosition()
{
return (m_cashPosition-m_fees);
}
int Agent::getTargetedStock()
{
return getFavouriteStock();
}
double Agent::getFeeRate(OrderType a_orderType)
{
return 0.0;
}
std::map<int,Order> * Agent::getPendingOrders()
{
return &m_pendingOrders;
}