-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArbitrageBot.cpp
More file actions
123 lines (111 loc) · 4.85 KB
/
ArbitrageBot.cpp
File metadata and controls
123 lines (111 loc) · 4.85 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
#include "ArbitrageBot.h"
#include <iostream>
#include <fstream>
ArbitrageBot::ArbitrageBot(const std::vector<Exchange*>& exchanges, const std::vector<std::string>& symbols, double feePercent, double minThreshold)
: exchanges(exchanges), symbols(symbols), feePercent(feePercent), minThreshold(minThreshold) {
// Initialize portfolio: start with $10,000 and 0 coins
cash = 10000.0;
for (const auto& sym : symbols) {
coinBalances[sym] = 0.0;
}
}
void ArbitrageBot::scanAndTrade() {
for (const auto& symbol : symbols) {
double minPrice = 1e9, maxPrice = -1.0;
Exchange* buyEx = nullptr;
Exchange* sellEx = nullptr;
for (auto* ex : exchanges) {
double price = ex->getPrice(symbol);
if (price > 0 && price < minPrice) {
minPrice = price;
buyEx = ex;
}
if (price > maxPrice) {
maxPrice = price;
sellEx = ex;
}
}
if (buyEx && sellEx && sellEx != buyEx) {
// Portfolio simulation: buy 1 coin if enough cash, sell if enough coin
double buyFee = minPrice * (feePercent / 100.0);
double sellFee = maxPrice * (feePercent / 100.0);
double netProfit = (maxPrice - minPrice) - (buyFee + sellFee);
if (netProfit > minThreshold && cash >= minPrice + buyFee && coinBalances[symbol] >= 1.0) {
// Simulate buying 1 coin and selling 1 coin
cash -= (minPrice + buyFee);
cash += (maxPrice - sellFee);
profit += netProfit;
tradeLog.push_back({symbol, buyEx->getName(), sellEx->getName(), minPrice, maxPrice, netProfit});
std::cout << "Arbitrage: Buy " << symbol << " on " << buyEx->getName()
<< " at $" << minPrice << ", sell on " << sellEx->getName()
<< " at $" << maxPrice << ". Net Profit (after fees): $" << netProfit << "\n";
} else if (netProfit > minThreshold && cash >= minPrice + buyFee) {
// Only buy if not enough coin to sell
cash -= (minPrice + buyFee);
coinBalances[symbol] += 1.0;
std::cout << "Bought 1 " << symbol << " on " << buyEx->getName() << " at $" << minPrice << " (fee: $" << buyFee << ")\n";
} else if (netProfit > minThreshold && coinBalances[symbol] >= 1.0) {
// Only sell if not enough cash to buy
cash += (maxPrice - sellFee);
coinBalances[symbol] -= 1.0;
std::cout << "Sold 1 " << symbol << " on " << sellEx->getName() << " at $" << maxPrice << " (fee: $" << sellFee << ")\n";
}
}
}
}
double ArbitrageBot::getProfit() const {
return profit;
}
void ArbitrageBot::printTradeLog() const {
std::cout << "\n--- Trade Log ---\n";
if (tradeLog.empty()) {
std::cout << "No trades executed yet.\n";
return;
}
for (const auto& entry : tradeLog) {
std::cout << "Symbol: " << entry.symbol
<< ", Buy: " << entry.buyExchange << " ($" << entry.buyPrice << ")"
<< ", Sell: " << entry.sellExchange << " ($" << entry.sellPrice << ")"
<< ", Net Profit: $" << entry.profit << "\n";
}
}
void ArbitrageBot::setMinThreshold(double threshold) {
minThreshold = threshold;
}
void ArbitrageBot::setFeePercent(double fee) {
feePercent = fee;
}
void ArbitrageBot::reset() {
profit = 0.0;
tradeLog.clear();
cash = 10000.0;
for (auto& kv : coinBalances) kv.second = 0.0;
}
void ArbitrageBot::exportTradeLogCSV(const std::string& filename) const {
std::ofstream file(filename);
file << "Symbol,BuyExchange,BuyPrice,SellExchange,SellPrice,NetProfit\n";
for (const auto& entry : tradeLog) {
file << entry.symbol << "," << entry.buyExchange << "," << entry.buyPrice << ","
<< entry.sellExchange << "," << entry.sellPrice << "," << entry.profit << "\n";
}
file.close();
std::cout << "Trade log exported to " << filename << "\n";
}
void ArbitrageBot::printStatistics() const {
std::cout << "\n--- Statistics ---\n";
std::cout << "Cash: $" << cash << "\n";
for (const auto& kv : coinBalances) {
std::cout << kv.first << ": " << kv.second << " coins\n";
}
if (!tradeLog.empty()) {
double best = tradeLog[0].profit, worst = tradeLog[0].profit, sum = 0.0;
for (const auto& entry : tradeLog) {
if (entry.profit > best) best = entry.profit;
if (entry.profit < worst) worst = entry.profit;
sum += entry.profit;
}
std::cout << "Total trades: " << tradeLog.size() << "\n";
std::cout << "Best trade: $" << best << ", Worst trade: $" << worst << "\n";
std::cout << "Average profit: $" << (sum / tradeLog.size()) << "\n";
}
}