-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.cpp
More file actions
94 lines (77 loc) · 2.25 KB
/
Bot.cpp
File metadata and controls
94 lines (77 loc) · 2.25 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
#include "Bot.h"
#include "Position/Position.h"
#include "ExchangeAPI/ExchangeWrapper.h"
#include <string>
#include <utility>
#include <fstream>
#include <iostream>
#include <ctime>
Bot::Bot(std::string exchange1, std::string exchange2, double quantity){
ExchangeWrapper wrapper;
ex1_ = wrapper(exchange1, quantity);
ex2_ = wrapper(exchange2, quantity);
quantity_ = quantity;
}
Bot::~Bot(){
delete ex1_; ex1_ = nullptr;
delete ex2_; ex2_ = nullptr;
}
void Bot::startBot(long endTime){
while(std::time(nullptr) < endTime){
std::pair<Position, bool> potentialArb = tryArbitrage();
if (std::get<1>(potentialArb) == true){
writeToTextFile(std::get<0>(potentialArb));
}
}
}
void Bot::startBot(int numTrades){
int totalTrades = 0;
while(totalTrades < numTrades){
std::pair<Position, bool> potentialArb = tryArbitrage();
if (std::get<1>(potentialArb) == true){
writeToTextFile(std::get<0>(potentialArb));
totalTrades++;
}
}
}
std::pair<Position, bool> Bot::tryArbitrage(){
double e1Spot = ex1_->getSpotPrice();
double e2Spot = ex2_->getSpotPrice();
if(isProfitableSpread(e1Spot, e2Spot)){
Position tradeData = executeArbitrage(e1Spot, e2Spot);
return std::pair<Position,bool>(tradeData, true);
}
else {
return std::pair<Position,bool>(Position(), false);
}
}
void Bot::writeToTextFile(Position p){
std::string pos = "";
pos += (std::to_string(p.q) + ",");
pos += (std::to_string(p.sP) + ",");
pos += (std::to_string(p.lP) + ",");
pos += ((std::to_string(p.completionTime) + "\n"));
std::cin >> pos;
std::ofstream out("closedPositions.txt");
out << pos;
}
Position Bot::executeArbitrage(double spot1, double spot2){
//TODO: IMPLEMENT ARBITRAGE
ex1_->goLong(); ex2_->goShort();
//TODO: VERIFY FUNDS ARE SETTLED
Position tradeData(quantity_, spot1, spot2);
tradeData.completionTime = std::time(nullptr);
tradeData.isComplete = true;
return tradeData;
}
bool Bot::isProfitableSpread(double spot1, double spot2){
//TODO: Include additional parameters beyond just ROI
double e1Commission = ex1_->getCommission();
double e2Commission = ex2_->getCommission();
double spread = spot1-spot2;
double adjSpread = spread-(spread*(e1Commission+e2Commission));
if (adjSpread > 0){
return true;
}
return false;
}