quantex.strategy
Strategy
Bases: ABC
Base class for all trading strategies.
This class owns a quantex.models.Portfolio
instance which keeps
track of cash and Position
objects. For convenience and backward
compatibility, the underlying positions
mapping is exposed directly so
that existing strategy implementations that reference
self.positions[<symbol>]
continue to work unchanged.
Source code in src/quantex/strategy.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
|
cash
property
Current available cash in the underlying Portfolio.
Example
if self.cash > 10_000:
self.buy("AAPL", 5)
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The cash balance that can be deployed for new positions. |
price_history
property
Price history up to and including the current bar.
Example
history = self.price_history latest_btc = history["BTC"].iloc[-1]
prices
property
Lazy dict of symbol→price for the current bar (lightweight).
__init__(data_sources=None, symbols=None, *, initial_cash=0.0)
Initializes a new strategy instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_sources
|
Mapping[str, DataSource] | None
|
Mapping from a source name to a concrete
|
None
|
symbols
|
list[str] | None
|
List of tradable symbols to initialize |
None
|
initial_cash
|
float
|
Starting cash for the internal |
0.0
|
Source code in src/quantex/strategy.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
buy(symbol, quantity, limit_price=None)
Creates and submits a buy order using the current strategy timestamp.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symbol
|
str
|
The symbol to buy. |
required |
quantity
|
float
|
The quantity to buy. Must be positive. |
required |
limit_price
|
float | None
|
If provided, a limit order is created. Otherwise, a market order is created. |
None
|
Source code in src/quantex/strategy.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
close_position(symbol)
Creates and submits an order to close the entire position for a symbol.
This is a helper method that checks if a position is open for the given symbol and, if so, creates a market order to close it at the current strategy timestamp.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symbol
|
str
|
The symbol of the position to close. |
required |
Source code in src/quantex/strategy.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
get_lookback_prices(lookback_period)
Returns an aligned lookback window for all symbols.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lookback_period
|
int
|
Number of bars (inclusive of the current bar) to return. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: DataFrame with the last lookback_period rows from |
DataFrame
|
pyattr: |
DataFrame
|
observations available (e.g. at the beginning of a backtest), the |
DataFrame
|
entire available history is returned instead. The returned frame |
DataFrame
|
is guaranteed to be free of missing timestamps across symbols due |
DataFrame
|
to the forward-filling performed by the engine. |
Source code in src/quantex/strategy.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
|
get_price(symbol)
Returns the latest price for symbol.
This accesses the numpy price row injected by the engine and is
therefore O(1) without any pandas overhead. Raises KeyError
if the
symbol is not part of the backtest universe.
Source code in src/quantex/strategy.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
run()
abstractmethod
Executes the strategy logic for the current bar.
Concrete strategies must override this method. It should inspect the available data sources and make trading decisions.
Source code in src/quantex/strategy.py
97 98 99 100 101 102 103 104 |
|
sell(symbol, quantity, limit_price=None)
Creates and submits a sell order using the current strategy timestamp.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
symbol
|
str
|
The symbol to sell. |
required |
quantity
|
float
|
The quantity to sell. Must be positive. |
required |
limit_price
|
float | None
|
If provided, a limit order is created. Otherwise, a market order is created. |
None
|
Source code in src/quantex/strategy.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
submit_order(order)
Queues an order to be executed by the engine.
Strategies should call this method to simulate realistic order routing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
order
|
Order
|
The |
required |
Source code in src/quantex/strategy.py
189 190 191 192 193 194 195 196 197 198 |
|