This repository has been archived by the owner on Jun 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Trade.cpp
121 lines (108 loc) · 3.79 KB
/
Trade.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
#include "Headers/Trade.hpp"
//#include <string>
/*
* converts json from oanda into a single Trade object
*/
Trade _convertTrade(json j){
TradeType type = LONG;
//std::cout << "Current units:" << '\n';
if (std::stoi(j.at("currentUnits").get<std::string>()) < 0){
type = SHORT;
}
Trade t = Trade(
type, //TODO: change this to an inline-if (Whatever those are called I forget)
abs(std::stoi(j.at("currentUnits").get<std::string>())),
Price(
std::stoi(j.at("openTime").get<std::string>()),
std::stod(j.at("price").get<std::string>()),
std::stod(j.at("price").get<std::string>())
),
50.0, // This is the amount of leverage, assumed to be 50 <- this needs work
j.at("instrument").get<std::string>()
);
t.profit = std::stod(j.at("unrealizedPL").get<std::string>());
return t;
}
/*
* Creates an array of Trade objects using a response from the oanda server
* Takes the FULL json response from the executor, not just the `at("trades")` part
* Calls _convertTrade at least 1 time to do this
*/
std::vector<Trade> Trade::translateTrades(json response){
std::cerr << "Translating trades:" << '\n';
std::vector<Trade> trades;
json j = response.at("trades").get<json>();
for (auto& element : j) {
trades.push_back(_convertTrade(element));
std::cout << trades.back().tradeAsString() << '\n';
}
return trades;
}
/*
* Printable form of a Trade object's information
*/
std::string Trade::tradeAsString(){
std::string s = "Trade : ";
s.append(type == LONG ? "LONG\n" : "SHORT\n");
s.append("Current Units: ");
s.append(std::to_string(units));
s.append("\nTime: ");
s.append(std::to_string(initialPrice.date));
s.append("\nProfit: ");
s.append(std::to_string(profit));
s.append("\n");
return s;
}
/*
* Constructor for a trade
* units : number of base currency being traded
* Instrument : Synonym for table, ex: "EUR_USD"
*/
Trade::Trade(TradeType type, int units, Price price, double leverage, std::string instrument) : type(type), units(units), initialPrice(price), leverage(leverage), instrument(instrument), finalPrice(Price(0, 0.0, 0.0)) {
open = true;
type == LONG ? marginUsed = units * price.ask / leverage : marginUsed = units * price.bid / leverage;
}
/*
* Returns the unrealized profit of the position in USD
USED IN TESTING ONLY!
IN PRODUCTION -> refresh list of trades and use broker-given unrealized profit/loss
*/
double Trade::calcProfit(Price price) {
profit = 0.0;
finalPrice = price;
if (type == LONG) {
profit = (double)units * (price.bid - initialPrice.ask);
}else {
profit = (double)units * (initialPrice.bid - price.ask);
}
return profit;
}
/*
* Close this trade
* returns profit plus the amount spent on the trade
*/
double Trade::close(Price p) {
double useMe = calcProfit(p);
open = false;
closeDate = p.date;
return useMe + marginUsed;
}
/*
* Constructor for a price object
*
* Trade objects have initialPrice and finalPrice objects to calculate profit
*/
Price::Price(int date, double ask, double bid) : bid(bid), ask(ask), date(date){
}
Price::Price(std::string table, int date) {
sql::Driver * driver = get_driver_instance();
sql::Connection *con = driver->connect("tcp://127.0.0.1:3306", "root", "");
sql::Statement * stmt = con->createStatement();
std::string query = "SELECT * FROM quotesdb." + table + " WHERE date = " + std::to_string(date);
sql::ResultSet * res = stmt->executeQuery(query);
res->next();
bid = res->getDouble("closeBid");
ask = res->getDouble("closeAsk");
delete con;
delete res;
}