1 //  This file is part of Qt Bitcoin Trader
2 //      https://github.com/JulyIGHOR/QtBitcoinTrader
3 //  Copyright (C) 2013-2021 July Ighor <julyighor@gmail.com>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  In addition, as a special exception, the copyright holders give
11 //  permission to link the code of portions of this program with the
12 //  OpenSSL library under certain conditions as described in each
13 //  individual source file, and distribute linked combinations including
14 //  the two.
15 //
16 //  You must obey the GNU General Public License in all respects for all
17 //  of the code used other than OpenSSL. If you modify file(s) with this
18 //  exception, you may extend this exception to your version of the
19 //  file(s), but you are not obligated to do so. If you do not wish to do
20 //  so, delete this exception statement from your version. If you delete
21 //  this exception statement from all source files in the program, then
22 //  also delete it here.
23 //
24 //  This program is distributed in the hope that it will be useful,
25 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
26 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27 //  GNU General Public License for more details.
28 //
29 //  You should have received a copy of the GNU General Public License
30 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
31 
32 #ifndef EXCHANGE_H
33 #define EXCHANGE_H
34 
35 #include <QThread>
36 #include <QTimer>
37 #include <QTime>
38 #include <openssl/hmac.h>
39 #include "main.h"
40 #include <QtCore/qmath.h>
41 #include "qtbitcointrader.h"
42 #include "julyhttp.h"
43 #include "orderitem.h"
44 #include "tradesitem.h"
45 #include "julymath.h"
46 #include "indicatorengine.h"
47 
48 struct DepthItem;
49 
50 class Exchange : public QObject
51 {
52     Q_OBJECT
53 
54 public:
55     bool exchangeDisplayOnlyCurrentPairOpenOrders;
56     bool clearOpenOrdersOnCurrencyChanged;
57     bool clearHistoryOnCurrencyChanged;
58     bool exchangeTickerSupportsHiLowPrices;
59     bool isDepthEnabled() const;
60     std::atomic_bool depthEnabledFlag{};
61     virtual void filterAvailableUSDAmountValue(double* amount);
62 
63     CurrencyPairItem defaultCurrencyParams;
64     bool balanceDisplayAvailableAmount;
65     int minimumRequestIntervalAllowed;
66     int minimumRequestTimeoutAllowed;
67     double decAmountFromOpenOrder;
68     int calculatingFeeMode;//0: direct multiply; 1: rounded by decimals; 3: real fee
69     bool buySellAmountExcludedFee;
70 
71     CurrencyPairItem currencyPairInfo;
72     double lastTickerLast;
73     double lastTickerHigh;
74     double lastTickerLow;
75     double lastTickerSell;
76     double lastTickerBuy;
77     double lastTickerVolume;
78 
79     double lastBtcBalance;
80     double lastUsdBalance;
81     double lastAvUsdBalance;
82     double lastVolume;
83     double lastFee;
84 
85     QByteArray lastDepthData;
86     QByteArray lastHistory;
87     QByteArray lastOrders;
88 
89     QString currencyMapFile;
90     bool multiCurrencyTradeSupport;
91     bool isLastTradesTypeSupported;
92     bool exchangeSupportsAvailableAmount;
93     bool checkDuplicatedOID;
94     bool forceDepthLoad;
95     bool tickerOnly;
96     bool supportsLoginIndicator;
97     bool supportsAccountVolume;
98     bool supportsExchangeFee;
99     bool supportsExchangeVolume;
100 
101     bool orderBookItemIsDedicatedOrder;
102 
103     QScopedPointer<QTimer> secondTimer;
104 
105     QString domain;
106     quint16 port;
107     bool    useSsl;
108 
109     int m_pairChangeCount;
110 
111     void setApiKeySecret(QByteArray key, QByteArray secret);
112 
113     QByteArray& getApiKey();
114     QByteArray getApiSign();
115 
116     virtual void clearVariables();
117     void translateUnicodeStr(QString* str);
118     void translateUnicodeOne(QByteArray* str);
119     static QByteArray getMidData(const QString& a, const QString& b, const QByteArray* data);
120     QByteArray getMidVal(const QString& a, const QString& b, const QByteArray* data);
121     void setupApi(QtBitcoinTrader*, bool tickerOnly = false);
122     Exchange();
123     ~Exchange();
124 
125 private:
126     QByteArray privateKey;
127 
128     QList<char*> apiKeyChars;
129     QList<char*> apiSignChars;
130 
131 signals:
132     void started();
133     void threadFinished();
134 
135     void availableAmountChanged(QString, double);
136     void depthRequested();
137     void depthRequestReceived();
138     void depthFirstOrder(QString, double, double, bool);
139 
140     void depthSubmitOrders(QString, QList<DepthItem>* asks, QList<DepthItem>* bids);
141 
142     void addLastTrades(QString, QList<TradesItem>* trades);
143 
144     void orderBookChanged(QString, QList<OrderItem>* orders);
145     void showErrorMessage(QString);
146 
147     void historyChanged(QList<HistoryItem>*);
148 
149     void ordersIsEmpty();
150     void orderCanceled(QString, QByteArray);
151 
152     void accVolumeChanged(double);
153     void accFeeChanged(QString, double);
154     void accBtcBalanceChanged(QString, double);
155     void accUsdBalanceChanged(QString, double);
156     void loginChanged(QString);
157     void apiDownChanged(bool);
158     void softLagChanged(int);
159 private slots:
160     void sslErrors(const QList<QSslError>&);
161     void quitExchange();
162 public slots:
163     virtual void secondSlot();
164     virtual void dataReceivedAuth(const QByteArray&, int, int);
165     virtual void reloadDepth();
166     virtual void clearValues();
167     virtual void getHistory(bool);
168     virtual void buy(const QString&, double, double);
169     virtual void sell(const QString&, double, double);
170     virtual void cancelOrder(const QString&, const QByteArray&);
171 
172     void run();
173 
174 protected:
175     bool checkValue(QByteArray& valueStr, double& lastValue);
176 };
177 
178 #endif // EXCHANGE_H
179