7 if (reader_type == expected_type) {
20 WU_CsvReader csv = (WU_CsvReader)reader;
22 return csv->data_type;
29 WU_Portfolio portfolio = runner->portfolio;
30 WU_Strategy strategy = runner->strategy;
31 void** input_buffers = malloc(
sizeof(
void*) * runner->num_readers);
33 if (!input_buffers || !converted) {
34 fprintf(stderr,
"Error: Failed to allocate input buffers\n");
40 printf(
"Starting backtest with %d input(s)...\n", runner->num_readers);
44 bool all_valid =
true;
45 for (
int i = 0; i < runner->num_readers; i++) {
52 WU_DataType expected_type = strategy->input_types[i];
56 input_buffers[i] = &converted[i];
61 input_buffers[i] = &converted[i];
63 else if (actual_type == expected_type) {
64 input_buffers[i] = data;
67 fprintf(stderr,
"Error at iteration %d: Input %d has type %d but strategy expects type %d\n",
68 iteration, i, actual_type, expected_type);
78 if (verbose && iteration % 100 == 0) {
79 printf(
"Iteration %d | Signals: %d | Value: %.2f | P&L: %.2f\n",
81 strategy->num_outputs,
89 printf(
"Backtest completed after %d iterations\n", iteration);
96 WU_Portfolio portfolio,
100 if (!portfolio || !strategy || !readers) {
101 fprintf(stderr,
"Error: NULL argument passed to wu_runner_new\n");
105 for (
int i = 0; i < num_readers; i++) {
107 fprintf(stderr,
"Error: Reader %d is NULL\n", i);
111 WU_DataType expected_type = strategy->input_types[i];
113 fprintf(stderr,
"Error: Reader %d produces type %d but strategy expects type %d\n",
114 i, reader_type, expected_type);
118 WU_Runner runner = malloc(
sizeof(
struct WU_Runner_));
120 fprintf(stderr,
"Error: Failed to allocate runner\n");
123 runner->readers = malloc(
sizeof(WU_Reader) * num_readers);
124 if (!runner->readers) {
125 fprintf(stderr,
"Error: Failed to allocate reader array\n");
129 memcpy(runner->readers, readers,
sizeof(WU_Reader) * num_readers);
130 runner->portfolio = portfolio;
131 runner->strategy = strategy;
132 runner->num_readers = num_readers;
141 for (
int i = 0; i < runner->num_readers; i++) {
144 free(runner->readers);
#define wu_trade_to_single_value(trade)
#define wu_candle_to_single_value(candle)
#define wu_portfolio_update(portfolio, signals)
The update macro allows you to update the portfolio with new signals.
#define wu_portfolio_delete(portfolio)
Frees the resources associated with the portfolio.
#define wu_portfolio_value(portfolio)
Calculates the current value of the portfolio.
#define wu_portfolio_pnl(portfolio)
Calculates the profit and loss (PnL) of the portfolio.
#define wu_reader_delete(reader)
#define wu_reader_next(reader)
void wu_runner_free(WU_Runner runner)
static bool validate_input_type(WU_DataType reader_type, WU_DataType expected_type)
static WU_DataType get_reader_data_type(WU_Reader reader)
static void run_backtest(WU_Runner runner, bool verbose)
WU_Runner wu_runner_new(WU_Portfolio portfolio, WU_Strategy strategy, WU_Reader readers[])
Creates a new runner with multiple readers.
#define wu_strategy_delete(strategy)
#define wu_strategy_num_inputs(strategy)
#define wu_strategy_update(strategy, inputs)
WU_Candle represents an aggregated data point to represent how prices moved within a specific time pe...
WU_Runner is a backtest runner that processes data from multiple readers, updates a strategy,...
WU_Single represents a single value with a timestamp, which can be a price, an indicator value,...
WU_Trade represents a single trade in the market, including the timestamp, price, volume,...
WU_DataType
WU_DataType represents the type of input data, which can be a candle, a trade, or a single value.
@ WU_DATA_TYPE_SINGLE_VALUE