Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"python.defaultInterpreterPath": "./.venv/bin/python",
"python.defaultInterpreterPath": ".venv/bin/python",
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
Expand Down
9 changes: 7 additions & 2 deletions makefiles/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ mypy-local:
lint-local: ##@lint Run lint tools
lint-local: bandit-local black-local flake8-local isort-local mypy-local

.PHONY: clean-imports
clean-imports: ##@local Remove unused imports
clean-imports:
autoflake --in-place --remove-all-unused-imports --recursive pytradebacktest tests

.PHONY: reformat
reformat: ##@local Reformat module
reformat: files ?= ${SERVICE} tests
reformat:
reformat: clean-imports
${POETRY} run isort --overwrite-in-place ${files}
${POETRY} run black ${files}

PHONY: test-local
test-local: ##@local Run test suite
test-local: venv
${POETRY} run pytest -s --tb=native --durations=5 --cov=${SERVICE} --cov-report=html tests
${POETRY} run pytest -s --tb=native --durations=5 --cov=${SERVICE} --cov-report=term-missing tests
${POETRY} run coverage report --fail-under=90
48 changes: 32 additions & 16 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ python = "^3.10"
pytrade = {path = "lib/PyTrade", develop = true}
pandas = "^2.2.2"
pytest-asyncio = "^0.25.1"
autoflake = "^2.3.1"


[tool.poetry.group.dev.dependencies]
Expand Down Expand Up @@ -47,4 +48,8 @@ exclude_also = [
"@(abc\\.)?abstractmethod",
]
omit = [
]
]

[tool.pytest.ini_options]
addopts = ["--import-mode=importlib"]
pythonpath = [ "." ]
20 changes: 15 additions & 5 deletions pytradebacktest/backtest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Type

from pytrade.indicator import Indicator
from pytrade.strategy import FxStrategy

from pytradebacktest.broker import BacktestBroker
from pytradebacktest.data import MarketData
from pytradebacktest.strategy import BacktestStrategyWrapper


class Backtest:
Expand All @@ -25,21 +25,31 @@ def __init__(

async def run(self):

broker = BacktestBroker(self.data)
# Monkey patch indicators so their update does not
# need to recalculate after each increment, instead
# store their initial values from the full data context
# and then increment based on the current context length
def increment_indicator(self):
if not hasattr(self, "_backtest_values"):
self._backtest_values = self._values.copy()
self._values = self._backtest_values[: len(self._data)]

strategy = BacktestStrategyWrapper(broker, self.data, self.kstrategy)
Indicator._update = increment_indicator

broker = BacktestBroker(self.data, self.cash, self.comission, self.margin)

strategy = self.kstrategy(broker, self.data)
strategy.init()

while self.data.next():

broker.next()
await strategy.next()
strategy.next()

# Increment data points
# Update indicators
# Update trades
# Claculate results/stats
pass

def plot(self):
pass
Loading
Loading