forked from zeroreserve/ZeroReserve
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyOrders.cpp
More file actions
186 lines (156 loc) · 5.16 KB
/
Copy pathMyOrders.cpp
File metadata and controls
186 lines (156 loc) · 5.16 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
174
175
176
177
178
179
180
181
182
183
184
185
/*
This file is part of the Zero Reserve Plugin for Retroshare.
Zero Reserve is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zero Reserve is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Zero Reserve. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MyOrders.h"
#include "ZeroReservePlugin.h"
#include "p3ZeroReserverRS.h"
#include "Payment.h"
#include "TmContract.h"
#include "ZRBitcoin.h"
#include "Router.h"
#include "zrdb.h"
#include <iostream>
#include <sstream>
MyOrders * MyOrders::me = NULL;
MyOrders * MyOrders::Instance()
{
return me;
}
MyOrders::MyOrders( OrderBook * bids, OrderBook * asks ) :
m_bids( bids ),
m_asks( asks )
{
m_bids->setMyOrders( this );
m_asks->setMyOrders( this );
me = this;
}
ZR::RetVal MyOrders::init()
{
try{
OrderList myorders;
ZrDB::Instance()->loadOrders( &myorders );
for( OrderIterator it = myorders.begin(); it != myorders.end(); it++){
Order * order = *it;
addOrder( order );
if( order->m_orderType == Order::ASK ){
m_asks->addOrder( order );
}
else{
m_bids->addOrder( order );
}
}
}
catch( std::runtime_error & e ){
g_ZeroReservePlugin->placeMsg( std::string( "Exception caught: " ) + e.what() );
return ZR::ZR_FAILURE;
}
return ZR::ZR_SUCCESS;
}
int MyOrders::columnCount(const QModelIndex&) const
{
return 3;
}
QVariant MyOrders::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Horizontal) {
switch (section)
{
case 0:
return QString("Type");
case 1:
return QString("Volume");
case 2:
return QString("Price");
}
}
}
return QVariant();
}
QVariant MyOrders::data( const QModelIndex& index, int role ) const
{
if (role == Qt::DisplayRole && index.row() < m_filteredOrders.size()){
Order * order = m_filteredOrders[index.row()];
switch(index.column())
{
case 0:
if(order->m_orderType == Order::ASK )
return QVariant( "Sell");
else
return QVariant( "Buy");
case 1:
return QVariant( order->m_amount.toDouble() );
case 2:
return QVariant( order->m_price.toDouble() );
default:
return QVariant();
}
}
return QVariant();
}
void MyOrders::match()
{
RsStackMutex orderMutex( m_order_mutex );
for( OrderIterator it = m_orders.begin(); it != m_orders.end(); it++ ){
Order * order = *it;
if( order->m_orderType == Order::BID ){
match( order );
}
}
}
ZR::RetVal MyOrders::match( Order * myOrder )
{
if( myOrder->m_locked ) return ZR::ZR_FINISH;
OrderList asks;
m_asks->filterOrders( asks, myOrder->m_currency );
ZR::ZR_Number amount = myOrder->m_amount;
for( OrderIterator askIt = asks.begin(); askIt != asks.end(); askIt++ ){
Order * other = *askIt;
if( other->m_ignored ) continue; // trying to execute this order did not go well in the past. Don't try again.
if( other->m_isMyOrder ) continue; // don't fill own orders
if( myOrder->m_matched.find( other->m_order_id ) != myOrder->m_matched.end() ) continue; // matched that already
if( myOrder->m_price < other->m_price ) break; // no need to try and find matches beyond
std::cerr << "Zero Reserve: Match at ask price " << other->m_price.toStdString() << std::endl;
myOrder->m_matched.insert( other->m_order_id );
if( amount > other->m_amount ){
buy( other, myOrder, other->m_amount );
}
else {
buy( other, myOrder, amount );
return ZR::ZR_FINISH;
}
amount -= other->m_amount;
}
return ZR::ZR_SUCCESS;
}
void MyOrders::buy( Order * other, Order * myOrder, const ZR::ZR_Number amount )
{
TmContractCoordinator * tm = new TmContractCoordinator( other, myOrder, amount );
if( ZR::ZR_FAILURE == tm->init() ) delete tm;
}
void MyOrders::cancelOrder( int index )
{
std::cerr << "Zero Reserve: Cancelling order: " << index << std::endl;
p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() );
Order * order = m_filteredOrders[ index ];
remove( order->m_order_id );
if( order->m_orderType == Order::ASK ){
m_asks->remove( order->m_order_id );
}
else{
m_bids->remove( order->m_order_id );
}
order->m_purpose = Order::CANCEL;
p3zr->publishOrder( order );
}