Quant API¶
Money flow¶
money_flow
¶
Smart-money and real/legal money-flow analytics.
_REQUIRED
module-attribute
¶
_REQUIRED = (
"final_price",
"close",
"volume",
"buy_I_Volume",
"sell_I_Volume",
"buy_I_Count",
"sell_I_Count",
)
restore
¶
to_pandas
¶
Source code in src/iranquant/quant/_utils.py
compute_money_flow
¶
Add smart-money, per-capita and real/legal flow columns.
Works with both Pandas and Polars inputs and returns a frame of the same
type. All monetary per-capita values are expressed in Rials
(*_m variants are in million Tomans).
The required input columns are final_price, close, volume,
buy_I_Volume, sell_I_Volume, buy_I_Count and sell_I_Count
(all present in :func:iranquant.full_history). Columns are computed as:
real_buy_per_capita = buy_I_Volume * final_price / max(buy_I_Count, 1)real_sell_per_capita = sell_I_Volume * final_price / max(sell_I_Count, 1)buyer_power_ratio = real_buy_per_capita / max(real_sell_per_capita, 1)net_real_value = (buy_I_Volume - sell_I_Volume) * final_priceownership_change_pct = ((buy_I_Volume - sell_I_Volume) / max(volume, 1)) * 100close_vs_final_pct = ((close - final_price) / max(final_price, 1)) * 100vol_ratio = volume / rolling_mean(volume, vol_window)
smart_money_entry is True when vol_ratio >= 1.8 and
buyer_power_ratio >= 1.5 and net_real_value > 0 and
close >= final_price. smart_money_exit mirrors it with
buyer_power_ratio <= 0.67, net_real_value < 0 and
close <= final_price.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
DataFrame
|
A history frame. Any extra columns (including |
required |
vol_window
|
int
|
Rolling window used for the volume-ratio baseline. Must be
at least |
20
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A frame of the same engine with all input columns plus: |
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
KeyError
|
If any required input column is missing. |
Examples:
>>> flow = compute_money_flow(iq.full_history("فولاد"))
>>> flow[["jdate", "buyer_power_ratio", "smart_money_entry"]].tail(3)
The Polars path preserves pl.DataFrame::
>>> import polars as pl
>>> flow = compute_money_flow(iq.full_history("فولاد", engine="polars"))
>>> flow.filter(pl.col("smart_money_entry")).height
Source code in src/iranquant/quant/money_flow.py
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 | |
Momentum and swing legs¶
momentum
¶
Swing-leg detection and multi-timeframe movement power.
LEG_COLUMNS
module-attribute
¶
LEG_COLUMNS = [
"leg_id",
"direction",
"start_date",
"end_date",
"start_price",
"end_price",
"leg_return_pct",
"leg_bars",
"leg_velocity",
"avg_volume",
"leg_volume_expansion",
]
__all__
module-attribute
¶
empty_like
¶
restore
¶
to_pandas
¶
Source code in src/iranquant/quant/_utils.py
_zigzag
¶
Return alternating swing pivots using a percentage-reversal ZigZag.
Source code in src/iranquant/quant/momentum.py
detect_swing_legs
¶
Detect ZigZag swing legs from the close column.
A percentage-reversal ZigZag is run over the closing prices. A leg is
confirmed whenever price reverses by at least reversal_pct from the
running extreme; the final, still-active leg is appended so the output
always ends at the last bar. Input and output engine types match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
DataFrame
|
A history frame containing |
required |
reversal_pct
|
float
|
Minimum reversal, in percent, required to confirm a new
pivot. Must be positive. Defaults to |
5.0
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per leg (completed legs plus the active leg) with columns: |
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
positional indexes when |
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
divided by the previous leg's; |
DataFrame
|
previous average is zero. |
DataFrame
|
An empty frame with the same columns is returned for empty input or |
DataFrame
|
when no pivots are found. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
KeyError
|
If the |
Examples:
>>> legs = detect_swing_legs(iq.history("فولاد"), reversal_pct=5.0)
>>> legs[["direction", "leg_return_pct", "leg_velocity"]].tail(5)
>>> legs[legs["leg_volume_expansion"] > 1.5]["direction"].tolist()
Source code in src/iranquant/quant/momentum.py
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 | |
_signed_efficiency_ratio
¶
Source code in src/iranquant/quant/momentum.py
compute_movement_power
¶
compute_movement_power(
frame: DataFrame,
windows: tuple[int, int, int] = (5, 20, 60),
) -> DataFrame
Compute a signed multi-timeframe Kaufman efficiency power score.
For each window w the signed Kaufman efficiency ratio is::
er_w = sign(close - close.shift(w))
* abs(close - close.shift(w))
/ rolling_sum(abs(close.diff()), w)
The score starts as a weighted sum of the signed ratios. With exactly
three windows the weights are (0.5, 0.3, 0.2); otherwise all windows
are equally weighted. The score is then multiplied by a volume factor
(volume / rolling_mean(volume, min(windows)) clipped to [0.5, 2.0])
and, when present, a buyer-power factor
(clip(buyer_power_ratio, 0.5, 3.0) / 1.5 clipped to [0.5, 1.5]),
scaled by 100 and clipped to [-100, +100].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
DataFrame
|
A history frame containing |
required |
windows
|
tuple[int, int, int]
|
One or more positive lookback windows. Defaults to
|
(5, 20, 60)
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A frame of the same engine with all input columns plus: |
DataFrame
|
|
DataFrame
|
(for example |
DataFrame
|
|
DataFrame
|
buyer-power-adjusted score in |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
KeyError
|
If the |
Examples:
>>> power = compute_movement_power(iq.full_history("فولاد"))
>>> power[["jdate", "er_5", "er_20", "movement_power_score"]].tail(3)
>>> power.sort_values("movement_power_score", ascending=False).head()
Source code in src/iranquant/quant/momentum.py
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 | |
Screener¶
screener
¶
Vectorized market-wide screening helpers.
MarketScreener
¶
Vectorized filters over a MarketWatch (or batch) DataFrame.
Every filter accepts an optional alternative frame; when omitted the frame passed to the constructor is used. Results preserve the input engine type (Pandas in, Pandas out; Polars in, Polars out) and are reset to a clean index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
DataFrame | None
|
An optional MarketWatch frame from
:func: |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If a filter runs without a bound or call-time frame. |
TypeError
|
If the selected frame is neither a Pandas nor a Polars DataFrame (raised lazily when a filter runs). |
Examples:
>>> screener = MarketScreener(iq.market_watch())
>>> screener.filter_buy_queue()[["symbol", "close", "tmax"]]
>>> MarketScreener().filter_buy_queue(iq.market_watch()).head()
Source code in src/iranquant/quant/screener.py
filter_buy_queue
¶
Symbols locked at tmax with a positive buy-queue volume.
The mask is (tmax > 0) & (close >= tmax) & (sum(zmd) > 0). The
buy-queue volume is the sum of zmd (bid volume) across all order-book
levels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
DataFrame | None
|
Optional alternative frame; defaults to the constructor frame. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The matching rows in the same engine as the input. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Examples:
Source code in src/iranquant/quant/screener.py
filter_sell_queue
¶
Symbols locked at tmin with a positive sell-queue volume.
The mask is (tmin > 0) & (close <= tmin) & (sum(zmo) > 0). The
sell-queue volume is the sum of zmo (ask volume) across all
order-book levels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
DataFrame | None
|
Optional alternative frame; defaults to the constructor frame. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The matching rows in the same engine as the input. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Examples:
Source code in src/iranquant/quant/screener.py
filter_positive_tick_surge
¶
filter_positive_tick_surge(
frame: DataFrame | None = None,
min_diff_pct: float = 1.5,
) -> DataFrame
Symbols whose last price exceeds the final price by min_diff_pct.
The mask is
((close - final_price) / max(final_price, 1)) * 100 >= min_diff_pct.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
DataFrame | None
|
Optional alternative frame; defaults to the constructor frame. |
None
|
min_diff_pct
|
float
|
Minimum percentage gap between the last traded price
and the closing price. Defaults to |
1.5
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The matching rows in the same engine as the input. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Examples:
>>> surges = screener.filter_positive_tick_surge(min_diff_pct=1.5)
>>> surges[["symbol", "close", "final_price"]].head()
Source code in src/iranquant/quant/screener.py
scan_market
¶
scan_market(
custom_predicate: Callable[
[DataFrame], DataFrame | Series
],
frame: DataFrame | None = None,
) -> DataFrame
Apply a custom Pandas predicate/mask and restore the engine type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
custom_predicate
|
Callable[[DataFrame], DataFrame | Series]
|
A callable that receives the frame as a
:class: |
required |
frame
|
DataFrame | None
|
Optional alternative frame; defaults to the constructor frame. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The filtered rows converted back to the input engine. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Examples:
>>> high_volume = screener.scan_market(
... lambda df: df["volume"] > df["volume"].median()
... )
>>> high_volume[["symbol", "volume"]].sort_values("volume").tail()