To represent a strategy, an abstract TradeStrategyBase class is created. All strategies is an extension of TradeStrategyBase.
When price rises above an absolute threshold p_change_threshold
percent, default to 2%.
Buy and hold for hold_stock_threshold
days, default to 20.
When price falls consecutively for two days, and cumulatively more than p_change_threshold
percent in total, default to 10%.
Buy and hold for hold_stock_threshold
days, default to 10.
Use _x_days
(Default 5) of data as moving average, when price rises or falls by Moving Average +- _std_mutiplier
(Default 2.0) * Moving Standard Deviation.
Close when price moves back to moving average.
Use close price to draw a trend, use the derivative to find the slope
. Use the derivative again to find the velocity
for change.
- Long when price is rising (slope > _buy_slope_threshold > 0) & accelerating (velocity > _buy_velocity_threshold > 0) and,
- Close when price starting to fall (slope < -_close_slope_threshold < 0 and velocity < -_close_velocity_threshold < 0)
- Short when price is falling (slope < _sell_slope_threshold < 0) & accelerating (velocity < _sell_velocity_threshold < 0) and,
- Close when price starting to rise (slope > _close_slope_threshold > 0 and velocity > _close_velocity_threshold > 0)
Similar to Strategy 4, optimized for performance.
This section covers the back testing and optimization of strategies.
Use one set of 2 parameter, p_change_threshold
(p1) & hold_stock_threshold
(p2).
Trading AXP, when Price rises above 3%, buy and hold for 5 days.
Strategy Resulted 4.85%.
Use one set of 2 parameter, p_change_threshold
(p1) & hold_stock_threshold
(p2).
Trading AXP, when Price falls above 5% in 2 consecutive days, buy and hold for 3 days.
Strategy Resulted -2.02%.
Use one set of 2 parameter, std_mutiplier
(p1) & x_days
(p2).
Trading AXP, take x_days
of rolling average and standard deviation, and trade when price deviates from moving average too much.
Strategy Resulted 1.31%.
Use one set of 6 threshold parameter.
Trading AXP, buy when slope and velocity is more than 0.1 and close when both is 0. Sell when slope and velocity is less then -0.1 and close when both is 0
Strategy Resulted 200.62%.
Utilizing Strategy 4, applying to Forex (AUDUSD).
Strategy Resulted 281.11%% for last 288 trade days. See Trades below
Finding the Optimal Parameters for a given strategy.
Using itertools
and ThreadPoolExecutor
to basktest with 1024 sets of parameters, and find out which set yields best results.
Best Result: 210.019729%.
Note, backtesting this way takes VERY VERY long time. For this case, 3:11 to complete 1024 tests. Hence the following faster method.
Using numpy
and masks
to basktest with 1024 sets of parameters, and find out which set yields best results.
Best Result: 177.03%. Finished in 24:48s.
Building upon Numpy Backtest, wrap it with Namba JIT to boost performance.
Best Result: 177.03%. Finished in 15:34s.
Turbo charge Namba with MultiProcess.
Best Result: 177.03%. Finished in 07:22s.
Backtesting 15,625 sets of params to find the best params for Slope & Velocity Strategy.
Best Result: 281.89% p.a.
Shadow: Green = LONG, Red = SHORT, Blank = CLOSE.
Find a Linear Regression Model that describes the relationship between date
and price
using OLS.
price = k * price + b
The result is
price = 0.0574 * price + 85.6971
Utilizing Strategy4, Trading AXP, Use 10^6 sets of 6 parameters, back test all sets, and find the best 5 sets of parameters which yields the highest profit.