Skip to content

milas-melt/backtester_framework

Repository files navigation

Custom Python Framework Backtester

  • Breakout strat with combined signals from different breakout criteria.
  • Look Back Straddle Deltas MA Cross Over strat
  • Returns prediction using ML for seasonality detection fed into a random forest for prediction. (unsupervised-to-supervised learning methods)
  • Backtester from 1969 to 2023 with rolling_table and performance analysis

Benchmark

Equally weighted portfolio of all commodities in the dataset.

Breakout strat

Breakout trading is a popular and dynamic trading strategy employed by traders. It revolves around identifying and capitalizing on significant price movements that occur when an asset's price breaks through a well-defined level of support or resistance.

High Low

Signal strat

"Buy a higher high, sell a lower low, and hold the position to make a profit."

  • Long if today’s return is the highest of all the returns in the window.
  • Short if today’s return is the lowest of all the returns in the window.
  • Else, hold.

Performance

Screenshot 2023-06-03 at 20 00 42

Mean Std

Signal strat

  • Long when return is higher than mean +1 * std
  • Short when return is below the mean -1 * std
  • Else, hold.

Performance

Screenshot 2023-06-03 at 20 01 11

Combine the two criteria

Many ways of doing so. Assuming no tx fees, one solution is to compute the equally weighted average of both signals.

Performance

Screenshot 2023-06-03 at 20 01 33

Lookback straddle delta MA crossover

Method

image We assume the first day price is one and compute the subsequent price based on. We then compute that 5 day Moving Average of the delta. We choose to use the delta’s MA rather than the price volatility since it can catch up the reversal more quickly, as shown in the figure below.

Thus, it would be helpful to limit our loss.

Screenshot 2023-06-03 at 20 19 04

Signal strat

The signals are generated as follow:

  • If the current delta crosses the 5 Day delta MA to go down, we sell.
  • If the current delta crosses the 5 Day delta MA to go up, we buy.

Performance

Screenshot 2023-06-03 at 20 19 45 Screenshot 2023-06-03 at 20 20 03

According to both performance graphs, it is clear that although the strategy generated is better than the benchmark, further computation is required in order to define how well the strategy does in comparison to the others. A similar computation with other signals cannot be done within less than 6 hours at least which is why the trategy is backtested on 5 months data only.

Machine Learning Methods

First, the prophet model was used to decompose the returns into different time intervals, according to the signals seasonalities and patterns (such as harvesing cycles for agricultural commodities, for instance).

Screenshot 2023-06-03 at 20 24 51

Then, these variables are fed in a random forest model for future return predictions.

Screenshot 2023-06-03 at 20 26 01

Error Metrics

MSE: 0.0346 R^2: 0.000305.

The MSE is small however, the R^2 suggests that the model requires further improvements.

About

Multi-strat backtesting engine for commodity futures

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors