Skip to content

Commit 3be1db3

Browse files
authored
Updated load command flow, and added a load script (#40)
1 parent 15cf528 commit 3be1db3

6 files changed

Lines changed: 66 additions & 75 deletions

File tree

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ Xcode - Library Path
9494
The `BacktestingEngine` binary exposes a subcommand CLI:
9595

9696
```
97-
BacktestingEngine load <path> [path...]
98-
Read each file as raw JSON, Base64-encode it, and LPUSH onto the Redis
97+
BacktestingEngine load
98+
Base64-encode a built-in strategy JSON (defined in
99+
source/commands/loadCommand.cpp) and LPUSH it onto the Redis
99100
`strategy_queue` list.
100101
101102
BacktestingEngine run <questdb-host>
@@ -105,9 +106,6 @@ BacktestingEngine run <questdb-host>
105106
BacktestingEngine run <questdb-host> <base64-config>
106107
Decode the supplied Base64 strategy and execute it directly, bypassing
107108
Redis.
108-
109-
BacktestingEngine -h | --help
110-
Show usage.
111109
```
112110

113111
Defaults are `127.0.0.1:6379` for the Redis endpoint and `strategy_queue` for the list key (see `include/redisRunner.hpp` and `include/redisLoader.hpp`).

include/commands/loadCommand.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
#pragma once
88

9-
// Backs the `load` subcommand: reads each strategy file from disk and LPUSHes
10-
// it onto the Redis `strategy_queue` via RedisLoader.
9+
// Backs the `load` subcommand: LPUSHes a strategy JSON (defined in
10+
// source/commands/loadCommand.cpp) onto the Redis `strategy_queue`.
1111
class LoadCommand {
1212
public:
13-
static int run(int argc, const char* argv[]);
13+
static int run();
1414
};

scripts/load.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
# Builds the engine and pushes the built-in strategy JSON
3+
# (source/commands/loadCommand.cpp) onto the Redis `strategy_queue`.
4+
5+
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6+
7+
if ! source "$current_dir/build.sh"; then
8+
echo "Error: Build failed. Aborting."
9+
exit 1
10+
fi
11+
12+
if [ ! -f "$BUILD_DIR/$EXECUTABLE_NAME" ]; then
13+
echo "Error: Executable $EXECUTABLE_NAME not found in $BUILD_DIR."
14+
ls -la "$BUILD_DIR"
15+
exit 1
16+
fi
17+
18+
if ! redis-cli -h localhost ping >/dev/null 2>&1; then
19+
echo "redis-server not reachable on localhost:6379 — aborting"
20+
exit 1
21+
fi
22+
23+
exec ./"$BUILD_DIR/$EXECUTABLE_NAME" load

scripts/run.sh

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,7 @@ if ! redis-cli -h localhost ping >/dev/null 2>&1; then
2424
exit 0
2525
fi
2626

27-
json='{
28-
"RUN_ID": "UNIQUE_IDENTIFIER",
29-
"SYMBOLS": "EURUSD,AUDUSD",
30-
"LAST_MONTHS": 2,
31-
"STRATEGY": {
32-
"UUID": "",
33-
"TRADING_VARIABLES": {
34-
"STRATEGY": "RandomStrategy",
35-
"STOP_DISTANCE_IN_PIPS": "1.5",
36-
"LIMIT_DISTANCE_IN_PIPS": "1.5",
37-
"TRADING_SIZE": "1"
38-
},
39-
"OHLC_VARIABLES": [
40-
{
41-
"OHLC_COUNT": 60,
42-
"OHLC_MINUTES": 100
43-
}
44-
],
45-
"STRATEGY_VARIABLES": {
46-
"OHLC_RSI_VARIABLES": {
47-
"RSI_LONG": 60,
48-
"RSI_SHORT": 40
49-
}
50-
}
51-
}
52-
}'
53-
54-
tmp=$(mktemp -t backtesting-load.XXXXXX)
55-
trap 'rm -f "$tmp"' EXIT
56-
printf '%s' "$json" > "$tmp"
57-
58-
if ! ./"$BUILD_DIR/$EXECUTABLE_NAME" load "$tmp"; then
27+
if ! ./"$BUILD_DIR/$EXECUTABLE_NAME" load; then
5928
exit 1
6029
fi
6130

source/commands/loadCommand.cpp

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,37 @@
66

77
#include "loadCommand.hpp"
88

9-
#include <fstream>
10-
#include <iostream>
11-
#include <sstream>
12-
#include <string>
9+
#include <boost/decimal/literals.hpp>
10+
#include <nlohmann/json.hpp>
1311

1412
#include "redisLoader.hpp"
13+
#include "trading_definitions.hpp"
1514

16-
int LoadCommand::run(int argc, const char* argv[]) {
17-
if (argc < 3) {
18-
std::cerr << "Usage: " << argv[0] << " load <path> [path...]"
19-
<< std::endl;
20-
return 1;
21-
}
22-
for (int i = 2; i < argc; ++i) {
23-
const std::string path = argv[i];
24-
std::ifstream ifs(path);
25-
if (!ifs) {
26-
std::cerr << "BacktestingEngine: failed to open file: " << path
27-
<< std::endl;
28-
return 1;
29-
}
30-
std::ostringstream buffer;
31-
buffer << ifs.rdbuf();
32-
if (ifs.bad()) {
33-
std::cerr << "BacktestingEngine: failed to read file: " << path
34-
<< std::endl;
35-
return 1;
36-
}
37-
const int rc = RedisLoader::load(buffer.str(), "127.0.0.1", 6379,
38-
"strategy_queue");
39-
if (rc != 0) {
40-
return rc;
41-
}
42-
}
43-
return 0;
44-
}
15+
int LoadCommand::run() {
16+
using namespace boost::decimal::literals;
17+
using namespace trading_definitions;
18+
19+
const Configuration config{
20+
.RUN_ID = "UNIQUE_IDENTIFIER",
21+
.SYMBOLS = "EURUSD,AUDUSD",
22+
.LAST_MONTHS = 2,
23+
.STRATEGY = Strategy{
24+
.UUID = "",
25+
.TRADING_VARIABLES = TradingVariables{
26+
.STRATEGY = "RandomStrategy",
27+
.STOP_DISTANCE_IN_PIPS = 1.5_DD,
28+
.LIMIT_DISTANCE_IN_PIPS = 1.5_DD,
29+
.TRADING_SIZE = 1_DD,
30+
},
31+
.OHLC_VARIABLES = {
32+
OHLCVariables{.OHLC_COUNT = 60, .OHLC_MINUTES = 100},
33+
},
34+
.STRATEGY_VARIABLES = StrategyVariables{
35+
.OHLC_RSI_VARIABLES = OHLCRSIVariables{.RSI_LONG = 60, .RSI_SHORT = 40},
36+
},
37+
},
38+
};
39+
40+
const nlohmann::json j = config;
41+
return RedisLoader::load(j.dump());
42+
}

source/main.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@
1212
#include "loadCommand.hpp"
1313
#include "runCommand.hpp"
1414

15+
// Entry point
1516
int main(int argc, const char* argv[]) {
17+
1618
if (argc < 2) {
17-
std::cerr << "BacktestingEngine: missing subcommand. See README.md for usage."
19+
std::cerr << "BacktestingEngine: missing a subcommand. See README.md for usage"
1820
<< std::endl;
1921
return 1;
2022
}
2123

2224
const std::string_view subcommand = argv[1];
2325

24-
if (subcommand == "load") return LoadCommand::run(argc, argv);
26+
if (subcommand == "load") return LoadCommand::run();
2527
if (subcommand == "run") return RunCommand::run(argc, argv);
2628

2729
std::cerr << "BacktestingEngine: unknown subcommand. See README.md for usage."
2830
<< std::endl;
31+
2932
return 1;
3033
}

0 commit comments

Comments
 (0)