Skip to content

Commit 955413d

Browse files
authored
31 feat tidy up the code refactoring code smells and more (#32)
* removing tickIndex, we don't need this variable * Improving the connection flow * Improving code smells from sonarcloud
1 parent bc3dae1 commit 955413d

8 files changed

Lines changed: 22 additions & 21 deletions

File tree

include/strategies/randomStrategy.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ class RandomStrategy : public IStrategy {
5757
// is passed by mutable reference so future strategies (trailing
5858
// stops, partial closes, scale-ins) can act on open positions
5959
// here without changing the interface.
60-
void during(std::size_t tickValue,
61-
const PriceData& price,
60+
void during(const PriceData& price,
6261
TradeManager& tradeManager) override;
6362

6463
private:

include/strategies/strategy.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ class IStrategy {
5656
// analogue is just passing the manager as a parameter; C# has no
5757
// distinction between reference and pointer so the by-ref nature
5858
// is implicit there.
59-
virtual void during(std::size_t tickValue,
60-
const PriceData& price,
59+
virtual void during(const PriceData& price,
6160
TradeManager& tradeManager) = 0;
6261
};

include/utilities/base64.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
class Base64 {
1515
public:
16-
static const std::string b64encode(const void* data, const size_t &len);
17-
static const std::string b64decode(const void* data, const size_t &len);
16+
static const std::string b64encode(const unsigned char* data, const size_t &len);
17+
static const std::string b64decode(const unsigned char* data, const size_t &len);
1818
static std::string b64encode(const std::string& str);
1919
static std::string b64decode(const std::string& str64);
2020
static bool isValidBase64(const std::string& input);

source/databaseConnection.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
#include <boost/decimal.hpp>
1414

1515
static std::chrono::system_clock::time_point fastParseTimestamp(const char* ts) {
16-
int year = 0, month = 0, day = 0, hour = 0, min = 0, sec = 0, usec = 0;
16+
int year = 0;
17+
int month = 0;
18+
int day = 0;
19+
int hour = 0;
20+
int min = 0;
21+
int sec = 0;
22+
int usec = 0;
1723
const int parsedFields =
1824
std::sscanf(ts, "%4d-%2d-%2d %2d:%2d:%2d.%d", &year, &month, &day, &hour, &min, &sec, &usec);
1925
if (parsedFields != 6 && parsedFields != 7) {
@@ -59,7 +65,8 @@ std::vector<PriceData> DatabaseConnection::streamQuery(const std::string& query)
5965

6066
for (std::size_t i = 0; i < result.size(); ++i) {
6167
const auto& row = result[static_cast<pqxx::result::size_type>(i)];
62-
boost::decimal::decimal64_t ask, bid;
68+
boost::decimal::decimal64_t ask;
69+
boost::decimal::decimal64_t bid;
6370
auto symbol = row[0].view();
6471
auto sv1 = row[1].view();
6572
auto sv2 = row[2].view();

source/operations.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ void Operations::run(const std::vector<PriceData>& ticks,
6464

6565
const auto& tradingVars = config.STRATEGY.TRADING_VARIABLES;
6666

67-
std::size_t tickIndex = 0;
6867
for (const auto& tick : ticks) {
6968

7069
// Close any trade whose stop-loss or take-profit fired on this tick
@@ -90,9 +89,7 @@ void Operations::run(const std::vector<PriceData>& ticks,
9089
// (e.g. trailing stops, partial closes). The default
9190
// RandomStrategy implementation is a no-op now that exits are
9291
// handled by reviewStopAndLimit above.
93-
strategy->during(tickIndex, tick, *tradeManager);
94-
95-
++tickIndex;
92+
strategy->during(tick, *tradeManager);
9693
}
9794

9895
std::cout << "Final PnL: " << std::fixed << std::setprecision(2) << tradeManager->calculatePnl() << std::endl;

source/strategies/randomStrategy.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ std::optional<Direction> RandomStrategy::decide(const PriceData& /*tick*/) {
2323
return coin(rng) ? Direction::LONG : Direction::SHORT;
2424
}
2525

26-
void RandomStrategy::during(std::size_t /*tickValue*/,
27-
const PriceData& /*price*/,
26+
void RandomStrategy::during(const PriceData& /*price*/,
2827
TradeManager& /*tradeManager*/) {
2928
// Exits are handled centrally by Operations using each trade's
3029
// stop-loss / take-profit pip distances. Strategies that want

source/trading/tradeManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace {
1616
std::string nextTradeId() {
1717
static std::atomic<uint64_t> counter{0};
18-
return "T" + std::to_string(counter.fetch_add(1, std::memory_order_relaxed));
18+
return "T" + std::to_string(counter.fetch_add(1));
1919
}
2020
}
2121

source/utilities/base64.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ std::string Base64::checkInput(const std::string& base64_input) {
3333
}
3434

3535
// Code adapted from Stack Overflow https://stackoverflow.com/a/37109258/20806857
36-
const std::string Base64::b64encode(const void* data, const size_t &len)
36+
const std::string Base64::b64encode(const unsigned char* data, const size_t &len)
3737
{
3838
std::string result((len + 2) / 3 * 4, '=');
39-
unsigned char *p = (unsigned char*) data;
39+
const unsigned char *p = data;
4040
char *str = &result[0];
4141
size_t j = 0, pad = len % 3;
4242
const size_t last = len - pad;
@@ -59,11 +59,11 @@ const std::string Base64::b64encode(const void* data, const size_t &len)
5959
return result;
6060
}
6161

62-
const std::string Base64::b64decode(const void* data, const size_t &len)
62+
const std::string Base64::b64decode(const unsigned char* data, const size_t &len)
6363
{
6464
if (len == 0) return "";
6565

66-
unsigned char *p = (unsigned char*) data;
66+
const unsigned char *p = data;
6767
size_t j = 0,
6868
pad1 = len % 4 || p[len - 1] == '=',
6969
pad2 = pad1 && (len % 4 > 2 || p[len - 2] != '=');
@@ -93,12 +93,12 @@ const std::string Base64::b64decode(const void* data, const size_t &len)
9393

9494
std::string Base64::b64encode(const std::string& str)
9595
{
96-
return b64encode(str.c_str(), str.size());
96+
return b64encode(reinterpret_cast<const unsigned char*>(str.c_str()), str.size());
9797
}
9898

9999
std::string Base64::b64decode(const std::string& str64)
100100
{
101-
return b64decode(str64.c_str(), str64.size());
101+
return b64decode(reinterpret_cast<const unsigned char*>(str64.c_str()), str64.size());
102102
}
103103

104104
bool Base64::isValidBase64(const std::string& input) {

0 commit comments

Comments
 (0)