Skip to content

Commit

Permalink
Fix bug in MovingAverage strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
diogomatoschaves committed Feb 5, 2024
1 parent 172b056 commit d09c4c3
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 146 deletions.
11 changes: 5 additions & 6 deletions stratestic/strategies/moving_average/ma.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def calculate_positions(self, data):
pd.DataFrame
OHLCV data with additional 'side' column containing -1 for short, 1 for long.
"""
data["side"] = np.where(data["SMA"] > data[self.close_col], 1, 0)
data["side"] = np.where(data["SMA"] < data[self.close_col], -1, data["side"])
data["side"] = np.where(data["SMA"] < data[self.close_col], 1, 0)
data["side"] = np.where(data["SMA"] > data[self.close_col], -1, data["side"])
return data

def get_signal(self, row=None):
Expand All @@ -122,10 +122,9 @@ def get_signal(self, row=None):
if row is None:
row = self.data.iloc[-1]

if row["SMA"] > row[self.close_col]:
if row["SMA"] < row[self.close_col]:
return 1
elif row["SMA"] < row[self.close_col]:
elif row["SMA"] > row[self.close_col]:
return -1

elif row["SMA"] == row[self.close_col]:
else:
return 0
6 changes: 3 additions & 3 deletions tests/backtesting/combining/test_strategy_combiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,23 @@ def test_strategy_combiner_input_validation(
"strategies": [Momentum(2), MovingAverage(2)],
"method": "Majority"
},
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, -1.0, 0.0, -1.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, -1.0, 0.0, 1.0],
id='2_strategies-Majority'
),
pytest.param(
{
"strategies": [Momentum(2), MovingAverage(2)],
"method": "Unanimous"
},
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, -1.0, 0.0, -1.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, -1.0, 0.0, 1.0],
id='2_strategies-Unanimous'
),
pytest.param(
{
"strategies": [Momentum(2), MovingAverage(2), BollingerBands(3, 1)],
"method": "Majority"
},
[0.0, 0.0, 0.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0],
[0.0, 0.0, 0.0, 1.0, 1.0, 1.0, -1.0, 0.0, -1.0, 0.0, 1.0],
id='3_strategies-Majority'
),
pytest.param(
Expand Down
2 changes: 1 addition & 1 deletion tests/backtesting/iterative/test_iterative.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_optimize_parameters(self, fixture, mocked_plotly_figure_show):
"method": "Majority"
},
[{"window": (2, 4)}, {"ma": (1, 3)}, {}],
[{'window': 3.0}, {'ma': 1.0}, {'ma': 3.0, 'sd': 1.0}],
[{'window': 3.0}, {'ma': 2.0}, {'ma': 3.0, 'sd': 1.0}],
id='3_strategies-majority'
),
pytest.param(
Expand Down
6 changes: 3 additions & 3 deletions tests/backtesting/iterative/test_iterative_with_margin.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ def test_exceptions(self, leverage, symbol, second_leverage, exception, mocked_p
@pytest.mark.parametrize(
"margin_threshold, expected_result",
[
pytest.param(0.1, 18, id="margin_threshold=0.1"),
pytest.param(0.5, 45, id="margin_threshold=0.5"),
pytest.param(1, 55, id="margin_threshold=1")
pytest.param(0.1, 23, id="margin_threshold=0.1"),
pytest.param(0.5, 91, id="margin_threshold=0.5"),
pytest.param(1, 124, id="margin_threshold=1")
],
)
def test_maximum_leverage(
Expand Down
72 changes: 36 additions & 36 deletions tests/backtesting/vectorized/out/moving_average_no_trading_costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
expected_performance = 0.985296
expected_outperformance = -0.030969

expected_optimization_results = ({"ma": 1.0}, 0.999557)
expected_optimization_results = ({"ma": 3.0}, 0.999557)

expected_results = [
{
Expand Down Expand Up @@ -60,7 +60,7 @@
"taker_buy_quote_volume": 4588065.23181743,
"returns": 0.002946423556387118,
"SMA": 55429.82000000001,
"side": -1,
"side": 1,
"strategy_returns": 0.0,
"strategy_returns_tc": 0.0,
"accumulated_returns": 1.002950768528602,
Expand All @@ -80,12 +80,12 @@
"taker_buy_quote_volume": 22979821.33981915,
"returns": 0.006818529518377586,
"SMA": 55565.7,
"side": -1,
"strategy_returns": -0.006818529518377586,
"strategy_returns_tc": -0.006818529518377586,
"side": 1,
"strategy_returns": 0.006818529518377586,
"strategy_returns_tc": 0.006818529518377586,
"accumulated_returns": 1.0098127857970252,
"accumulated_strategy_returns": 0.993204663909056,
"accumulated_strategy_returns_tc": 0.993204663909056,
"accumulated_strategy_returns": 1.0068418286158654,
"accumulated_strategy_returns_tc": 1.0068418286158654,
},
{
"close_time": Timestamp("2023-09-01 14:39:59.999000+0000", tz="UTC"),
Expand All @@ -100,12 +100,12 @@
"taker_buy_quote_volume": 20037884.70780964,
"returns": 0.005926179097336494,
"SMA": 55784.692500000005,
"side": -1,
"strategy_returns": -0.005926179097336494,
"strategy_returns_tc": -0.005926179097336494,
"side": 1,
"strategy_returns": 0.005926179097336494,
"strategy_returns_tc": 0.005926179097336494,
"accumulated_returns": 1.0158148844101784,
"accumulated_strategy_returns": 0.9873361612642191,
"accumulated_strategy_returns_tc": 0.9873361612642191,
"accumulated_strategy_returns": 1.0128262685320526,
"accumulated_strategy_returns_tc": 1.0128262685320526,
},
{
"close_time": Timestamp("2023-09-01 14:44:59.999000+0000", tz="UTC"),
Expand All @@ -120,12 +120,12 @@
"taker_buy_quote_volume": 10007121.78892216,
"returns": -0.00170962942015996,
"SMA": 55979.6575,
"side": -1,
"strategy_returns": 0.00170962942015996,
"strategy_returns_tc": 0.00170962942015996,
"side": 1,
"strategy_returns": -0.00170962942015996,
"strategy_returns_tc": -0.00170962942015996,
"accumulated_returns": 1.0140797010812264,
"accumulated_strategy_returns": 0.9890255839449716,
"accumulated_strategy_returns_tc": 0.9890255839449716,
"accumulated_strategy_returns": 1.0110961902636069,
"accumulated_strategy_returns_tc": 1.0110961902636069,
},
{
"close_time": Timestamp("2023-09-01 14:49:59.999000+0000", tz="UTC"),
Expand All @@ -140,12 +140,12 @@
"taker_buy_quote_volume": 8146012.47892439,
"returns": 0.0003967606653441501,
"SMA": 56139.335,
"side": -1,
"strategy_returns": -0.0003967606653441501,
"strategy_returns_tc": -0.0003967606653441501,
"side": 1,
"strategy_returns": 0.0003967606653441501,
"strategy_returns_tc": 0.0003967606653441501,
"accumulated_returns": 1.0144821278464156,
"accumulated_strategy_returns": 0.9886332553316708,
"accumulated_strategy_returns_tc": 0.9886332553316708,
"accumulated_strategy_returns": 1.0114974330541975,
"accumulated_strategy_returns_tc": 1.0114974330541975,
},
{
"close_time": Timestamp("2023-09-01 14:54:59.999000+0000", tz="UTC"),
Expand All @@ -160,12 +160,12 @@
"taker_buy_quote_volume": 7529521.30623853,
"returns": -0.0008209293091875578,
"SMA": 56192.465000000004,
"side": 1,
"strategy_returns": 0.0008209293091875578,
"strategy_returns_tc": 0.0008209293091875578,
"side": -1,
"strategy_returns": -0.0008209293091875578,
"strategy_returns_tc": -0.0008209293091875578,
"accumulated_returns": 1.0136496514828948,
"accumulated_strategy_returns": 0.9894451865704871,
"accumulated_strategy_returns_tc": 0.9894451865704871,
"accumulated_strategy_returns": 1.0106674059086556,
"accumulated_strategy_returns_tc": 1.0106674059086556,
},
{
"close_time": Timestamp("2023-09-01 14:59:59.999000+0000", tz="UTC"),
Expand All @@ -180,12 +180,12 @@
"taker_buy_quote_volume": 9447425.0774598,
"returns": 0.0006607487960858385,
"SMA": 56171.759999999995,
"side": -1,
"strategy_returns": 0.0006607487960858385,
"strategy_returns_tc": 0.0006607487960858385,
"side": 1,
"strategy_returns": -0.0006607487960858385,
"strategy_returns_tc": -0.0006607487960858385,
"accumulated_returns": 1.014319640592638,
"accumulated_strategy_returns": 0.9900991773243144,
"accumulated_strategy_returns_tc": 0.9900991773243144,
"accumulated_strategy_returns": 1.0099998292114958,
"accumulated_strategy_returns_tc": 1.0099998292114958,
},
{
"close_time": Timestamp("2023-09-01 15:04:59.999000+0000", tz="UTC"),
Expand All @@ -201,10 +201,10 @@
"returns": 0.0019165664875115606,
"SMA": 56202.027500000004,
"side": 0,
"strategy_returns": -0.0019165664875115606,
"strategy_returns_tc": -0.0019165664875115606,
"strategy_returns": 0.0019165664875115606,
"strategy_returns_tc": 0.0019165664875115606,
"accumulated_returns": 1.016265515727322,
"accumulated_strategy_returns": 0.9882034036901499,
"accumulated_strategy_returns_tc": 0.9882034036901499,
"accumulated_strategy_returns": 1.0119374172015592,
"accumulated_strategy_returns_tc": 1.0119374172015592,
},
]
Loading

0 comments on commit d09c4c3

Please sign in to comment.