Skip to content

Commit

Permalink
Integrated recommendation strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
AbYT101 committed Jun 26, 2024
1 parent 4b8a3a0 commit 83be8e1
Show file tree
Hide file tree
Showing 7 changed files with 700 additions and 80 deletions.
3 changes: 2 additions & 1 deletion app/models/backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Backtest(db.Model):
start_date = db.Column(db.Date)
end_date = db.Column(db.Date)
inital_cash = db.Column(db.Integer)
fee = db.Column(db.Integer)
fee = db.Column(db.Float)
# status = db.Column(db.String(50))
created_at = db.Column(db.DateTime, default=db.func.current_timestamp())

Expand Down Expand Up @@ -36,6 +36,7 @@ class Result(db.Model):
losing_trades = db.Column(db.Integer)
max_drawdown = db.Column(db.Numeric(10, 2))
sharpe_ratio = db.Column(db.Numeric(10, 2))
is_best = db.Column(db.Boolean, default=False, nullable=True)

class Metric(db.Model):
__tablename__ = 'metrics'
Expand Down
3 changes: 2 additions & 1 deletion app/routes/backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def get_backtest_results(backtest_id):
'winning_trades': result.winning_trades,
'losing_trades': result.losing_trades,
'max_drawdown': float(result.max_drawdown),
'sharpe_ratio': float(result.sharpe_ratio)
'sharpe_ratio': float(result.sharpe_ratio),
'is_best': result.is_best
})

return jsonify({'results': result_list}), 200
29 changes: 15 additions & 14 deletions app/services/backtest_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,16 @@ def run_and_evaluate_backtest(backtest_id, symbol, initial_cash, fee, start_date
]

results = []
result_objects = []
for strategy in strategies:

print(strategy, symbol, initial_cash, fee, start_date, end_date)
result = run_backtest(strategy, symbol, initial_cash, fee, start_date, end_date)
result['backtest_id'] = backtest_id
if(strategy == RsiBollingerBandsStrategy):
result['strategy'] = 'RsiBollingerBandsStrategy'
elif (strategy == MacdStrategy):
result['strategy'] = 'MacdStrategy'
elif(strategy == StochasticOscillatorStrategy):
result['strategy'] = 'StochasticOscillatorStrategy'
result['strategy'] = strategy.__name__

result_obj = Result(**result)
db.session.add(result_obj)
result_objects.append(result_obj)

metrics = {
"total_return": result['total_return'],
Expand All @@ -65,11 +62,11 @@ def run_and_evaluate_backtest(backtest_id, symbol, initial_cash, fee, start_date
}
mlflow_service.log_metrics(run_name=f"Backtest_{backtest_id}", metrics=metrics)

# Uncomment to publish results to Kafka
# kafka_service.produce('backtest_results', {
# "backtest_id": backtest_id,
# "metrics": metrics
# })
# publish results to Kafka
kafka_service.produce('backtest_results', {
"backtest_id": backtest_id,
"metrics": metrics
})

db.session.commit()
results.append(result)
Expand All @@ -84,10 +81,14 @@ def run_and_evaluate_backtest(backtest_id, symbol, initial_cash, fee, start_date
scores = [score_backtest(result, min_return, max_return, min_sharpe, max_sharpe, min_drawdown, max_drawdown) for result in results]

best_strategy_index = scores.index(max(scores))
best_strategy = strategies[best_strategy_index]

for idx, result_obj in enumerate(result_objects):
result_obj.is_best = (idx == best_strategy_index)

db.session.commit()

print("Best Strategy:")
print(best_strategy.__name__)
print(strategies[best_strategy_index].__name__)
print("Score:")
print(scores[best_strategy_index])
print("Metrics:")
Expand Down
Loading

0 comments on commit 83be8e1

Please sign in to comment.