I am trying to develop a trade simulation for practical purposes.
At the end of the code, I see that the values within the object have changed, but when it returns, it continues with the initial data. I couldn’t understand where my mistake is. Thanks.
The logic is quite simple: give tokens to the seller and receive USDT in return, or vice versa.
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <string>
using namespace std;
int getRandomNumber(int min, int max) {
return min + rand() % ((max + 1) - min);
}
class Wallet {
private:
double token;
double usdt;
public:
Wallet(double token, double usdt) : token(token), usdt(usdt) {}
void deposit(const string& asset, double amount) {
if (asset == "token") {
token += amount;
}
else {
usdt += amount;
}
}
void withdraw(const string& asset, double amount) {
if (asset == "token") {
token -= amount;
}
else {
usdt -= amount;
}
}
double get(const string& asset) const {
if (asset == "token") {
return token;
}
else {
return usdt;
}
}
void print() const {
cout << "Token Amount: " << token << " | Usdt Amount: " << usdt << endl;
}
};
int main() {
srand(static_cast<unsigned int>(time(nullptr)));
Wallet main(150, 5000);
Wallet w1(150, 50);
Wallet w2(150, 50);
Wallet w3(150, 50);
Wallet w4(150, 50);
Wallet w5(150, 50);
Wallet pool(2500, 1000);
vector<Wallet> wallets = { w1, w2, w3, w4, w5 };
vector<Wallet> allWallets = { main, w1, w2, w3, w4, w5, pool };
while (true) {
// Current Pool Price
double price = pool.get("usdt") / pool.get("token");
cout << "Current Price: $" << price << endl;
for (const auto& wal : allWallets) {
wal.print();
}
Sleep(3000);
// Select a random wallet
int ind = getRandomNumber(0, wallets.size() - 1);
Wallet& wal = wallets[ind];
double usdt = wal.get("usdt");
double token = wal.get("token");
cout << "Selected Wallet Token Amount: " << token << endl;
cout << "Selected Wallet Usdt Amount: " << usdt << endl;
cout << endl;
// Transaction Amount
int side = getRandomNumber(0, 1);
double amount = getRandomNumber(3, 6);
// Buy
if (side == 0) {
if (usdt >= amount) {
wal.withdraw("usdt", amount);
pool.deposit("usdt", amount);
double tokenAmount = amount / price;
wal.deposit("token", tokenAmount);
pool.withdraw("token", tokenAmount);
}
else {
double transferAmount = main.get("usdt") / 5;
wal.deposit("usdt", transferAmount);
main.withdraw("usdt", transferAmount);
continue;
}
}
else { // Sell
double tokenAmount = amount / price;
if (token >= tokenAmount) {
wal.withdraw("token", tokenAmount);
pool.deposit("token", tokenAmount);
double usdtAmount = tokenAmount * price;
wal.deposit("usdt", usdtAmount);
pool.withdraw("usdt", usdtAmount);
}
else {
double transferAmount = main.get("token") / 5;
wal.deposit("token", transferAmount);
main.withdraw("token", transferAmount);
continue;
}
}
cout << endl;
}
return 0;
}
New contributor
Umut Altındağ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.