onesecondtrader 0.32.0__py3-none-any.whl → 0.33.0__py3-none-any.whl
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/__init__.py +49 -5
- onesecondtrader/indicators/base.py +21 -11
- onesecondtrader/strategies/__init__.py +2 -0
- onesecondtrader/strategies/base.py +2 -2
- onesecondtrader/strategies/sma_crossover.py +35 -0
- {onesecondtrader-0.32.0.dist-info → onesecondtrader-0.33.0.dist-info}/METADATA +1 -1
- {onesecondtrader-0.32.0.dist-info → onesecondtrader-0.33.0.dist-info}/RECORD +9 -8
- {onesecondtrader-0.32.0.dist-info → onesecondtrader-0.33.0.dist-info}/WHEEL +0 -0
- {onesecondtrader-0.32.0.dist-info → onesecondtrader-0.33.0.dist-info}/licenses/LICENSE +0 -0
onesecondtrader/__init__.py
CHANGED
|
@@ -1,5 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
__all__ = [
|
|
2
|
+
"BarPeriod",
|
|
3
|
+
"BarProcessed",
|
|
4
|
+
"BarReceived",
|
|
5
|
+
"BrokerBase",
|
|
6
|
+
"Close",
|
|
7
|
+
"FillRecord",
|
|
8
|
+
"High",
|
|
9
|
+
"Indicator",
|
|
10
|
+
"InputSource",
|
|
11
|
+
"Low",
|
|
12
|
+
"Open",
|
|
13
|
+
"OrderFilled",
|
|
14
|
+
"OrderRecord",
|
|
15
|
+
"OrderSide",
|
|
16
|
+
"OrderSubmission",
|
|
17
|
+
"OrderType",
|
|
18
|
+
"SimpleMovingAverage",
|
|
19
|
+
"SimulatedBroker",
|
|
20
|
+
"SMACrossover",
|
|
21
|
+
"StrategyBase",
|
|
22
|
+
"Volume",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
from onesecondtrader.brokers import BrokerBase, SimulatedBroker
|
|
26
|
+
from onesecondtrader.events import (
|
|
27
|
+
BarProcessed,
|
|
28
|
+
BarReceived,
|
|
29
|
+
OrderFilled,
|
|
30
|
+
OrderSubmission,
|
|
31
|
+
)
|
|
32
|
+
from onesecondtrader.indicators import (
|
|
33
|
+
Close,
|
|
34
|
+
High,
|
|
35
|
+
Indicator,
|
|
36
|
+
Low,
|
|
37
|
+
Open,
|
|
38
|
+
SimpleMovingAverage,
|
|
39
|
+
Volume,
|
|
40
|
+
)
|
|
41
|
+
from onesecondtrader.models import (
|
|
42
|
+
BarPeriod,
|
|
43
|
+
FillRecord,
|
|
44
|
+
InputSource,
|
|
45
|
+
OrderRecord,
|
|
46
|
+
OrderSide,
|
|
47
|
+
OrderType,
|
|
48
|
+
)
|
|
49
|
+
from onesecondtrader.strategies import SMACrossover, StrategyBase
|
|
@@ -13,9 +13,8 @@ class Indicator(abc.ABC):
|
|
|
13
13
|
def __init__(self, max_history: int = 100, plot_at: int = 99) -> None:
|
|
14
14
|
self._lock = threading.Lock()
|
|
15
15
|
self._max_history = max(1, int(max_history))
|
|
16
|
-
|
|
17
|
-
self.
|
|
18
|
-
# 0 = main price chart, 1-98 = subcharts, 99 = no plot
|
|
16
|
+
self._current_symbol: str = ""
|
|
17
|
+
self._history_data: dict[str, collections.deque[float]] = {}
|
|
19
18
|
self._plot_at = plot_at
|
|
20
19
|
|
|
21
20
|
@property
|
|
@@ -29,22 +28,33 @@ class Indicator(abc.ABC):
|
|
|
29
28
|
|
|
30
29
|
def update(self, incoming_bar: events.BarReceived) -> None:
|
|
31
30
|
symbol = incoming_bar.symbol
|
|
31
|
+
self._current_symbol = symbol
|
|
32
32
|
value = self._compute_indicator(incoming_bar)
|
|
33
33
|
with self._lock:
|
|
34
|
-
if symbol not in self.
|
|
35
|
-
self.
|
|
36
|
-
self.
|
|
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
37
|
|
|
38
|
-
|
|
38
|
+
@property
|
|
39
|
+
def latest(self) -> float:
|
|
39
40
|
with self._lock:
|
|
40
|
-
|
|
41
|
-
return
|
|
41
|
+
h = self._history_data.get(self._current_symbol, collections.deque())
|
|
42
|
+
return h[-1] if h else np.nan
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
@property
|
|
45
|
+
def history(self) -> collections.deque[float]:
|
|
44
46
|
with self._lock:
|
|
45
|
-
h = self.
|
|
47
|
+
h = self._history_data.get(self._current_symbol, collections.deque())
|
|
46
48
|
return collections.deque(h, maxlen=self._max_history)
|
|
47
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
|
+
|
|
48
58
|
@property
|
|
49
59
|
def plot_at(self) -> int:
|
|
50
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,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: onesecondtrader
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.33.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,4 +1,4 @@
|
|
|
1
|
-
onesecondtrader/__init__.py,sha256=
|
|
1
|
+
onesecondtrader/__init__.py,sha256=CLUi62mHp8tBzp-58mig3M4SEhPELPihHfQQzcD9Nz8,901
|
|
2
2
|
onesecondtrader/brokers/__init__.py,sha256=YofzD0qlrfo_BzL6gwiHb9by7Webp36TQ_1O5EV0uzY,124
|
|
3
3
|
onesecondtrader/brokers/base.py,sha256=b6Xq2gBUdy3RTpnUfpUiNSXwbuq_bRIjXJ-s89Xu7nE,1345
|
|
4
4
|
onesecondtrader/brokers/simulated.py,sha256=pJvZ7b76xAs-NBbOX_v78IJgVrdnuTLCadqj8kYQ5sg,12694
|
|
@@ -10,7 +10,7 @@ onesecondtrader/events/responses.py,sha256=w_BH1nkkPyxQjh30EXEVFcUGDoMprFc2PuAaq
|
|
|
10
10
|
onesecondtrader/indicators/__init__.py,sha256=hRg3FCP1FT7LYOLzztybWn48gTR5QvewzzdELPYbdoY,239
|
|
11
11
|
onesecondtrader/indicators/averages.py,sha256=DpRRdY5G5ze3jwNOV19PPjV6slA0IEeOOla67yChi2Y,1900
|
|
12
12
|
onesecondtrader/indicators/bar.py,sha256=0H07mKNiUx5cE1fQvx1oPVY0R_MXcmAAgsLek175vTk,1115
|
|
13
|
-
onesecondtrader/indicators/base.py,sha256=
|
|
13
|
+
onesecondtrader/indicators/base.py,sha256=64HJD8JWBnUdR7PLd02N3hbNrRfKMjWHEKGeSyMRms8,1889
|
|
14
14
|
onesecondtrader/messaging/__init__.py,sha256=vMRDabHBgse_vZRTRFtnU8M8v2sY_o4pHjGzgu3hp3E,115
|
|
15
15
|
onesecondtrader/messaging/eventbus.py,sha256=Y8VbDZlEz8Q6KcCkfXRKsVIixsctBMRW1a5ANw297Ls,1576
|
|
16
16
|
onesecondtrader/messaging/subscriber.py,sha256=ImpFmu5IstLXLoKVMaebmLp5MXN6225vHLdTL1ZOPvw,2106
|
|
@@ -18,9 +18,10 @@ onesecondtrader/models/__init__.py,sha256=7amHCQ6BAhHKps0ke63E-zh8IJNmkdDogZq-Pf
|
|
|
18
18
|
onesecondtrader/models/data.py,sha256=fBmddVl6EXYC5u2UnvQ59DXAXeZeIb48KP1ZdeTL52A,322
|
|
19
19
|
onesecondtrader/models/orders.py,sha256=y6Ar-6fMqaOd_hRnRGvfWUF0Z13H_2hfTOW3ROOk0A8,254
|
|
20
20
|
onesecondtrader/models/records.py,sha256=vdCWBtoDQs5R4iB_8_3fXkxWEvoCxOssk9XBnS4l7Vk,599
|
|
21
|
-
onesecondtrader/strategies/__init__.py,sha256=
|
|
22
|
-
onesecondtrader/strategies/base.py,sha256=
|
|
23
|
-
onesecondtrader
|
|
24
|
-
onesecondtrader-0.
|
|
25
|
-
onesecondtrader-0.
|
|
26
|
-
onesecondtrader-0.
|
|
21
|
+
onesecondtrader/strategies/__init__.py,sha256=5TlEckz3RnwZTs1Isj0wJ_9Og5R9MMXBL90Vu9b45io,126
|
|
22
|
+
onesecondtrader/strategies/base.py,sha256=kIi6by4Y8YuB9gPMPMr2Unm5_i9SGAANyiW2UaHiRO0,11206
|
|
23
|
+
onesecondtrader/strategies/sma_crossover.py,sha256=s2u_uL_D5CrZTACiAbojnrLrLf4jqIPdfOCiNDEIsDA,1119
|
|
24
|
+
onesecondtrader-0.33.0.dist-info/METADATA,sha256=U06iSqkTLBBhLzZCiIl5r-f4-FjpVPF6Qimcgq_kunk,9682
|
|
25
|
+
onesecondtrader-0.33.0.dist-info/WHEEL,sha256=3ny-bZhpXrU6vSQ1UPG34FoxZBp3lVcvK0LkgUz6VLk,88
|
|
26
|
+
onesecondtrader-0.33.0.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
27
|
+
onesecondtrader-0.33.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|