straufi 0.1.0a1__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.
- straufi-0.1.0a1/PKG-INFO +179 -0
- straufi-0.1.0a1/README.md +164 -0
- straufi-0.1.0a1/pyproject.toml +70 -0
- straufi-0.1.0a1/pyproject.toml.orig +61 -0
- straufi-0.1.0a1/src/straufi/__init__.py +32 -0
- straufi-0.1.0a1/src/straufi/cli/__init__.py +125 -0
- straufi-0.1.0a1/src/straufi/cli/api/__init__.py +9 -0
- straufi-0.1.0a1/src/straufi/cli/api/api.py +95 -0
- straufi-0.1.0a1/src/straufi/cli/api/browser_login.py +144 -0
- straufi-0.1.0a1/src/straufi/cli/api/client.py +71 -0
- straufi-0.1.0a1/src/straufi/cli/api/url.py +12 -0
- straufi-0.1.0a1/src/straufi/cli/auth/__init__.py +13 -0
- straufi-0.1.0a1/src/straufi/cli/auth/auth.py +64 -0
- straufi-0.1.0a1/src/straufi/cli/auth/credentials.py +160 -0
- straufi-0.1.0a1/src/straufi/cli/auth/errors.py +10 -0
- straufi-0.1.0a1/src/straufi/cli/auth/keyring.py +42 -0
- straufi-0.1.0a1/src/straufi/cli/commands/__init__.py +13 -0
- straufi-0.1.0a1/src/straufi/cli/commands/_common.py +22 -0
- straufi-0.1.0a1/src/straufi/cli/commands/backtest.py +8 -0
- straufi-0.1.0a1/src/straufi/cli/commands/build.py +97 -0
- straufi-0.1.0a1/src/straufi/cli/commands/deploy.py +8 -0
- straufi-0.1.0a1/src/straufi/cli/commands/init.py +76 -0
- straufi-0.1.0a1/src/straufi/cli/commands/login.py +25 -0
- straufi-0.1.0a1/src/straufi/cli/commands/logout.py +15 -0
- straufi-0.1.0a1/src/straufi/cli/commands/pause.py +11 -0
- straufi-0.1.0a1/src/straufi/cli/commands/resume.py +12 -0
- straufi-0.1.0a1/src/straufi/cli/commands/status.py +37 -0
- straufi-0.1.0a1/src/straufi/cli/local/__init__.py +0 -0
- straufi-0.1.0a1/src/straufi/cli/local/environment.py +104 -0
- straufi-0.1.0a1/src/straufi/cli/local/package.py +114 -0
- straufi-0.1.0a1/src/straufi/cli/local/project.py +179 -0
- straufi-0.1.0a1/src/straufi/cli/models/__init__.py +21 -0
- straufi-0.1.0a1/src/straufi/cli/models/models.py +63 -0
- straufi-0.1.0a1/src/straufi/cli/templates/README.md.tmpl +14 -0
- straufi-0.1.0a1/src/straufi/cli/templates/gitignore.tmpl +18 -0
- straufi-0.1.0a1/src/straufi/cli/templates/pyproject.toml.tmpl +20 -0
- straufi-0.1.0a1/src/straufi/cli/templates/strategy.py.tmpl +63 -0
- straufi-0.1.0a1/src/straufi/cli/ui.py +81 -0
- straufi-0.1.0a1/src/straufi/config.py +177 -0
- straufi-0.1.0a1/src/straufi/enums.py +74 -0
- straufi-0.1.0a1/src/straufi/frequency.py +66 -0
- straufi-0.1.0a1/src/straufi/helpers.py +39 -0
- straufi-0.1.0a1/src/straufi/indicators.py +133 -0
- straufi-0.1.0a1/src/straufi/instrument.py +67 -0
- straufi-0.1.0a1/src/straufi/lib/__init__.py +1 -0
- straufi-0.1.0a1/src/straufi/lib/checks.py +63 -0
- straufi-0.1.0a1/src/straufi/lib/durations.py +47 -0
- straufi-0.1.0a1/src/straufi/lib/errors.py +13 -0
- straufi-0.1.0a1/src/straufi/lib/runtime.py +77 -0
- straufi-0.1.0a1/src/straufi/order.py +255 -0
- straufi-0.1.0a1/src/straufi/strategy.py +180 -0
straufi-0.1.0a1/PKG-INFO
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: straufi
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: Write trading strategies for the Straufi platform, and deploy them with the straufi CLI.
|
|
5
|
+
Author: Straufi Team
|
|
6
|
+
Requires-Dist: httpx>=0.27
|
|
7
|
+
Requires-Dist: keyring>=25.7.0
|
|
8
|
+
Requires-Dist: pandas>=2.2
|
|
9
|
+
Requires-Dist: pathspec>=0.12
|
|
10
|
+
Requires-Dist: rich>=14
|
|
11
|
+
Requires-Dist: tomlkit>=0.13
|
|
12
|
+
Requires-Dist: typer>=0.15
|
|
13
|
+
Requires-Python: >=3.12
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# straufi
|
|
17
|
+
|
|
18
|
+
Write trading strategies in Python, and run them on [Straufi](https://straufi.com).
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
uv add straufi # or `pip install straufi` or `poetry add straufi`
|
|
22
|
+
straufi login # Logs in in your browser (`--no-browser` prints the URL instead)
|
|
23
|
+
straufi init my-strategy # Creates a strategy on Straufi, and its project and virtual environment
|
|
24
|
+
cd my-strategy
|
|
25
|
+
straufi build # Uploads the code and builds it on Straufi
|
|
26
|
+
straufi backtest # Opens the page to test a build on past data
|
|
27
|
+
straufi deploy # Opens the page to run a build
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Writing a strategy
|
|
31
|
+
|
|
32
|
+
A Straufi strategy is a [Python class](https://docs.python.org/3/tutorial/classes.html) that extends the `Strategy` base class.
|
|
33
|
+
It must define two methods:
|
|
34
|
+
- `config()` (`@staticmethod`) that returns a `Config` object that tells Straufi the instruments, indicators, and historical data you need.
|
|
35
|
+
- `run()` that is called when your strategy is triggered, with the data of all your instruments.
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from straufi import Config, Every, Instrument, Order, OrderSide, OrderType, Strategy
|
|
39
|
+
from straufi.indicators import RSI, BollingerBands
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class MeanReversion(Strategy):
|
|
43
|
+
@staticmethod
|
|
44
|
+
def config():
|
|
45
|
+
return Config(
|
|
46
|
+
instruments={
|
|
47
|
+
"btc": Instrument("BTC", "USDT", "binance"),
|
|
48
|
+
"eth": Instrument("ETH", "USDT", "binance"),
|
|
49
|
+
},
|
|
50
|
+
indicators={
|
|
51
|
+
"rsi14": RSI(period=14),
|
|
52
|
+
"bb": BollingerBands(period=20),
|
|
53
|
+
},
|
|
54
|
+
lookback=24,
|
|
55
|
+
interval="1h",
|
|
56
|
+
frequency=Every("1h"),
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
def run(self) -> None:
|
|
60
|
+
budgets = self.get_budget()
|
|
61
|
+
positions = self.get_positions()
|
|
62
|
+
|
|
63
|
+
for _, instrument in self.instruments():
|
|
64
|
+
df = self.get_df(instrument)
|
|
65
|
+
last = df.iloc[-1]
|
|
66
|
+
|
|
67
|
+
broker_budget = budgets.get(instrument.broker, {})
|
|
68
|
+
quote_available = broker_budget.get(instrument.quote, 0.0) # e.g. how much USDT we can spend
|
|
69
|
+
base_owned = broker_budget.get(instrument.base, 0.0) # e.g. how much BTC we can sell
|
|
70
|
+
|
|
71
|
+
print(f"{instrument}: close={last.close} rsi={last.rsi14:.1f} {instrument.quote}={quote_available}")
|
|
72
|
+
|
|
73
|
+
if any(order.instrument == instrument for order in positions):
|
|
74
|
+
# We already have an open order on this instrument
|
|
75
|
+
continue
|
|
76
|
+
|
|
77
|
+
if last.rsi14 < 30 and last.close < last.bb_lower and quote_available > 0:
|
|
78
|
+
self.place_order(
|
|
79
|
+
Order(
|
|
80
|
+
instrument=instrument,
|
|
81
|
+
type=OrderType.MARKET,
|
|
82
|
+
side=OrderSide.BUY,
|
|
83
|
+
quote_volume=quote_available * 0.1, # Spend 10% of what's available
|
|
84
|
+
)
|
|
85
|
+
)
|
|
86
|
+
elif last.rsi14 > 70 and last.close > last.bb_upper and base_owned > 0:
|
|
87
|
+
self.place_order(
|
|
88
|
+
Order(
|
|
89
|
+
instrument=instrument,
|
|
90
|
+
type=OrderType.MARKET,
|
|
91
|
+
side=OrderSide.SELL,
|
|
92
|
+
volume=base_owned, # Sell everything we own
|
|
93
|
+
)
|
|
94
|
+
)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Every time the strategy is triggered, `run` is called once, with the data of all your instruments. In `run`, you can use:
|
|
98
|
+
- `self.get_df()`: pandas DataFrame with the OHLCV and indicators of your instruments.
|
|
99
|
+
- `self.get_budget()`: how much of each asset you have, by broker then symbol: `{"binance": {"USDT": 100.0}}`.
|
|
100
|
+
- `self.get_positions()`: all your open orders, as `Order`s.
|
|
101
|
+
- `self.place_order()`: places orders on the market.
|
|
102
|
+
- `self.cancel_order()`: cancels open orders (those retrieved from `get_positions()`).
|
|
103
|
+
- `self.instruments()`: iterates over your instruments, as `(alias, Instrument)` pairs.
|
|
104
|
+
|
|
105
|
+
### Indicators
|
|
106
|
+
|
|
107
|
+
Indicators are computed by Straufi. Each one becomes a column named after its key or, for indicators that produce
|
|
108
|
+
several values, one column per value named `<key>_<value>`:
|
|
109
|
+
|
|
110
|
+
| Indicator | Columns for key `k` |
|
|
111
|
+
|---|---|
|
|
112
|
+
| `SMA(period)`, `EMA(period)`, `RSI(period=14)`, `ATR(period=14)` | `k` |
|
|
113
|
+
| `BollingerBands(period=20)` | `k_upper`, `k_middle`, `k_lower` |
|
|
114
|
+
| `MACD(short_period=12, long_period=26, signal_period=9)` | `k_macd`, `k_signal` |
|
|
115
|
+
|
|
116
|
+
`SMA`, `EMA`, `RSI`, `BollingerBands` and `MACD` also accept `price_type` (`"open"`, `"high"`, `"low"` or `"close"`, the default).
|
|
117
|
+
|
|
118
|
+
### Orders
|
|
119
|
+
|
|
120
|
+
| `type` | Requires |
|
|
121
|
+
|---|---|
|
|
122
|
+
| `MARKET` | `volume` (base asset) **or** `quote_volume` (quote asset) |
|
|
123
|
+
| `LIMIT`, `LIMIT_MAKER` | `volume`, `limit_price` |
|
|
124
|
+
| `ICEBERG` | `volume`, `limit_price`, `display_volume` |
|
|
125
|
+
| `STOP_LOSS`, `TAKE_PROFIT` | `volume`, `trigger_price` and/or `trailing_delta` |
|
|
126
|
+
| `STOP_LOSS_LIMIT`, `TAKE_PROFIT_LIMIT` | `volume`, `limit_price`, `trigger_price` and/or `trailing_delta` |
|
|
127
|
+
|
|
128
|
+
Orders that stay in the order book take a `time_in_force` (`GTC` by default, or `IOC`). Invalid orders are rejected
|
|
129
|
+
outright, raising a `straufi.ValidationError` explaining what's wrong.
|
|
130
|
+
|
|
131
|
+
Helpers: `crossed_above(series, other)` and `crossed_below(series, other)` tell whether a crossover happened on
|
|
132
|
+
the last candle.
|
|
133
|
+
|
|
134
|
+
## The project
|
|
135
|
+
|
|
136
|
+
`straufi init` creates the strategy on Straufi, and its project and virtual environment (`.venv`) with uv, Poetry or
|
|
137
|
+
pip, whichever is available first, with pandas, numpy and ruff installed. Strategies start in paper mode, with the name
|
|
138
|
+
of their directory: use `--mode live` and `--name` to change that, and `--no-install` to only create the files.
|
|
139
|
+
|
|
140
|
+
The `[tool.straufi]` section of `pyproject.toml` tells Straufi where your strategy is, and which one it is:
|
|
141
|
+
|
|
142
|
+
```toml
|
|
143
|
+
[tool.straufi]
|
|
144
|
+
entrypoint = "strategy:MeanReversion" # <file without .py>:<class>
|
|
145
|
+
id = "..." # The strategy on Straufi
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Commands
|
|
149
|
+
|
|
150
|
+
Run them from the project, or point to it with `--project`.
|
|
151
|
+
|
|
152
|
+
| Command | Does |
|
|
153
|
+
|---|---|
|
|
154
|
+
| `straufi build` | Packages the project and builds it on Straufi. With `--dry-run`, only runs the checks and shows the files that would be packaged, without sending anything |
|
|
155
|
+
| `straufi backtest` | Opens the page to backtest a build of the strategy on past data, in your browser |
|
|
156
|
+
| `straufi deploy` | Opens the page to deploy a build of the strategy, in your browser |
|
|
157
|
+
| `straufi status` | Shows what the strategy runs, when it runs next, and its last executions |
|
|
158
|
+
| `straufi pause` / `straufi resume` | Stops or restarts the strategy. Its open orders stay open |
|
|
159
|
+
|
|
160
|
+
`build` only builds the code again if it changed since the last build. Pressing Ctrl+C while it runs cancels it.
|
|
161
|
+
|
|
162
|
+
`backtest` and `deploy` only open the webapp: you choose the build, its budget and the period of a backtest there. Build
|
|
163
|
+
the code first for it to show up there.
|
|
164
|
+
|
|
165
|
+
## What gets packaged
|
|
166
|
+
|
|
167
|
+
Everything in the project except virtual environments, caches, `.env` files and what your `.gitignore` files ignore.
|
|
168
|
+
Commit a lockfile (`uv.lock`, `poetry.lock`, `pylock.toml` or `requirements.txt`): without one, each build may install
|
|
169
|
+
different versions of your dependencies. Symbolic links are refused. A project can have up to 5000 files and 200 MB, and
|
|
170
|
+
20 MB once compressed.
|
|
171
|
+
|
|
172
|
+
## Logging in
|
|
173
|
+
|
|
174
|
+
`straufi login` opens Straufi in your browser: log in, authorize the CLI, and come back to your terminal. Your login is
|
|
175
|
+
kept in your system's keyring (macOS Keychain, Windows Credential Manager, or Secret Service on Linux), or in
|
|
176
|
+
`~/.config/straufi/credentials.json`, readable by you only, when there is none. It lasts as long as you use it at least
|
|
177
|
+
once every 30 days. `straufi logout` revokes it.
|
|
178
|
+
|
|
179
|
+
In CI, set the `STRAUFI_TOKEN` environment variable instead: it is used as-is, and never saved.
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# straufi
|
|
2
|
+
|
|
3
|
+
Write trading strategies in Python, and run them on [Straufi](https://straufi.com).
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
uv add straufi # or `pip install straufi` or `poetry add straufi`
|
|
7
|
+
straufi login # Logs in in your browser (`--no-browser` prints the URL instead)
|
|
8
|
+
straufi init my-strategy # Creates a strategy on Straufi, and its project and virtual environment
|
|
9
|
+
cd my-strategy
|
|
10
|
+
straufi build # Uploads the code and builds it on Straufi
|
|
11
|
+
straufi backtest # Opens the page to test a build on past data
|
|
12
|
+
straufi deploy # Opens the page to run a build
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Writing a strategy
|
|
16
|
+
|
|
17
|
+
A Straufi strategy is a [Python class](https://docs.python.org/3/tutorial/classes.html) that extends the `Strategy` base class.
|
|
18
|
+
It must define two methods:
|
|
19
|
+
- `config()` (`@staticmethod`) that returns a `Config` object that tells Straufi the instruments, indicators, and historical data you need.
|
|
20
|
+
- `run()` that is called when your strategy is triggered, with the data of all your instruments.
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from straufi import Config, Every, Instrument, Order, OrderSide, OrderType, Strategy
|
|
24
|
+
from straufi.indicators import RSI, BollingerBands
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class MeanReversion(Strategy):
|
|
28
|
+
@staticmethod
|
|
29
|
+
def config():
|
|
30
|
+
return Config(
|
|
31
|
+
instruments={
|
|
32
|
+
"btc": Instrument("BTC", "USDT", "binance"),
|
|
33
|
+
"eth": Instrument("ETH", "USDT", "binance"),
|
|
34
|
+
},
|
|
35
|
+
indicators={
|
|
36
|
+
"rsi14": RSI(period=14),
|
|
37
|
+
"bb": BollingerBands(period=20),
|
|
38
|
+
},
|
|
39
|
+
lookback=24,
|
|
40
|
+
interval="1h",
|
|
41
|
+
frequency=Every("1h"),
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
def run(self) -> None:
|
|
45
|
+
budgets = self.get_budget()
|
|
46
|
+
positions = self.get_positions()
|
|
47
|
+
|
|
48
|
+
for _, instrument in self.instruments():
|
|
49
|
+
df = self.get_df(instrument)
|
|
50
|
+
last = df.iloc[-1]
|
|
51
|
+
|
|
52
|
+
broker_budget = budgets.get(instrument.broker, {})
|
|
53
|
+
quote_available = broker_budget.get(instrument.quote, 0.0) # e.g. how much USDT we can spend
|
|
54
|
+
base_owned = broker_budget.get(instrument.base, 0.0) # e.g. how much BTC we can sell
|
|
55
|
+
|
|
56
|
+
print(f"{instrument}: close={last.close} rsi={last.rsi14:.1f} {instrument.quote}={quote_available}")
|
|
57
|
+
|
|
58
|
+
if any(order.instrument == instrument for order in positions):
|
|
59
|
+
# We already have an open order on this instrument
|
|
60
|
+
continue
|
|
61
|
+
|
|
62
|
+
if last.rsi14 < 30 and last.close < last.bb_lower and quote_available > 0:
|
|
63
|
+
self.place_order(
|
|
64
|
+
Order(
|
|
65
|
+
instrument=instrument,
|
|
66
|
+
type=OrderType.MARKET,
|
|
67
|
+
side=OrderSide.BUY,
|
|
68
|
+
quote_volume=quote_available * 0.1, # Spend 10% of what's available
|
|
69
|
+
)
|
|
70
|
+
)
|
|
71
|
+
elif last.rsi14 > 70 and last.close > last.bb_upper and base_owned > 0:
|
|
72
|
+
self.place_order(
|
|
73
|
+
Order(
|
|
74
|
+
instrument=instrument,
|
|
75
|
+
type=OrderType.MARKET,
|
|
76
|
+
side=OrderSide.SELL,
|
|
77
|
+
volume=base_owned, # Sell everything we own
|
|
78
|
+
)
|
|
79
|
+
)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Every time the strategy is triggered, `run` is called once, with the data of all your instruments. In `run`, you can use:
|
|
83
|
+
- `self.get_df()`: pandas DataFrame with the OHLCV and indicators of your instruments.
|
|
84
|
+
- `self.get_budget()`: how much of each asset you have, by broker then symbol: `{"binance": {"USDT": 100.0}}`.
|
|
85
|
+
- `self.get_positions()`: all your open orders, as `Order`s.
|
|
86
|
+
- `self.place_order()`: places orders on the market.
|
|
87
|
+
- `self.cancel_order()`: cancels open orders (those retrieved from `get_positions()`).
|
|
88
|
+
- `self.instruments()`: iterates over your instruments, as `(alias, Instrument)` pairs.
|
|
89
|
+
|
|
90
|
+
### Indicators
|
|
91
|
+
|
|
92
|
+
Indicators are computed by Straufi. Each one becomes a column named after its key or, for indicators that produce
|
|
93
|
+
several values, one column per value named `<key>_<value>`:
|
|
94
|
+
|
|
95
|
+
| Indicator | Columns for key `k` |
|
|
96
|
+
|---|---|
|
|
97
|
+
| `SMA(period)`, `EMA(period)`, `RSI(period=14)`, `ATR(period=14)` | `k` |
|
|
98
|
+
| `BollingerBands(period=20)` | `k_upper`, `k_middle`, `k_lower` |
|
|
99
|
+
| `MACD(short_period=12, long_period=26, signal_period=9)` | `k_macd`, `k_signal` |
|
|
100
|
+
|
|
101
|
+
`SMA`, `EMA`, `RSI`, `BollingerBands` and `MACD` also accept `price_type` (`"open"`, `"high"`, `"low"` or `"close"`, the default).
|
|
102
|
+
|
|
103
|
+
### Orders
|
|
104
|
+
|
|
105
|
+
| `type` | Requires |
|
|
106
|
+
|---|---|
|
|
107
|
+
| `MARKET` | `volume` (base asset) **or** `quote_volume` (quote asset) |
|
|
108
|
+
| `LIMIT`, `LIMIT_MAKER` | `volume`, `limit_price` |
|
|
109
|
+
| `ICEBERG` | `volume`, `limit_price`, `display_volume` |
|
|
110
|
+
| `STOP_LOSS`, `TAKE_PROFIT` | `volume`, `trigger_price` and/or `trailing_delta` |
|
|
111
|
+
| `STOP_LOSS_LIMIT`, `TAKE_PROFIT_LIMIT` | `volume`, `limit_price`, `trigger_price` and/or `trailing_delta` |
|
|
112
|
+
|
|
113
|
+
Orders that stay in the order book take a `time_in_force` (`GTC` by default, or `IOC`). Invalid orders are rejected
|
|
114
|
+
outright, raising a `straufi.ValidationError` explaining what's wrong.
|
|
115
|
+
|
|
116
|
+
Helpers: `crossed_above(series, other)` and `crossed_below(series, other)` tell whether a crossover happened on
|
|
117
|
+
the last candle.
|
|
118
|
+
|
|
119
|
+
## The project
|
|
120
|
+
|
|
121
|
+
`straufi init` creates the strategy on Straufi, and its project and virtual environment (`.venv`) with uv, Poetry or
|
|
122
|
+
pip, whichever is available first, with pandas, numpy and ruff installed. Strategies start in paper mode, with the name
|
|
123
|
+
of their directory: use `--mode live` and `--name` to change that, and `--no-install` to only create the files.
|
|
124
|
+
|
|
125
|
+
The `[tool.straufi]` section of `pyproject.toml` tells Straufi where your strategy is, and which one it is:
|
|
126
|
+
|
|
127
|
+
```toml
|
|
128
|
+
[tool.straufi]
|
|
129
|
+
entrypoint = "strategy:MeanReversion" # <file without .py>:<class>
|
|
130
|
+
id = "..." # The strategy on Straufi
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Commands
|
|
134
|
+
|
|
135
|
+
Run them from the project, or point to it with `--project`.
|
|
136
|
+
|
|
137
|
+
| Command | Does |
|
|
138
|
+
|---|---|
|
|
139
|
+
| `straufi build` | Packages the project and builds it on Straufi. With `--dry-run`, only runs the checks and shows the files that would be packaged, without sending anything |
|
|
140
|
+
| `straufi backtest` | Opens the page to backtest a build of the strategy on past data, in your browser |
|
|
141
|
+
| `straufi deploy` | Opens the page to deploy a build of the strategy, in your browser |
|
|
142
|
+
| `straufi status` | Shows what the strategy runs, when it runs next, and its last executions |
|
|
143
|
+
| `straufi pause` / `straufi resume` | Stops or restarts the strategy. Its open orders stay open |
|
|
144
|
+
|
|
145
|
+
`build` only builds the code again if it changed since the last build. Pressing Ctrl+C while it runs cancels it.
|
|
146
|
+
|
|
147
|
+
`backtest` and `deploy` only open the webapp: you choose the build, its budget and the period of a backtest there. Build
|
|
148
|
+
the code first for it to show up there.
|
|
149
|
+
|
|
150
|
+
## What gets packaged
|
|
151
|
+
|
|
152
|
+
Everything in the project except virtual environments, caches, `.env` files and what your `.gitignore` files ignore.
|
|
153
|
+
Commit a lockfile (`uv.lock`, `poetry.lock`, `pylock.toml` or `requirements.txt`): without one, each build may install
|
|
154
|
+
different versions of your dependencies. Symbolic links are refused. A project can have up to 5000 files and 200 MB, and
|
|
155
|
+
20 MB once compressed.
|
|
156
|
+
|
|
157
|
+
## Logging in
|
|
158
|
+
|
|
159
|
+
`straufi login` opens Straufi in your browser: log in, authorize the CLI, and come back to your terminal. Your login is
|
|
160
|
+
kept in your system's keyring (macOS Keychain, Windows Credential Manager, or Secret Service on Linux), or in
|
|
161
|
+
`~/.config/straufi/credentials.json`, readable by you only, when there is none. It lasts as long as you use it at least
|
|
162
|
+
once every 30 days. `straufi logout` revokes it.
|
|
163
|
+
|
|
164
|
+
In CI, set the `STRAUFI_TOKEN` environment variable instead: it is used as-is, and never saved.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "straufi"
|
|
3
|
+
version = "0.1.0a1"
|
|
4
|
+
description = "Write trading strategies for the Straufi platform, and deploy them with the straufi CLI."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
dependencies = [
|
|
8
|
+
"httpx>=0.27",
|
|
9
|
+
"keyring>=25.7.0",
|
|
10
|
+
"pandas>=2.2",
|
|
11
|
+
"pathspec>=0.12",
|
|
12
|
+
"rich>=14",
|
|
13
|
+
"tomlkit>=0.13",
|
|
14
|
+
"typer>=0.15",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[[project.authors]]
|
|
18
|
+
name = "Straufi Team"
|
|
19
|
+
|
|
20
|
+
[project.scripts]
|
|
21
|
+
straufi = "straufi.cli:main"
|
|
22
|
+
|
|
23
|
+
[dependency-groups]
|
|
24
|
+
dev = [
|
|
25
|
+
"pyright[nodejs]>=1.1.414",
|
|
26
|
+
"pytest>=9.1",
|
|
27
|
+
"ruff>=0.16",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[tool.uv.workspace]
|
|
31
|
+
members = ["packages/*"]
|
|
32
|
+
|
|
33
|
+
[tool.pytest.ini_options]
|
|
34
|
+
testpaths = ["tests"]
|
|
35
|
+
addopts = "--import-mode=importlib"
|
|
36
|
+
|
|
37
|
+
[tool.ruff]
|
|
38
|
+
line-length = 120
|
|
39
|
+
target-version = "py312"
|
|
40
|
+
|
|
41
|
+
[tool.ruff.lint]
|
|
42
|
+
select = [
|
|
43
|
+
"E",
|
|
44
|
+
"W",
|
|
45
|
+
"F",
|
|
46
|
+
"I",
|
|
47
|
+
"UP",
|
|
48
|
+
"B",
|
|
49
|
+
"SIM",
|
|
50
|
+
"C4",
|
|
51
|
+
"PIE",
|
|
52
|
+
"RUF",
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
[tool.ruff.lint.isort]
|
|
56
|
+
known-first-party = ["straufi"]
|
|
57
|
+
|
|
58
|
+
[tool.pyright]
|
|
59
|
+
include = [
|
|
60
|
+
"src",
|
|
61
|
+
"tests",
|
|
62
|
+
]
|
|
63
|
+
pythonVersion = "3.12"
|
|
64
|
+
typeCheckingMode = "standard"
|
|
65
|
+
venvPath = "."
|
|
66
|
+
venv = ".venv"
|
|
67
|
+
|
|
68
|
+
[build-system]
|
|
69
|
+
requires = ["uv_build>=0.12.19,<0.13.0"]
|
|
70
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "straufi"
|
|
3
|
+
version = "0.1.0a1"
|
|
4
|
+
description = "Write trading strategies for the Straufi platform, and deploy them with the straufi CLI."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [{ name = "Straufi Team" }]
|
|
7
|
+
requires-python = ">=3.12"
|
|
8
|
+
dependencies = [
|
|
9
|
+
"httpx>=0.27",
|
|
10
|
+
"keyring>=25.7.0",
|
|
11
|
+
"pandas>=2.2",
|
|
12
|
+
"pathspec>=0.12",
|
|
13
|
+
"rich>=14",
|
|
14
|
+
"tomlkit>=0.13",
|
|
15
|
+
"typer>=0.15",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[project.scripts]
|
|
19
|
+
straufi = "straufi.cli:main"
|
|
20
|
+
|
|
21
|
+
[dependency-groups]
|
|
22
|
+
dev = ["pyright[nodejs]>=1.1.414", "pytest>=9.1", "ruff>=0.16"]
|
|
23
|
+
|
|
24
|
+
[tool.uv.workspace]
|
|
25
|
+
members = ["packages/*"]
|
|
26
|
+
|
|
27
|
+
[tool.pytest.ini_options]
|
|
28
|
+
testpaths = ["tests"]
|
|
29
|
+
addopts = "--import-mode=importlib"
|
|
30
|
+
|
|
31
|
+
[tool.ruff]
|
|
32
|
+
line-length = 120
|
|
33
|
+
target-version = "py312"
|
|
34
|
+
|
|
35
|
+
[tool.ruff.lint]
|
|
36
|
+
select = [
|
|
37
|
+
"E",
|
|
38
|
+
"W", # pycodestyle
|
|
39
|
+
"F", # pyflakes
|
|
40
|
+
"I", # isort
|
|
41
|
+
"UP", # pyupgrade
|
|
42
|
+
"B", # bugbear
|
|
43
|
+
"SIM", # simplify
|
|
44
|
+
"C4", # comprehensions
|
|
45
|
+
"PIE",
|
|
46
|
+
"RUF",
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[tool.ruff.lint.isort]
|
|
50
|
+
known-first-party = ["straufi"]
|
|
51
|
+
|
|
52
|
+
[tool.pyright]
|
|
53
|
+
include = ["src", "tests"]
|
|
54
|
+
pythonVersion = "3.12"
|
|
55
|
+
typeCheckingMode = "standard"
|
|
56
|
+
venvPath = "."
|
|
57
|
+
venv = ".venv"
|
|
58
|
+
|
|
59
|
+
[build-system]
|
|
60
|
+
requires = ["uv_build>=0.12.19,<0.13.0"]
|
|
61
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from importlib.metadata import version
|
|
2
|
+
|
|
3
|
+
from .config import Config
|
|
4
|
+
from .enums import OrderSide, OrderStatus, OrderTimeInForce, OrderType, PriceType
|
|
5
|
+
from .frequency import Every, Frequency
|
|
6
|
+
from .helpers import crossed_above, crossed_below
|
|
7
|
+
from .instrument import Budget, Instrument
|
|
8
|
+
from .lib.errors import NotRunningError, StraufiError, ValidationError
|
|
9
|
+
from .order import Order
|
|
10
|
+
from .strategy import Strategy
|
|
11
|
+
|
|
12
|
+
__version__ = version("straufi")
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"Budget",
|
|
16
|
+
"Config",
|
|
17
|
+
"Every",
|
|
18
|
+
"Frequency",
|
|
19
|
+
"Instrument",
|
|
20
|
+
"NotRunningError",
|
|
21
|
+
"Order",
|
|
22
|
+
"OrderSide",
|
|
23
|
+
"OrderStatus",
|
|
24
|
+
"OrderTimeInForce",
|
|
25
|
+
"OrderType",
|
|
26
|
+
"PriceType",
|
|
27
|
+
"Strategy",
|
|
28
|
+
"StraufiError",
|
|
29
|
+
"ValidationError",
|
|
30
|
+
"crossed_above",
|
|
31
|
+
"crossed_below",
|
|
32
|
+
]
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Annotated
|
|
3
|
+
|
|
4
|
+
import typer
|
|
5
|
+
from typer.core import TyperGroup
|
|
6
|
+
|
|
7
|
+
from ..lib.errors import StraufiError
|
|
8
|
+
from . import commands, ui
|
|
9
|
+
from .api.client import StrategyMode
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class RootGroup(TyperGroup):
|
|
13
|
+
def invoke(self, ctx):
|
|
14
|
+
"""Shows our errors as a clean message instead of a Python traceback."""
|
|
15
|
+
try:
|
|
16
|
+
return super().invoke(ctx)
|
|
17
|
+
except StraufiError as e:
|
|
18
|
+
ui.error(str(e))
|
|
19
|
+
raise typer.Exit(1) from None
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
app = typer.Typer(
|
|
23
|
+
cls=RootGroup,
|
|
24
|
+
help="Create, backtest and deploy Straufi trading strategies.",
|
|
25
|
+
context_settings={"help_option_names": ["-h", "--help"]},
|
|
26
|
+
no_args_is_help=True,
|
|
27
|
+
add_completion=False,
|
|
28
|
+
pretty_exceptions_enable=False,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
ProjectOption = Annotated[Path, typer.Option("--project", "-p", help="The strategy project directory.")]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def main() -> None:
|
|
35
|
+
app()
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _version_cb(value: bool) -> None:
|
|
39
|
+
if value:
|
|
40
|
+
from .. import __version__
|
|
41
|
+
|
|
42
|
+
ui.info(f"straufi {__version__}")
|
|
43
|
+
raise typer.Exit()
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@app.callback()
|
|
47
|
+
def _root(
|
|
48
|
+
version: Annotated[
|
|
49
|
+
bool, typer.Option("--version", "-v", callback=_version_cb, is_eager=True, help="Show the version and exit.")
|
|
50
|
+
] = False,
|
|
51
|
+
) -> None:
|
|
52
|
+
pass
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@app.command()
|
|
56
|
+
def login(
|
|
57
|
+
browser: Annotated[
|
|
58
|
+
bool, typer.Option(help="Open the login page in your browser. With --no-browser, only print its URL.")
|
|
59
|
+
] = True,
|
|
60
|
+
) -> None:
|
|
61
|
+
"""Log in to Straufi.
|
|
62
|
+
|
|
63
|
+
In CI, set the STRAUFI_TOKEN environment variable instead.
|
|
64
|
+
"""
|
|
65
|
+
commands.login.run(browser)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@app.command()
|
|
69
|
+
def logout() -> None:
|
|
70
|
+
"""Log out of Straufi."""
|
|
71
|
+
commands.logout.run()
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@app.command()
|
|
75
|
+
def init(
|
|
76
|
+
path: Annotated[Path, typer.Argument(help="Directory of the new project, e.g. my-strategy.")],
|
|
77
|
+
name: Annotated[str | None, typer.Option(help="Name of the strategy. Defaults to the directory's.")] = None,
|
|
78
|
+
mode: Annotated[StrategyMode, typer.Option(help="Trade with fake money (paper) or real money (live).")] = (
|
|
79
|
+
StrategyMode.PAPER
|
|
80
|
+
),
|
|
81
|
+
install: Annotated[
|
|
82
|
+
bool, typer.Option(help="Create a virtual environment and install the dependencies (uv, Poetry or pip).")
|
|
83
|
+
] = True,
|
|
84
|
+
) -> None:
|
|
85
|
+
"""Create a new strategy on Straufi."""
|
|
86
|
+
commands.init.run(path, name, mode, install)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@app.command()
|
|
90
|
+
def build(
|
|
91
|
+
path: ProjectOption = Path("."),
|
|
92
|
+
dry_run: Annotated[bool, typer.Option(help="Only perform the pre-checks without creating a build.")] = False,
|
|
93
|
+
) -> None:
|
|
94
|
+
"""Package and build your project to be used on Straufi by backtests or live run."""
|
|
95
|
+
commands.build.run(path, dry_run)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@app.command()
|
|
99
|
+
def backtest(path: ProjectOption = Path(".")) -> None:
|
|
100
|
+
"""Open the page to test a build of the strategy on past data, in your browser."""
|
|
101
|
+
commands.backtest.run(path)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@app.command()
|
|
105
|
+
def deploy(path: ProjectOption = Path(".")) -> None:
|
|
106
|
+
"""Open the page to deploy a build of the strategy, in your browser."""
|
|
107
|
+
commands.deploy.run(path)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@app.command()
|
|
111
|
+
def pause(path: ProjectOption = Path(".")) -> None:
|
|
112
|
+
"""Stop running the strategy. Its open orders stay open."""
|
|
113
|
+
commands.pause.run(path)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
@app.command()
|
|
117
|
+
def resume(path: ProjectOption = Path(".")) -> None:
|
|
118
|
+
"""Start running the strategy again."""
|
|
119
|
+
commands.resume.run(path)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@app.command()
|
|
123
|
+
def status(path: ProjectOption = Path(".")) -> None:
|
|
124
|
+
"""Show the current informations of the strategy."""
|
|
125
|
+
commands.status.run(path)
|