onesecondtrader 0.32.0__tar.gz → 0.34.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/PKG-INFO +1 -1
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/pyproject.toml +1 -1
- onesecondtrader-0.34.0/src/onesecondtrader/__init__.py +52 -0
- onesecondtrader-0.34.0/src/onesecondtrader/datafeeds/__init__.py +7 -0
- onesecondtrader-0.34.0/src/onesecondtrader/datafeeds/base.py +19 -0
- onesecondtrader-0.34.0/src/onesecondtrader/datafeeds/simulated.py +99 -0
- onesecondtrader-0.34.0/src/onesecondtrader/indicators/base.py +60 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/strategies/__init__.py +2 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/strategies/base.py +2 -2
- onesecondtrader-0.34.0/src/onesecondtrader/strategies/sma_crossover.py +35 -0
- onesecondtrader-0.32.0/src/onesecondtrader/__init__.py +0 -5
- onesecondtrader-0.32.0/src/onesecondtrader/indicators/base.py +0 -50
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/LICENSE +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/README.md +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/brokers/__init__.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/brokers/base.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/brokers/simulated.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/events/__init__.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/events/bases.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/events/market.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/events/requests.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/events/responses.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/indicators/__init__.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/indicators/averages.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/indicators/bar.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/messaging/__init__.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/messaging/eventbus.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/messaging/subscriber.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/models/__init__.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/models/data.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/models/orders.py +0 -0
- {onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/models/records.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: onesecondtrader
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.34.0
|
|
4
4
|
Summary: The Trading Infrastructure Toolkit for Python. Research, simulate, and deploy algorithmic trading strategies — all in one place.
|
|
5
5
|
License-File: LICENSE
|
|
6
6
|
Author: Nils P. Kujath
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "onesecondtrader"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.34.0"
|
|
4
4
|
description = "The Trading Infrastructure Toolkit for Python. Research, simulate, and deploy algorithmic trading strategies — all in one place."
|
|
5
5
|
authors = [
|
|
6
6
|
{name = "Nils P. Kujath",email = "63961429+NilsKujath@users.noreply.github.com"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
"BarPeriod",
|
|
3
|
+
"BarProcessed",
|
|
4
|
+
"BarReceived",
|
|
5
|
+
"BrokerBase",
|
|
6
|
+
"Close",
|
|
7
|
+
"Datafeed",
|
|
8
|
+
"FillRecord",
|
|
9
|
+
"High",
|
|
10
|
+
"Indicator",
|
|
11
|
+
"InputSource",
|
|
12
|
+
"Low",
|
|
13
|
+
"Open",
|
|
14
|
+
"OrderFilled",
|
|
15
|
+
"OrderRecord",
|
|
16
|
+
"OrderSide",
|
|
17
|
+
"OrderSubmission",
|
|
18
|
+
"OrderType",
|
|
19
|
+
"SimulatedBroker",
|
|
20
|
+
"SimulatedDatafeed",
|
|
21
|
+
"SimpleMovingAverage",
|
|
22
|
+
"SMACrossover",
|
|
23
|
+
"StrategyBase",
|
|
24
|
+
"Volume",
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
from onesecondtrader.brokers import BrokerBase, SimulatedBroker
|
|
28
|
+
from onesecondtrader.datafeeds import Datafeed, SimulatedDatafeed
|
|
29
|
+
from onesecondtrader.events import (
|
|
30
|
+
BarProcessed,
|
|
31
|
+
BarReceived,
|
|
32
|
+
OrderFilled,
|
|
33
|
+
OrderSubmission,
|
|
34
|
+
)
|
|
35
|
+
from onesecondtrader.indicators import (
|
|
36
|
+
Close,
|
|
37
|
+
High,
|
|
38
|
+
Indicator,
|
|
39
|
+
Low,
|
|
40
|
+
Open,
|
|
41
|
+
SimpleMovingAverage,
|
|
42
|
+
Volume,
|
|
43
|
+
)
|
|
44
|
+
from onesecondtrader.models import (
|
|
45
|
+
BarPeriod,
|
|
46
|
+
FillRecord,
|
|
47
|
+
InputSource,
|
|
48
|
+
OrderRecord,
|
|
49
|
+
OrderSide,
|
|
50
|
+
OrderType,
|
|
51
|
+
)
|
|
52
|
+
from onesecondtrader.strategies import SMACrossover, StrategyBase
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import abc
|
|
2
|
+
|
|
3
|
+
from onesecondtrader import events, messaging, models
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Datafeed(abc.ABC):
|
|
7
|
+
def __init__(self, event_bus: messaging.EventBus) -> None:
|
|
8
|
+
self._event_bus = event_bus
|
|
9
|
+
|
|
10
|
+
def _publish(self, event: events.EventBase) -> None:
|
|
11
|
+
self._event_bus.publish(event)
|
|
12
|
+
|
|
13
|
+
@abc.abstractmethod
|
|
14
|
+
def stream(self, symbols: list[str], bar_period: models.BarPeriod) -> None:
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
@abc.abstractmethod
|
|
18
|
+
def shutdown(self) -> None:
|
|
19
|
+
pass
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import pathlib
|
|
2
|
+
import threading
|
|
3
|
+
|
|
4
|
+
import pandas as pd
|
|
5
|
+
|
|
6
|
+
from onesecondtrader import events, messaging, models
|
|
7
|
+
from .base import Datafeed
|
|
8
|
+
|
|
9
|
+
_RTYPE_MAP = {
|
|
10
|
+
models.BarPeriod.SECOND: 32,
|
|
11
|
+
models.BarPeriod.MINUTE: 33,
|
|
12
|
+
models.BarPeriod.HOUR: 34,
|
|
13
|
+
models.BarPeriod.DAY: 35,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class SimulatedDatafeed(Datafeed):
|
|
18
|
+
csv_path: str = ""
|
|
19
|
+
|
|
20
|
+
def __init__(self, event_bus: messaging.EventBus) -> None:
|
|
21
|
+
super().__init__(event_bus)
|
|
22
|
+
self._thread: threading.Thread | None = None
|
|
23
|
+
self._stop_event = threading.Event()
|
|
24
|
+
|
|
25
|
+
def stream(self, symbols: list[str], bar_period: models.BarPeriod) -> None:
|
|
26
|
+
csv_path = pathlib.Path(self.csv_path)
|
|
27
|
+
if self._thread and self._thread.is_alive():
|
|
28
|
+
raise RuntimeError("Already streaming")
|
|
29
|
+
if not csv_path.exists():
|
|
30
|
+
raise FileNotFoundError(f"CSV file not found: {csv_path}")
|
|
31
|
+
if not symbols:
|
|
32
|
+
raise ValueError("symbols list cannot be empty")
|
|
33
|
+
|
|
34
|
+
self._stop_event.clear()
|
|
35
|
+
self._thread = threading.Thread(
|
|
36
|
+
target=self._stream,
|
|
37
|
+
args=(symbols, bar_period),
|
|
38
|
+
name=self.__class__.__name__,
|
|
39
|
+
daemon=False,
|
|
40
|
+
)
|
|
41
|
+
self._thread.start()
|
|
42
|
+
|
|
43
|
+
def wait(self) -> None:
|
|
44
|
+
if self._thread:
|
|
45
|
+
self._thread.join()
|
|
46
|
+
|
|
47
|
+
def shutdown(self) -> None:
|
|
48
|
+
self._stop_event.set()
|
|
49
|
+
if self._thread and self._thread.is_alive():
|
|
50
|
+
self._thread.join()
|
|
51
|
+
|
|
52
|
+
def _stream(self, symbols: list[str], bar_period: models.BarPeriod) -> None:
|
|
53
|
+
symbols_set = set(symbols)
|
|
54
|
+
rtype = _RTYPE_MAP[bar_period]
|
|
55
|
+
|
|
56
|
+
for chunk in pd.read_csv(
|
|
57
|
+
self.csv_path,
|
|
58
|
+
usecols=[
|
|
59
|
+
"ts_event",
|
|
60
|
+
"rtype",
|
|
61
|
+
"open",
|
|
62
|
+
"high",
|
|
63
|
+
"low",
|
|
64
|
+
"close",
|
|
65
|
+
"volume",
|
|
66
|
+
"symbol",
|
|
67
|
+
],
|
|
68
|
+
dtype={
|
|
69
|
+
"ts_event": int,
|
|
70
|
+
"rtype": int,
|
|
71
|
+
"open": int,
|
|
72
|
+
"high": int,
|
|
73
|
+
"low": int,
|
|
74
|
+
"close": int,
|
|
75
|
+
"volume": int,
|
|
76
|
+
"symbol": str,
|
|
77
|
+
},
|
|
78
|
+
chunksize=10_000,
|
|
79
|
+
):
|
|
80
|
+
for row in chunk.itertuples():
|
|
81
|
+
if self._stop_event.is_set():
|
|
82
|
+
return
|
|
83
|
+
|
|
84
|
+
if row.symbol not in symbols_set or row.rtype != rtype:
|
|
85
|
+
continue
|
|
86
|
+
|
|
87
|
+
self._publish(
|
|
88
|
+
events.BarReceived(
|
|
89
|
+
ts_event=pd.Timestamp(row.ts_event, unit="ns", tz="UTC"),
|
|
90
|
+
symbol=row.symbol,
|
|
91
|
+
bar_period=bar_period,
|
|
92
|
+
open=row.open / 1e9,
|
|
93
|
+
high=row.high / 1e9,
|
|
94
|
+
low=row.low / 1e9,
|
|
95
|
+
close=row.close / 1e9,
|
|
96
|
+
volume=row.volume,
|
|
97
|
+
)
|
|
98
|
+
)
|
|
99
|
+
self._event_bus.wait_until_system_idle()
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import abc
|
|
4
|
+
import collections
|
|
5
|
+
import threading
|
|
6
|
+
|
|
7
|
+
import numpy as np
|
|
8
|
+
|
|
9
|
+
from onesecondtrader import events
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Indicator(abc.ABC):
|
|
13
|
+
def __init__(self, max_history: int = 100, plot_at: int = 99) -> None:
|
|
14
|
+
self._lock = threading.Lock()
|
|
15
|
+
self._max_history = max(1, int(max_history))
|
|
16
|
+
self._current_symbol: str = ""
|
|
17
|
+
self._history_data: dict[str, collections.deque[float]] = {}
|
|
18
|
+
self._plot_at = plot_at
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
@abc.abstractmethod
|
|
22
|
+
def name(self) -> str:
|
|
23
|
+
pass
|
|
24
|
+
|
|
25
|
+
@abc.abstractmethod
|
|
26
|
+
def _compute_indicator(self, incoming_bar: events.BarReceived) -> float:
|
|
27
|
+
pass
|
|
28
|
+
|
|
29
|
+
def update(self, incoming_bar: events.BarReceived) -> None:
|
|
30
|
+
symbol = incoming_bar.symbol
|
|
31
|
+
self._current_symbol = symbol
|
|
32
|
+
value = self._compute_indicator(incoming_bar)
|
|
33
|
+
with self._lock:
|
|
34
|
+
if symbol not in self._history_data:
|
|
35
|
+
self._history_data[symbol] = collections.deque(maxlen=self._max_history)
|
|
36
|
+
self._history_data[symbol].append(value)
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
def latest(self) -> float:
|
|
40
|
+
with self._lock:
|
|
41
|
+
h = self._history_data.get(self._current_symbol, collections.deque())
|
|
42
|
+
return h[-1] if h else np.nan
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def history(self) -> collections.deque[float]:
|
|
46
|
+
with self._lock:
|
|
47
|
+
h = self._history_data.get(self._current_symbol, collections.deque())
|
|
48
|
+
return collections.deque(h, maxlen=self._max_history)
|
|
49
|
+
|
|
50
|
+
def __getitem__(self, index: int) -> float:
|
|
51
|
+
# Returns np.nan on out-of-bounds access. Since np.nan comparisons always
|
|
52
|
+
# return False, strategies can skip explicit length checks.
|
|
53
|
+
try:
|
|
54
|
+
return self.history[index]
|
|
55
|
+
except IndexError:
|
|
56
|
+
return np.nan
|
|
57
|
+
|
|
58
|
+
@property
|
|
59
|
+
def plot_at(self) -> int:
|
|
60
|
+
return self._plot_at
|
|
@@ -39,7 +39,7 @@ class StrategyBase(messaging.Subscriber, abc.ABC):
|
|
|
39
39
|
self._submitted_modifications: dict[uuid.UUID, models.OrderRecord] = {}
|
|
40
40
|
self._submitted_cancellations: dict[uuid.UUID, models.OrderRecord] = {}
|
|
41
41
|
|
|
42
|
-
# OHLCV as indicators for history access: self.bar.close.history
|
|
42
|
+
# OHLCV as indicators for history access: self.bar.close.history
|
|
43
43
|
self.bar = SimpleNamespace(
|
|
44
44
|
open=self.add_indicator(indicators.Open()),
|
|
45
45
|
high=self.add_indicator(indicators.High()),
|
|
@@ -195,7 +195,7 @@ class StrategyBase(messaging.Subscriber, abc.ABC):
|
|
|
195
195
|
ohlcv_names = {"OPEN", "HIGH", "LOW", "CLOSE", "VOLUME"}
|
|
196
196
|
|
|
197
197
|
indicator_values = {
|
|
198
|
-
f"{ind.plot_at:02d}_{ind.name}": ind.latest
|
|
198
|
+
f"{ind.plot_at:02d}_{ind.name}": ind.latest
|
|
199
199
|
for ind in self._indicators
|
|
200
200
|
if ind.name not in ohlcv_names
|
|
201
201
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from onesecondtrader import events, indicators, models
|
|
2
|
+
from .base import StrategyBase
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class SMACrossover(StrategyBase):
|
|
6
|
+
fast_period: int = 20
|
|
7
|
+
slow_period: int = 100
|
|
8
|
+
quantity: float = 1.0
|
|
9
|
+
|
|
10
|
+
def setup(self) -> None:
|
|
11
|
+
self.fast_sma = self.add_indicator(
|
|
12
|
+
indicators.SimpleMovingAverage(period=self.fast_period)
|
|
13
|
+
)
|
|
14
|
+
self.slow_sma = self.add_indicator(
|
|
15
|
+
indicators.SimpleMovingAverage(period=self.slow_period)
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
def on_bar(self, event: events.BarReceived) -> None:
|
|
19
|
+
if (
|
|
20
|
+
self.fast_sma[-2] <= self.slow_sma[-2]
|
|
21
|
+
and self.fast_sma.latest > self.slow_sma.latest
|
|
22
|
+
and self.position <= 0
|
|
23
|
+
):
|
|
24
|
+
self.submit_order(
|
|
25
|
+
models.OrderType.MARKET, models.OrderSide.BUY, self.quantity
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
if (
|
|
29
|
+
self.fast_sma[-2] >= self.slow_sma[-2]
|
|
30
|
+
and self.fast_sma.latest < self.slow_sma.latest
|
|
31
|
+
and self.position >= 0
|
|
32
|
+
):
|
|
33
|
+
self.submit_order(
|
|
34
|
+
models.OrderType.MARKET, models.OrderSide.SELL, self.quantity
|
|
35
|
+
)
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import abc
|
|
4
|
-
import collections
|
|
5
|
-
import threading
|
|
6
|
-
|
|
7
|
-
import numpy as np
|
|
8
|
-
|
|
9
|
-
from onesecondtrader import events
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class Indicator(abc.ABC):
|
|
13
|
-
def __init__(self, max_history: int = 100, plot_at: int = 99) -> None:
|
|
14
|
-
self._lock = threading.Lock()
|
|
15
|
-
self._max_history = max(1, int(max_history))
|
|
16
|
-
# Keyed by symbol only - each strategy subscribes to one timeframe, so the indicator only sees bars from that timeframe.
|
|
17
|
-
self._history: dict[str, collections.deque[float]] = {}
|
|
18
|
-
# 0 = main price chart, 1-98 = subcharts, 99 = no plot
|
|
19
|
-
self._plot_at = plot_at
|
|
20
|
-
|
|
21
|
-
@property
|
|
22
|
-
@abc.abstractmethod
|
|
23
|
-
def name(self) -> str:
|
|
24
|
-
pass
|
|
25
|
-
|
|
26
|
-
@abc.abstractmethod
|
|
27
|
-
def _compute_indicator(self, incoming_bar: events.BarReceived) -> float:
|
|
28
|
-
pass
|
|
29
|
-
|
|
30
|
-
def update(self, incoming_bar: events.BarReceived) -> None:
|
|
31
|
-
symbol = incoming_bar.symbol
|
|
32
|
-
value = self._compute_indicator(incoming_bar)
|
|
33
|
-
with self._lock:
|
|
34
|
-
if symbol not in self._history:
|
|
35
|
-
self._history[symbol] = collections.deque(maxlen=self._max_history)
|
|
36
|
-
self._history[symbol].append(value)
|
|
37
|
-
|
|
38
|
-
def latest(self, symbol: str) -> float:
|
|
39
|
-
with self._lock:
|
|
40
|
-
history = self._history.get(symbol, collections.deque())
|
|
41
|
-
return history[-1] if history else np.nan
|
|
42
|
-
|
|
43
|
-
def history(self, symbol: str) -> collections.deque[float]:
|
|
44
|
-
with self._lock:
|
|
45
|
-
h = self._history.get(symbol, collections.deque())
|
|
46
|
-
return collections.deque(h, maxlen=self._max_history)
|
|
47
|
-
|
|
48
|
-
@property
|
|
49
|
-
def plot_at(self) -> int:
|
|
50
|
-
return self._plot_at
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/indicators/__init__.py
RENAMED
|
File without changes
|
{onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/indicators/averages.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{onesecondtrader-0.32.0 → onesecondtrader-0.34.0}/src/onesecondtrader/messaging/subscriber.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|