release 20 (e30ce773f3)
Browse files- .env.example +2 -0
- CHANGELOG.md +175 -1
- console/src/pages/Overview.tsx +73 -3
- console/src/pages/Trades.tsx +8 -6
- fly_trader/__main__.py +10 -0
- fly_trader/agent/crash_exit.py +130 -0
- fly_trader/agent/fly_live.py +19 -2
- fly_trader/agent/fly_session.py +19 -1
- fly_trader/agent/live_first.py +13 -0
- fly_trader/agent/minute_engine.py +57 -7
- fly_trader/agent/paper_trading.py +37 -16
- fly_trader/agent/regime.py +186 -0
- fly_trader/agent/rising_clean.py +160 -0
- fly_trader/agent/selector_session.py +14 -5
- fly_trader/agent/sizing.py +11 -8
- fly_trader/config.py +17 -5
- fly_trader/ingest/pumpstream.py +3 -0
- fly_trader/markets.py +5 -3
- fly_trader/ops/reset.py +3 -3
- fly_trader/ops/wallets.py +20 -0
- fly_trader/rh/curves.py +50 -0
- fly_trader/rh/index.py +17 -12
- fly_trader/rh/live.py +7 -1
- fly_trader/rh/meta.py +4 -3
- fly_trader/rh/schema.py +3 -0
- fly_trader/rh/stream.py +32 -8
- fly_trader/rh/templates.py +73 -0
- fly_trader/rh/token_check.py +29 -2
- fly_trader/rh/v4_facts.py +313 -0
- fly_trader/rh/v4_universe.py +59 -3
- fly_trader/signer/core.py +6 -4
- fly_trader/signer/policy.py +36 -2
- fly_trader/train/flow.py +40 -1
- fly_trader/ui/api/overview.py +76 -7
- fly_trader/ui/api/trades.py +6 -5
- fly_trader/ui/static/console/assets/index-BxxFsYRV.js +0 -0
.env.example
CHANGED
|
@@ -75,6 +75,8 @@ RH_TESTNET=0
|
|
| 75 |
# RH_RPC_URL=https://rpc.mainnet.chain.robinhood.com
|
| 76 |
# RH_RPC_URL_BLOCKS=https://robinhood-rpc.publicnode.com
|
| 77 |
# QUICKNODE_RH_API_URL=
|
|
|
|
|
|
|
| 78 |
# The address RH_BOT_PRIVATE_KEY must derive to (a guard, not a secret). Needed only for live.
|
| 79 |
RH_BOT_ADDRESS=
|
| 80 |
# RH_BOT_PRIVATE_KEY (hex) goes in this .env only, on its own line, when you enable live. It is read from here at run
|
|
|
|
| 75 |
# RH_RPC_URL=https://rpc.mainnet.chain.robinhood.com
|
| 76 |
# RH_RPC_URL_BLOCKS=https://robinhood-rpc.publicnode.com
|
| 77 |
# QUICKNODE_RH_API_URL=
|
| 78 |
+
# Alchemy, if you have a key, serves the transactions ahead of QuickNode (never logged).
|
| 79 |
+
# ALCHEMY_API_KEY=
|
| 80 |
# The address RH_BOT_PRIVATE_KEY must derive to (a guard, not a secret). Needed only for live.
|
| 81 |
RH_BOT_ADDRESS=
|
| 82 |
# RH_BOT_PRIVATE_KEY (hex) goes in this .env only, on its own line, when you enable live. It is read from here at run
|
CHANGELOG.md
CHANGED
|
@@ -3,7 +3,181 @@
|
|
| 3 |
Releases are identified by date. The Docker distribution (branch `distribution`, Hugging Face `bryceweiner/fly-trader`)
|
| 4 |
ships signed releases built from this development line.
|
| 5 |
|
| 6 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
### Upgrading
|
| 9 |
|
|
|
|
| 3 |
Releases are identified by date. The Docker distribution (branch `distribution`, Hugging Face `bryceweiner/fly-trader`)
|
| 4 |
ships signed releases built from this development line.
|
| 5 |
|
| 6 |
+
## 2026-11-07, release 20: A Solana coin that falls 25 % within 5 minutes is sold at once
|
| 7 |
+
|
| 8 |
+
### Upgrading
|
| 9 |
+
|
| 10 |
+
- **Nothing to do on a Docker install:** its updater installs this release within the hour, as usual. The models are
|
| 11 |
+
release 19's (selector and fly of 2026-10-26).
|
| 12 |
+
|
| 13 |
+
### Robinhood Chain
|
| 14 |
+
|
| 15 |
+
- **A plain v4 coin is bought only when its contract matches a known-good template.** Both coins that took the wallet's
|
| 16 |
+
tokens passed every check we had, each with a backdoor of its own. Each coin's code is now reduced to a template: the
|
| 17 |
+
compiler's metadata and every constant or address masked, so coins built from one source share it. A template is known
|
| 18 |
+
good when at least 5 coins from 5 different creators built from it did not rug, none of its coins is a scam or was
|
| 19 |
+
refused for its code, and it is not a proxy. Did not rug means 24 or more 15-minute stretches clearing the live floors
|
| 20 |
+
at 6 hours old or more. Built from 13,753 plain v4 coins to 2026-11-05.
|
| 21 |
+
- **Nine templates pass,** covering 63.5 % of the coins. Every plain v4 trade the books ever made (both scams and one
|
| 22 |
+
other coin, 23 live and 6 paper trades) was outside them.
|
| 23 |
+
- **A coin off every template is refused** at registration, before the creator probe. Coins accepted before the check
|
| 24 |
+
are read again, 25 a minute, the ones trading now first; one that deviates is never bought again (its minutes still
|
| 25 |
+
price it). A template that reaches the rule later needs a rebuild to be added.
|
| 26 |
+
- **A coin that took the wallet's tokens no longer pauses RH as an execution gap.** The gap check pauses RH entries when
|
| 27 |
+
live trails its paper twin by 2 points a trade over 50 trades. It counted the two coins whose contracts took or wiped
|
| 28 |
+
the wallet's tokens (live −100 %, paper about −8 %), which alone read as a 3.2-point gap and paused RH from 2026-11-05
|
| 29 |
+
20:20. It now leaves out a position the wallet check found gone from the wallet while it was held, as it leaves out an
|
| 30 |
+
operator's sale. Without those two, live ran 0.6 points ahead of paper. A loss the wallet's own orders made still counts.
|
| 31 |
+
- **A graduating coin's curve history no longer stops RH's minutes.** When a Pons coin graduates, its curve trades give
|
| 32 |
+
its launch facts. The live stream read them inside its index step, and a coin that sat weeks on its curve took hundreds
|
| 33 |
+
of queries. On 2026-11-04 and 11-05 two such graduations held every RH minute, and the fly, for 40+ minutes each. A
|
| 34 |
+
thread of the RH stream now back-fills them one chunk at a time, saving its progress, so a restart resumes. The live
|
| 35 |
+
step only notes where the curve ends.
|
| 36 |
+
- **A graduate's facts wait for its back-fill:** its rh_meta row is built once the curve is in, as before. A new
|
| 37 |
+
graduate cannot trade for its first 6 hours anyway.
|
| 38 |
+
- **A plain v4 coin whose creator can move holders' tokens is never bought.** On 2026-11-03 the wallet bought ANT
|
| 39 |
+
(0x36c2861a) at 11:05. 22 seconds later its creator called the token's `transferFrom` with no allowance and burned the
|
| 40 |
+
wallet's 3,714.6 tokens. The wallet check then paused RH entries, the timed sale reverted with nothing to sell, and the
|
| 41 |
+
position was written off for 0.00226 ETH. The contract check had passed it ("no owner or admin powers"): it reads
|
| 42 |
+
function names, and this power was hidden inside the standard `transferFrom`.
|
| 43 |
+
- **The check now simulates it.** At registration it sends `transferFrom(PoolManager, dead, 1)` from the creator, who
|
| 44 |
+
holds no allowance. A standard token reverts; a coin where it succeeds is refused.
|
| 45 |
+
- **Coins already live** were probed the same way. Of the 92 with the launch facts to trade, ANT alone failed. A coin
|
| 46 |
+
refused after it went live is never bought again; its minutes still price it.
|
| 47 |
+
- **Each new buy takes the take-profit its recent trades favour.** Every 10 minutes the engine replays the selector book's
|
| 48 |
+
trades closed in the last 14 days on their own minute closes. It compares the base rule (sell at x2 on a coin bought far
|
| 49 |
+
below its high, else hold) with a sale at +20, +30 or +50 % on any coin. A line replaces the base rule only when its
|
| 50 |
+
mean beat the base rule's over at least 8 trades. A tie goes to the later sale.
|
| 51 |
+
- **Why:** when Robinhood coins pop and fade, a lower line keeps the pop. On the out-of-sample RH book (08-09..11-02) the
|
| 52 |
+
choice added +0.136 of the bankroll against the base rule's +1.598, ahead in each period and in every half-month it
|
| 53 |
+
changed anything. Over 21 and 28 days it was ahead too; over 7 days it lost. On Solana every window lost, so Solana
|
| 54 |
+
keeps the base rule.
|
| 55 |
+
- **The line in use never decides its own comparison.** Each buy's decision stores the base rule's line
|
| 56 |
+
(`tp_base_x`) beside the one it took. Every line is replayed on the path, with the real exit's costs kept as a share.
|
| 57 |
+
- **Live orders stay first.** The choice is made in the paper phase, after the wallet's orders. A live position takes
|
| 58 |
+
its paper entry's line, as before. Each change rings the console's bell.
|
| 59 |
+
- **Plain Uniswap v4 coins can trade.** Selector #791 was refit with their history, and now each such coin gets the
|
| 60 |
+
launch facts that model trained on, so the creator rules can read it. A coin without those facts stays out, as before.
|
| 61 |
+
The wallet buys these coins only with `RH_V4_WALLET=1`; at the default `0` the paper books trade them alone.
|
| 62 |
+
- **The facts:** `fly-trader rh-v4-facts` finds a coin's launch block, its creator (the mint's sender), who held what at
|
| 63 |
+
that block, and its opening reserve. The stream starts this as a child process at the lowest CPU priority, mid-minute
|
| 64 |
+
with no live order waiting, at most every 10 minutes. It takes the busiest coins within an hour of the 6-hour age
|
| 65 |
+
floor that clear the floors now.
|
| 66 |
+
- **The job runs inside HyperSync's 10 calls a minute.** Since 2026-11-02 18:01 HyperSync answers 10 a minute and the
|
| 67 |
+
live feed uses them all, so the job, which stood down at every refusal, found no new coin's facts. Its calls now wait
|
| 68 |
+
for a slot: 8-20 s into a minute, once the stream has written the minute that ended, two a minute at most. It stands
|
| 69 |
+
down only when HyperSync fails some other way, or the live index is silent or more than 120 blocks behind. Its first
|
| 70 |
+
runs, on 2026-11-03, found three coins' facts in 29-87 s each.
|
| 71 |
+
- **The 2,469 coins #791 trained on** took their facts from that research, so a live row is the row the model saw.
|
| 72 |
+
- **Measured:** read-only on 30 live coins, 28 found a launch and 27 of those matched the research on creator, launch
|
| 73 |
+
time and holdings; the other two stay out, as a coin with no launch does. On a local fork, 28 of 30 coins completed a
|
| 74 |
+
buy and a sell through the real order code, at a median cost of 1.26 % before gas.
|
| 75 |
+
|
| 76 |
+
- **Robinhood Chain transactions go through Alchemy when `ALCHEMY_API_KEY` is set.** QuickNode's free trial ended on
|
| 77 |
+
2026-10-30, and from then every send, receipt and balance read failed. The wallet's balance could not be read, so no
|
| 78 |
+
Robinhood Chain buy could start. No live position was open.
|
| 79 |
+
- **Now:** Alchemy comes first, then QuickNode, for every Robinhood Chain call except log reads. `RH_RPC_URL_TX` still
|
| 80 |
+
wins when set. Like QuickNode, Alchemy serves mainnet only while `RH_RPC_URL` is the default, so a run pointed at
|
| 81 |
+
anvil or the testnet rehearsal never reaches it.
|
| 82 |
+
- **Measured on 2026-10-31:** every trading call, past-block reads to 20,000 blocks, batches and sends worked; 69 steady
|
| 83 |
+
reads had a p90 of 307 ms and no errors. The console has used it since that day.
|
| 84 |
+
- **Cost:** a new connection takes about 1.1 s and a reused one 330-520 ms, against QuickNode's 438 and 138 ms, so an
|
| 85 |
+
order is an estimated 2 s slower.
|
| 86 |
+
- Tests blank `ALCHEMY_API_KEY`, and logged errors scrub it.
|
| 87 |
+
|
| 88 |
+
### Trading
|
| 89 |
+
|
| 90 |
+
- **A Solana coin that falls 25 % within 5 minutes is sold at once.** On 2026-11-07 the wallet bought 519AMD at 04:08.
|
| 91 |
+
From 04:31 to 04:38 its holders sold about 75 SOL and the price fell 90 %. The position was held its 240 minutes and
|
| 92 |
+
sold at −82 % (−0.195 SOL). Every price the Solana stream keeps for a held coin is now checked as it arrives. A price
|
| 93 |
+
25 % or more under the coin's high of the last 5 minutes (since the entry, the entry price included) sells the
|
| 94 |
+
wallet's position then, at the forced slippage, as a 'crash' exit. The sale is sent again every 15 s while it stays
|
| 95 |
+
open.
|
| 96 |
+
- **Paper books sell the same coin** in that minute's bookkeeping, at the first price 5 s or more after the crash,
|
| 97 |
+
about when the wallet's sale lands. A coin that passes the bar again may be bought again at once.
|
| 98 |
+
- **Replayed** on the Solana trades since 10-17, selling at the crash's price made live +0.30 SOL since 10-28 instead
|
| 99 |
+
of −0.24, and 519AMD −25 % instead of −82 %. Checked only on minute closes it made no difference, so it watches every
|
| 100 |
+
swap.
|
| 101 |
+
- **Not on Robinhood Chain:** the same replay lost there in every variant.
|
| 102 |
+
- **A Solana buy whose pool opens a fee vault at the buyer's expense is signed.** On 2026-11-06 the signer refused the
|
| 103 |
+
buy of 34Ct2K twice as spending more than ordered. Pump.fun's AMM had opened an empty wrapped-SOL account for a fee
|
| 104 |
+
vault inside the route, and the wallet paid its rent (1,488,440 lamports). The paper books bought it and sold it at
|
| 105 |
+
+18 %. The signer now looks at every account the transaction writes. It allows the rent of up to 2 empty token
|
| 106 |
+
accounts the route newly opens for a program's vault, each holding no more than the rent of its size. Rent into a
|
| 107 |
+
wallet's account, an account that receives tokens, or one that already existed is still refused.
|
| 108 |
+
- **While few young coins pass the bar, a Solana coin 20 hours or more past graduation is not bought.** The rule switches
|
| 109 |
+
itself on and off with the selector's own pass rate. When fewer coins pass than usual, the older ones that still pass
|
| 110 |
+
have been losers.
|
| 111 |
+
- **The trigger:** the buys the selector allowed of coins younger than 20.4 hours, one per coin per 240 minutes, a day
|
| 112 |
+
over the last 3 days against the last 30. Quiet when the log ratio is -0.23 or lower, the lowest fifth of the history
|
| 113 |
+
before 2026-10-02. Only young coins count, so the rule never feeds its own trigger. With under 14 days of history, or
|
| 114 |
+
a failed read, the rule stands aside.
|
| 115 |
+
- **Why:** on the out-of-sample book under today's rules, the old coins bought in quiet spells averaged -3.5 % a trade,
|
| 116 |
+
against +10.5 % for the rest. The rule helped in 8 of the 9 half-months it acted in. Skipping old coins whatever the
|
| 117 |
+
pass rate loses money, so the rule acts only in quiet spells.
|
| 118 |
+
- **On the live wallet since 2026-10-17** it would have skipped 7 buys that lost 0.304 SOL, both 7rTNgVdQ buys among
|
| 119 |
+
them.
|
| 120 |
+
- **Solana only:** on Robinhood Chain it does not help.
|
| 121 |
+
- **Live orders stay first.** The trigger is read in the paper phase, at most every 10 minutes (one indexed query, about
|
| 122 |
+
0.35 s). The buy rows read the stored flag. Training and every model stay as they were. Each switch rings the
|
| 123 |
+
console's bell.
|
| 124 |
+
- **Strategy 1 runs as a two-week paper test, and its go-live moves from the console.** A selector pick takes twice its
|
| 125 |
+
stake when the market is recovering and the coin's launch was clean. A new Solana paper book, `paper_rising`, trades the
|
| 126 |
+
selector's picks that way beside `paper_selector`. The wallet changes only after a go-live passes. Nothing runs until
|
| 127 |
+
an operator starts a test on the Overview page.
|
| 128 |
+
- **Rising:** the share of coins with a pool of at least 10 SOL that are up on the hour gained at least 2.76 points over
|
| 129 |
+
6 hours, read from closed minutes. **Clean:** holder concentration at graduation (`grad_hhi`) is at most 0.0197, and
|
| 130 |
+
unknown is not clean. Both cuts come from trades before 2026-09-12 and stay frozen.
|
| 131 |
+
- **The go-live is a stored time, not an `.env` setting.** The Overview's new card sets, moves or clears it, with no
|
| 132 |
+
restart. A new test sets it 14 days out.
|
| 133 |
+
- **At that time the test is judged once.** If its closed doubled trades made money, the wallet doubles these stakes.
|
| 134 |
+
If not, the wallet stays as it is, the date clears and the console's bell says why. Moving or clearing the date takes
|
| 135 |
+
the wallet back to plain stakes from its next buy.
|
| 136 |
+
- **Live orders stay first.** Before a go-live, a live minute only counts its breadth, which takes under 0.1 ms. After
|
| 137 |
+
one, a minute with a buy reads the setting once more, so a cleared date binds that buy. If doubling fails, that
|
| 138 |
+
minute's orders still go out at plain stakes. The doubled size keeps every cap, and a failed buy is tried again at its
|
| 139 |
+
plain stake.
|
| 140 |
+
- **Measured:** 7,466 real minutes (10-26 to 10-31) through the real engine and selector #640 on a copy of the data.
|
| 141 |
+
The book made the selector's 30 buys, in the same minutes, with the same 30 exits, and the 12 buys since the current
|
| 142 |
+
rules are exactly what the live console bought. The live breadth matches the study's (correlation 0.993; rising or
|
| 143 |
+
not agrees 97.9 % of the time).
|
| 144 |
+
- **Tests** no longer take `KELLY_FRACTION` from the operator's `.env`.
|
| 145 |
+
- **A Solana coin whose creator held 10 % or more at graduation is never bought.** On 2026-11-01 one such coin lost 89 %,
|
| 146 |
+
nearly all of that day's loss. The filter applies to live rows only, like the DLMM filter, so training and every model
|
| 147 |
+
stay as they were.
|
| 148 |
+
- **Why this cut:** on the out-of-sample book these coins made -9.0 and -5.0 % a trade in the two later periods, against
|
| 149 |
+
+1.3 and +6.6 % for the rest. On real paper trades they made -5.8 % with 24 % rugs, against +8.8 % with 9 %.
|
| 150 |
+
- **Solana only:** on Robinhood Chain these coins made money with no more rugs than the rest, so that chain is unchanged.
|
| 151 |
+
|
| 152 |
+
### Console
|
| 153 |
+
|
| 154 |
+
- **Realized P&L on both chains now comes from the wallet.** The console added up closed trades, so it missed money that
|
| 155 |
+
moved the wallet without a trade. It now shows what the wallet holds, with open positions at cost, minus the starting
|
| 156 |
+
amount, on the Overview and the Trades page. The Trades page's line ends at that figure. Starting amounts are unchanged:
|
| 157 |
+
5.000011 SOL and 0.225 ETH.
|
| 158 |
+
- **Solana:** +0.0320 SOL, not +0.0008. All 565 wallet transactions add up to the lamport. The wallet received +0.0312
|
| 159 |
+
SOL from pump.fun's `DistributeFeeToHolders` as a holder of the coins it bought.
|
| 160 |
+
- A withdrawal is not a loss, and an open position counts at cost until it is sold.
|
| 161 |
+
- **Robinhood Chain sizing reasons say ETH.** They said SOL.
|
| 162 |
+
|
| 163 |
+
### Platform
|
| 164 |
+
|
| 165 |
+
- **A full stream relay buffer takes about 350 MB, not 2.4 GB.** Each buffered event stayed in the 8 KB block orjson had
|
| 166 |
+
built it in, for an event under 1 KB. The relay now keeps an exact-size copy. `SOL_FEED` still defaults to `pumpapi`.
|
| 167 |
+
- **Measured:** one archived hour (2.9 M events) through the relay on macOS: 2,427 MB before, 361 MB after. With the
|
| 168 |
+
buffer full on the 1 GB Linux server: 345 MB, and 402 MB a day later.
|
| 169 |
+
- **The steady memory climb with the buffer full is gone.** glibc's thread cache kept freed events from merging, so the
|
| 170 |
+
heap grew ~24 MB/h and a 1 GB server would have run out in under a day. The service and the Docker image now turn
|
| 171 |
+
that cache off (`GLIBC_TUNABLES`). In a loopback test, memory stayed flat through 22 buffer turnovers; the test
|
| 172 |
+
without it grew in a straight line. The live server grew ~3 MB/h over its first day: ~0.2 MB/h between client
|
| 173 |
+
reconnects when measured, with rises across them.
|
| 174 |
+
- **The setup guide's numbers were out of date:** the relay forwards 23 % of the events, not 10 %, at ~52 KB/s
|
| 175 |
+
(75 KB/s in the busiest minute), and the default buffer covers 17-27 minutes in a busy hour (65 on 10-31), not 30+. A
|
| 176 |
+
512 MB server is too small.
|
| 177 |
+
- **The setup guide's order:** the certificate step now comes after the install, so its hook can start the relay, and
|
| 178 |
+
BBR is a step.
|
| 179 |
+
|
| 180 |
+
## 2026-11-04, release 19: Code no install ever ran is gone
|
| 181 |
|
| 182 |
### Upgrading
|
| 183 |
|
console/src/pages/Overview.tsx
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
// Overview, laid out like Dashtreme's index: the selector book's KPI widgets, what the system is doing in plain words
|
| 2 |
// beside the win rate, the money's book (its wealth and open positions), the fly's brain, this minute's scoring with the top
|
| 3 |
-
// scores, the race between the selector and the plastic fly, the
|
| 4 |
-
// (ui/api/overview.py; the brain ui/api/brain.py).
|
|
|
|
| 5 |
import { BrainPanel } from '../brain'
|
| 6 |
import { PageHead } from '../components/ChainPicker'
|
| 7 |
import { BarChart, LineChart, RadialGauge, type Series } from '../components/Charts'
|
|
@@ -55,6 +56,10 @@ type Minute = {
|
|
| 55 |
}
|
| 56 |
type RaceBook = { book: string; title: string; pnl: number | null; n: number; daily: number[] }
|
| 57 |
type Race = { hidden: boolean; unit: string; handover?: boolean; success?: string | null; caption?: string | null; metrics?: RaceBook[]; series?: Series[]; days?: string[] }
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
type Position = { id: number; mint: string; token: string; strategy: string; held_min: number; exits_in_min: number; cost: number; ret: number | null
|
| 59 |
correctness?: number | null }
|
| 60 |
type Book = { unit: string; history: [string, number][]; positions: Position[]; live?: boolean; book?: string; title?: string; sub?: string; observer?: boolean
|
|
@@ -91,7 +96,7 @@ function SummaryPanel() {
|
|
| 91 |
<WidgetGrid>
|
| 92 |
<Widget label={d.label} icon="wealth" value={amount(d.wealth, u)} color={d.live ? 'magenta' : null} help={d.wealth_help} spark={d.spark.length > 1 ? d.spark : null}
|
| 93 |
delta={d.wealth !== null ? `${signed(d.wealth - d.capital, 4)} ${u} since start` : null} />
|
| 94 |
-
<Widget label="Realized P&L" icon="pnl" value={amount(d.pnl, u, 4, true)} help=
|
| 95 |
spark={d.pnl_spark.length > 1 ? d.pnl_spark : null} />
|
| 96 |
<Widget label="Closed trades" icon="count" value={int(d.closed)} delta={winRate !== null ? `${Math.round(winRate * 100)}% winners` : null} tone="off"
|
| 97 |
progress={winRate} />
|
|
@@ -190,6 +195,70 @@ function RacePanel() {
|
|
| 190 |
)
|
| 191 |
}
|
| 192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
/** The money's book: its whole wealth history, and the open positions (a live one can be sold now, after a confirmation). */
|
| 194 |
function BookPanel() {
|
| 195 |
const { chain, market } = useApp()
|
|
@@ -355,6 +424,7 @@ export default function Overview() {
|
|
| 355 |
<BrainPanel variant="memecoin" />
|
| 356 |
<ThisMinute />
|
| 357 |
<RacePanel />
|
|
|
|
| 358 |
<SetupsPanel />
|
| 359 |
<HoursPanel />
|
| 360 |
<TickerCard />
|
|
|
|
| 1 |
// Overview, laid out like Dashtreme's index: the selector book's KPI widgets, what the system is doing in plain words
|
| 2 |
// beside the win rate, the money's book (its wealth and open positions), the fly's brain, this minute's scoring with the top
|
| 3 |
+
// scores, the race between the selector and the plastic fly, the rising + clean test and its go-live, the selector's setups
|
| 4 |
+
// per day, and the live ticker (ui/api/overview.py; the brain ui/api/brain.py).
|
| 5 |
+
import { useState } from 'react'
|
| 6 |
import { BrainPanel } from '../brain'
|
| 7 |
import { PageHead } from '../components/ChainPicker'
|
| 8 |
import { BarChart, LineChart, RadialGauge, type Series } from '../components/Charts'
|
|
|
|
| 56 |
}
|
| 57 |
type RaceBook = { book: string; title: string; pnl: number | null; n: number; daily: number[] }
|
| 58 |
type Race = { hidden: boolean; unit: string; handover?: boolean; success?: string | null; caption?: string | null; metrics?: RaceBook[]; series?: Series[]; days?: string[] }
|
| 59 |
+
type RisingStat = { closed: number; open: number; pnl: number; mean: number | null }
|
| 60 |
+
type Rising = { hidden: boolean; running?: boolean; observer?: boolean; days?: number; unit?: string; started_at?: string; go_live_at?: string | null
|
| 61 |
+
live_since?: string | null; held?: { trades: number; pnl_sol: number; go_live_at: string } | null; breadth_chg?: number | null; rise?: number; rising?: boolean | null
|
| 62 |
+
book?: RisingStat; selector?: RisingStat; doubled?: RisingStat; rest?: RisingStat; series?: Series[] }
|
| 63 |
type Position = { id: number; mint: string; token: string; strategy: string; held_min: number; exits_in_min: number; cost: number; ret: number | null
|
| 64 |
correctness?: number | null }
|
| 65 |
type Book = { unit: string; history: [string, number][]; positions: Position[]; live?: boolean; book?: string; title?: string; sub?: string; observer?: boolean
|
|
|
|
| 96 |
<WidgetGrid>
|
| 97 |
<Widget label={d.label} icon="wealth" value={amount(d.wealth, u)} color={d.live ? 'magenta' : null} help={d.wealth_help} spark={d.spark.length > 1 ? d.spark : null}
|
| 98 |
delta={d.wealth !== null ? `${signed(d.wealth - d.capital, 4)} ${u} since start` : null} />
|
| 99 |
+
<Widget label="Realized P&L" icon="pnl" value={amount(d.pnl, u, 4, true)} help={d.live ? 'The wallet now, with open positions at cost, minus the starting amount.' : 'Closed trades, after fees and price impact.'}
|
| 100 |
spark={d.pnl_spark.length > 1 ? d.pnl_spark : null} />
|
| 101 |
<Widget label="Closed trades" icon="count" value={int(d.closed)} delta={winRate !== null ? `${Math.round(winRate * 100)}% winners` : null} tone="off"
|
| 102 |
progress={winRate} />
|
|
|
|
| 195 |
)
|
| 196 |
}
|
| 197 |
|
| 198 |
+
/** A time as a datetime-local input's value: this machine's local time, to the minute. */
|
| 199 |
+
const localInput = (iso: string) => {
|
| 200 |
+
const d = new Date(iso)
|
| 201 |
+
return new Date(d.getTime() - d.getTimezoneOffset() * 60000).toISOString().slice(0, 16)
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
const localText = (iso: string) => localInput(iso).replace('T', ' ')
|
| 205 |
+
|
| 206 |
+
/** The rising + clean paper test: the test book against the selector's since it started, and the go-live, set here. */
|
| 207 |
+
function RisingPanel() {
|
| 208 |
+
const { chain } = useApp()
|
| 209 |
+
const r = useTopic<Rising>('overview.rising', { chain })
|
| 210 |
+
const [edit, setEdit] = useState<string | null>(null) // the time being typed; null: the saved one
|
| 211 |
+
if (!r.data) return r.error ? <Callout kind="error">{r.error}</Callout> : null
|
| 212 |
+
const d = r.data
|
| 213 |
+
if (d.hidden) return null
|
| 214 |
+
const title = 'Strategy 1: rising + clean'
|
| 215 |
+
const sub = 'A selector pick takes twice its stake when the market is recovering and the launch was clean.'
|
| 216 |
+
if (!d.running) {
|
| 217 |
+
return (
|
| 218 |
+
<Card title={title} icon="up" sub={sub} actions={(
|
| 219 |
+
<ActionButton label={`Start the ${d.days}-day test`} icon="play" name="overview.rising_start" disabled={d.observer}
|
| 220 |
+
help={d.observer ? 'This console only observes: start it from the console that hosts the workers.' : undefined}
|
| 221 |
+
confirm={{ title: 'Start the paper test?', confirmLabel: 'Start', body: <>A paper book takes the selector's picks with these stakes doubled, beside the selector's own book. Nothing changes on the wallet. The go-live is set {d.days} days out, and you can move it here.</> }} />
|
| 222 |
+
)}>
|
| 223 |
+
<Caption>No test is running.</Caption>
|
| 224 |
+
</Card>
|
| 225 |
+
)
|
| 226 |
+
}
|
| 227 |
+
const u = d.unit ?? 'SOL'
|
| 228 |
+
const saved = d.go_live_at ? localInput(d.go_live_at) : ''
|
| 229 |
+
const when = edit ?? saved
|
| 230 |
+
const parsed = when ? new Date(when) : null // a year out of range is an invalid date, not a crash
|
| 231 |
+
const iso = parsed && !Number.isNaN(parsed.getTime()) ? parsed.toISOString() : null
|
| 232 |
+
const stat = (s?: RisingStat) => `${s?.closed ?? 0} closed · ${s?.open ?? 0} open`
|
| 233 |
+
return (
|
| 234 |
+
<Card title={title} icon="up" sub={sub}>
|
| 235 |
+
{r.error && <Callout kind="error">{r.error} (showing the last good data)</Callout>}
|
| 236 |
+
{d.live_since && <Callout kind="success" icon="payments">Live since {localText(d.live_since)}: the wallet doubles these stakes.</Callout>}
|
| 237 |
+
{d.held && <Callout kind="warning">Stayed on paper at {localText(d.held.go_live_at)}: {d.held.trades} doubled trades made {amount(d.held.pnl_sol, u, 4, true)}. Set a new go-live below.</Callout>}
|
| 238 |
+
<Metrics min={170}>
|
| 239 |
+
<Metric compact label="Test book" icon="up" value={amount(d.book?.pnl, u, 4, true)} delta={stat(d.book)} tone="off" help="Realized P&L of paper_rising since the test began." />
|
| 240 |
+
<Metric compact label="Selector" icon={BOOK_ICON.selector} value={amount(d.selector?.pnl, u, 4, true)} delta={stat(d.selector)} tone="off" help="Realized P&L of paper_selector over the same time: the same picks at plain stakes." />
|
| 241 |
+
<Metric compact label="Doubled trades" icon="target" value={amount(d.doubled?.pnl, u, 4, true)} delta={`${stat(d.doubled)}${d.doubled?.mean == null ? '' : ` · ${pct(d.doubled.mean, 1)}`}`} tone="off" help="What the go-live judges: its closed doubled trades must have made money." />
|
| 242 |
+
<Metric compact label="Rest of the trades" icon="count" value={amount(d.rest?.pnl, u, 4, true)} delta={`${stat(d.rest)}${d.rest?.mean == null ? '' : ` · ${pct(d.rest.mean, 1)}`}`} tone="off" />
|
| 243 |
+
<Metric compact label="Breadth, 6 h" icon="pulse" value={d.breadth_chg == null ? '—' : `${num(d.breadth_chg * 100, 1, true)} pts`} delta={d.rising == null ? null : d.rising ? 'rising' : `needs ${num((d.rise ?? 0) * 100, 1, true)} pts`} tone="off" help="Change over 6 hours in the share of coins (pool at least 10 SOL) up on the hour." />
|
| 244 |
+
</Metrics>
|
| 245 |
+
{(d.series ?? []).length > 0 && <LineChart series={d.series!} height={220} yFormat={(v) => `${v.toFixed(3)} ${u}`} />}
|
| 246 |
+
<div className="d-flex flex-wrap align-items-end gap-2 mt-3">
|
| 247 |
+
<div>
|
| 248 |
+
<label className="form-label" htmlFor="rising-go-live">Go-live (this machine's time)</label>
|
| 249 |
+
<input id="rising-go-live" className="form-control" type="datetime-local" max="9999-12-31T23:59" value={when} disabled={d.observer} onChange={(e) => setEdit(e.target.value)} />
|
| 250 |
+
</div>
|
| 251 |
+
<ActionButton label="Save" icon="calendar" name="overview.rising_go_live" body={{ at: iso }} variant="primary" disabled={d.observer || !iso || when === saved} onDone={() => setEdit(null)}
|
| 252 |
+
confirm={{ title: `Go-live ${iso ? localText(iso) : ''}?`, confirmLabel: 'Save', danger: true,
|
| 253 |
+
body: <>At that time the test is judged. If its doubled trades have made money, the wallet starts doubling these stakes. If not, it stays on paper and the date clears.{d.live_since ? ' The wallet goes back to plain stakes until then.' : ''}</> }} />
|
| 254 |
+
<ActionButton label="No date" icon="trash" name="overview.rising_go_live" body={{ at: null }} disabled={d.observer || !d.go_live_at} onDone={() => setEdit(null)}
|
| 255 |
+
confirm={{ title: 'Clear the go-live?', confirmLabel: 'Clear', body: <>The wallet keeps plain stakes until you set a date{d.live_since ? ', from its next buy' : ''}. The paper test keeps running.</> }} />
|
| 256 |
+
</div>
|
| 257 |
+
{!d.live_since && !d.held && <Caption>{d.go_live_at ? `Goes live ${localText(d.go_live_at)} if the doubled trades have made money by then.` : 'No go-live date: the wallet keeps plain stakes.'} Test started {ago(d.started_at)}.</Caption>}
|
| 258 |
+
</Card>
|
| 259 |
+
)
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
/** The money's book: its whole wealth history, and the open positions (a live one can be sold now, after a confirmation). */
|
| 263 |
function BookPanel() {
|
| 264 |
const { chain, market } = useApp()
|
|
|
|
| 424 |
<BrainPanel variant="memecoin" />
|
| 425 |
<ThisMinute />
|
| 426 |
<RacePanel />
|
| 427 |
+
<RisingPanel />
|
| 428 |
<SetupsPanel />
|
| 429 |
<HoursPanel />
|
| 430 |
<TickerCard />
|
console/src/pages/Trades.tsx
CHANGED
|
@@ -20,7 +20,7 @@ const DONUT = ['#ffffff', 'rgba(255,255,255,0.6)', 'rgba(255,255,255,0.35)', 'rg
|
|
| 20 |
type Books = { chain: string; unit: string; books: string[]; default: string; help: string; live?: boolean }
|
| 21 |
type Summary = {
|
| 22 |
chain: string; book: string; unit: string
|
| 23 |
-
metrics: { n: number; pnl: number | null; avg_ret: number | null; wins: number; win_rate: number | null; open: number; open_cost: number }
|
| 24 |
cum: [string, number][]
|
| 25 |
daily: { day: string; n: number; pnl: number; wins: number }[]
|
| 26 |
exits: { kind: string; n: number; pnl: number }[]
|
|
@@ -87,7 +87,8 @@ export default function Trades() {
|
|
| 87 |
|
| 88 |
// ---------------------------------------------------------------- the book at a glance
|
| 89 |
function Dashboard({ d }: { d: Summary }) {
|
| 90 |
-
const u = d.unit, m = d.metrics
|
|
|
|
| 91 |
const losses = m.n - m.wins
|
| 92 |
const best = d.daily.length ? d.daily.reduce((a, x) => (x.pnl > a.pnl ? x : a)) : null
|
| 93 |
const worst = d.daily.length ? d.daily.reduce((a, x) => (x.pnl < a.pnl ? x : a)) : null
|
|
@@ -97,8 +98,9 @@ function Dashboard({ d }: { d: Summary }) {
|
|
| 97 |
<WidgetGrid>
|
| 98 |
<Widget label="Trades" value={int(m.n)} icon="count" spark={d.daily.map((x) => x.n)} sparkType="bar" delta={d.daily.length ? `${d.daily.length} trading days` : null} tone="off"
|
| 99 |
help="Closed positions in this book; the bars are trades closed per day (last 90 days)." />
|
| 100 |
-
<Widget label="Realized P&L" value={amount(
|
| 101 |
-
color={
|
|
|
|
| 102 |
<Widget label="Average return" value={pct(m.avg_ret)} icon="up" help="Per trade, after fees and price impact."
|
| 103 |
color={m.avg_ret == null || m.avg_ret === 0 ? null : m.avg_ret > 0 ? 'teal' : 'danger'} />
|
| 104 |
<Widget label="Winners" value={m.win_rate === null ? '—' : pct(m.win_rate, 0, false)} icon="target" progress={m.win_rate}
|
|
@@ -110,10 +112,10 @@ function Dashboard({ d }: { d: Summary }) {
|
|
| 110 |
<div className="col-12 col-xl-8">
|
| 111 |
<Card title="Cumulative realized P&L" icon="chart" sub={`${d.book} · ${u}`}>
|
| 112 |
{d.cum.length > 1
|
| 113 |
-
? <LineChart series={[{ name: `P&L (${u})`, data:
|
| 114 |
: <Empty>No closed trades in this book yet.</Empty>}
|
| 115 |
<RowGroup items={[
|
| 116 |
-
{ label: 'realized P&L', value: signed(
|
| 117 |
{ label: 'average per trade', value: signed(m.avg_ret, pct(m.avg_ret)) },
|
| 118 |
{ label: 'best day', value: best ? signed(best.pnl, `${amount(best.pnl, u, 4, true)} · ${best.day.slice(5)}`) : '—' },
|
| 119 |
{ label: 'worst day', value: worst ? signed(worst.pnl, `${amount(worst.pnl, u, 4, true)} · ${worst.day.slice(5)}`) : '—' },
|
|
|
|
| 20 |
type Books = { chain: string; unit: string; books: string[]; default: string; help: string; live?: boolean }
|
| 21 |
type Summary = {
|
| 22 |
chain: string; book: string; unit: string
|
| 23 |
+
metrics: { n: number; pnl: number | null; realized?: number | null; avg_ret: number | null; wins: number; win_rate: number | null; open: number; open_cost: number }
|
| 24 |
cum: [string, number][]
|
| 25 |
daily: { day: string; n: number; pnl: number; wins: number }[]
|
| 26 |
exits: { kind: string; n: number; pnl: number }[]
|
|
|
|
| 87 |
|
| 88 |
// ---------------------------------------------------------------- the book at a glance
|
| 89 |
function Dashboard({ d }: { d: Summary }) {
|
| 90 |
+
const u = d.unit, m = d.metrics, realized = m.realized ?? m.pnl
|
| 91 |
+
const wallet = realized !== m.pnl, shift = (realized ?? 0) - (m.pnl ?? 0), cum = d.cum.map(([t, v]) => [t, v + shift] as [string, number]) // the line ends at the figure shown
|
| 92 |
const losses = m.n - m.wins
|
| 93 |
const best = d.daily.length ? d.daily.reduce((a, x) => (x.pnl > a.pnl ? x : a)) : null
|
| 94 |
const worst = d.daily.length ? d.daily.reduce((a, x) => (x.pnl < a.pnl ? x : a)) : null
|
|
|
|
| 98 |
<WidgetGrid>
|
| 99 |
<Widget label="Trades" value={int(m.n)} icon="count" spark={d.daily.map((x) => x.n)} sparkType="bar" delta={d.daily.length ? `${d.daily.length} trading days` : null} tone="off"
|
| 100 |
help="Closed positions in this book; the bars are trades closed per day (last 90 days)." />
|
| 101 |
+
<Widget label="Realized P&L" value={amount(realized, u, 4, true)} icon="pnl" spark={cum.map((x) => x[1])}
|
| 102 |
+
color={realized == null || realized === 0 ? null : realized > 0 ? 'success' : 'danger'}
|
| 103 |
+
help={wallet ? 'The wallet now, with open positions at cost, minus the starting amount.' : 'Realized P&L of every closed trade; the line is its running total.'} />
|
| 104 |
<Widget label="Average return" value={pct(m.avg_ret)} icon="up" help="Per trade, after fees and price impact."
|
| 105 |
color={m.avg_ret == null || m.avg_ret === 0 ? null : m.avg_ret > 0 ? 'teal' : 'danger'} />
|
| 106 |
<Widget label="Winners" value={m.win_rate === null ? '—' : pct(m.win_rate, 0, false)} icon="target" progress={m.win_rate}
|
|
|
|
| 112 |
<div className="col-12 col-xl-8">
|
| 113 |
<Card title="Cumulative realized P&L" icon="chart" sub={`${d.book} · ${u}`}>
|
| 114 |
{d.cum.length > 1
|
| 115 |
+
? <LineChart series={[{ name: `P&L (${u})`, data: cum }]} height={280} yFormat={(v) => fmtAxis(v)} />
|
| 116 |
: <Empty>No closed trades in this book yet.</Empty>}
|
| 117 |
<RowGroup items={[
|
| 118 |
+
{ label: 'realized P&L', value: signed(realized, amount(realized, u, 4, true)) },
|
| 119 |
{ label: 'average per trade', value: signed(m.avg_ret, pct(m.avg_ret)) },
|
| 120 |
{ label: 'best day', value: best ? signed(best.pnl, `${amount(best.pnl, u, 4, true)} · ${best.day.slice(5)}`) : '—' },
|
| 121 |
{ label: 'worst day', value: worst ? signed(worst.pnl, `${amount(worst.pnl, u, 4, true)} · ${worst.day.slice(5)}`) : '—' },
|
fly_trader/__main__.py
CHANGED
|
@@ -149,6 +149,13 @@ def _cmd_rh_v4_backfill(args):
|
|
| 149 |
f"{out.get('outcomes', 0)} first-hour outcomes")
|
| 150 |
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
def _cmd_refetch_archive(args):
|
| 153 |
from .ingest import replay_pull
|
| 154 |
out = replay_pull.refetch_main()
|
|
@@ -387,6 +394,9 @@ def build_parser() -> argparse.ArgumentParser:
|
|
| 387 |
p_v4.add_argument("--no-keys", action="store_true", help="skip the pool-key import")
|
| 388 |
p_v4.add_argument("--no-history", action="store_true", help="skip the live pools' history")
|
| 389 |
p_v4.set_defaults(fn=_cmd_rh_v4_backfill)
|
|
|
|
|
|
|
|
|
|
| 390 |
sub.add_parser("refetch-archive", help="download older archive hours again to keep every venue's SOL legs (run apart from the console, under nice)").set_defaults(fn=_cmd_refetch_archive)
|
| 391 |
sub.add_parser("selector-eval").set_defaults(fn=_cmd_selector_eval)
|
| 392 |
ts_ = sub.add_parser("train-selector"); ts_.add_argument("--days", type=int, default=None, help="most recent days only (default: the whole corpus)")
|
|
|
|
| 149 |
f"{out.get('outcomes', 0)} first-hour outcomes")
|
| 150 |
|
| 151 |
|
| 152 |
+
def _cmd_rh_v4_facts(args):
|
| 153 |
+
from .logging_setup import setup
|
| 154 |
+
from .rh import v4_facts
|
| 155 |
+
setup("rh_v4_facts")
|
| 156 |
+
print(f"rh v4 facts: {v4_facts.run_locked(limit=args.limit)}")
|
| 157 |
+
|
| 158 |
+
|
| 159 |
def _cmd_refetch_archive(args):
|
| 160 |
from .ingest import replay_pull
|
| 161 |
out = replay_pull.refetch_main()
|
|
|
|
| 394 |
p_v4.add_argument("--no-keys", action="store_true", help="skip the pool-key import")
|
| 395 |
p_v4.add_argument("--no-history", action="store_true", help="skip the live pools' history")
|
| 396 |
p_v4.set_defaults(fn=_cmd_rh_v4_backfill)
|
| 397 |
+
p_vf = sub.add_parser("rh-v4-facts", help="find the launch facts of plain v4 coins near the universe's age (the stream starts it as a child process)")
|
| 398 |
+
p_vf.add_argument("--limit", type=int, default=20)
|
| 399 |
+
p_vf.set_defaults(fn=_cmd_rh_v4_facts)
|
| 400 |
sub.add_parser("refetch-archive", help="download older archive hours again to keep every venue's SOL legs (run apart from the console, under nice)").set_defaults(fn=_cmd_refetch_archive)
|
| 401 |
sub.add_parser("selector-eval").set_defaults(fn=_cmd_selector_eval)
|
| 402 |
ts_ = sub.add_parser("train-selector"); ts_.add_argument("--days", type=int, default=None, help="most recent days only (default: the whole corpus)")
|
fly_trader/agent/crash_exit.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Crash exit, Solana (Bryce, 2026-11-07: "If we see it drop 25% in 5 minutes or less we should exit immediately. If it
|
| 2 |
+
passes the bar later and we rebuy, then fine").
|
| 3 |
+
|
| 4 |
+
On 2026-11-07 the wallet bought 519AMD at 04:08; from 04:31 to 04:38 its holders sold ~75 SOL and the price fell 90 %;
|
| 5 |
+
the position was held to its 240 minutes and sold at -82 % (-0.195 SOL). Replayed over every Solana trade since 10-17
|
| 6 |
+
(data/scratch/crash_exit_20261107): selling the moment a held coin is 25 % under its high of the last 5 minutes made
|
| 7 |
+
live +0.30 SOL since 10-28 instead of -0.24, and 519AMD -25 % instead of -82 %; the same rule read once a minute, on
|
| 8 |
+
the minute's close, made nothing (-0.22). So it watches every swap, not the minutes. On Robinhood Chain it lost in every
|
| 9 |
+
variant: not used there.
|
| 10 |
+
|
| 11 |
+
- ``observe`` (ingest/pumpstream.Aggregator.ingest, each PumpSwap leg it keeps): a coin a Solana book holds keeps its
|
| 12 |
+
prices of the last ``WINDOW_S``; a price ``DROP`` or more under their high (the entry price counted while inside the
|
| 13 |
+
window, nothing before the entry) is a crash: noted (``crashed``), and the wallet's position, if any, is sold at once
|
| 14 |
+
from a thread of its own (agent/fly_live.LiveMirror.crash_exit, the forced slippage), tried again on later prices
|
| 15 |
+
while it is still open. Cheap on the stream's thread: dictionary reads; the database and the order go to ``_POOL``.
|
| 16 |
+
- Paper books (agent/paper_trading.plan_minute) sell a position whose coin crashed after its entry, in the minute's
|
| 17 |
+
bookkeeping, as forced 'crash' exits, at the first price the stream saw ``FILL_DELAY_S`` or more after the crash
|
| 18 |
+
(about when the wallet's sale lands; the crash's own price until one comes): a paper fill at the crash's very price
|
| 19 |
+
would beat the wallet's on every crash and read as an execution gap (agent/fly_live.LiveMirror.gap).
|
| 20 |
+
- A crashed coin may be bought again whenever it passes the bar again.
|
| 21 |
+
- The held coins are re-read every ``REFRESH_S`` (a new position is watched within seconds of its entry).
|
| 22 |
+
"""
|
| 23 |
+
from __future__ import annotations
|
| 24 |
+
|
| 25 |
+
import logging
|
| 26 |
+
import threading
|
| 27 |
+
import time
|
| 28 |
+
from collections import deque
|
| 29 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 30 |
+
|
| 31 |
+
from ..db.apilog import record_event
|
| 32 |
+
from ..db.connection import transaction
|
| 33 |
+
|
| 34 |
+
log = logging.getLogger(__name__)
|
| 35 |
+
DROP = 0.25 # a fall this deep under the window's high is a crash
|
| 36 |
+
WINDOW_S = 300.0 # the window's length: 5 minutes
|
| 37 |
+
REFRESH_S = 5.0 # the held coins re-read this often
|
| 38 |
+
RETRY_S = 15.0 # a crashed coin's live sale is sent again this long after the last try while it stays open
|
| 39 |
+
KEEP_S = 86_400.0 # a crash is remembered this long (a paper position of the coin opened before it still sells)
|
| 40 |
+
FILL_DELAY_S = 5.0 # paper's crash sale: the first price this long after the crash (the wallet's sale: order, signer, landing)
|
| 41 |
+
_POOL = ThreadPoolExecutor(max_workers=2, thread_name_prefix="crash-exit")
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class Watch:
|
| 45 |
+
def __init__(self):
|
| 46 |
+
self.lock = threading.Lock()
|
| 47 |
+
self.held: dict[str, tuple[float, float]] = {} # mint -> (first open position's entry, epoch s; its entry price)
|
| 48 |
+
self.ticks: dict[str, deque] = {} # mint -> (epoch s, price) of the last WINDOW_S
|
| 49 |
+
self.crashes: dict[str, list] = {} # mint -> [epoch s, price, the window's high, paper's fill or None]
|
| 50 |
+
self.sent: dict[str, float] = {} # mint -> when its live sale was last sent
|
| 51 |
+
self.refreshed = 0.0; self.refreshing = False
|
| 52 |
+
|
| 53 |
+
# -------------------------------------------------------------- the stream's thread
|
| 54 |
+
def observe(self, mint: str, ts: float, price: float, now: float | None = None) -> None:
|
| 55 |
+
now = time.time() if now is None else now
|
| 56 |
+
if now - self.refreshed >= REFRESH_S and not self.refreshing:
|
| 57 |
+
self.refreshing = True
|
| 58 |
+
_POOL.submit(self._refresh)
|
| 59 |
+
h = self.held.get(mint)
|
| 60 |
+
if h is None or price <= 0 or ts < h[0]:
|
| 61 |
+
return
|
| 62 |
+
dq = self.ticks.get(mint)
|
| 63 |
+
if dq is None:
|
| 64 |
+
dq = self.ticks[mint] = deque()
|
| 65 |
+
dq.append((ts, price))
|
| 66 |
+
while dq and dq[0][0] < ts - WINDOW_S:
|
| 67 |
+
dq.popleft()
|
| 68 |
+
high = max(p for _, p in dq)
|
| 69 |
+
if h[0] >= ts - WINDOW_S:
|
| 70 |
+
high = max(high, h[1])
|
| 71 |
+
c = self.crashes.get(mint)
|
| 72 |
+
known = c is not None and c[0] >= h[0] # this holding's crash, its live sale maybe not done yet
|
| 73 |
+
if known and c[3] is None and ts >= c[0] + FILL_DELAY_S:
|
| 74 |
+
c[3] = price
|
| 75 |
+
if not known and price > (1.0 - DROP) * high:
|
| 76 |
+
return
|
| 77 |
+
with self.lock:
|
| 78 |
+
first = not known
|
| 79 |
+
if first:
|
| 80 |
+
c = self.crashes[mint] = [ts, price, high, None]; self.sent.pop(mint, None)
|
| 81 |
+
due = now - self.sent.get(mint, 0.0) >= RETRY_S
|
| 82 |
+
if due:
|
| 83 |
+
self.sent[mint] = now
|
| 84 |
+
if first:
|
| 85 |
+
log.warning("crash: %s at %.6g, %.0f%% under its 5-minute high %.6g", mint, price, (1 - price / high) * 100, high)
|
| 86 |
+
if due:
|
| 87 |
+
_POOL.submit(self._sell, mint, c[1], c[2], first)
|
| 88 |
+
|
| 89 |
+
# -------------------------------------------------------------- its own threads
|
| 90 |
+
def _refresh(self) -> None:
|
| 91 |
+
try:
|
| 92 |
+
with transaction() as conn:
|
| 93 |
+
rows = conn.execute("""SELECT mint, min(extract(epoch FROM opened_at)) AS t0,
|
| 94 |
+
(array_agg(entry_price ORDER BY opened_at))[1] AS px
|
| 95 |
+
FROM positions WHERE status = 'open' AND COALESCE(chain, 'sol') = 'sol' GROUP BY mint""").fetchall()
|
| 96 |
+
held = {r["mint"]: (float(r["t0"]), float(r["px"] or 0.0)) for r in rows}
|
| 97 |
+
with self.lock:
|
| 98 |
+
self.held = held
|
| 99 |
+
for m in [m for m in self.ticks if m not in held]:
|
| 100 |
+
self.ticks.pop(m, None)
|
| 101 |
+
cut = time.time() - KEEP_S
|
| 102 |
+
for m in [m for m, c in self.crashes.items() if c[0] < cut]:
|
| 103 |
+
self.crashes.pop(m, None); self.sent.pop(m, None)
|
| 104 |
+
except Exception:
|
| 105 |
+
log.exception("crash exit: reading the held coins failed")
|
| 106 |
+
finally:
|
| 107 |
+
self.refreshed = time.time(); self.refreshing = False
|
| 108 |
+
|
| 109 |
+
def _sell(self, mint: str, price: float, high: float, first: bool) -> None:
|
| 110 |
+
why = f"crash: {(1 - price / high) * 100:.0f}% under its 5-minute high"
|
| 111 |
+
try:
|
| 112 |
+
from . import live_first
|
| 113 |
+
n = live_first.crash_sale("sol", mint, why)
|
| 114 |
+
if first:
|
| 115 |
+
record_event("warning", "crash_exit", f"{mint} fell {(1 - price / high) * 100:.0f}% within 5 minutes: "
|
| 116 |
+
+ (f"the wallet's position sold ({n})" if n else "no live position to sell; paper sells it this minute"),
|
| 117 |
+
{"mint": mint, "price": price, "high": high, "live_sales": n})
|
| 118 |
+
except Exception:
|
| 119 |
+
log.exception("crash exit: selling %s failed", mint)
|
| 120 |
+
|
| 121 |
+
# -------------------------------------------------------------- the engine's thread
|
| 122 |
+
def crashed(self, mint: str, since) -> tuple[float, float] | None:
|
| 123 |
+
"""(epoch s, paper's price) of ``mint``'s crash at or after ``since`` (a datetime: a position's entry), else None."""
|
| 124 |
+
c = self.crashes.get(mint)
|
| 125 |
+
if c is None or c[0] < since.timestamp():
|
| 126 |
+
return None
|
| 127 |
+
return c[0], c[3] if c[3] is not None else c[1]
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
WATCH = Watch()
|
fly_trader/agent/fly_live.py
CHANGED
|
@@ -11,6 +11,8 @@ The paper book ``paper_fly`` keeps running as the mirror. Only after the handove
|
|
| 11 |
agent/live_first.chain_take_profits): a position whose minute closed at its take-profit line (set at its entry on a
|
| 12 |
coin bought far below its high, agent/paper_trading.take_profit_x) is sold at the exit slippage, as a 'take_profit'
|
| 13 |
exit, tried again each minute that closes at the line;
|
|
|
|
|
|
|
| 14 |
- entries (``minute``: the moment the fly has decided, before any paper trade, learning or bookkeeping of the minute):
|
| 15 |
the execution worker's results are drained (execution/worker.py books live positions from balance-verified fills);
|
| 16 |
every paper entry of this minute is mirrored with the same score, line and certainty bands, sized from the wallet
|
|
@@ -188,6 +190,20 @@ class LiveMirror:
|
|
| 188 |
n += self._sell(p, did, config.SLIPPAGE_EXIT_BPS, "take_profit")
|
| 189 |
return n
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
def sell_now(self, conn, position_id: int) -> int:
|
| 192 |
"""The operator's Sell Now (the console's open positions, agent/live_first.sell_now): one open position of this book
|
| 193 |
sold at once, at the exit's slippage stepping up to the forced one, and booked by the worker like any exit, as a
|
|
@@ -261,12 +277,13 @@ class LiveMirror:
|
|
| 261 |
skipped.append((e["mint"], "held")); self._blocked(conn, ctx, run_id, beat_id, e, "held", "the wallet holds this token or has an order of it queued"); continue
|
| 262 |
size, why = sizing.size_position(e["score"], e.get("threshold", line), e.get("table") or table or [], bankroll, cash, e["info"]["resq"],
|
| 263 |
flat=config.FLY_SIZING == "flat", margin=e.get("size_margin"),
|
| 264 |
-
creator_heavy=bool(e["info"].get("creator_heavy")))
|
| 265 |
if blocked or size <= 0:
|
| 266 |
skipped.append((e["mint"], blocked or why)); self._blocked(conn, ctx, run_id, beat_id, e, blocked or "sizing", why, size); continue
|
| 267 |
if self._buy(e, size):
|
| 268 |
n_enter += 1; cash -= size; held_mints.add(e["mint"]); self.sent[e["decision_id"]] = {"e": e, "ts": m1, "tries": 1}
|
| 269 |
-
conn.execute("UPDATE decisions SET book_targets = array_append(COALESCE(book_targets, '{}'), 'live') WHERE id = %s",
|
|
|
|
| 270 |
else:
|
| 271 |
skipped.append((e["mint"], "held")); self._blocked(conn, ctx, run_id, beat_id, e, "held", "an order of this token is queued or in flight", size)
|
| 272 |
self.due_mark = self.mark_ctx = (ctx, beat_id)
|
|
|
|
| 11 |
agent/live_first.chain_take_profits): a position whose minute closed at its take-profit line (set at its entry on a
|
| 12 |
coin bought far below its high, agent/paper_trading.take_profit_x) is sold at the exit slippage, as a 'take_profit'
|
| 13 |
exit, tried again each minute that closes at the line;
|
| 14 |
+
- crash exits (``crash_exit``, agent/crash_exit.py: any moment, from the crash watcher's thread): a held coin 25 % under
|
| 15 |
+
its 5-minute high is sold at once at the forced slippage, as a 'crash' exit;
|
| 16 |
- entries (``minute``: the moment the fly has decided, before any paper trade, learning or bookkeeping of the minute):
|
| 17 |
the execution worker's results are drained (execution/worker.py books live positions from balance-verified fills);
|
| 18 |
every paper entry of this minute is mirrored with the same score, line and certainty bands, sized from the wallet
|
|
|
|
| 190 |
n += self._sell(p, did, config.SLIPPAGE_EXIT_BPS, "take_profit")
|
| 191 |
return n
|
| 192 |
|
| 193 |
+
def crash_exit(self, conn, mint: str, why: str) -> int:
|
| 194 |
+
"""``mint`` crashed (agent/crash_exit.py): its open position sold at once at the forced slippage, as a forced
|
| 195 |
+
'crash' exit (paper sells it the same way: in the gap like any exit); how many went out (0: none open, or an order
|
| 196 |
+
of it already queued or in flight)."""
|
| 197 |
+
n = 0
|
| 198 |
+
with self.exit_lock:
|
| 199 |
+
if mint in self.worker.pending():
|
| 200 |
+
return 0
|
| 201 |
+
for p in ledger.open_positions(conn, BOOK):
|
| 202 |
+
if p["mint"] == mint:
|
| 203 |
+
did = self._decision(conn, datetime.now(timezone.utc), None, None, p, why, True)
|
| 204 |
+
n += self._sell(p, did, config.SLIPPAGE_FORCED_BPS, "crash")
|
| 205 |
+
return n
|
| 206 |
+
|
| 207 |
def sell_now(self, conn, position_id: int) -> int:
|
| 208 |
"""The operator's Sell Now (the console's open positions, agent/live_first.sell_now): one open position of this book
|
| 209 |
sold at once, at the exit's slippage stepping up to the forced one, and booked by the worker like any exit, as a
|
|
|
|
| 277 |
skipped.append((e["mint"], "held")); self._blocked(conn, ctx, run_id, beat_id, e, "held", "the wallet holds this token or has an order of it queued"); continue
|
| 278 |
size, why = sizing.size_position(e["score"], e.get("threshold", line), e.get("table") or table or [], bankroll, cash, e["info"]["resq"],
|
| 279 |
flat=config.FLY_SIZING == "flat", margin=e.get("size_margin"),
|
| 280 |
+
creator_heavy=bool(e["info"].get("creator_heavy")), stake_x=e.get("stake_x", 1.0))
|
| 281 |
if blocked or size <= 0:
|
| 282 |
skipped.append((e["mint"], blocked or why)); self._blocked(conn, ctx, run_id, beat_id, e, blocked or "sizing", why, size); continue
|
| 283 |
if self._buy(e, size):
|
| 284 |
n_enter += 1; cash -= size; held_mints.add(e["mint"]); self.sent[e["decision_id"]] = {"e": e, "ts": m1, "tries": 1}
|
| 285 |
+
conn.execute("UPDATE decisions SET book_targets = array_append(COALESCE(book_targets, '{}'), 'live'), detail = detail || %s::jsonb WHERE id = %s",
|
| 286 |
+
(json.dumps({"live_stake_x": e["stake_x"]} if "stake_x" in e else {}), e["decision_id"])) # which buys the go-live doubled (agent/rising_clean.py)
|
| 287 |
else:
|
| 288 |
skipped.append((e["mint"], "held")); self._blocked(conn, ctx, run_id, beat_id, e, "held", "an order of this token is queued or in flight", size)
|
| 289 |
self.due_mark = self.mark_ctx = (ctx, beat_id)
|
fly_trader/agent/fly_session.py
CHANGED
|
@@ -59,7 +59,7 @@ from ..execution.broker_paper import PaperBroker
|
|
| 59 |
from ..ops.reset import activity_dir, fly_state_dir
|
| 60 |
from ..train import fly_calibrate, fly_governance as gov, fly_selector, selector, strategies
|
| 61 |
from ..train.decisions import X_COLS
|
| 62 |
-
from . import handover, live_first, paper_trading
|
| 63 |
|
| 64 |
log = logging.getLogger(__name__)
|
| 65 |
BOOK, KIND = "paper_fly", "fly" # the Solana book's names (another chain's book takes its own from fly_trader/markets)
|
|
@@ -164,6 +164,7 @@ class FlyBook:
|
|
| 164 |
(self.run_id, self.KIND, json.dumps({"channels": self.cfg, "book": self.BOOK, "chain": chain}, default=str), self.boot_id))
|
| 165 |
self._restore_tags(conn)
|
| 166 |
self.has_seat = handover.state(conn, chain) is not None # whose decision the wallet trades (on_minute); refreshed after each minute's live orders
|
|
|
|
| 167 |
record_event("info", "fly", "fly session started", {"run_id": self.run_id, "bootstrap": self.boot_id, "channels": self.cfg, "lines": self.lines["plastic"],
|
| 168 |
"pending": len(self.pending), "learning_frozen": self.learning_frozen})
|
| 169 |
log.info("fly session: bootstrap %d on %s, channels %s, lines %s, %d pending tags", self.boot_id, brain_device.describe(self.fly.net.dev), self.cfg,
|
|
@@ -524,8 +525,11 @@ class FlyBook:
|
|
| 524 |
allow=d.get("allow"), reasons=d.get("reason"), live_allow=live_allow,
|
| 525 |
size_margins=d.get("size_margin"), correctness=d.get("pclean"))
|
| 526 |
if self.trades_live(conn):
|
|
|
|
| 527 |
live = self._go_live(ctx, plan["beat_id"], plan["entries"], plan["rebuy"])
|
| 528 |
timing["orders_s"] = time.time() - ctx.m1_epoch # the wallet's orders queued
|
|
|
|
|
|
|
| 529 |
self._phase = {"m1": ctx.m1, "picks": scored["picks"], "plan": plan, "live": live, "timing": timing}
|
| 530 |
return {"minute": ctx.m1.isoformat(), "picks": scored["picks"], "live": live}
|
| 531 |
|
|
@@ -567,6 +571,7 @@ class FlyBook:
|
|
| 567 |
elif hour > self.last_hour:
|
| 568 |
self.last_hour = hour; self._hourly(conn, ctx.m1_epoch)
|
| 569 |
self.has_seat = handover.state(conn, self.chain) is not None # for the next live phase: a handover, or the seat cleared by hand
|
|
|
|
| 570 |
self._write_capture()
|
| 571 |
self._write_state()
|
| 572 |
if learned.get("keys"): # a row still waiting for its live fill keeps its features (tags survive restarts)
|
|
@@ -598,6 +603,19 @@ class FlyBook:
|
|
| 598 |
"""The chain's live mirror, one per process and owned by the runner (agent/live_first.py); None until made."""
|
| 599 |
return live_first.mirror_of(self.chain)
|
| 600 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 601 |
def _go_live(self, ctx, beat_id: int, entries: list, rebuy: dict) -> dict:
|
| 602 |
"""The wallet's orders of this minute, in their own savepoint: a wallet RPC timeout must not roll back the paper plan."""
|
| 603 |
try:
|
|
|
|
| 59 |
from ..ops.reset import activity_dir, fly_state_dir
|
| 60 |
from ..train import fly_calibrate, fly_governance as gov, fly_selector, selector, strategies
|
| 61 |
from ..train.decisions import X_COLS
|
| 62 |
+
from . import handover, live_first, paper_trading, rising_clean
|
| 63 |
|
| 64 |
log = logging.getLogger(__name__)
|
| 65 |
BOOK, KIND = "paper_fly", "fly" # the Solana book's names (another chain's book takes its own from fly_trader/markets)
|
|
|
|
| 164 |
(self.run_id, self.KIND, json.dumps({"channels": self.cfg, "book": self.BOOK, "chain": chain}, default=str), self.boot_id))
|
| 165 |
self._restore_tags(conn)
|
| 166 |
self.has_seat = handover.state(conn, chain) is not None # whose decision the wallet trades (on_minute); refreshed after each minute's live orders
|
| 167 |
+
self.rising_live = chain == "sol" and rising_clean.wallet_doubles(conn) # the wallet doubles rising + clean picks; refreshed the same way
|
| 168 |
record_event("info", "fly", "fly session started", {"run_id": self.run_id, "bootstrap": self.boot_id, "channels": self.cfg, "lines": self.lines["plastic"],
|
| 169 |
"pending": len(self.pending), "learning_frozen": self.learning_frozen})
|
| 170 |
log.info("fly session: bootstrap %d on %s, channels %s, lines %s, %d pending tags", self.boot_id, brain_device.describe(self.fly.net.dev), self.cfg,
|
|
|
|
| 525 |
allow=d.get("allow"), reasons=d.get("reason"), live_allow=live_allow,
|
| 526 |
size_margins=d.get("size_margin"), correctness=d.get("pclean"))
|
| 527 |
if self.trades_live(conn):
|
| 528 |
+
failed = self._double(ctx, plan["entries"]) if self.rising_live and plan["entries"] else None
|
| 529 |
live = self._go_live(ctx, plan["beat_id"], plan["entries"], plan["rebuy"])
|
| 530 |
timing["orders_s"] = time.time() - ctx.m1_epoch # the wallet's orders queued
|
| 531 |
+
if failed:
|
| 532 |
+
record_event("error", "fly_live", f"rising + clean stakes failed: {failed}", {"chain": self.chain, "minute": ctx.m1.isoformat()})
|
| 533 |
self._phase = {"m1": ctx.m1, "picks": scored["picks"], "plan": plan, "live": live, "timing": timing}
|
| 534 |
return {"minute": ctx.m1.isoformat(), "picks": scored["picks"], "live": live}
|
| 535 |
|
|
|
|
| 571 |
elif hour > self.last_hour:
|
| 572 |
self.last_hour = hour; self._hourly(conn, ctx.m1_epoch)
|
| 573 |
self.has_seat = handover.state(conn, self.chain) is not None # for the next live phase: a handover, or the seat cleared by hand
|
| 574 |
+
self.rising_live = self.chain == "sol" and rising_clean.wallet_doubles(conn) # the go-live passed or moved: from the next live phase
|
| 575 |
self._write_capture()
|
| 576 |
self._write_state()
|
| 577 |
if learned.get("keys"): # a row still waiting for its live fill keeps its features (tags survive restarts)
|
|
|
|
| 603 |
"""The chain's live mirror, one per process and owned by the runner (agent/live_first.py); None until made."""
|
| 604 |
return live_first.mirror_of(self.chain)
|
| 605 |
|
| 606 |
+
def _double(self, ctx, entries: list) -> str | None:
|
| 607 |
+
"""The wallet's rising + clean stakes (agent/rising_clean.py), in their own savepoint, read again here: the operator
|
| 608 |
+
may have cleared the go-live since ``rising_live`` was. Should they fail, this minute's orders still go out, at
|
| 609 |
+
plain stakes, and the error comes back to be recorded after them."""
|
| 610 |
+
try:
|
| 611 |
+
with ctx.conn.transaction():
|
| 612 |
+
if rising_clean.wallet_doubles(ctx.conn):
|
| 613 |
+
rising_clean.for_wallet(ctx, entries, self.broker)
|
| 614 |
+
except Exception as e:
|
| 615 |
+
log.exception("%s: rising + clean stakes of minute %s failed; plain stakes", self.name, ctx.m1.isoformat())
|
| 616 |
+
return f"{type(e).__name__}: {str(e)[:160]}"
|
| 617 |
+
return None
|
| 618 |
+
|
| 619 |
def _go_live(self, ctx, beat_id: int, entries: list, rebuy: dict) -> dict:
|
| 620 |
"""The wallet's orders of this minute, in their own savepoint: a wallet RPC timeout must not roll back the paper plan."""
|
| 621 |
try:
|
fly_trader/agent/live_first.py
CHANGED
|
@@ -6,6 +6,8 @@
|
|
| 6 |
straddled a boundary).
|
| 7 |
- ``chain_take_profits``: a live position's take-profit sale depends on its minute's close alone, so it goes out the
|
| 8 |
moment the engine reads the minute, before the minute's features and the fly's scores (agent/minute_engine.live_phase).
|
|
|
|
|
|
|
| 9 |
- Anything that can run beside a live order does: the engine wakes the moment a stream writes a minute
|
| 10 |
(``minute_published``), each chain's wallet balance is read while its minute's features and scores are computed
|
| 11 |
(``prefetch``), and the chains' live phases run at once (agent/minute_engine.run_ready).
|
|
@@ -191,6 +193,17 @@ def sell_now(chain: str, position_id: int, wait_s: float = SELL_NOW_WAIT_S) -> d
|
|
| 191 |
"error": None if booked or res is None else (res.error or "it sold only part of the position: the rest stays open")}
|
| 192 |
|
| 193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
def idle_mirrors() -> None:
|
| 195 |
"""Between minutes: each mirror's bookkeeping that waits for an idle wallet (the wealth mark, sweeps, the reconcile)."""
|
| 196 |
for chain, m in list(_MIRRORS.items()):
|
|
|
|
| 6 |
straddled a boundary).
|
| 7 |
- ``chain_take_profits``: a live position's take-profit sale depends on its minute's close alone, so it goes out the
|
| 8 |
moment the engine reads the minute, before the minute's features and the fly's scores (agent/minute_engine.live_phase).
|
| 9 |
+
- ``crash_sale``: a held Solana coin 25 % under its 5-minute high (agent/crash_exit.py) is sold the moment the stream
|
| 10 |
+
sees the price, from the crash watcher's thread, not at a minute.
|
| 11 |
- Anything that can run beside a live order does: the engine wakes the moment a stream writes a minute
|
| 12 |
(``minute_published``), each chain's wallet balance is read while its minute's features and scores are computed
|
| 13 |
(``prefetch``), and the chains' live phases run at once (agent/minute_engine.run_ready).
|
|
|
|
| 193 |
"error": None if booked or res is None else (res.error or "it sold only part of the position: the rest stays open")}
|
| 194 |
|
| 195 |
|
| 196 |
+
|
| 197 |
+
def crash_sale(chain: str, mint: str, why: str) -> int:
|
| 198 |
+
"""A coin the wallet holds crashed (agent/crash_exit.py): the chain's mirror sells it at once (its ``crash_exit``),
|
| 199 |
+
from the crash watcher's own thread, not waiting for any minute. How many sales went out (0: none held, one in
|
| 200 |
+
flight, or the wallet may not sign)."""
|
| 201 |
+
m = mirror_of(chain)
|
| 202 |
+
if m is None or signing_refused(chain):
|
| 203 |
+
return 0
|
| 204 |
+
with transaction() as conn:
|
| 205 |
+
return m.crash_exit(conn, mint, why)
|
| 206 |
+
|
| 207 |
def idle_mirrors() -> None:
|
| 208 |
"""Between minutes: each mirror's bookkeeping that waits for an idle wallet (the wealth mark, sweeps, the reconcile)."""
|
| 209 |
for chain, m in list(_MIRRORS.items()):
|
fly_trader/agent/minute_engine.py
CHANGED
|
@@ -7,9 +7,11 @@ synthetic trades, exactly as in training. Graduation time and creation/creator f
|
|
| 7 |
same source training uses. The full feature vector (``decisions.X_COLS``) of the rows in the models' universe
|
| 8 |
(``selector.in_universe``) that pass the eligibility gate (pool ≥ ``MIN_RESQ_SOL``, 15-minute volume ≥
|
| 9 |
``MIN_VOL_15M_SOL``, no scale break — a close 50× from the previous one or a reserve above 100,000 SOL — since the
|
| 10 |
-
mint's state began) goes to every book; each picks
|
|
|
|
| 11 |
|
| 12 |
-
Books — ``SelectorBook`` (agent/selector_session.py)
|
|
|
|
| 13 |
``on_bars(t_start, bars)`` (every processed minute, warm-up included: open, close and exit cost of every traded mint),
|
| 14 |
``on_minute(ctx)``, ``open_mints(conn)``, ``maybe_reload()``, ``done`` and ``finish()``. Each book trades inside its own
|
| 15 |
savepoint, so one book's failure never undoes the other's minute.
|
|
@@ -53,16 +55,21 @@ from ..market.exit_cost import cost_at_size, cost_label
|
|
| 53 |
from ..market.features import FEATURES, FIDX, TokenMeta, TokenState
|
| 54 |
from ..train.corpus_meta import FEATURE_COLS as META_COLS, sol_select
|
| 55 |
from ..train.decisions import MIN_RESQ_SOL, MIN_VOL_15M_SOL, X_COLS
|
| 56 |
-
from ..train.flow import FlowWindow, MarketWindow
|
| 57 |
from ..train.mature import MASKED
|
| 58 |
from ..train.selector import creator_heavy, in_universe
|
| 59 |
-
from . import live_first
|
| 60 |
|
| 61 |
log = logging.getLogger(__name__)
|
| 62 |
WARMUP_MIN = 1440 # training warms each day on the previous day's candles
|
| 63 |
V4_WARM_H = 3.0 # a plain v4 memecoin trades only once the engine has fed it this long (the longest feature window):
|
| 64 |
# it goes live mid-session without the history before (rh/v4_universe.go_live)
|
| 65 |
IDLE_EVICT_S = 86400 # training has no history for a mint idle since before the previous day
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
STREAM_STALE_S = 180 # no entries or exits when the stream's newest complete minute is older than this
|
| 67 |
META_TTL_S = 600
|
| 68 |
MODEL_CHECK_S = 600 # how often books reload newer models and new books are admitted
|
|
@@ -147,6 +154,10 @@ class MinuteEngine:
|
|
| 147 |
self.states: dict[str, MintState] = {}
|
| 148 |
self.meta_cache: dict[str, dict] = {}; self.last_sweep = time.time(); self.last_minute: float | None = None
|
| 149 |
self.market = MarketWindow() # mkt_vol_1h over every traded mint (as mature._market_windows)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
def has_book(self, name: str) -> bool:
|
| 152 |
return any(b.name == name for b in self.books)
|
|
@@ -273,6 +284,8 @@ class MinuteEngine:
|
|
| 273 |
s.hist.append((t_end, float(a["nb"] + a["ns"]), float(a["n_traders"])))
|
| 274 |
s.flow.append(t_end, a["buy"], a["sell"], a.get("n_buyers"), a.get("wash_sol"), a.get("wash_buy_sol"), a.get("top_sell_sol"), a.get("insider_sell_sol"), a.get("skill_buy"))
|
| 275 |
f, _mask = st.features(t_end, s.meta)
|
|
|
|
|
|
|
| 276 |
ht = np.array([h[0] for h in s.hist]); hn = np.array([h[1] for h in s.hist]); htr = np.array([h[2] for h in s.hist])
|
| 277 |
for k, w in (("1m", 60), ("5m", 300), ("15m", 900), ("1h", 3600)):
|
| 278 |
f[FIDX[f"logn_{k}"]] = math.log1p(float(hn[ht > t_end - w].sum()))
|
|
@@ -298,14 +311,33 @@ class MinuteEngine:
|
|
| 298 |
"mcap": price * s.meta.supply, "fee_rate": a.get("fee_rate"), "decimals": s.decimals, "pool": s.pool, "program_label": s.program_label, "broken": s.broken,
|
| 299 |
"skill_missing": a.get("skill_buy") is None, "curve_known": bool(meta.get("curve_known")), "chain": self.spec.chain,
|
| 300 |
"quote_class": meta.get("quote_class"), "source": meta.get("source"), "seen_h": (t_end - s.first_seen) / 3600.0,
|
| 301 |
-
"creator_heavy": creator_heavy(meta.get("dev_share"))
|
|
|
|
|
|
|
| 302 |
return x, info
|
| 303 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
def eligible(self, info: dict) -> bool:
|
| 305 |
if self.rh: # the same gates in the chain's unit (x K), as training (train/decisions)
|
| 306 |
if info.get("source") == "v4" and (info.get("seen_h") or 0.0) < V4_WARM_H:
|
| 307 |
return False # its feature windows are not full yet (V4_WARM_H)
|
| 308 |
return not info["broken"] and info["resq"] is not None and info["resq"] >= self.spec.min_resq() and info["logvol_15m"] >= math.log1p(self.spec.min_vol_15m())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
return not info["broken"] and info["resq"] is not None and info["resq"] >= MIN_RESQ_SOL and info["logvol_15m"] >= math.log1p(MIN_VOL_15M_SOL)
|
| 310 |
|
| 311 |
def _stream_through(self) -> float | None:
|
|
@@ -329,7 +361,7 @@ class MinuteEngine:
|
|
| 329 |
for mint, a in agg.items():
|
| 330 |
x, info = self._features(conn, mint, a, m1_epoch)
|
| 331 |
bars[mint] = (m1_epoch - 60.0, info["open"], info["price"], info["ec"])
|
| 332 |
-
if in_universe(x[None], X_COLS)[0] and self.eligible(info):
|
| 333 |
xs.append(x); infos.append(info); mints.append(mint)
|
| 334 |
return (np.stack(xs) if xs else np.zeros((0, len(X_COLS)), np.float32)), infos, mints, bars
|
| 335 |
|
|
@@ -347,6 +379,11 @@ class MinuteEngine:
|
|
| 347 |
bars[mint] = (t1 - 60.0, info["open"], info["price"], info["ec"])
|
| 348 |
for b in self.my_books():
|
| 349 |
b.on_bars(t1 - 60.0, bars)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
log.info("warm-up: %d token-minutes over the last %d minutes (%d tokens)", n, minutes, len(self.states))
|
| 351 |
return n
|
| 352 |
|
|
@@ -426,6 +463,11 @@ class MinuteEngine:
|
|
| 426 |
log.exception("%s: minute %s failed", b.name, ctx.m1.isoformat())
|
| 427 |
record_event("error", b.name, f"minute failed: {ctx.m1.isoformat()}")
|
| 428 |
self._sweep(ctx.m1_epoch, held)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
for b in [b for b in self.my_books() if b.done]:
|
| 430 |
b.finish(); self.books.remove(b)
|
| 431 |
return out
|
|
@@ -456,7 +498,7 @@ def _status(key: str, value: dict) -> None:
|
|
| 456 |
|
| 457 |
def admit_books(engine: MinuteEngine, live: bool) -> list:
|
| 458 |
"""Books that qualify now and are not running yet (the fly first: its presence decides whether the selector trades)."""
|
| 459 |
-
from . import fly_session, handover
|
| 460 |
from .selector_session import NoModel, SelectorBook
|
| 461 |
new = []
|
| 462 |
for m in markets.enabled(): # Solana first: another chain's fly shares the Solana fly's brain
|
|
@@ -476,6 +518,14 @@ def admit_books(engine: MinuteEngine, live: bool) -> list:
|
|
| 476 |
new.append(SelectorBook(m.chain))
|
| 477 |
except NoModel as e:
|
| 478 |
_status(m.selector_status_key, {"stage": "waiting for a model", "detail": str(e), "chain": m.chain})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
return new
|
| 480 |
|
| 481 |
|
|
|
|
| 7 |
same source training uses. The full feature vector (``decisions.X_COLS``) of the rows in the models' universe
|
| 8 |
(``selector.in_universe``) that pass the eligibility gate (pool ≥ ``MIN_RESQ_SOL``, 15-minute volume ≥
|
| 9 |
``MIN_VOL_15M_SOL``, no scale break — a close 50× from the previous one or a reserve above 100,000 SOL — since the
|
| 10 |
+
mint's state began; on Solana a creator that held under ``CREATOR_HELD_MAX`` at graduation) goes to every book; each picks
|
| 11 |
+
its model's columns by name.
|
| 12 |
|
| 13 |
+
Books — ``SelectorBook`` (agent/selector_session.py), ``RisingBook`` (agent/rising_clean.py: a paper test on the selector's
|
| 14 |
+
picks) and ``FlyBook`` (agent/fly_session.py) — implement ``name``,
|
| 15 |
``on_bars(t_start, bars)`` (every processed minute, warm-up included: open, close and exit cost of every traded mint),
|
| 16 |
``on_minute(ctx)``, ``open_mints(conn)``, ``maybe_reload()``, ``done`` and ``finish()``. Each book trades inside its own
|
| 17 |
savepoint, so one book's failure never undoes the other's minute.
|
|
|
|
| 55 |
from ..market.features import FEATURES, FIDX, TokenMeta, TokenState
|
| 56 |
from ..train.corpus_meta import FEATURE_COLS as META_COLS, sol_select
|
| 57 |
from ..train.decisions import MIN_RESQ_SOL, MIN_VOL_15M_SOL, X_COLS
|
| 58 |
+
from ..train.flow import BreadthWindow, FlowWindow, MarketWindow
|
| 59 |
from ..train.mature import MASKED
|
| 60 |
from ..train.selector import creator_heavy, in_universe
|
| 61 |
+
from . import live_first, regime
|
| 62 |
|
| 63 |
log = logging.getLogger(__name__)
|
| 64 |
WARMUP_MIN = 1440 # training warms each day on the previous day's candles
|
| 65 |
V4_WARM_H = 3.0 # a plain v4 memecoin trades only once the engine has fed it this long (the longest feature window):
|
| 66 |
# it goes live mid-session without the history before (rh/v4_universe.go_live)
|
| 67 |
IDLE_EVICT_S = 86400 # training has no history for a mint idle since before the previous day
|
| 68 |
+
# A Solana coin whose creator still held this share of the supply at graduation is never bought (Bryce, 2026-11-01). Live rows
|
| 69 |
+
# only, as the DLMM filter (ingest/pumpstream.FEED_POOLS): training keeps these coins, so no model or definition changes. On
|
| 70 |
+
# the out-of-sample book they made -9.0 / -5.0 % a trade against +1.3 / +6.6 % for the rest, on real paper trades -5.8 % with
|
| 71 |
+
# 24 % rugs against +8.8 % with 9 %; on Robinhood Chain they made money with no more rugs, so the rule stops at Solana.
|
| 72 |
+
CREATOR_HELD_MAX = 0.10
|
| 73 |
STREAM_STALE_S = 180 # no entries or exits when the stream's newest complete minute is older than this
|
| 74 |
META_TTL_S = 600
|
| 75 |
MODEL_CHECK_S = 600 # how often books reload newer models and new books are admitted
|
|
|
|
| 154 |
self.states: dict[str, MintState] = {}
|
| 155 |
self.meta_cache: dict[str, dict] = {}; self.last_sweep = time.time(); self.last_minute: float | None = None
|
| 156 |
self.market = MarketWindow() # mkt_vol_1h over every traded mint (as mature._market_windows)
|
| 157 |
+
self.breadth = BreadthWindow() # the share of coins rising (agent/rising_clean.py), warm-up included
|
| 158 |
+
self.quiet = None if self.rh else regime.QuietBar() # few young coins pass the bar: old coins are not bought (agent/regime.py)
|
| 159 |
+
self.exits = regime.ExitChoice() if self.rh else None # the take-profit RH's recent trades favour (agent/regime.py)
|
| 160 |
+
self.refused: set[str] = set(); self.refused_at = -math.inf # RH: plain v4 coins refused after they went live (rh/token_check)
|
| 161 |
|
| 162 |
def has_book(self, name: str) -> bool:
|
| 163 |
return any(b.name == name for b in self.books)
|
|
|
|
| 284 |
s.hist.append((t_end, float(a["nb"] + a["ns"]), float(a["n_traders"])))
|
| 285 |
s.flow.append(t_end, a["buy"], a["sell"], a.get("n_buyers"), a.get("wash_sol"), a.get("wash_buy_sol"), a.get("top_sell_sol"), a.get("insider_sell_sol"), a.get("skill_buy"))
|
| 286 |
f, _mask = st.features(t_end, s.meta)
|
| 287 |
+
if not self.rh:
|
| 288 |
+
self.breadth.add(t_end - 60.0, resq, f[FIDX["ret_1h"]])
|
| 289 |
ht = np.array([h[0] for h in s.hist]); hn = np.array([h[1] for h in s.hist]); htr = np.array([h[2] for h in s.hist])
|
| 290 |
for k, w in (("1m", 60), ("5m", 300), ("15m", 900), ("1h", 3600)):
|
| 291 |
f[FIDX[f"logn_{k}"]] = math.log1p(float(hn[ht > t_end - w].sum()))
|
|
|
|
| 311 |
"mcap": price * s.meta.supply, "fee_rate": a.get("fee_rate"), "decimals": s.decimals, "pool": s.pool, "program_label": s.program_label, "broken": s.broken,
|
| 312 |
"skill_missing": a.get("skill_buy") is None, "curve_known": bool(meta.get("curve_known")), "chain": self.spec.chain,
|
| 313 |
"quote_class": meta.get("quote_class"), "source": meta.get("source"), "seen_h": (t_end - s.first_seen) / 3600.0,
|
| 314 |
+
"creator_heavy": creator_heavy(meta.get("dev_share")), # its buys are capped (agent/sizing.size_position)
|
| 315 |
+
"grad_hhi": meta.get("grad_hhi"), # a clean launch (agent/rising_clean.py): None, not known
|
| 316 |
+
"creator_held": meta.get("dev_hold_share")} # the creator's share at graduation (``eligible``)
|
| 317 |
return x, info
|
| 318 |
|
| 319 |
+
def _refresh_refused(self, conn, now: float) -> None:
|
| 320 |
+
"""RH: the plain v4 coins the contract check refused (rh_v4_tokens), at most every ``regime.REFRESH_S``: a coin refused
|
| 321 |
+
after it went live (a re-check found a power the first one missed) is never bought again; its minutes still price it."""
|
| 322 |
+
if not self.rh or now - self.refused_at < regime.REFRESH_S:
|
| 323 |
+
return
|
| 324 |
+
self.refused_at = now
|
| 325 |
+
try:
|
| 326 |
+
with conn.transaction():
|
| 327 |
+
self.refused = {r["token"].lower() for r in conn.execute("SELECT token FROM rh_v4_tokens WHERE status = 'refused'").fetchall()}
|
| 328 |
+
except Exception:
|
| 329 |
+
log.exception("rh: refused v4 coins: read failed; the last list stands")
|
| 330 |
+
|
| 331 |
def eligible(self, info: dict) -> bool:
|
| 332 |
if self.rh: # the same gates in the chain's unit (x K), as training (train/decisions)
|
| 333 |
if info.get("source") == "v4" and (info.get("seen_h") or 0.0) < V4_WARM_H:
|
| 334 |
return False # its feature windows are not full yet (V4_WARM_H)
|
| 335 |
return not info["broken"] and info["resq"] is not None and info["resq"] >= self.spec.min_resq() and info["logvol_15m"] >= math.log1p(self.spec.min_vol_15m())
|
| 336 |
+
held = info.get("creator_held")
|
| 337 |
+
if held is not None and held >= CREATOR_HELD_MAX:
|
| 338 |
+
return False
|
| 339 |
+
if self.quiet is not None and self.quiet.blocks(info):
|
| 340 |
+
return False
|
| 341 |
return not info["broken"] and info["resq"] is not None and info["resq"] >= MIN_RESQ_SOL and info["logvol_15m"] >= math.log1p(MIN_VOL_15M_SOL)
|
| 342 |
|
| 343 |
def _stream_through(self) -> float | None:
|
|
|
|
| 361 |
for mint, a in agg.items():
|
| 362 |
x, info = self._features(conn, mint, a, m1_epoch)
|
| 363 |
bars[mint] = (m1_epoch - 60.0, info["open"], info["price"], info["ec"])
|
| 364 |
+
if in_universe(x[None], X_COLS)[0] and self.eligible(info) and mint not in self.refused: # the models' universe (train/selector.in_universe)
|
| 365 |
xs.append(x); infos.append(info); mints.append(mint)
|
| 366 |
return (np.stack(xs) if xs else np.zeros((0, len(X_COLS)), np.float32)), infos, mints, bars
|
| 367 |
|
|
|
|
| 379 |
bars[mint] = (t1 - 60.0, info["open"], info["price"], info["ec"])
|
| 380 |
for b in self.my_books():
|
| 381 |
b.on_bars(t1 - 60.0, bars)
|
| 382 |
+
if self.quiet is not None:
|
| 383 |
+
self.quiet.refresh(conn, m1_epoch)
|
| 384 |
+
if self.exits is not None:
|
| 385 |
+
self.exits.refresh(conn, m1_epoch)
|
| 386 |
+
self._refresh_refused(conn, m1_epoch)
|
| 387 |
log.info("warm-up: %d token-minutes over the last %d minutes (%d tokens)", n, minutes, len(self.states))
|
| 388 |
return n
|
| 389 |
|
|
|
|
| 463 |
log.exception("%s: minute %s failed", b.name, ctx.m1.isoformat())
|
| 464 |
record_event("error", b.name, f"minute failed: {ctx.m1.isoformat()}")
|
| 465 |
self._sweep(ctx.m1_epoch, held)
|
| 466 |
+
if self.quiet is not None: # the next minutes' trigger, read after every order of this one
|
| 467 |
+
self.quiet.maybe_refresh(conn, ctx.m1_epoch); out["quiet_bar"] = self.quiet.status()
|
| 468 |
+
if self.exits is not None: # the next buys' take-profit, chosen after every order of this one
|
| 469 |
+
self.exits.maybe_refresh(conn, ctx.m1_epoch); out["take_profit"] = self.exits.status()
|
| 470 |
+
self._refresh_refused(conn, ctx.m1_epoch)
|
| 471 |
for b in [b for b in self.my_books() if b.done]:
|
| 472 |
b.finish(); self.books.remove(b)
|
| 473 |
return out
|
|
|
|
| 498 |
|
| 499 |
def admit_books(engine: MinuteEngine, live: bool) -> list:
|
| 500 |
"""Books that qualify now and are not running yet (the fly first: its presence decides whether the selector trades)."""
|
| 501 |
+
from . import fly_session, handover, rising_clean
|
| 502 |
from .selector_session import NoModel, SelectorBook
|
| 503 |
new = []
|
| 504 |
for m in markets.enabled(): # Solana first: another chain's fly shares the Solana fly's brain
|
|
|
|
| 518 |
new.append(SelectorBook(m.chain))
|
| 519 |
except NoModel as e:
|
| 520 |
_status(m.selector_status_key, {"stage": "waiting for a model", "detail": str(e), "chain": m.chain})
|
| 521 |
+
if not engine.has_book(rising_clean.KIND):
|
| 522 |
+
try: # a paper test never stops the runner
|
| 523 |
+
with transaction() as conn:
|
| 524 |
+
test = rising_clean.state(conn)
|
| 525 |
+
if test is not None:
|
| 526 |
+
new.append(rising_clean.RisingBook())
|
| 527 |
+
except Exception:
|
| 528 |
+
log.exception("the rising + clean book did not start")
|
| 529 |
return new
|
| 530 |
|
| 531 |
|
fly_trader/agent/paper_trading.py
CHANGED
|
@@ -3,7 +3,8 @@
|
|
| 3 |
exits: positions held their own hold (the strategy's, stored on the position) or ``horizon_s`` are sold at this minute's
|
| 4 |
price (or the last traded price: the training label's exit), and earlier a position whose minute closed at its
|
| 5 |
take-profit line (``take_profit_x``: a buy of a coin trading far below its high since graduation is sold once it doubles,
|
| 6 |
-
config.TAKE_PROFIT_X / TAKE_PROFIT_DIP; its token is not bought again before its hold would have ended)
|
|
|
|
| 7 |
entries: scores at or above the book's line open a position sized from the book's certainty bands
|
| 8 |
(``agent/sizing.py``), one position per token, unless blocked (global kill switch, paused entries, the book's own
|
| 9 |
drawdown halt, a minute traded more than ``ENTRY_MAX_LAG_S`` after its end, or the caller's ``block``); marks and wealth
|
|
@@ -27,17 +28,16 @@ import json
|
|
| 27 |
|
| 28 |
import numpy as np
|
| 29 |
|
| 30 |
-
from . import rails, sizing
|
| 31 |
from .. import config
|
| 32 |
from ..execution import ledger
|
| 33 |
from ..market.exit_cost import exit_cost_fraction
|
| 34 |
|
| 35 |
|
| 36 |
-
def
|
| 37 |
-
"""The take-profit line of a buy of ``mint`` at ``price``, a multiple of its entry price: ``config.TAKE_PROFIT_X``
|
| 38 |
when its chain takes profits (``TAKE_PROFIT_CHAINS``) and the coin trades at least ``TAKE_PROFIT_DIP`` below its high
|
| 39 |
-
since graduation (``ctx.high``: this minute's close included), else None (held its hold).
|
| 40 |
-
(``detail.take_profit_x``), so a live position of the same decision carries it too (execution/ledger.open_positions)."""
|
| 41 |
high = getattr(ctx, "high", None)
|
| 42 |
if config.TAKE_PROFIT_X <= 0 or market.chain not in config.TAKE_PROFIT_CHAINS or high is None or not price:
|
| 43 |
return None
|
|
@@ -45,6 +45,18 @@ def take_profit_x(ctx, market, mint: str, price: float | None) -> float | None:
|
|
| 45 |
return float(config.TAKE_PROFIT_X) if hi and price <= hi * (1.0 - config.TAKE_PROFIT_DIP) else None
|
| 46 |
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def at_take_profit(p: dict, price: float | None) -> bool:
|
| 49 |
"""Whether ``price`` (a minute's close) reached open position ``p``'s take-profit line."""
|
| 50 |
x, entry = p.get("take_profit_x"), float(p.get("entry_price") or 0.0)
|
|
@@ -63,7 +75,7 @@ def took_profit(conn, book: str, m1, horizon_s: float) -> set:
|
|
| 63 |
|
| 64 |
def plan_minute(ctx, *, book: str, run_id: str, beat_no: int, broker, kind: str, mints: list, infos: list, scores, threshold,
|
| 65 |
table: list | None, horizon_s: float, block: str | None = None, holds=None, strategies=None, allow=None, reasons=None,
|
| 66 |
-
tables: list | None = None, flat: bool = False, live_allow=None, size_margins=None, correctness=None) -> dict:
|
| 67 |
"""The minute's beat, exits and entries, decided and quoted but not booked: every entry and blocked pick is already a
|
| 68 |
``decisions`` row (the live mirror buys under the entry's id); ``book_minute`` books the rest.
|
| 69 |
|
|
@@ -82,7 +94,9 @@ def plan_minute(ctx, *, book: str, run_id: str, beat_no: int, broker, kind: str,
|
|
| 82 |
|
| 83 |
``size_margins`` / ``correctness`` (train/strategies.decide's ``size_margin`` / ``pclean``, per row): where the
|
| 84 |
strategy's correctness sizes a row, its band is read at that margin instead of score − line; every decision, entry and
|
| 85 |
-
rebuy carries both (``size_margin``, ``correctness``: the console's correctness %), so a live mirror sizes it the same.
|
|
|
|
|
|
|
| 86 |
conn, m1 = ctx.conn, ctx.m1
|
| 87 |
beat = conn.execute("INSERT INTO beats (run_id, ts, beat_no, n_slots_active, notes) VALUES (%s,%s,%s,%s,%s) RETURNING id",
|
| 88 |
(run_id, m1, beat_no, len(mints), json.dumps({"minute": m1.isoformat(), "mints_traded": len(ctx.agg), "book": book}))).fetchone()["id"]
|
|
@@ -91,13 +105,15 @@ def plan_minute(ctx, *, book: str, run_id: str, beat_no: int, broker, kind: str,
|
|
| 91 |
fresh = getattr(ctx, "lag_s", 0.0) <= config.ENTRY_MAX_LAG_S # a take-profit on a minute's close, never on a minutes-old one
|
| 92 |
for p in opens_before:
|
| 93 |
held = (m1 - p["opened_at"]).total_seconds(); due = held >= float(p.get("hold_s") or horizon_s)
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
continue
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
program_label=ctx.cost_label(p["mint"], p.get("program_label")), pool_fee=ctx.fee(p["mint"]))
|
| 100 |
-
exits.append((p, held, kw, broker.quote_sell(position=p, **kw), "take_profit" if tp else None))
|
| 101 |
leaving = {int(p["id"]) for p, *_ in exits}
|
| 102 |
opens_now = [p for p in opens_before if int(p["id"]) not in leaving]; open_mints = {p["mint"] for p in opens_now}
|
| 103 |
paid = {p["mint"]: float(p["cost_sol"]) for p in opens_now}; rebuy = {} # a token it keeps: what it paid, this minute's verdict on buying it again
|
|
@@ -123,8 +139,10 @@ def plan_minute(ctx, *, book: str, run_id: str, beat_no: int, broker, kind: str,
|
|
| 123 |
mg = None if size_margins is None else float(size_margins[k]); mg = mg if mg is not None and np.isfinite(mg) else None
|
| 124 |
pc = None if correctness is None else float(correctness[k]); pc = pc if pc is not None and np.isfinite(pc) else None
|
| 125 |
cx = {key: v for key, v in (("correctness", pc), ("size_margin", mg)) if v is not None} # only where correctness sizes or scores the row
|
|
|
|
|
|
|
| 126 |
size, why = sizing.size_position(float(sc), thr_k, tab_k or [], bankroll, cash, i["resq"], flat=flat, market=market, margin=mg,
|
| 127 |
-
creator_heavy=bool(i.get("creator_heavy")))
|
| 128 |
denied = allow is not None and not bool(allow[k])
|
| 129 |
if tp_held is None: # this minute's own take-profits are not booked yet
|
| 130 |
tp_held = took_profit(conn, book, m1, horizon_s) | {p["mint"] for p, _, _, _, why in exits if why == "take_profit"}
|
|
@@ -147,7 +165,8 @@ def plan_minute(ctx, *, book: str, run_id: str, beat_no: int, broker, kind: str,
|
|
| 147 |
did = conn.execute("INSERT INTO decisions (beat_id, run_id, ts, mint, pool, kind, m_hat, size_sol, forced, reason, book_targets, detail) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,false,%s,%s,%s) RETURNING id",
|
| 148 |
(beat, run_id, m1, m, i["pool"], f"{kind}_enter", float(sc), size, f"live only ({rail} binds {book}): {strat_k + ': ' if strat_k else ''}score {sc:.3f} ≥ {thr_k:.3f}; {why}", None,
|
| 149 |
json.dumps({"book": book, "live_only": True, "score": float(sc), "threshold": thr_k, "size_sol": size, "sizing": why, "resq": i["resq"], "age_h": i["age_h"],
|
| 150 |
-
"price": i["price"], "strategy": strat_k, "hold_s": hold_k, "take_profit_x": tp_k,
|
|
|
|
| 151 |
q = broker.quote_buy(size_sol=size, price=i["price"], res_quote_sol=i["resq"], mcap_sol=i["mcap"], decimals=i["decimals"],
|
| 152 |
program_label=ctx.cost_label(m, i["program_label"]), pool_fee=ctx.fee(m))
|
| 153 |
if q.ok:
|
|
@@ -161,7 +180,8 @@ def plan_minute(ctx, *, book: str, run_id: str, beat_no: int, broker, kind: str,
|
|
| 161 |
did = conn.execute("INSERT INTO decisions (beat_id, run_id, ts, mint, pool, kind, m_hat, size_sol, forced, reason, book_targets, detail) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,false,%s,%s,%s) RETURNING id",
|
| 162 |
(beat, run_id, m1, m, i["pool"], f"{kind}_enter", float(sc), size, f"{strat_k + ': ' if strat_k else ''}score {sc:.3f} ≥ {thr_k:.3f}; {why}", [book],
|
| 163 |
json.dumps({"book": book, "score": float(sc), "threshold": thr_k, "size_sol": size, "sizing": why, "resq": i["resq"], "age_h": i["age_h"], "price": i["price"],
|
| 164 |
-
"strategy": strat_k, "hold_s": hold_k, "take_profit_x": tp_k,
|
|
|
|
| 165 |
args = dict(size_sol=size, price=i["price"], res_quote_sol=i["resq"], mcap_sol=i["mcap"], decimals=i["decimals"],
|
| 166 |
program_label=ctx.cost_label(m, i["program_label"]), pool_fee=ctx.fee(m))
|
| 167 |
q = broker.quote_buy(**args)
|
|
@@ -183,6 +203,7 @@ def book_minute(ctx, plan: dict) -> dict:
|
|
| 183 |
n_exit = 0
|
| 184 |
for p, held, kw, _, why in plan["exits"]:
|
| 185 |
reason = (f"took profit: {kw['price'] / float(p['entry_price']) - 1:+.0%} over its entry (its ×{float(p['take_profit_x']):g} line)" if why == "take_profit"
|
|
|
|
| 186 |
else f"held {held/60:.0f} min")
|
| 187 |
did = conn.execute("INSERT INTO decisions (beat_id, run_id, ts, mint, pool, kind, size_sol, forced, reason, detail) VALUES (%s,%s,%s,%s,%s,%s,%s,false,%s,%s) RETURNING id",
|
| 188 |
(beat, plan["run_id"], m1, p["mint"], p["pool"], f"{plan['kind']}_exit", float(p["cost_sol"]), reason, json.dumps({"book": book}))).fetchone()["id"]
|
|
|
|
| 3 |
exits: positions held their own hold (the strategy's, stored on the position) or ``horizon_s`` are sold at this minute's
|
| 4 |
price (or the last traded price: the training label's exit), and earlier a position whose minute closed at its
|
| 5 |
take-profit line (``take_profit_x``: a buy of a coin trading far below its high since graduation is sold once it doubles,
|
| 6 |
+
config.TAKE_PROFIT_X / TAKE_PROFIT_DIP; its token is not bought again before its hold would have ended), and a Solana
|
| 7 |
+
position whose coin fell 25 % under its 5-minute high after its entry at that crash's price (agent/crash_exit.py);
|
| 8 |
entries: scores at or above the book's line open a position sized from the book's certainty bands
|
| 9 |
(``agent/sizing.py``), one position per token, unless blocked (global kill switch, paused entries, the book's own
|
| 10 |
drawdown halt, a minute traded more than ``ENTRY_MAX_LAG_S`` after its end, or the caller's ``block``); marks and wealth
|
|
|
|
| 28 |
|
| 29 |
import numpy as np
|
| 30 |
|
| 31 |
+
from . import crash_exit, rails, sizing
|
| 32 |
from .. import config
|
| 33 |
from ..execution import ledger
|
| 34 |
from ..market.exit_cost import exit_cost_fraction
|
| 35 |
|
| 36 |
|
| 37 |
+
def take_profit_base(ctx, market, mint: str, price: float | None) -> float | None:
|
| 38 |
+
"""The base take-profit line of a buy of ``mint`` at ``price``, a multiple of its entry price: ``config.TAKE_PROFIT_X``
|
| 39 |
when its chain takes profits (``TAKE_PROFIT_CHAINS``) and the coin trades at least ``TAKE_PROFIT_DIP`` below its high
|
| 40 |
+
since graduation (``ctx.high``: this minute's close included), else None (held its hold)."""
|
|
|
|
| 41 |
high = getattr(ctx, "high", None)
|
| 42 |
if config.TAKE_PROFIT_X <= 0 or market.chain not in config.TAKE_PROFIT_CHAINS or high is None or not price:
|
| 43 |
return None
|
|
|
|
| 45 |
return float(config.TAKE_PROFIT_X) if hi and price <= hi * (1.0 - config.TAKE_PROFIT_DIP) else None
|
| 46 |
|
| 47 |
|
| 48 |
+
def take_profit_x(ctx, market, mint: str, price: float | None) -> float | None:
|
| 49 |
+
"""The take-profit line a buy takes: the base rule's (``take_profit_base``), or on Robinhood Chain the line its recent
|
| 50 |
+
trades favour (agent/regime.ExitChoice, ``ctx.engine.exits``) when one beat the base rule. Stored on the entry decision
|
| 51 |
+
(``detail.take_profit_x``, the base rule's beside it as ``tp_base_x``), so a live position of the same decision carries
|
| 52 |
+
it too (execution/ledger.open_positions)."""
|
| 53 |
+
base = take_profit_base(ctx, market, mint, price)
|
| 54 |
+
choice = getattr(getattr(ctx, "engine", None), "exits", None)
|
| 55 |
+
if choice is None or config.TAKE_PROFIT_X <= 0 or market.chain not in config.TAKE_PROFIT_CHAINS or not price:
|
| 56 |
+
return base
|
| 57 |
+
return choice.line(base)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
def at_take_profit(p: dict, price: float | None) -> bool:
|
| 61 |
"""Whether ``price`` (a minute's close) reached open position ``p``'s take-profit line."""
|
| 62 |
x, entry = p.get("take_profit_x"), float(p.get("entry_price") or 0.0)
|
|
|
|
| 75 |
|
| 76 |
def plan_minute(ctx, *, book: str, run_id: str, beat_no: int, broker, kind: str, mints: list, infos: list, scores, threshold,
|
| 77 |
table: list | None, horizon_s: float, block: str | None = None, holds=None, strategies=None, allow=None, reasons=None,
|
| 78 |
+
tables: list | None = None, flat: bool = False, live_allow=None, size_margins=None, correctness=None, marks: list | None = None) -> dict:
|
| 79 |
"""The minute's beat, exits and entries, decided and quoted but not booked: every entry and blocked pick is already a
|
| 80 |
``decisions`` row (the live mirror buys under the entry's id); ``book_minute`` books the rest.
|
| 81 |
|
|
|
|
| 94 |
|
| 95 |
``size_margins`` / ``correctness`` (train/strategies.decide's ``size_margin`` / ``pclean``, per row): where the
|
| 96 |
strategy's correctness sizes a row, its band is read at that margin instead of score − line; every decision, entry and
|
| 97 |
+
rebuy carries both (``size_margin``, ``correctness``: the console's correctness %), so a live mirror sizes it the same.
|
| 98 |
+
|
| 99 |
+
``marks`` (per row, a dict): carried the same way; its ``stake_x`` multiplies the row's stake (agent/rising_clean.py)."""
|
| 100 |
conn, m1 = ctx.conn, ctx.m1
|
| 101 |
beat = conn.execute("INSERT INTO beats (run_id, ts, beat_no, n_slots_active, notes) VALUES (%s,%s,%s,%s,%s) RETURNING id",
|
| 102 |
(run_id, m1, beat_no, len(mints), json.dumps({"minute": m1.isoformat(), "mints_traded": len(ctx.agg), "book": book}))).fetchone()["id"]
|
|
|
|
| 105 |
fresh = getattr(ctx, "lag_s", 0.0) <= config.ENTRY_MAX_LAG_S # a take-profit on a minute's close, never on a minutes-old one
|
| 106 |
for p in opens_before:
|
| 107 |
held = (m1 - p["opened_at"]).total_seconds(); due = held >= float(p.get("hold_s") or horizon_s)
|
| 108 |
+
crash = None if due else crash_exit.WATCH.crashed(p["mint"], p["opened_at"]) # sold at the crash's price (agent/crash_exit.py)
|
| 109 |
+
tp = not due and crash is None and fresh and at_take_profit(p, ctx.prices.get(p["mint"]))
|
| 110 |
+
if not (due or tp or crash):
|
| 111 |
continue
|
| 112 |
+
sell_px = crash[1] if crash else ctx.prices.get(p["mint"])
|
| 113 |
+
px = sell_px or float(p.get("last_mark_price") or p["entry_price"])
|
| 114 |
+
kw = dict(price=sell_px, res_quote_sol=ctx.resqs.get(p["mint"]) or ctx.last_resq(p["mint"]), mcap_sol=ctx.mcap(p["mint"], px),
|
| 115 |
program_label=ctx.cost_label(p["mint"], p.get("program_label")), pool_fee=ctx.fee(p["mint"]))
|
| 116 |
+
exits.append((p, held, kw, broker.quote_sell(position=p, **kw), "crash" if crash else "take_profit" if tp else None))
|
| 117 |
leaving = {int(p["id"]) for p, *_ in exits}
|
| 118 |
opens_now = [p for p in opens_before if int(p["id"]) not in leaving]; open_mints = {p["mint"] for p in opens_now}
|
| 119 |
paid = {p["mint"]: float(p["cost_sol"]) for p in opens_now}; rebuy = {} # a token it keeps: what it paid, this minute's verdict on buying it again
|
|
|
|
| 139 |
mg = None if size_margins is None else float(size_margins[k]); mg = mg if mg is not None and np.isfinite(mg) else None
|
| 140 |
pc = None if correctness is None else float(correctness[k]); pc = pc if pc is not None and np.isfinite(pc) else None
|
| 141 |
cx = {key: v for key, v in (("correctness", pc), ("size_margin", mg)) if v is not None} # only where correctness sizes or scores the row
|
| 142 |
+
if marks is not None:
|
| 143 |
+
cx.update(marks[k])
|
| 144 |
size, why = sizing.size_position(float(sc), thr_k, tab_k or [], bankroll, cash, i["resq"], flat=flat, market=market, margin=mg,
|
| 145 |
+
creator_heavy=bool(i.get("creator_heavy")), stake_x=cx.get("stake_x", 1.0))
|
| 146 |
denied = allow is not None and not bool(allow[k])
|
| 147 |
if tp_held is None: # this minute's own take-profits are not booked yet
|
| 148 |
tp_held = took_profit(conn, book, m1, horizon_s) | {p["mint"] for p, _, _, _, why in exits if why == "take_profit"}
|
|
|
|
| 165 |
did = conn.execute("INSERT INTO decisions (beat_id, run_id, ts, mint, pool, kind, m_hat, size_sol, forced, reason, book_targets, detail) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,false,%s,%s,%s) RETURNING id",
|
| 166 |
(beat, run_id, m1, m, i["pool"], f"{kind}_enter", float(sc), size, f"live only ({rail} binds {book}): {strat_k + ': ' if strat_k else ''}score {sc:.3f} ≥ {thr_k:.3f}; {why}", None,
|
| 167 |
json.dumps({"book": book, "live_only": True, "score": float(sc), "threshold": thr_k, "size_sol": size, "sizing": why, "resq": i["resq"], "age_h": i["age_h"],
|
| 168 |
+
"price": i["price"], "strategy": strat_k, "hold_s": hold_k, "take_profit_x": tp_k,
|
| 169 |
+
"tp_base_x": take_profit_base(ctx, market, m, i["price"]), **cx}))).fetchone()["id"]
|
| 170 |
q = broker.quote_buy(size_sol=size, price=i["price"], res_quote_sol=i["resq"], mcap_sol=i["mcap"], decimals=i["decimals"],
|
| 171 |
program_label=ctx.cost_label(m, i["program_label"]), pool_fee=ctx.fee(m))
|
| 172 |
if q.ok:
|
|
|
|
| 180 |
did = conn.execute("INSERT INTO decisions (beat_id, run_id, ts, mint, pool, kind, m_hat, size_sol, forced, reason, book_targets, detail) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,false,%s,%s,%s) RETURNING id",
|
| 181 |
(beat, run_id, m1, m, i["pool"], f"{kind}_enter", float(sc), size, f"{strat_k + ': ' if strat_k else ''}score {sc:.3f} ≥ {thr_k:.3f}; {why}", [book],
|
| 182 |
json.dumps({"book": book, "score": float(sc), "threshold": thr_k, "size_sol": size, "sizing": why, "resq": i["resq"], "age_h": i["age_h"], "price": i["price"],
|
| 183 |
+
"strategy": strat_k, "hold_s": hold_k, "take_profit_x": tp_k, "tp_base_x": take_profit_base(ctx, market, m, i["price"]),
|
| 184 |
+
**cx}))).fetchone()["id"]
|
| 185 |
args = dict(size_sol=size, price=i["price"], res_quote_sol=i["resq"], mcap_sol=i["mcap"], decimals=i["decimals"],
|
| 186 |
program_label=ctx.cost_label(m, i["program_label"]), pool_fee=ctx.fee(m))
|
| 187 |
q = broker.quote_buy(**args)
|
|
|
|
| 203 |
n_exit = 0
|
| 204 |
for p, held, kw, _, why in plan["exits"]:
|
| 205 |
reason = (f"took profit: {kw['price'] / float(p['entry_price']) - 1:+.0%} over its entry (its ×{float(p['take_profit_x']):g} line)" if why == "take_profit"
|
| 206 |
+
else f"crash: sold at {kw['price'] / float(p['entry_price']) - 1:+.0%} from its entry, 25% under its 5-minute high" if why == "crash"
|
| 207 |
else f"held {held/60:.0f} min")
|
| 208 |
did = conn.execute("INSERT INTO decisions (beat_id, run_id, ts, mint, pool, kind, size_sol, forced, reason, detail) VALUES (%s,%s,%s,%s,%s,%s,%s,false,%s,%s) RETURNING id",
|
| 209 |
(beat, plan["run_id"], m1, p["mint"], p["pool"], f"{plan['kind']}_exit", float(p["cost_sol"]), reason, json.dumps({"book": book}))).fetchone()["id"]
|
fly_trader/agent/regime.py
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Rules for the market we are in, switched on by a trigger read from closed data (Bryce, 2026-11-03: "there are rules that need
|
| 2 |
+
to be applied now that don't need to be applied earlier in the year. Discover those rules and what the proper triggers for them
|
| 3 |
+
should be"; the study is data/scratch/regime_20261103).
|
| 4 |
+
|
| 5 |
+
Quiet bar, old coin (Solana): while few young coins pass the selector's bar, a coin at least ``OLD_H`` hours past its graduation
|
| 6 |
+
is not bought. The trigger is the selector's own pass rate: the buys it allowed (``fly_scored.teacher_allow``, one per coin per
|
| 7 |
+
240 minutes, as the book trades them) of coins younger than ``OLD_H``, per day over the last ``SHORT_D`` days against the last
|
| 8 |
+
``LONG_D``; quiet when log((short + 0.5) / (long + 0.5)) <= ``QUIET_LOG``. Counting young coins only keeps the rule out of its
|
| 9 |
+
own trigger. With under ``MIN_LONG_D`` days of history, or a failed read, the market is not quiet and the rule stands aside.
|
| 10 |
+
- Measured on #640's out-of-sample Solana book under today's rules, 05-10..11-02, at deployed stakes: the old coins it skips
|
| 11 |
+
averaged -3.5 % a trade against +10.5 % for the rest bought in quiet spells; it helped in 8 of the 9 half-months it acted in.
|
| 12 |
+
Skipping the same coins at every pass rate loses money: the rule belongs to quiet spells. On Robinhood Chain it does not help.
|
| 13 |
+
- On the live wallet since 10-17 the same rule (with the study's first cut) would have skipped 6 buys that lost 0.281 SOL,
|
| 14 |
+
both 7rTNgVdQ buys among them.
|
| 15 |
+
Live money first (CLAUDE.md): the rate is read in the paper phase, at most every ``REFRESH_S`` (one indexed query, ~0.35 s),
|
| 16 |
+
after the wallet's orders; ``MinuteEngine.eligible`` reads the cached flag.
|
| 17 |
+
|
| 18 |
+
The take-profit its recent trades favour (Robinhood Chain): ``ExitChoice``, below.
|
| 19 |
+
"""
|
| 20 |
+
from __future__ import annotations
|
| 21 |
+
|
| 22 |
+
import logging
|
| 23 |
+
import math
|
| 24 |
+
|
| 25 |
+
from ..db.apilog import record_event
|
| 26 |
+
|
| 27 |
+
log = logging.getLogger(__name__)
|
| 28 |
+
OLD_H = 20.4 # hours past graduation: the rule's old coin, and the young coins its trigger counts are younger
|
| 29 |
+
SHORT_D, LONG_D, MIN_LONG_D = 3, 30, 14
|
| 30 |
+
QUIET_LOG = -0.230 # the lowest fifth of the study's history before 10-02 (the book's own young picks)
|
| 31 |
+
ENTRY_S = 240 * 60.0 # one buy per coin per hold, as the book trades the selector's picks
|
| 32 |
+
REFRESH_S = 600
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def pass_rates(rows, now: float) -> tuple[float, float, float] | None:
|
| 36 |
+
"""(young buys a day over ``SHORT_D`` days, over ``LONG_D`` days or the history there is, the log ratio) from ``rows``
|
| 37 |
+
of (mint, decision time, graduation time) the selector allowed; None with under ``MIN_LONG_D`` days of history."""
|
| 38 |
+
by: dict[str, list[float]] = {}
|
| 39 |
+
for mint, t, g in rows:
|
| 40 |
+
if g is not None and t <= now and (t - g) / 3600.0 < OLD_H:
|
| 41 |
+
by.setdefault(mint, []).append(t)
|
| 42 |
+
if not by:
|
| 43 |
+
return None
|
| 44 |
+
entries = []
|
| 45 |
+
for ts in by.values():
|
| 46 |
+
last = -math.inf
|
| 47 |
+
for t in sorted(ts):
|
| 48 |
+
if t >= last + ENTRY_S:
|
| 49 |
+
entries.append(t); last = t
|
| 50 |
+
days = min(float(LONG_D), (now - min(entries)) / 86400.0)
|
| 51 |
+
if days < MIN_LONG_D:
|
| 52 |
+
return None
|
| 53 |
+
short = sum(1 for t in entries if t >= now - SHORT_D * 86400.0) / SHORT_D
|
| 54 |
+
long_ = sum(1 for t in entries if t >= now - days * 86400.0) / days
|
| 55 |
+
return short, long_, math.log((short + 0.5) / (long_ + 0.5))
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
class QuietBar:
|
| 59 |
+
"""The trigger's last reading; ``blocks`` is the rule."""
|
| 60 |
+
__slots__ = ("short", "long", "ratio", "quiet", "at")
|
| 61 |
+
|
| 62 |
+
def __init__(self):
|
| 63 |
+
self.short = self.long = self.ratio = None; self.quiet = False; self.at = -math.inf
|
| 64 |
+
|
| 65 |
+
def refresh(self, conn, now: float) -> None:
|
| 66 |
+
self.at = now
|
| 67 |
+
try:
|
| 68 |
+
with conn.transaction(): # a savepoint: a failed read never aborts the minute's transaction
|
| 69 |
+
rows = conn.execute("""SELECT f.mint, extract(epoch from f.ts)::float8 AS t, extract(epoch from cm.graduated_at)::float8 AS g
|
| 70 |
+
FROM fly_scored f JOIN corpus_meta cm ON cm.mint = f.mint
|
| 71 |
+
WHERE f.chain = 'sol' AND f.teacher_allow AND f.ts > to_timestamp(%s) AND f.ts <= to_timestamp(%s)""",
|
| 72 |
+
(now - (LONG_D + 1) * 86400.0, now)).fetchall()
|
| 73 |
+
r = pass_rates([(x["mint"], x["t"], x["g"]) for x in rows], now)
|
| 74 |
+
except Exception:
|
| 75 |
+
log.exception("quiet-bar trigger: read failed; the rule stands aside")
|
| 76 |
+
r = None
|
| 77 |
+
was = self.quiet
|
| 78 |
+
self.short, self.long, self.ratio = r if r is not None else (None, None, None)
|
| 79 |
+
self.quiet = r is not None and r[2] <= QUIET_LOG
|
| 80 |
+
if self.quiet != was: # the console's bell shows each switch
|
| 81 |
+
msg = (f"quiet bar {'on: Solana coins at least ' + format(OLD_H, 'g') + ' h old are not bought' if self.quiet else 'off'}; young buys a day "
|
| 82 |
+
f"{self.short or 0.0:.2f} over {SHORT_D} d against {self.long or 0.0:.2f} (log ratio {self.ratio if self.ratio is not None else float('nan'):+.3f}, trigger {QUIET_LOG:+.3f})")
|
| 83 |
+
log.info(msg); record_event("info", "regime", msg, self.status())
|
| 84 |
+
|
| 85 |
+
def maybe_refresh(self, conn, now: float) -> None:
|
| 86 |
+
if now - self.at >= REFRESH_S:
|
| 87 |
+
self.refresh(conn, now)
|
| 88 |
+
|
| 89 |
+
def blocks(self, info: dict) -> bool:
|
| 90 |
+
age = info.get("age_h")
|
| 91 |
+
return self.quiet and age is not None and age >= OLD_H
|
| 92 |
+
|
| 93 |
+
def status(self) -> dict:
|
| 94 |
+
return {"quiet": self.quiet, "young_buys_3d": self.short, "young_buys_30d": self.long, "log_ratio": self.ratio, "trigger": QUIET_LOG, "old_h": OLD_H}
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
# ---------------------------------------------------------------- Robinhood Chain: the take-profit its recent trades favour
|
| 98 |
+
# Each new RH buy takes the take-profit line that would have done best on the selector book's trades closed in the last
|
| 99 |
+
# ``EXIT_WINDOW_D`` days: the base rule (agent/paper_trading.take_profit_base: x2 on a coin bought far below its high, else
|
| 100 |
+
# none) or a sale at +20 / +30 / +50 % on any coin. A line replaces the base rule only when its mean beat the base rule's over
|
| 101 |
+
# at least ``EXIT_MIN_TRADES`` trades; each line's return is replayed on the trade's own minute closes (rh_minutes) and keeps
|
| 102 |
+
# its real exit's costs as a share, so the line in use never decides its own comparison.
|
| 103 |
+
# - Measured on #640's out-of-sample RH book, 08-09..11-02 (data/scratch/regime_20261103, 14 days): +0.136 of the bankroll
|
| 104 |
+
# against the base rule's +1.598, ahead in each period (+0.039 / +0.019 / +0.078) and in every half-month it changed (4 of 4);
|
| 105 |
+
# 21 and 28 days ahead too, 7 days behind. On Solana every window loses, so Solana keeps the base rule.
|
| 106 |
+
EXIT_WINDOW_D, EXIT_MIN_TRADES, EXIT_LEVELS, EXIT_BOOK = 14, 8, (1.2, 1.3, 1.5), "paper_rh_selector"
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def exit_price(path, entry: float, x: float | None, t_end: float) -> float | None:
|
| 110 |
+
"""The close a position bought at ``entry`` would have been sold on: the first minute close at or above ``x`` × ``entry``
|
| 111 |
+
(a take-profit, sold on that close as the books do) before ``t_end``, else the last close up to ``t_end`` (its hold);
|
| 112 |
+
None without a close. ``path``: (minute start, close) in time order."""
|
| 113 |
+
last = None
|
| 114 |
+
for t, c in path:
|
| 115 |
+
if t > t_end:
|
| 116 |
+
break
|
| 117 |
+
if c is None or c <= 0:
|
| 118 |
+
continue
|
| 119 |
+
if x and c >= x * entry:
|
| 120 |
+
return c
|
| 121 |
+
last = c
|
| 122 |
+
return last
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def exit_means(rows) -> tuple[dict, int]:
|
| 126 |
+
"""Each line's mean return over ``rows`` of (realized return, entry price, line used, base line, path, hold end): every
|
| 127 |
+
line replayed on the path, scaled by the real exit (its costs kept as a share)."""
|
| 128 |
+
acc = {"base": [], **{x: [] for x in EXIT_LEVELS}}
|
| 129 |
+
for r, entry, used, base, path, t_end in rows:
|
| 130 |
+
p_used = exit_price(path, entry, used, t_end)
|
| 131 |
+
if not p_used:
|
| 132 |
+
continue
|
| 133 |
+
for k in acc:
|
| 134 |
+
p = exit_price(path, entry, base if k == "base" else k, t_end)
|
| 135 |
+
if p:
|
| 136 |
+
acc[k].append((1.0 + r) * p / p_used - 1.0)
|
| 137 |
+
n = min(len(v) for v in acc.values())
|
| 138 |
+
return {k: sum(v) / len(v) for k, v in acc.items() if v}, n
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
class ExitChoice:
|
| 142 |
+
"""The line new RH buys take (``line``), re-chosen at most every ``REFRESH_S`` in the paper phase."""
|
| 143 |
+
__slots__ = ("x", "means", "n", "at")
|
| 144 |
+
|
| 145 |
+
def __init__(self):
|
| 146 |
+
self.x = None; self.means: dict = {}; self.n = 0; self.at = -math.inf
|
| 147 |
+
|
| 148 |
+
def refresh(self, conn, now: float) -> None:
|
| 149 |
+
self.at = now; rows = []
|
| 150 |
+
try:
|
| 151 |
+
with conn.transaction(): # a savepoint: a failed read never aborts the minute's transaction
|
| 152 |
+
pos = conn.execute("""SELECT p.mint, extract(epoch from COALESCE(d.ts, p.opened_at))::float8 AS t0, p.entry_price, p.cost_sol, p.realized_sol,
|
| 153 |
+
COALESCE(p.hold_s, (d.detail->>'hold_s')::float, 14400) AS hold_s, (d.detail->>'take_profit_x')::float AS used,
|
| 154 |
+
CASE WHEN d.detail ? 'tp_base_x' THEN (d.detail->>'tp_base_x')::float
|
| 155 |
+
ELSE (d.detail->>'take_profit_x')::float END AS base -- older buys: no choice existed
|
| 156 |
+
FROM positions p LEFT JOIN decisions d ON d.id = p.entry_decision_id
|
| 157 |
+
WHERE p.book = %s AND p.status = 'closed' AND (p.forced_exit_kind IS NULL OR p.forced_exit_kind = 'take_profit')
|
| 158 |
+
AND p.closed_at > to_timestamp(%s) AND p.closed_at <= to_timestamp(%s) AND p.cost_sol > 0 AND p.entry_price > 0
|
| 159 |
+
AND p.realized_sol IS NOT NULL""", (EXIT_BOOK, now - EXIT_WINDOW_D * 86400.0, now)).fetchall()
|
| 160 |
+
for p in pos:
|
| 161 |
+
t_end = p["t0"] + float(p["hold_s"]) - 60.0
|
| 162 |
+
path = conn.execute("SELECT extract(epoch from ts)::float8 AS t, close FROM rh_minutes WHERE mint = %s AND ts >= to_timestamp(%s) AND ts <= to_timestamp(%s) ORDER BY ts",
|
| 163 |
+
(p["mint"], p["t0"], t_end)).fetchall()
|
| 164 |
+
rows.append((float(p["realized_sol"]) / float(p["cost_sol"]), float(p["entry_price"]), p["used"], p["base"],
|
| 165 |
+
[(r["t"], float(r["close"]) if r["close"] is not None else None) for r in path], t_end))
|
| 166 |
+
means, n = exit_means(rows)
|
| 167 |
+
except Exception:
|
| 168 |
+
log.exception("RH take-profit choice: read failed; new buys take the base rule")
|
| 169 |
+
means, n = {}, 0
|
| 170 |
+
was = self.x; self.means, self.n = means, n
|
| 171 |
+
best = max(EXIT_LEVELS, key=lambda k: (round(means.get(k, -math.inf), 9), k)) if means else None # a tie sells later
|
| 172 |
+
self.x = best if best is not None and n >= EXIT_MIN_TRADES and means.get(best, -math.inf) > means.get("base", math.inf) else None
|
| 173 |
+
if self.x != was: # the console's bell shows each change
|
| 174 |
+
msg = (f"RH take-profit: new buys sell at {'+' + format(100 * (self.x - 1), '.0f') + ' % on any coin' if self.x else 'the base rule (x2 on a coin bought far below its high)'}; "
|
| 175 |
+
f"over the last {EXIT_WINDOW_D} days' {n} trades: " + ", ".join(f"{'base' if k == 'base' else '+' + format(100 * (k - 1), '.0f') + ' %'} {100 * v:+.1f} %" for k, v in means.items()))
|
| 176 |
+
log.info(msg); record_event("info", "regime", msg, self.status())
|
| 177 |
+
|
| 178 |
+
def maybe_refresh(self, conn, now: float) -> None:
|
| 179 |
+
if now - self.at >= REFRESH_S:
|
| 180 |
+
self.refresh(conn, now)
|
| 181 |
+
|
| 182 |
+
def line(self, base: float | None) -> float | None:
|
| 183 |
+
return self.x if self.x is not None else base
|
| 184 |
+
|
| 185 |
+
def status(self) -> dict:
|
| 186 |
+
return {"take_profit_x": self.x, "trades": self.n, "means": {("base" if k == "base" else f"x{k:g}"): v for k, v in self.means.items()}, "window_days": EXIT_WINDOW_D}
|
fly_trader/agent/rising_clean.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Rising + clean, the winners study's strategy (2026-10-31, data/scratch/winners_20261031): a selector pick takes twice its
|
| 2 |
+
stake when the market is recovering and the coin's launch was clean, Solana only.
|
| 3 |
+
|
| 4 |
+
- rising: the share of coins up on the hour gained at least ``RISE_6H`` over 6 h (``MinuteEngine.breadth``,
|
| 5 |
+
train/flow.BreadthWindow), read from closed minutes;
|
| 6 |
+
- clean: holder concentration at graduation (``corpus_meta.grad_hhi``) at most ``CLEAN_HHI``; unknown is not clean.
|
| 7 |
+
Both cuts are the study's, set on the book's trades before 2026-09-12 and frozen: the top third of breadth changes, the
|
| 8 |
+
lowest third of concentrations.
|
| 9 |
+
|
| 10 |
+
The paper test: ``RisingBook`` trades ``paper_rising``, the selector's own rules and picks with these stakes doubled, beside
|
| 11 |
+
``paper_selector``. ``ui_settings['rising_clean']`` is the test ({started_at, go_live_at, live_since, held}; no row: no test,
|
| 12 |
+
no book), written from the console (ui/api/overview.py), so the go-live moves without a restart or an .env change.
|
| 13 |
+
|
| 14 |
+
The go-live (Bryce, 2026-11-01): at ``go_live_at`` the book judges the test once (``decide_go_live``). Its doubled trades
|
| 15 |
+
closed at a profit: the wallet's stakes on these picks double from then on (``live_since``, ``for_wallet``). Otherwise the
|
| 16 |
+
wallet stays as it is, the date is cleared and the reason kept (``held``) until the operator sets a new one. Setting or
|
| 17 |
+
clearing the date takes the wallet back to its plain stakes first (``set_go_live``), from its next buy.
|
| 18 |
+
|
| 19 |
+
Live money first (CLAUDE.md): before a go-live a live minute only counts its breadth (under 0.1 ms), the fly holds
|
| 20 |
+
``live_since`` in memory, read after each minute's live orders. After one, a minute with wallet entries reads it once more
|
| 21 |
+
before doubling them, so a cleared date binds the next buy; a failed entry is tried again at its plain stake.
|
| 22 |
+
"""
|
| 23 |
+
from __future__ import annotations
|
| 24 |
+
|
| 25 |
+
import json
|
| 26 |
+
import logging
|
| 27 |
+
from datetime import datetime, timedelta, timezone
|
| 28 |
+
|
| 29 |
+
from ..db.apilog import record_event
|
| 30 |
+
from ..market.exit_cost import label_size
|
| 31 |
+
from .selector_session import SelectorBook
|
| 32 |
+
|
| 33 |
+
log = logging.getLogger(__name__)
|
| 34 |
+
KEY, STATUS_KEY = "rising_clean", "rising_status"
|
| 35 |
+
BOOK, KIND = "paper_rising", "rising"
|
| 36 |
+
RISE_6H, CLEAN_HHI, STAKE_X = 0.027572083, 0.019701237, 2.0
|
| 37 |
+
TEST_DAYS = 14
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def state(conn, lock: bool = False) -> dict | None:
|
| 41 |
+
"""The test (ISO times; ``go_live_at`` None: no date; ``live_since`` None: the wallet is not doubling; ``held``: the last
|
| 42 |
+
go-live refused), None when no test was started. ``lock``: for a change, so the console and the book's judgement of the
|
| 43 |
+
go-live take turns and neither overwrites the other."""
|
| 44 |
+
r = conn.execute("SELECT value FROM ui_settings WHERE key = %s" + (" FOR UPDATE" if lock else ""), (KEY,)).fetchone()
|
| 45 |
+
if not r or not r["value"]:
|
| 46 |
+
return None
|
| 47 |
+
return r["value"] if isinstance(r["value"], dict) else json.loads(r["value"])
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def _write(conn, value: dict) -> None:
|
| 51 |
+
conn.execute("INSERT INTO ui_settings (key, value) VALUES (%s, %s) ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value, updated_at = now()",
|
| 52 |
+
(KEY, json.dumps(value)))
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def start(conn, now: datetime | None = None) -> dict:
|
| 56 |
+
"""The test starts: the book joins at the engine's next admission, the go-live ``TEST_DAYS`` from now."""
|
| 57 |
+
now = now or datetime.now(timezone.utc)
|
| 58 |
+
value = {"started_at": now.isoformat(), "go_live_at": (now + timedelta(days=TEST_DAYS)).isoformat(), "live_since": None, "held": None}
|
| 59 |
+
_write(conn, value)
|
| 60 |
+
record_event("info", KIND, f"rising + clean paper test started, go-live {value['go_live_at']}", value)
|
| 61 |
+
return value
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def set_go_live(conn, at: datetime | None) -> dict:
|
| 65 |
+
"""Moves the go-live, or clears it (``at`` None). Either way the wallet goes back to its plain stakes until a go-live passes."""
|
| 66 |
+
s = state(conn, lock=True)
|
| 67 |
+
if s is None:
|
| 68 |
+
raise ValueError("the rising + clean test has not started")
|
| 69 |
+
value = {**s, "go_live_at": at.isoformat() if at else None, "live_since": None, "held": None}
|
| 70 |
+
_write(conn, value)
|
| 71 |
+
record_event("warning", KIND, f"rising + clean go-live {'moved to ' + value['go_live_at'] if at else 'cleared'}",
|
| 72 |
+
{"from": s.get("go_live_at"), "to": value["go_live_at"], "was_live_since": s.get("live_since")})
|
| 73 |
+
return value
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def wallet_doubles(conn) -> bool:
|
| 77 |
+
"""Whether the wallet takes these stakes now."""
|
| 78 |
+
return bool((state(conn) or {}).get("live_since"))
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def doubled_result(conn, since: str) -> tuple[int, float]:
|
| 82 |
+
"""(closed doubled trades of the test, their realized P&L)."""
|
| 83 |
+
r = conn.execute("""SELECT count(*) AS n, COALESCE(sum(p.realized_sol), 0) AS pnl FROM positions p JOIN decisions d ON d.id = p.entry_decision_id
|
| 84 |
+
WHERE p.book = %s AND p.status = 'closed' AND p.opened_at >= %s AND (d.detail->>'stake_x')::float > 1""", (BOOK, since)).fetchone()
|
| 85 |
+
return int(r["n"]), float(r["pnl"])
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def _due(s: dict | None, now: datetime) -> bool:
|
| 89 |
+
return s is not None and not s.get("live_since") and bool(s.get("go_live_at")) and now >= datetime.fromisoformat(s["go_live_at"])
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def decide_go_live(conn, now: datetime) -> str | None:
|
| 93 |
+
"""Once ``go_live_at`` has passed, the test is judged: 'live' or 'held' (None: nothing to judge yet)."""
|
| 94 |
+
if not _due(state(conn), now): # most minutes: no lock
|
| 95 |
+
return None
|
| 96 |
+
s = state(conn, lock=True)
|
| 97 |
+
if not _due(s, now): # the console moved the date just now
|
| 98 |
+
return None
|
| 99 |
+
n, pnl = doubled_result(conn, s["started_at"])
|
| 100 |
+
detail = {"trades": n, "pnl_sol": pnl, "go_live_at": s["go_live_at"], "at": now.isoformat()}
|
| 101 |
+
if pnl > 0:
|
| 102 |
+
_write(conn, {**s, "live_since": now.isoformat(), "held": None})
|
| 103 |
+
record_event("warning", KIND, f"rising + clean goes live: the wallet doubles these stakes ({n} doubled trades made {pnl:+.4f} SOL)", detail)
|
| 104 |
+
return "live"
|
| 105 |
+
_write(conn, {**s, "go_live_at": None, "held": detail})
|
| 106 |
+
record_event("warning", KIND, f"rising + clean stays on paper: {n} doubled trades made {pnl:+.4f} SOL; set a new go-live date in the console", detail)
|
| 107 |
+
return "held"
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def qualifies(breadth_chg: float | None, grad_hhi: float | None) -> bool:
|
| 111 |
+
return breadth_chg is not None and grad_hhi is not None and breadth_chg >= RISE_6H and grad_hhi <= CLEAN_HHI
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def mark(breadth_chg: float | None, info: dict) -> dict:
|
| 115 |
+
"""What a pick of the row ``info`` carries on ``paper_rising``'s decisions (agent/paper_trading.plan_minute)."""
|
| 116 |
+
return {"stake_x": STAKE_X if qualifies(breadth_chg, info.get("grad_hhi")) else 1.0, "breadth_chg": breadth_chg, "grad_hhi": info.get("grad_hhi")}
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def for_wallet(ctx, entries: list, broker) -> int:
|
| 120 |
+
"""The minute's wallet entries that qualify take ``STAKE_X`` (agent/fly_live.py sizes them), and the most they may pay is
|
| 121 |
+
their paper fill at that size, under the label size. An entry the caps leave no room to double, or that paper would not
|
| 122 |
+
fill at that size, keeps its stake. How many doubled."""
|
| 123 |
+
chg = ctx.engine.breadth.change(ctx.t_start); n = 0
|
| 124 |
+
for e in entries:
|
| 125 |
+
i = e["info"]; plain = float(e["size_sol"])
|
| 126 |
+
if i.get("creator_heavy") or not qualifies(chg, i.get("grad_hhi")):
|
| 127 |
+
continue
|
| 128 |
+
size = min(plain * STAKE_X, label_size(i["resq"]))
|
| 129 |
+
if size <= plain:
|
| 130 |
+
continue
|
| 131 |
+
q = broker.quote_buy(size_sol=size, price=i["price"], res_quote_sol=i["resq"], mcap_sol=i["mcap"], decimals=i["decimals"],
|
| 132 |
+
program_label=ctx.cost_label(e["mint"], i["program_label"]), pool_fee=ctx.fee(e["mint"]))
|
| 133 |
+
if q.ok:
|
| 134 |
+
e["stake_x"] = STAKE_X; e["ref_price"] = size / (q.token_delta / 10 ** int(i["decimals"])); n += 1
|
| 135 |
+
return n
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
class RisingBook(SelectorBook):
|
| 139 |
+
"""``paper_rising``: a selector of its own (the book outlives ``paper_selector`` at a handover), the same picks, these
|
| 140 |
+
stakes multiplied. It judges the go-live."""
|
| 141 |
+
|
| 142 |
+
def __init__(self):
|
| 143 |
+
self.chg: float | None = None
|
| 144 |
+
super().__init__("sol", names=(KIND, BOOK, STATUS_KEY))
|
| 145 |
+
|
| 146 |
+
def _handed(self, conn, ctx) -> bool:
|
| 147 |
+
return False
|
| 148 |
+
|
| 149 |
+
def _marks(self, ctx) -> list:
|
| 150 |
+
return [mark(self.chg, i) for i in ctx.infos]
|
| 151 |
+
|
| 152 |
+
def on_minute(self, ctx) -> dict:
|
| 153 |
+
self.chg = ctx.engine.breadth.change(ctx.t_start)
|
| 154 |
+
out = super().on_minute(ctx)
|
| 155 |
+
if ctx.trade:
|
| 156 |
+
decide_go_live(ctx.conn, ctx.m1)
|
| 157 |
+
return out
|
| 158 |
+
|
| 159 |
+
def _write_status(self, conn, status: dict) -> None:
|
| 160 |
+
super()._write_status(conn, {**status, "breadth_chg": self.chg})
|
fly_trader/agent/selector_session.py
CHANGED
|
@@ -75,12 +75,13 @@ def fail_closed(groups: set, infos: list, d: dict) -> dict:
|
|
| 75 |
|
| 76 |
|
| 77 |
class SelectorBook:
|
| 78 |
-
def __init__(self, chain: str = "sol"):
|
|
|
|
| 79 |
from .. import markets
|
| 80 |
from ..train import selector as sel
|
| 81 |
self.chain = chain; self.market = markets.for_chain(chain)
|
| 82 |
-
self.name = "selector" if chain == "sol" else f"selector_{chain}"
|
| 83 |
-
self.
|
| 84 |
pin = pinned_snapshot()
|
| 85 |
self.model = sel.load_snapshot(pin) if pin is not None else sel.load_latest()
|
| 86 |
if self.model is None:
|
|
@@ -144,6 +145,14 @@ class SelectorBook:
|
|
| 144 |
d = self.model.decide(ctx.X, X_COLS, ctx.t_start)
|
| 145 |
return fail_closed(set((getattr(self.model, "metrics", None) or {}).get("groups") or []), ctx.infos, d)
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
def on_minute(self, ctx) -> dict:
|
| 148 |
conn = ctx.conn
|
| 149 |
d = self._decide(ctx)
|
|
@@ -162,13 +171,13 @@ class SelectorBook:
|
|
| 162 |
status = {**summary, "stage": "stream stale: holding (no entries or exits)", "updated_at": datetime.now(timezone.utc).isoformat()}
|
| 163 |
self._write_status(conn, status)
|
| 164 |
return status
|
| 165 |
-
handed =
|
| 166 |
self.beat_no += 1
|
| 167 |
extra = {} if d is None else {"holds": d["hold_s"], "strategies": d["strategy"], "allow": d["allow"], "reasons": d["reason"], "tables": d["tables"],
|
| 168 |
"size_margins": d.get("size_margin"), "correctness": d.get("pclean")}
|
| 169 |
st = paper_trading.trade_minute(ctx, book=self.BOOK, run_id=self.run_id, beat_no=self.beat_no, broker=self.broker, kind=self.KIND, mints=ctx.mints, infos=ctx.infos,
|
| 170 |
scores=scores, threshold=thr, table=getattr(self.model, "sizing", None), horizon_s=self.horizon_s,
|
| 171 |
-
block="handed over to the fly" if handed else None, **extra)
|
| 172 |
if handed and st["open"] == 0:
|
| 173 |
self.done = True
|
| 174 |
record_event("info", "selector", "selector retired: the fly holds the seat and the book is flat", {"run_id": self.run_id})
|
|
|
|
| 75 |
|
| 76 |
|
| 77 |
class SelectorBook:
|
| 78 |
+
def __init__(self, chain: str = "sol", names: tuple[str, str, str] | None = None):
|
| 79 |
+
"""``names``: (name, book, status key) of a book that trades the selector on its own ledger (agent/rising_clean.py)."""
|
| 80 |
from .. import markets
|
| 81 |
from ..train import selector as sel
|
| 82 |
self.chain = chain; self.market = markets.for_chain(chain)
|
| 83 |
+
self.name, self.BOOK, self.status_key = names or ("selector" if chain == "sol" else f"selector_{chain}", self.market.selector_book, self.market.selector_status_key)
|
| 84 |
+
self.KIND = self.name
|
| 85 |
pin = pinned_snapshot()
|
| 86 |
self.model = sel.load_snapshot(pin) if pin is not None else sel.load_latest()
|
| 87 |
if self.model is None:
|
|
|
|
| 145 |
d = self.model.decide(ctx.X, X_COLS, ctx.t_start)
|
| 146 |
return fail_closed(set((getattr(self.model, "metrics", None) or {}).get("groups") or []), ctx.infos, d)
|
| 147 |
|
| 148 |
+
def _handed(self, conn, ctx) -> bool:
|
| 149 |
+
"""The fly holds this chain's seat and trades: this book stops entering and exits to flat."""
|
| 150 |
+
return handover.state(conn, self.chain) is not None and ctx.engine.has_book("fly" if self.chain == "sol" else f"fly_{self.chain}")
|
| 151 |
+
|
| 152 |
+
def _marks(self, ctx) -> list | None:
|
| 153 |
+
"""Per row, what the book adds to its picks (agent/paper_trading.plan_minute's ``marks``)."""
|
| 154 |
+
return None
|
| 155 |
+
|
| 156 |
def on_minute(self, ctx) -> dict:
|
| 157 |
conn = ctx.conn
|
| 158 |
d = self._decide(ctx)
|
|
|
|
| 171 |
status = {**summary, "stage": "stream stale: holding (no entries or exits)", "updated_at": datetime.now(timezone.utc).isoformat()}
|
| 172 |
self._write_status(conn, status)
|
| 173 |
return status
|
| 174 |
+
handed = self._handed(conn, ctx)
|
| 175 |
self.beat_no += 1
|
| 176 |
extra = {} if d is None else {"holds": d["hold_s"], "strategies": d["strategy"], "allow": d["allow"], "reasons": d["reason"], "tables": d["tables"],
|
| 177 |
"size_margins": d.get("size_margin"), "correctness": d.get("pclean")}
|
| 178 |
st = paper_trading.trade_minute(ctx, book=self.BOOK, run_id=self.run_id, beat_no=self.beat_no, broker=self.broker, kind=self.KIND, mints=ctx.mints, infos=ctx.infos,
|
| 179 |
scores=scores, threshold=thr, table=getattr(self.model, "sizing", None), horizon_s=self.horizon_s,
|
| 180 |
+
block="handed over to the fly" if handed else None, marks=self._marks(ctx), **extra)
|
| 181 |
if handed and st["open"] == 0:
|
| 182 |
self.done = True
|
| 183 |
record_event("info", "selector", "selector retired: the fly holds the seat and the book is flat", {"run_id": self.run_id})
|
fly_trader/agent/sizing.py
CHANGED
|
@@ -11,7 +11,8 @@ capped at ``MAX_POSITION_FRACTION`` of the deployable bankroll, the free cash ab
|
|
| 11 |
measured at, so the book behaves the same with 5 SOL or 500: a bigger bankroll buys no bigger than the backtest did,
|
| 12 |
it only runs out of cash later. A coin whose creator bought most of its supply at launch stakes at most
|
| 13 |
``CREATOR_HEAVY_CAP_FRACTION`` of the bankroll, 0.1 SOL of 5 (they rug often; train/selector.py). Below ``MIN_POSITION_SOL`` the buy is skipped. A model without a table (trained
|
| 14 |
-
before sizing) trades the fixed ``MAX_POSITION_SOL``, under the same caps.
|
|
|
|
| 15 |
"""
|
| 16 |
from __future__ import annotations
|
| 17 |
|
|
@@ -63,29 +64,31 @@ def band_for(table: list[dict], margin: float) -> dict | None:
|
|
| 63 |
|
| 64 |
def size_position(score: float, threshold: float, table: list[dict] | None, bankroll_sol: float, cash_sol: float,
|
| 65 |
res_quote_sol: float | None, flat: bool = False, market=None, margin: float | None = None,
|
| 66 |
-
creator_heavy: bool = False) -> tuple[float, str]:
|
| 67 |
"""(size in the book's unit, why). Size 0 means skip the buy. ``flat``: ``MAX_POSITION_FRACTION`` of the deployable
|
| 68 |
bankroll on every buy that cleared the line (the fly); otherwise the Kelly band of ``table``. ``market``
|
| 69 |
(fly_trader/markets; default Solana): the gas reserve, minimum position and label size of the book's own market.
|
| 70 |
``margin``: where the strategy's correctness sizes the row (train/strategies.decide's ``size_margin``), the margin its
|
| 71 |
correctness bands are read at, P(clean) − the bands' p0, instead of score − threshold. ``creator_heavy``: the coin's
|
| 72 |
creator bought most of its supply at launch (train/selector.creator_heavy) -- at most ``CREATOR_HEAVY_CAP_FRACTION`` of
|
| 73 |
-
the bankroll."""
|
| 74 |
-
rh = market is not None and market.chain != "sol"
|
| 75 |
reserve = market.gas_reserve() if rh else config.GAS_RESERVE_SOL; free = cash_sol - reserve
|
| 76 |
min_pos = market.min_position() if rh else config.MIN_POSITION_SOL
|
| 77 |
deployable = max(0.0, bankroll_sol - reserve)
|
| 78 |
if flat:
|
| 79 |
-
size = config.MAX_POSITION_FRACTION * deployable; why = f"flat {config.MAX_POSITION_FRACTION:g} of {deployable:.2f}
|
| 80 |
elif not table:
|
| 81 |
-
size = config.MAX_POSITION_SOL * (market.k() if rh else 1.0); why = f"fixed {size:g} {
|
| 82 |
else:
|
| 83 |
by_c = margin is not None and np.isfinite(margin)
|
| 84 |
b = band_for(table, float(margin) if by_c else score - threshold)
|
| 85 |
if b is None or b["kelly"] <= 0:
|
| 86 |
return 0.0, "no edge in this correctness band" if by_c else "no edge in this score band"
|
| 87 |
size = config.KELLY_FRACTION * b["kelly"] * deployable
|
| 88 |
-
why = (f"correctness band from +{b['lo']:.3f}" if by_c else f"band from +{b['lo']:.3f} over the line") + f": Kelly {b['kelly']:.2f} × {config.KELLY_FRACTION:g} of {deployable:.2f}
|
|
|
|
|
|
|
| 89 |
# the label size is the largest buy any backtest measured: past it the edge is an extrapolation that impact eats
|
| 90 |
caps = {"bankroll cap": config.MAX_POSITION_FRACTION * deployable, "free cash": free, "label size": label_size(res_quote_sol, market if rh else None)}
|
| 91 |
if creator_heavy:
|
|
@@ -95,7 +98,7 @@ def size_position(score: float, threshold: float, table: list[dict] | None, bank
|
|
| 95 |
size = caps[limit]; why += f", capped by {limit}"
|
| 96 |
if size < min_pos:
|
| 97 |
return 0.0, why + f" → {max(size, 0.0):.6f} is below the {min_pos:g} minimum"
|
| 98 |
-
return float(size), why + f" → {size:.3f}
|
| 99 |
|
| 100 |
|
| 101 |
def simulate_bankroll(ts: np.ndarray, horizon_s, margins: np.ndarray, returns: np.ndarray, table: list[dict] | None,
|
|
|
|
| 11 |
measured at, so the book behaves the same with 5 SOL or 500: a bigger bankroll buys no bigger than the backtest did,
|
| 12 |
it only runs out of cash later. A coin whose creator bought most of its supply at launch stakes at most
|
| 13 |
``CREATOR_HEAVY_CAP_FRACTION`` of the bankroll, 0.1 SOL of 5 (they rug often; train/selector.py). Below ``MIN_POSITION_SOL`` the buy is skipped. A model without a table (trained
|
| 14 |
+
before sizing) trades the fixed ``MAX_POSITION_SOL``, under the same caps. ``stake_x`` multiplies the stake before the caps
|
| 15 |
+
(agent/rising_clean.py: a rising market and a clean launch).
|
| 16 |
"""
|
| 17 |
from __future__ import annotations
|
| 18 |
|
|
|
|
| 64 |
|
| 65 |
def size_position(score: float, threshold: float, table: list[dict] | None, bankroll_sol: float, cash_sol: float,
|
| 66 |
res_quote_sol: float | None, flat: bool = False, market=None, margin: float | None = None,
|
| 67 |
+
creator_heavy: bool = False, stake_x: float = 1.0) -> tuple[float, str]:
|
| 68 |
"""(size in the book's unit, why). Size 0 means skip the buy. ``flat``: ``MAX_POSITION_FRACTION`` of the deployable
|
| 69 |
bankroll on every buy that cleared the line (the fly); otherwise the Kelly band of ``table``. ``market``
|
| 70 |
(fly_trader/markets; default Solana): the gas reserve, minimum position and label size of the book's own market.
|
| 71 |
``margin``: where the strategy's correctness sizes the row (train/strategies.decide's ``size_margin``), the margin its
|
| 72 |
correctness bands are read at, P(clean) − the bands' p0, instead of score − threshold. ``creator_heavy``: the coin's
|
| 73 |
creator bought most of its supply at launch (train/selector.creator_heavy) -- at most ``CREATOR_HEAVY_CAP_FRACTION`` of
|
| 74 |
+
the bankroll. ``stake_x``: a multiple of the stake, under the same caps."""
|
| 75 |
+
rh = market is not None and market.chain != "sol"; unit = market.unit if rh else "SOL"
|
| 76 |
reserve = market.gas_reserve() if rh else config.GAS_RESERVE_SOL; free = cash_sol - reserve
|
| 77 |
min_pos = market.min_position() if rh else config.MIN_POSITION_SOL
|
| 78 |
deployable = max(0.0, bankroll_sol - reserve)
|
| 79 |
if flat:
|
| 80 |
+
size = config.MAX_POSITION_FRACTION * deployable; why = f"flat {config.MAX_POSITION_FRACTION:g} of {deployable:.2f} {unit}"
|
| 81 |
elif not table:
|
| 82 |
+
size = config.MAX_POSITION_SOL * (market.k() if rh else 1.0); why = f"fixed {size:g} {unit} (model has no sizing table)"
|
| 83 |
else:
|
| 84 |
by_c = margin is not None and np.isfinite(margin)
|
| 85 |
b = band_for(table, float(margin) if by_c else score - threshold)
|
| 86 |
if b is None or b["kelly"] <= 0:
|
| 87 |
return 0.0, "no edge in this correctness band" if by_c else "no edge in this score band"
|
| 88 |
size = config.KELLY_FRACTION * b["kelly"] * deployable
|
| 89 |
+
why = (f"correctness band from +{b['lo']:.3f}" if by_c else f"band from +{b['lo']:.3f} over the line") + f": Kelly {b['kelly']:.2f} × {config.KELLY_FRACTION:g} of {deployable:.2f} {unit}"
|
| 90 |
+
if stake_x != 1.0:
|
| 91 |
+
size *= stake_x; why += f" × {stake_x:g}"
|
| 92 |
# the label size is the largest buy any backtest measured: past it the edge is an extrapolation that impact eats
|
| 93 |
caps = {"bankroll cap": config.MAX_POSITION_FRACTION * deployable, "free cash": free, "label size": label_size(res_quote_sol, market if rh else None)}
|
| 94 |
if creator_heavy:
|
|
|
|
| 98 |
size = caps[limit]; why += f", capped by {limit}"
|
| 99 |
if size < min_pos:
|
| 100 |
return 0.0, why + f" → {max(size, 0.0):.6f} is below the {min_pos:g} minimum"
|
| 101 |
+
return float(size), why + f" → {size:.3f} {unit}"
|
| 102 |
|
| 103 |
|
| 104 |
def simulate_bankroll(ts: np.ndarray, horizon_s, margins: np.ndarray, returns: np.ndarray, table: list[dict] | None,
|
fly_trader/config.py
CHANGED
|
@@ -16,7 +16,7 @@ load_dotenv(REPO_ROOT / ".env", override=False)
|
|
| 16 |
|
| 17 |
SECRET_ENV_NAMES = ("HELIUS_API_KEY", "JUPITER_API_KEY", "BOT_PRIVATE_KEY", "KALSHI_API_KEY_ID", "KALSHI_PRIVATE_KEY_PATH",
|
| 18 |
"SIGNER_RPC_URL", "RH_BOT_PRIVATE_KEY", "RH_RPC_URL_LOGS", "ENVIO_API_TOKEN", "QUICKNODE_API_KEY",
|
| 19 |
-
"QUICKNODE_RH_API_KEY", "QUICKNODE_RH_API_URL", "RH_RPC_URL_TX", "STREAM_RELAY_TOKEN")
|
| 20 |
|
| 21 |
|
| 22 |
def utcnow() -> datetime:
|
|
@@ -253,11 +253,22 @@ def quicknode_rh_url() -> str | None:
|
|
| 253 |
return url if not key or key in url else url.rstrip("/") + "/" + key
|
| 254 |
|
| 255 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
# sending and reading transactions (sends, receipts, nonces, balances, eth_call, gas): every RhRpc() without an explicit URL.
|
| 257 |
-
# Never logs: QuickNode's free Discover plan
|
| 258 |
-
# RH_RPC_URL_LOGS / RH_RPC_URL;
|
| 259 |
-
|
| 260 |
-
RH_RPC_URL_TX = env_str("RH_RPC_URL_TX") or (quicknode_rh_url() if env_str("RH_RPC_URL") is None and not RH_TESTNET else None)
|
| 261 |
# block headers for timing the index (rh/blocktime.py): PublicNode serves header batches fast but refuses eth_getLogs (403),
|
| 262 |
# the official RPC serves logs but rate-limits at ~20 requests/min (measured 2026-09-28) — so each does what it does well
|
| 263 |
RH_RPC_URL_BLOCKS = env_str("RH_RPC_URL_BLOCKS", "https://robinhood-rpc.publicnode.com")
|
|
@@ -314,6 +325,7 @@ RH_V4_ENABLED = env_bool("RH_V4_ENABLED", True) # find, chec
|
|
| 314 |
RH_V4_QUOTES = tuple(a.strip().lower() for a in env_str("RH_V4_QUOTES", RH_USDG).split(",") if a.strip()) # beside ETH and WETH: a plain memecoin's quote
|
| 315 |
RH_V4_HOOKS = tuple(a.strip().lower() for a in env_str("RH_V4_HOOKS", "").split(",") if a.strip()) # hooks allowed besides none (fees outside the Swap event)
|
| 316 |
RH_V4_CHECKS_PER_SLOT = env_int("RH_V4_CHECKS_PER_SLOT", 1) # contract checks a minute (each ~10 reads of the public RPC)
|
|
|
|
| 317 |
RH_V4_BACKFILL_RPM = env_float("RH_V4_BACKFILL_RPM", 30) # fly-trader rh-v4-backfill's own HyperSync pace (the console keeps its share)
|
| 318 |
KYBER_API = env_str("KYBER_API", "https://aggregator-api.kyberswap.com/robinhood/api/v1")
|
| 319 |
KYBER_ROUTER = env_str("KYBER_ROUTER", "0x6131B5fae19EA4f9D964eAc0408E4408b66337b5") # MetaAggregationRouterV2: the only router a built Kyber tx may call
|
|
|
|
| 16 |
|
| 17 |
SECRET_ENV_NAMES = ("HELIUS_API_KEY", "JUPITER_API_KEY", "BOT_PRIVATE_KEY", "KALSHI_API_KEY_ID", "KALSHI_PRIVATE_KEY_PATH",
|
| 18 |
"SIGNER_RPC_URL", "RH_BOT_PRIVATE_KEY", "RH_RPC_URL_LOGS", "ENVIO_API_TOKEN", "QUICKNODE_API_KEY",
|
| 19 |
+
"QUICKNODE_RH_API_KEY", "QUICKNODE_RH_API_URL", "RH_RPC_URL_TX", "STREAM_RELAY_TOKEN", "ALCHEMY_API_KEY")
|
| 20 |
|
| 21 |
|
| 22 |
def utcnow() -> datetime:
|
|
|
|
| 253 |
return url if not key or key in url else url.rstrip("/") + "/" + key
|
| 254 |
|
| 255 |
|
| 256 |
+
def alchemy_rh_url() -> str | None:
|
| 257 |
+
"""The Alchemy RH endpoint, from ALCHEMY_API_KEY."""
|
| 258 |
+
key = env_str("ALCHEMY_API_KEY")
|
| 259 |
+
return f"https://robinhood-mainnet.g.alchemy.com/v2/{key}" if key else None
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
def rh_tx_url() -> str | None:
|
| 263 |
+
"""RH_RPC_URL_TX, else Alchemy, else QuickNode. Those two serve mainnet only while RH_RPC_URL is the default: a run that
|
| 264 |
+
points RH_RPC_URL elsewhere (anvil, the testnet rehearsal) sends there."""
|
| 265 |
+
return env_str("RH_RPC_URL_TX") or ((alchemy_rh_url() or quicknode_rh_url()) if env_str("RH_RPC_URL") is None and not RH_TESTNET else None)
|
| 266 |
+
|
| 267 |
+
|
| 268 |
# sending and reading transactions (sends, receipts, nonces, balances, eth_call, gas): every RhRpc() without an explicit URL.
|
| 269 |
+
# Never logs: QuickNode's free Discover plan capped eth_getLogs at 5 blocks (measured 2026-10-09), so logs stay on HyperSync /
|
| 270 |
+
# RH_RPC_URL_LOGS / RH_RPC_URL; a free plan's credits cover the trading path, not a log tail.
|
| 271 |
+
RH_RPC_URL_TX = rh_tx_url()
|
|
|
|
| 272 |
# block headers for timing the index (rh/blocktime.py): PublicNode serves header batches fast but refuses eth_getLogs (403),
|
| 273 |
# the official RPC serves logs but rate-limits at ~20 requests/min (measured 2026-09-28) — so each does what it does well
|
| 274 |
RH_RPC_URL_BLOCKS = env_str("RH_RPC_URL_BLOCKS", "https://robinhood-rpc.publicnode.com")
|
|
|
|
| 325 |
RH_V4_QUOTES = tuple(a.strip().lower() for a in env_str("RH_V4_QUOTES", RH_USDG).split(",") if a.strip()) # beside ETH and WETH: a plain memecoin's quote
|
| 326 |
RH_V4_HOOKS = tuple(a.strip().lower() for a in env_str("RH_V4_HOOKS", "").split(",") if a.strip()) # hooks allowed besides none (fees outside the Swap event)
|
| 327 |
RH_V4_CHECKS_PER_SLOT = env_int("RH_V4_CHECKS_PER_SLOT", 1) # contract checks a minute (each ~10 reads of the public RPC)
|
| 328 |
+
RH_V4_WALLET = env_bool("RH_V4_WALLET", False) # the wallet buys plain v4 coins (0: the paper books trade them, the wallet waits for the operator)
|
| 329 |
RH_V4_BACKFILL_RPM = env_float("RH_V4_BACKFILL_RPM", 30) # fly-trader rh-v4-backfill's own HyperSync pace (the console keeps its share)
|
| 330 |
KYBER_API = env_str("KYBER_API", "https://aggregator-api.kyberswap.com/robinhood/api/v1")
|
| 331 |
KYBER_ROUTER = env_str("KYBER_ROUTER", "0x6131B5fae19EA4f9D964eAc0408E4408b66337b5") # MetaAggregationRouterV2: the only router a built Kyber tx may call
|
fly_trader/ingest/pumpstream.py
CHANGED
|
@@ -19,6 +19,7 @@ pool with the most legs. Minutes are written to ``pump_minutes`` once complete:
|
|
| 19 |
minute's end has arrived (event-time watermark) or, once no trade has arrived for 15 s, by wall clock; a leg for a minute
|
| 20 |
already written is dropped (never merged into it). ``pumpstream_status.flushed_through``
|
| 21 |
publishes the newest complete minute for the engine the moment it is written (the fly's live orders wait on it).
|
|
|
|
| 22 |
|
| 23 |
Lifecycle: ``create`` events go to ``pump_events`` as they arrive; at ``migrate`` a ``corpus_meta`` row is written
|
| 24 |
with the creation facts and the creator's point-in-time history (``train/corpus_meta.creator_history``, the training
|
|
@@ -48,6 +49,7 @@ import pyarrow.parquet as pq
|
|
| 48 |
import websockets
|
| 49 |
|
| 50 |
from .. import config
|
|
|
|
| 51 |
from ..db.apilog import record_event
|
| 52 |
from ..db.connection import transaction
|
| 53 |
from ..logging_setup import setup
|
|
@@ -202,6 +204,7 @@ class Aggregator:
|
|
| 202 |
if not ok:
|
| 203 |
self.stats["dropped_band"] += 1
|
| 204 |
return
|
|
|
|
| 205 |
m = int(ts_s // 60) * 60; pid = e.get("poolId") or ""
|
| 206 |
if self.flushed_through is not None and m <= self.flushed_through.timestamp():
|
| 207 |
self.stats["dropped_late"] += 1 # its minute is written and consumed; a partial re-write would corrupt it
|
|
|
|
| 19 |
minute's end has arrived (event-time watermark) or, once no trade has arrived for 15 s, by wall clock; a leg for a minute
|
| 20 |
already written is dropped (never merged into it). ``pumpstream_status.flushed_through``
|
| 21 |
publishes the newest complete minute for the engine the moment it is written (the fly's live orders wait on it).
|
| 22 |
+
Every kept leg's price of a coin a Solana book holds also goes to the crash watcher (agent/crash_exit.py) as it arrives.
|
| 23 |
|
| 24 |
Lifecycle: ``create`` events go to ``pump_events`` as they arrive; at ``migrate`` a ``corpus_meta`` row is written
|
| 25 |
with the creation facts and the creator's point-in-time history (``train/corpus_meta.creator_history``, the training
|
|
|
|
| 49 |
import websockets
|
| 50 |
|
| 51 |
from .. import config
|
| 52 |
+
from ..agent import crash_exit
|
| 53 |
from ..db.apilog import record_event
|
| 54 |
from ..db.connection import transaction
|
| 55 |
from ..logging_setup import setup
|
|
|
|
| 204 |
if not ok:
|
| 205 |
self.stats["dropped_band"] += 1
|
| 206 |
return
|
| 207 |
+
crash_exit.WATCH.observe(mint, ts_s, p) # a held coin 25 % under its 5-minute high is sold now (agent/crash_exit.py)
|
| 208 |
m = int(ts_s // 60) * 60; pid = e.get("poolId") or ""
|
| 209 |
if self.flushed_through is not None and m <= self.flushed_through.timestamp():
|
| 210 |
self.stats["dropped_late"] += 1 # its minute is written and consumed; a partial re-write would corrupt it
|
fly_trader/markets.py
CHANGED
|
@@ -39,14 +39,15 @@ class MarketSpec:
|
|
| 39 |
_gas_reserve: str
|
| 40 |
_kill_drawdown: str
|
| 41 |
_k: str | None # config name of ETH-per-SOL, None for SOL (k = 1)
|
|
|
|
| 42 |
|
| 43 |
@property
|
| 44 |
def books(self) -> tuple[str, str, str]:
|
| 45 |
return self.selector_book, self.fly_book, self.live_book
|
| 46 |
|
| 47 |
@property
|
| 48 |
-
def paper_books(self) -> tuple[str,
|
| 49 |
-
return self.selector_book, self.fly_book
|
| 50 |
|
| 51 |
def k(self) -> float:
|
| 52 |
return 1.0 if self._k is None else float(getattr(config, self._k))
|
|
@@ -78,7 +79,8 @@ class MarketSpec:
|
|
| 78 |
SOL = MarketSpec(chain="sol", name="Solana", unit="SOL", circuit_id=SOLANA_CIRCUIT, selector_book="paper_selector", fly_book="paper_fly",
|
| 79 |
live_book="live", handover_key="handover", fly_status_key="fly_status", selector_status_key="selector_status",
|
| 80 |
stream_status_key="pumpstream_status", program_label="Pump.fun Amm", _capital="CAPITAL_SOL", _label_size="LABEL_SIZE_SOL",
|
| 81 |
-
_min_position="MIN_POSITION_SOL", _gas_reserve="GAS_RESERVE_SOL", _kill_drawdown="KILL_SWITCH_DRAWDOWN", _k=None
|
|
|
|
| 82 |
RH = MarketSpec(chain="rh", name="Robinhood Chain", unit="ETH", circuit_id=RH_CIRCUIT, selector_book="paper_rh_selector", fly_book="paper_rh_fly",
|
| 83 |
live_book="live_rh", handover_key="handover_rh", fly_status_key="fly_status_rh", selector_status_key="selector_status_rh",
|
| 84 |
stream_status_key="rh_stream_status", program_label="Pons v4", _capital="RH_CAPITAL_ETH", _label_size="RH_LABEL_SIZE_ETH",
|
|
|
|
| 39 |
_gas_reserve: str
|
| 40 |
_kill_drawdown: str
|
| 41 |
_k: str | None # config name of ETH-per-SOL, None for SOL (k = 1)
|
| 42 |
+
test_books: tuple[str, ...] = () # paper books on trial beside the race (agent/rising_clean.py)
|
| 43 |
|
| 44 |
@property
|
| 45 |
def books(self) -> tuple[str, str, str]:
|
| 46 |
return self.selector_book, self.fly_book, self.live_book
|
| 47 |
|
| 48 |
@property
|
| 49 |
+
def paper_books(self) -> tuple[str, ...]:
|
| 50 |
+
return self.selector_book, self.fly_book, *self.test_books
|
| 51 |
|
| 52 |
def k(self) -> float:
|
| 53 |
return 1.0 if self._k is None else float(getattr(config, self._k))
|
|
|
|
| 79 |
SOL = MarketSpec(chain="sol", name="Solana", unit="SOL", circuit_id=SOLANA_CIRCUIT, selector_book="paper_selector", fly_book="paper_fly",
|
| 80 |
live_book="live", handover_key="handover", fly_status_key="fly_status", selector_status_key="selector_status",
|
| 81 |
stream_status_key="pumpstream_status", program_label="Pump.fun Amm", _capital="CAPITAL_SOL", _label_size="LABEL_SIZE_SOL",
|
| 82 |
+
_min_position="MIN_POSITION_SOL", _gas_reserve="GAS_RESERVE_SOL", _kill_drawdown="KILL_SWITCH_DRAWDOWN", _k=None,
|
| 83 |
+
test_books=("paper_rising",))
|
| 84 |
RH = MarketSpec(chain="rh", name="Robinhood Chain", unit="ETH", circuit_id=RH_CIRCUIT, selector_book="paper_rh_selector", fly_book="paper_rh_fly",
|
| 85 |
live_book="live_rh", handover_key="handover_rh", fly_status_key="fly_status_rh", selector_status_key="selector_status_rh",
|
| 86 |
stream_status_key="rh_stream_status", program_label="Pons v4", _capital="RH_CAPITAL_ETH", _label_size="RH_LABEL_SIZE_ETH",
|
fly_trader/ops/reset.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
|
| 3 |
``reset_training_state`` (every runner start, ``RESET_ON_START``) wipes, after archiving them as CSV under
|
| 4 |
data/pg_archive/reset_<ts>/, what belongs to no book that must persist: positions/orders/fills and wealth marks of any
|
| 5 |
-
book but the live book
|
| 6 |
sessions' (``RACE_RUN_KINDS``), those runs, and brain snapshots that are not models (``MODEL_KINDS``). NEVER touched:
|
| 7 |
the race books (paper_selector, paper_fly) and the live book with their runs, beats and decisions; the plastic fly's
|
| 8 |
tables (fly_*) and state file; the circuit (kill switch, trip, failure count, peak wealth — only rails.reset_circuit
|
|
@@ -33,8 +33,8 @@ from ..db.connection import transaction
|
|
| 33 |
|
| 34 |
log = logging.getLogger(__name__)
|
| 35 |
|
| 36 |
-
PRESERVED_BOOKS = ("live", "paper_selector", "paper_fly", "live_rh", "paper_rh_selector", "paper_rh_fly", "paper_kalshi_taker", "paper_kalshi_maker", "live_kalshi_taker", "live_kalshi_maker")
|
| 37 |
-
RACE_RUN_KINDS = ("selector", "fly", "kalshi_fly", "selector_rh", "fly_rh")
|
| 38 |
MODEL_KINDS = ("selector", "fly_selector", "fly_plastic", "kalshi_selector", "kalshi_fly_selector", "kalshi_fly_plastic")
|
| 39 |
FLY_BOOK, FLY_RUN_KIND = "paper_fly", "fly"
|
| 40 |
FLY_BOOKS, FLY_RUN_KINDS = ("paper_fly", "paper_rh_fly"), ("fly", "fly_rh") # one brain: both chains' fly books reset together
|
|
|
|
| 2 |
|
| 3 |
``reset_training_state`` (every runner start, ``RESET_ON_START``) wipes, after archiving them as CSV under
|
| 4 |
data/pg_archive/reset_<ts>/, what belongs to no book that must persist: positions/orders/fills and wealth marks of any
|
| 5 |
+
book but the live book, the race books and a paper test's (``PRESERVED_BOOKS``), decisions and beats of runs that are not the race
|
| 6 |
sessions' (``RACE_RUN_KINDS``), those runs, and brain snapshots that are not models (``MODEL_KINDS``). NEVER touched:
|
| 7 |
the race books (paper_selector, paper_fly) and the live book with their runs, beats and decisions; the plastic fly's
|
| 8 |
tables (fly_*) and state file; the circuit (kill switch, trip, failure count, peak wealth — only rails.reset_circuit
|
|
|
|
| 33 |
|
| 34 |
log = logging.getLogger(__name__)
|
| 35 |
|
| 36 |
+
PRESERVED_BOOKS = ("live", "paper_selector", "paper_fly", "live_rh", "paper_rh_selector", "paper_rh_fly", "paper_rising", "paper_kalshi_taker", "paper_kalshi_maker", "live_kalshi_taker", "live_kalshi_maker")
|
| 37 |
+
RACE_RUN_KINDS = ("selector", "fly", "kalshi_fly", "selector_rh", "fly_rh", "rising")
|
| 38 |
MODEL_KINDS = ("selector", "fly_selector", "fly_plastic", "kalshi_selector", "kalshi_fly_selector", "kalshi_fly_plastic")
|
| 39 |
FLY_BOOK, FLY_RUN_KIND = "paper_fly", "fly"
|
| 40 |
FLY_BOOKS, FLY_RUN_KINDS = ("paper_fly", "paper_rh_fly"), ("fly", "fly_rh") # one brain: both chains' fly books reset together
|
fly_trader/ops/wallets.py
CHANGED
|
@@ -10,6 +10,7 @@ from ..chain import withdraw as sol_withdraw
|
|
| 10 |
from ..chain.cluster_guard import SigningRefused
|
| 11 |
from ..chain.keys import WalletKeyError
|
| 12 |
from ..db.connection import transaction
|
|
|
|
| 13 |
from ..logging_setup import scrub
|
| 14 |
from ..markets import MARKETS
|
| 15 |
from ..rh import guard as rh_guard, withdraw as rh_withdraw
|
|
@@ -42,6 +43,25 @@ def address(chain: str) -> str | None:
|
|
| 42 |
return r["pubkey"] if r else None
|
| 43 |
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
def _positions(book: str) -> dict:
|
| 46 |
"""The live book's open positions, valued at its last wealth mark (net of exit cost), or at cost before any mark."""
|
| 47 |
with transaction() as conn:
|
|
|
|
| 10 |
from ..chain.cluster_guard import SigningRefused
|
| 11 |
from ..chain.keys import WalletKeyError
|
| 12 |
from ..db.connection import transaction
|
| 13 |
+
from ..execution import ledger
|
| 14 |
from ..logging_setup import scrub
|
| 15 |
from ..markets import MARKETS
|
| 16 |
from ..rh import guard as rh_guard, withdraw as rh_withdraw
|
|
|
|
| 43 |
return r["pubkey"] if r else None
|
| 44 |
|
| 45 |
|
| 46 |
+
def money(spec) -> dict | None:
|
| 47 |
+
"""The live book's money from its wallet, not its trades (None before the first wealth mark): ``start`` the first
|
| 48 |
+
mark, the amount it started with, and ``realized`` what it holds with open positions at cost, plus what was taken out,
|
| 49 |
+
less ``start``."""
|
| 50 |
+
book = spec.live_book
|
| 51 |
+
with transaction() as conn:
|
| 52 |
+
m = conn.execute("SELECT wealth, positions_value, exposure FROM wealth_marks WHERE book = %s ORDER BY ts DESC LIMIT 1", (book,)).fetchone()
|
| 53 |
+
if m is None:
|
| 54 |
+
return None
|
| 55 |
+
start = float(conn.execute("SELECT wealth FROM wealth_marks WHERE book = %s ORDER BY ts LIMIT 1", (book,)).fetchone()["wealth"])
|
| 56 |
+
if spec.chain == "sol":
|
| 57 |
+
out = float(conn.execute("SELECT COALESCE(sum((detail->>'sol')::float8), 0) AS s FROM wallet_events WHERE kind = 'withdrawal' AND detail->>'status' = 'confirmed'").fetchone()["s"])
|
| 58 |
+
held = ledger.deposits_held(conn, book)
|
| 59 |
+
else:
|
| 60 |
+
out = float(conn.execute("SELECT COALESCE(sum(wei), 0) AS w FROM rh_wallet_flows WHERE kind IN ('withdrawal', 'payout')").fetchone()["w"]) / rh_withdraw.WEI
|
| 61 |
+
held = float(conn.execute("SELECT COALESCE(sum(basis_eth), 0) AS s FROM rh_base_lots WHERE status = 'open'").fetchone()["s"])
|
| 62 |
+
return {"start": start, "realized": float(m["wealth"]) - float(m["positions_value"] or 0) + float(m["exposure"] or 0) + held + out - start}
|
| 63 |
+
|
| 64 |
+
|
| 65 |
def _positions(book: str) -> dict:
|
| 66 |
"""The live book's open positions, valued at its last wealth mark (net of exit cost), or at cost before any mark."""
|
| 67 |
with transaction() as conn:
|
fly_trader/rh/curves.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Graduates' curve back-fills, off the live stream. When a Pons coin graduates, its curve trades (launch → graduation, its
|
| 2 |
+
own curve contract) give its launch facts (rh/meta.py: creator holding, bundle, holder concentration). A coin that sat weeks
|
| 3 |
+
on its curve is hundreds of ``scan.MAX_RANGE`` chunks; run inside the live index step (until 2026-11-05) one such graduation
|
| 4 |
+
held every RH minute -- and the fly -- for 40+ minutes at a time. Here a thread of the rh_stream worker takes the oldest
|
| 5 |
+
graduate not yet back-filled, one chunk per transaction (progress in ``rh_tokens.curve_through``, so a restart resumes),
|
| 6 |
+
paced so the live passes keep HyperSync's quota. rh/meta.py builds a graduate's row only once its back-fill is done; a new
|
| 7 |
+
graduate cannot trade for its first ``MIN_AGE_H`` hours anyway."""
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import logging
|
| 11 |
+
import threading
|
| 12 |
+
|
| 13 |
+
from ..db.connection import transaction
|
| 14 |
+
from . import index
|
| 15 |
+
from .rpc import logs_rpc
|
| 16 |
+
|
| 17 |
+
log = logging.getLogger(__name__)
|
| 18 |
+
PACE_S = 2.0 # between chunks
|
| 19 |
+
IDLE_S = 30.0 # when no graduate waits
|
| 20 |
+
ERROR_S = 30.0
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def next_token(conn) -> str | None:
|
| 24 |
+
r = conn.execute("""SELECT token FROM rh_tokens WHERE NOT curve_indexed AND curve IS NOT NULL AND launch_block IS NOT NULL
|
| 25 |
+
AND COALESCE(curve_until, grad_block) IS NOT NULL ORDER BY graduated_at NULLS LAST, token LIMIT 1""").fetchone()
|
| 26 |
+
return r["token"] if r else None
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def run(stop: threading.Event, rpc=None) -> None:
|
| 30 |
+
rpc = rpc or logs_rpc(); rows = 0; chunks = 0
|
| 31 |
+
while not stop.is_set():
|
| 32 |
+
try:
|
| 33 |
+
with transaction() as conn:
|
| 34 |
+
tok = next_token(conn)
|
| 35 |
+
if tok is None:
|
| 36 |
+
stop.wait(IDLE_S); continue
|
| 37 |
+
with transaction() as conn:
|
| 38 |
+
n, done = index.index_curve_chunk(conn, rpc, tok)
|
| 39 |
+
rows += n; chunks += 1
|
| 40 |
+
if done:
|
| 41 |
+
log.info("rh curve back-fill: %s done (%d chunks, %d trades)", tok, chunks, rows); rows = chunks = 0
|
| 42 |
+
except Exception as e:
|
| 43 |
+
log.warning("rh curve back-fill: %s", str(e)[:200]); stop.wait(ERROR_S); continue
|
| 44 |
+
stop.wait(PACE_S)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def start(stop: threading.Event) -> threading.Thread:
|
| 48 |
+
t = threading.Thread(target=run, args=(stop,), name="rh_curves", daemon=True)
|
| 49 |
+
t.start()
|
| 50 |
+
return t
|
fly_trader/rh/index.py
CHANGED
|
@@ -140,14 +140,19 @@ def fill_timestamps(rpc, events: list[dict], lo: int, hi: int) -> None:
|
|
| 140 |
e["ts"] = t[e["block"]]
|
| 141 |
|
| 142 |
|
| 143 |
-
def
|
| 144 |
-
"""
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
logs = _get_logs(rpc, [r["curve"]], lo, hi, [[pons.TOPIC["CurveBuy"], pons.TOPIC["CurveSell"]]])
|
| 152 |
evs = [pons.decode(x) for x in _order(logs)]; evs = [e for e in evs if e]
|
| 153 |
fill_timestamps(rpc, evs, lo, hi)
|
|
@@ -157,9 +162,9 @@ def index_curve(conn, rpc, token: str, until_block: int) -> int:
|
|
| 157 |
"VALUES (%s, %s, %s, to_timestamp(%s), %s, %s, %s, %s, %s, %s, %s, %s) ON CONFLICT DO NOTHING",
|
| 158 |
(e["block"], e["log_index"], e["tx_hash"], e["ts"], token, e["side"], trader, e["recipient"], e["quote_raw"], e["tokens_raw"], e["fee_raw"], e["tax_raw"]))
|
| 159 |
n += 1
|
| 160 |
-
|
| 161 |
-
conn.execute("UPDATE rh_tokens SET curve_indexed =
|
| 162 |
-
return n
|
| 163 |
|
| 164 |
|
| 165 |
def apply_pons(conn, rpc, events: list[dict]) -> dict:
|
|
@@ -194,7 +199,7 @@ def apply_pons(conn, rpc, events: list[dict]) -> dict:
|
|
| 194 |
conn.execute("UPDATE rh_tokens SET pool_id = %s, status = 'graduated', graduated_at = COALESCE(graduated_at, to_timestamp(%s)) WHERE token = %s",
|
| 195 |
(e["pool_id"], e["ts"], tok))
|
| 196 |
n["pools"] += 1
|
| 197 |
-
|
| 198 |
return n
|
| 199 |
|
| 200 |
|
|
|
|
| 140 |
e["ts"] = t[e["block"]]
|
| 141 |
|
| 142 |
|
| 143 |
+
def index_curve_chunk(conn, rpc, token: str) -> tuple[int, bool]:
|
| 144 |
+
"""One chunk (``scan.MAX_RANGE`` blocks) of a graduate's curve trades (its own curve contract, launch → its pool's
|
| 145 |
+
registration), from where the last chunk stopped (``rh_tokens.curve_through``); the last sets ``curve_indexed``.
|
| 146 |
+
Idempotent. Returns (rows written, done). Runs off the live stream (rh/curves.py): a coin that sat weeks on its curve
|
| 147 |
+
is hundreds of chunks, which held the live minutes for 40+ minutes while it ran inside the index step (2026-11-04)."""
|
| 148 |
+
r = conn.execute("SELECT curve, launch_block, COALESCE(curve_until, grad_block) AS until, curve_through, curve_indexed FROM rh_tokens WHERE token = %s",
|
| 149 |
+
(token,)).fetchone()
|
| 150 |
+
if not r or not r["curve"] or r["launch_block"] is None or r["until"] is None or r["curve_indexed"]:
|
| 151 |
+
return 0, True
|
| 152 |
+
router = config.PONS_ROUTER.lower(); n = 0
|
| 153 |
+
lo = int(r["launch_block"]) if r["curve_through"] is None else int(r["curve_through"]) + 1; until = int(r["until"])
|
| 154 |
+
hi = min(until, lo + scan.MAX_RANGE - 1)
|
| 155 |
+
if lo <= until:
|
| 156 |
logs = _get_logs(rpc, [r["curve"]], lo, hi, [[pons.TOPIC["CurveBuy"], pons.TOPIC["CurveSell"]]])
|
| 157 |
evs = [pons.decode(x) for x in _order(logs)]; evs = [e for e in evs if e]
|
| 158 |
fill_timestamps(rpc, evs, lo, hi)
|
|
|
|
| 162 |
"VALUES (%s, %s, %s, to_timestamp(%s), %s, %s, %s, %s, %s, %s, %s, %s) ON CONFLICT DO NOTHING",
|
| 163 |
(e["block"], e["log_index"], e["tx_hash"], e["ts"], token, e["side"], trader, e["recipient"], e["quote_raw"], e["tokens_raw"], e["fee_raw"], e["tax_raw"]))
|
| 164 |
n += 1
|
| 165 |
+
done = hi >= until
|
| 166 |
+
conn.execute("UPDATE rh_tokens SET curve_through = %s, curve_indexed = %s WHERE token = %s", (max(hi, lo - 1), done, token))
|
| 167 |
+
return n, done
|
| 168 |
|
| 169 |
|
| 170 |
def apply_pons(conn, rpc, events: list[dict]) -> dict:
|
|
|
|
| 199 |
conn.execute("UPDATE rh_tokens SET pool_id = %s, status = 'graduated', graduated_at = COALESCE(graduated_at, to_timestamp(%s)) WHERE token = %s",
|
| 200 |
(e["pool_id"], e["ts"], tok))
|
| 201 |
n["pools"] += 1
|
| 202 |
+
conn.execute("UPDATE rh_tokens SET curve_until = COALESCE(curve_until, %s) WHERE token = %s", (e["block"], tok)) # rh/curves.py back-fills it
|
| 203 |
return n
|
| 204 |
|
| 205 |
|
fly_trader/rh/live.py
CHANGED
|
@@ -265,6 +265,8 @@ class RhLiveMirror:
|
|
| 265 |
mint = e["mint"].lower()
|
| 266 |
if mint in held_mints:
|
| 267 |
skipped.append((mint, "held")); self._blocked(conn, ctx, run_id, beat_id, e, "held", "the wallet holds this token or has an order of it queued"); continue
|
|
|
|
|
|
|
| 268 |
pool = pool_of(conn, e["info"].get("pool")); quote = ((pool or {}).get("quote_asset") or ZERO).lower()
|
| 269 |
_, stale_mark = A.base_mark(conn, quote)
|
| 270 |
if stale_mark:
|
|
@@ -385,8 +387,12 @@ class RhLiveMirror:
|
|
| 385 |
rows = conn.execute("""SELECT l.realized_sol / NULLIF(l.cost_sol, 0) AS rl, p.realized_sol / NULLIF(p.cost_sol, 0) AS rp
|
| 386 |
FROM positions l JOIN positions p ON p.entry_decision_id = l.entry_decision_id AND p.book = %s AND p.status = 'closed'
|
| 387 |
WHERE l.book = %s AND l.status = 'closed' AND l.entry_decision_id IS NOT NULL AND COALESCE(l.forced_exit_kind, '') <> 'operator'
|
|
|
|
|
|
|
| 388 |
ORDER BY l.closed_at DESC LIMIT %s""",
|
| 389 |
-
(MIRROR, BOOK, GAP_TRADES)).fetchall() # an operator's Sell Now is the operator's choice, not an execution gap
|
|
|
|
|
|
|
| 390 |
pairs = [(r["rl"], r["rp"]) for r in rows if r["rl"] is not None and r["rp"] is not None]
|
| 391 |
rl = np.array([a for a, _ in pairs], dtype=np.float64); rp = np.array([b for _, b in pairs], dtype=np.float64)
|
| 392 |
out = {"trades": int(len(rl)), "live_mean": float(rl.mean()) if len(rl) else None, "mirror_mean": float(rp.mean()) if len(rp) else None}
|
|
|
|
| 265 |
mint = e["mint"].lower()
|
| 266 |
if mint in held_mints:
|
| 267 |
skipped.append((mint, "held")); self._blocked(conn, ctx, run_id, beat_id, e, "held", "the wallet holds this token or has an order of it queued"); continue
|
| 268 |
+
if e["info"].get("source") == "v4" and not config.RH_V4_WALLET:
|
| 269 |
+
skipped.append((mint, "v4 paper only")); self._blocked(conn, ctx, run_id, beat_id, e, "v4 paper only", "a plain v4 coin: the wallet waits for the operator (RH_V4_WALLET)"); continue
|
| 270 |
pool = pool_of(conn, e["info"].get("pool")); quote = ((pool or {}).get("quote_asset") or ZERO).lower()
|
| 271 |
_, stale_mark = A.base_mark(conn, quote)
|
| 272 |
if stale_mark:
|
|
|
|
| 387 |
rows = conn.execute("""SELECT l.realized_sol / NULLIF(l.cost_sol, 0) AS rl, p.realized_sol / NULLIF(p.cost_sol, 0) AS rp
|
| 388 |
FROM positions l JOIN positions p ON p.entry_decision_id = l.entry_decision_id AND p.book = %s AND p.status = 'closed'
|
| 389 |
WHERE l.book = %s AND l.status = 'closed' AND l.entry_decision_id IS NOT NULL AND COALESCE(l.forced_exit_kind, '') <> 'operator'
|
| 390 |
+
AND NOT EXISTS (SELECT 1 FROM circuit_events e WHERE e.kind = 'rh_reconcile' AND e.ts >= l.opened_at
|
| 391 |
+
AND e.ts <= COALESCE(l.closed_at, now()) AND e.detail::text ILIKE '%%' || lower(l.mint) || '%%')
|
| 392 |
ORDER BY l.closed_at DESC LIMIT %s""",
|
| 393 |
+
(MIRROR, BOOK, GAP_TRADES)).fetchall() # an operator's Sell Now is the operator's choice, not an execution gap;
|
| 394 |
+
# nor a position the reconciler found gone from the wallet while it was held (ANT 2026-11-03, 0x493f2e7b 11-05: the coin's
|
| 395 |
+
# contract took or wiped the tokens; the reconcile pause handles those, and two of them alone read as a 3.2-point gap)
|
| 396 |
pairs = [(r["rl"], r["rp"]) for r in rows if r["rl"] is not None and r["rp"] is not None]
|
| 397 |
rl = np.array([a for a, _ in pairs], dtype=np.float64); rp = np.array([b for _, b in pairs], dtype=np.float64)
|
| 398 |
out = {"trades": int(len(rl)), "live_mean": float(rl.mean()) if len(rl) else None, "mirror_mean": float(rp.mean()) if len(rp) else None}
|
fly_trader/rh/meta.py
CHANGED
|
@@ -107,8 +107,8 @@ def _history(conn, creator: str | None, since, g, mint: str) -> dict:
|
|
| 107 |
def build_v4(conn, token: str) -> dict | None:
|
| 108 |
"""rh_meta facts of a plain v4 memecoin that passed its contract check (rh/v4_universe.py): its age counts from its
|
| 109 |
first pool's initialization (``graduated_at``: the 6 h floor), its creator is that pool's initializer, and what only a
|
| 110 |
-
Pons launch records (curve, dev buy, seed reserve, time to graduate) is unknown
|
| 111 |
-
the
|
| 112 |
t = conn.execute("SELECT v.*, a.class AS qclass FROM rh_v4_tokens v LEFT JOIN rh_assets a ON a.asset = v.quote_asset WHERE v.token = %s AND v.status = 'ok'",
|
| 113 |
(token,)).fetchone()
|
| 114 |
if not t or t["first_ts"] is None:
|
|
@@ -135,7 +135,8 @@ def run_once(limit: int = 500) -> dict:
|
|
| 135 |
n_new = n_out = 0
|
| 136 |
with transaction() as conn:
|
| 137 |
todo = conn.execute("SELECT t.token FROM rh_tokens t LEFT JOIN rh_meta m ON m.mint = t.token WHERE t.status = 'graduated' AND t.graduated_at IS NOT NULL "
|
| 138 |
-
"AND m.mint IS NULL
|
|
|
|
| 139 |
for r in todo:
|
| 140 |
row = build(conn, r["token"])
|
| 141 |
if row is None:
|
|
|
|
| 107 |
def build_v4(conn, token: str) -> dict | None:
|
| 108 |
"""rh_meta facts of a plain v4 memecoin that passed its contract check (rh/v4_universe.py): its age counts from its
|
| 109 |
first pool's initialization (``graduated_at``: the 6 h floor), its creator is that pool's initializer, and what only a
|
| 110 |
+
Pons launch records (curve, dev buy, seed reserve, time to graduate) is unknown until rh/v4_facts.py finds the coin's
|
| 111 |
+
launch on the chain, and the universe refuses it until then. Supply in whole tokens (18 decimals)."""
|
| 112 |
t = conn.execute("SELECT v.*, a.class AS qclass FROM rh_v4_tokens v LEFT JOIN rh_assets a ON a.asset = v.quote_asset WHERE v.token = %s AND v.status = 'ok'",
|
| 113 |
(token,)).fetchone()
|
| 114 |
if not t or t["first_ts"] is None:
|
|
|
|
| 135 |
n_new = n_out = 0
|
| 136 |
with transaction() as conn:
|
| 137 |
todo = conn.execute("SELECT t.token FROM rh_tokens t LEFT JOIN rh_meta m ON m.mint = t.token WHERE t.status = 'graduated' AND t.graduated_at IS NOT NULL "
|
| 138 |
+
"AND m.mint IS NULL AND (t.curve_indexed OR t.curve IS NULL OR t.launch_block IS NULL OR COALESCE(t.curve_until, t.grad_block) IS NULL) "
|
| 139 |
+
"ORDER BY t.graduated_at LIMIT %s", (limit,)).fetchall() # a row is built once: after its curve back-fill (rh/curves.py)
|
| 140 |
for r in todo:
|
| 141 |
row = build(conn, r["token"])
|
| 142 |
if row is None:
|
fly_trader/rh/schema.py
CHANGED
|
@@ -132,6 +132,9 @@ RH_DDL: list[str] = [
|
|
| 132 |
"ALTER TABLE rh_minutes ADD COLUMN IF NOT EXISTS fee_v smallint",
|
| 133 |
"ALTER TABLE rh_pools ADD COLUMN IF NOT EXISTS fee_obs int NOT NULL DEFAULT 0",
|
| 134 |
"ALTER TABLE rh_tokens ADD COLUMN IF NOT EXISTS curve_indexed boolean NOT NULL DEFAULT false",
|
|
|
|
|
|
|
|
|
|
| 135 |
"ALTER TABLE rh_assets ADD COLUMN IF NOT EXISTS ref_path jsonb",
|
| 136 |
"ALTER TABLE positions ADD COLUMN IF NOT EXISTS chain text NOT NULL DEFAULT 'sol'",
|
| 137 |
"ALTER TABLE positions ADD COLUMN IF NOT EXISTS quote_asset text",
|
|
|
|
| 132 |
"ALTER TABLE rh_minutes ADD COLUMN IF NOT EXISTS fee_v smallint",
|
| 133 |
"ALTER TABLE rh_pools ADD COLUMN IF NOT EXISTS fee_obs int NOT NULL DEFAULT 0",
|
| 134 |
"ALTER TABLE rh_tokens ADD COLUMN IF NOT EXISTS curve_indexed boolean NOT NULL DEFAULT false",
|
| 135 |
+
# the graduate's curve back-fill (rh/curves.py, off the live stream): the block it runs to (the pool's registration) and how far it got
|
| 136 |
+
"ALTER TABLE rh_tokens ADD COLUMN IF NOT EXISTS curve_until bigint",
|
| 137 |
+
"ALTER TABLE rh_tokens ADD COLUMN IF NOT EXISTS curve_through bigint",
|
| 138 |
"ALTER TABLE rh_assets ADD COLUMN IF NOT EXISTS ref_path jsonb",
|
| 139 |
"ALTER TABLE positions ADD COLUMN IF NOT EXISTS chain text NOT NULL DEFAULT 'sol'",
|
| 140 |
"ALTER TABLE positions ADD COLUMN IF NOT EXISTS quote_asset text",
|
fly_trader/rh/stream.py
CHANGED
|
@@ -30,6 +30,8 @@ RPC_CATCHUP_S = 3.0 # between catch-up passes while HyperSync is dow
|
|
| 30 |
SLOW_EVERY_S = 60.0 # meta and corpus refresh with each written minute, else at least this often
|
| 31 |
_SLOW = {"at": 0.0}
|
| 32 |
_V4 = {"minute": None} # the minute whose v4 slot ran (rh/v4_universe.slot: once a minute)
|
|
|
|
|
|
|
| 33 |
_CORPUS: dict = {"proc": None} # the child building a due corpus day (rh/corpus.run_locked)
|
| 34 |
|
| 35 |
|
|
@@ -115,6 +117,8 @@ def main(stop_event: threading.Event | None = None) -> None:
|
|
| 115 |
from ..db import schema
|
| 116 |
schema.apply_schema()
|
| 117 |
rpc = logs_rpc(); stop = stop_event or threading.Event()
|
|
|
|
|
|
|
| 118 |
log.info("rh_stream: logs from %s; the head from %s", "HyperSync" if config.env_str("ENVIO_API_TOKEN") else "the RPC",
|
| 119 |
"the dedicated RPC (RH_RPC_URL_LOGS)" if config.RH_RPC_URL_LOGS else "HyperSync / the public RPC")
|
| 120 |
while not stop.is_set():
|
|
@@ -154,13 +158,13 @@ def corpus_apart() -> dict:
|
|
| 154 |
|
| 155 |
|
| 156 |
def _stop_corpus() -> None:
|
| 157 |
-
p
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
|
| 165 |
|
| 166 |
def _orders_pending() -> bool:
|
|
@@ -190,10 +194,30 @@ def v4_slot(out: dict, now: float | None = None) -> dict:
|
|
| 190 |
return {}
|
| 191 |
_V4["minute"] = m
|
| 192 |
res = v4_universe.slot(deadline=m * 60 + live_first.MID_MINUTE[1], now=t)
|
| 193 |
-
|
|
|
|
| 194 |
return res
|
| 195 |
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
def _wait_s(written_through: int | None, now: float | None = None) -> float:
|
| 198 |
"""Until the next pass: the moment the last ended minute can be written (its end + ``minutes.GRACE_S``) if it is not
|
| 199 |
yet, else the next minute's, but never longer than ``RH_STREAM_POLL_S`` (``written_through``: the end of the last
|
|
|
|
| 30 |
SLOW_EVERY_S = 60.0 # meta and corpus refresh with each written minute, else at least this often
|
| 31 |
_SLOW = {"at": 0.0}
|
| 32 |
_V4 = {"minute": None} # the minute whose v4 slot ran (rh/v4_universe.slot: once a minute)
|
| 33 |
+
_FACTS = {"proc": None, "at": 0.0} # the launch-facts child (``facts_apart``) and when it last started
|
| 34 |
+
FACTS_EVERY_S = 600.0
|
| 35 |
_CORPUS: dict = {"proc": None} # the child building a due corpus day (rh/corpus.run_locked)
|
| 36 |
|
| 37 |
|
|
|
|
| 117 |
from ..db import schema
|
| 118 |
schema.apply_schema()
|
| 119 |
rpc = logs_rpc(); stop = stop_event or threading.Event()
|
| 120 |
+
from . import curves
|
| 121 |
+
curves.start(stop) # graduates' curve back-fills, never in front of a pass
|
| 122 |
log.info("rh_stream: logs from %s; the head from %s", "HyperSync" if config.env_str("ENVIO_API_TOKEN") else "the RPC",
|
| 123 |
"the dedicated RPC (RH_RPC_URL_LOGS)" if config.RH_RPC_URL_LOGS else "HyperSync / the public RPC")
|
| 124 |
while not stop.is_set():
|
|
|
|
| 158 |
|
| 159 |
|
| 160 |
def _stop_corpus() -> None:
|
| 161 |
+
for p in (_CORPUS["proc"], _FACTS["proc"]):
|
| 162 |
+
if p is not None and p.poll() is None:
|
| 163 |
+
p.terminate() # every file is written atomically, each coin's facts in one commit: an ended child loses little
|
| 164 |
+
try:
|
| 165 |
+
p.wait(timeout=30)
|
| 166 |
+
except subprocess.TimeoutExpired:
|
| 167 |
+
p.kill()
|
| 168 |
|
| 169 |
|
| 170 |
def _orders_pending() -> bool:
|
|
|
|
| 194 |
return {}
|
| 195 |
_V4["minute"] = m
|
| 196 |
res = v4_universe.slot(deadline=m * 60 + live_first.MID_MINUTE[1], now=t)
|
| 197 |
+
fx = facts_apart()
|
| 198 |
+
_status(v4=res, v4_facts=fx or None)
|
| 199 |
return res
|
| 200 |
|
| 201 |
|
| 202 |
+
def facts_apart() -> dict:
|
| 203 |
+
"""Plain v4 coins' launch facts are found by a child process at the lowest CPU priority (``fly-trader rh-v4-facts``,
|
| 204 |
+
rh/v4_facts.py), never in this thread: one child at a time, started at most every ``FACTS_EVERY_S`` from the v4 slot, so
|
| 205 |
+
mid-minute with no live order waiting. It is the child that looks for due coins; this only starts it."""
|
| 206 |
+
p = _FACTS["proc"]
|
| 207 |
+
if p is not None and p.poll() is None:
|
| 208 |
+
return {"running": p.pid}
|
| 209 |
+
if p is not None:
|
| 210 |
+
_FACTS["proc"] = None
|
| 211 |
+
if p.returncode:
|
| 212 |
+
log.warning("rh v4 facts child %d exited %d (its log: rh_v4_facts.log)", p.pid, p.returncode)
|
| 213 |
+
if time.monotonic() - _FACTS["at"] < FACTS_EVERY_S:
|
| 214 |
+
return {}
|
| 215 |
+
_FACTS["at"] = time.monotonic()
|
| 216 |
+
nice = ["/usr/bin/nice", "-n", "19"] if os.path.exists("/usr/bin/nice") else [] # not preexec_fn: unsafe in a threaded process
|
| 217 |
+
p = _FACTS["proc"] = subprocess.Popen([*nice, sys.executable, "-m", "fly_trader", "rh-v4-facts"], stdout=subprocess.DEVNULL)
|
| 218 |
+
return {"started": p.pid}
|
| 219 |
+
|
| 220 |
+
|
| 221 |
def _wait_s(written_through: int | None, now: float | None = None) -> float:
|
| 222 |
"""Until the next pass: the moment the last ended minute can be written (its end + ``minutes.GRACE_S``) if it is not
|
| 223 |
yet, else the next minute's, but never longer than ``RH_STREAM_POLL_S`` (``written_through``: the end of the last
|
fly_trader/rh/templates.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Known-good contract templates of plain v4 coins (Bryce, 2026-11-05: "build a bytecode template for proper coins based on
|
| 2 |
+
known good launches and then integrate that as a filter which rejects any coin that deviates. We might need multiple
|
| 3 |
+
templates but if at least 5 coins w/ diff creators share the same bytecode and don't rug those are 'known good'").
|
| 4 |
+
|
| 5 |
+
A coin's template is its runtime code reduced to a skeleton (a minimal proxy's implementation in its place): the compiler's
|
| 6 |
+
CBOR metadata trailer stripped, every PUSH5..PUSH32 operand masked (addresses, immutables, constructor-set constants),
|
| 7 |
+
PUSH1..PUSH4 kept (jump targets, selectors). Coins with one skeleton were compiled from one source with other constants.
|
| 8 |
+
|
| 9 |
+
Built 2026-11-06 (data/scratch/v4_templates_20261105) from the code of 13,753 plain v4 coins (every coin listed, every coin
|
| 10 |
+
the swap scan saw trade at a tradeable age). A template is known good when:
|
| 11 |
+
- at least 5 of its coins from 5 different creators did not rug: 24 or more 15-minute buckets clearing the live floors at
|
| 12 |
+
age 6 h or more (the swap scan to 10-28, the coins' own minutes after);
|
| 13 |
+
- none of its coins is a known scam (ANT 0x36c2861a, 0x493f2e7b) or was refused by the contract check for its code
|
| 14 |
+
(upgradeable proxy, trading switch, built-in tax);
|
| 15 |
+
- its code does not delegate (an EIP-1967 proxy's bytecode says nothing of the code it runs).
|
| 16 |
+
Nine templates pass: 8,733 of the coins (63.5 %). Every plain v4 trade the books ever made (both scams and one other coin,
|
| 17 |
+
23 live and 6 paper trades) was outside them. A template that reaches the rule later is added here by a rebuild.
|
| 18 |
+
|
| 19 |
+
``check`` (rh/v4_universe.run_checks) refuses an accepted coin whose template is not known good, before the creator
|
| 20 |
+
probe; ``retemplate`` re-reads the code of coins accepted before this check and refuses those that deviate.
|
| 21 |
+
"""
|
| 22 |
+
from __future__ import annotations
|
| 23 |
+
|
| 24 |
+
import hashlib
|
| 25 |
+
|
| 26 |
+
# template (sha256 of the skeleton) -> (coins, creators, creators of coins that did not rug), as built
|
| 27 |
+
KNOWN_GOOD: dict[str, tuple[int, int, int]] = {
|
| 28 |
+
"f6bdb52a97894128aef0d23ef09150a04f0659a4531cd52cb7e55a945c1b624e": (6566, 3170, 10),
|
| 29 |
+
"49af139e742f766f92f5e0d502fd8b6b97eebf39e2a6662b703c3499faa9f400": (1679, 1287, 202),
|
| 30 |
+
"0e7adef37012d6fbc674846e564af41274f892e5df77ac672abc6da6976e1dae": (380, 232, 16),
|
| 31 |
+
"16bb4db25b3d67731d9a8cbcf551f7efa3a1ad8be030781ccb624429035eb0b3": (36, 36, 12),
|
| 32 |
+
"00962c1adc4a1286685c2950b1b18f87728acc237ed62ef3f5bc66ddf80e9f5f": (23, 10, 7),
|
| 33 |
+
"dd104d00d426716e90090bd228507d2bc3b463d8cf9afb6b5cdd46f28b56675b": (18, 9, 6),
|
| 34 |
+
"13c9e24213c0be99d8c5a21bd07e3e5234bc815429a5817003185400fd3ac9f7": (12, 11, 5),
|
| 35 |
+
"db5d484c62c275f6671246bd6e6023ef86ab2758060adaee8c47942155e1a554": (12, 12, 5),
|
| 36 |
+
"7667934fcd575b13a87627f540231b8ab7b03c174b0a2d959ec47ba9ef73e38d": (7, 7, 5),
|
| 37 |
+
}
|
| 38 |
+
DEVIATES = "its contract matches no known-good template"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def strip_meta(b: bytes) -> bytes:
|
| 42 |
+
"""``b`` without the compiler's CBOR metadata trailer (its length in the last two bytes), if it has one."""
|
| 43 |
+
if len(b) > 2:
|
| 44 |
+
n = int.from_bytes(b[-2:], "big")
|
| 45 |
+
if 0 < n < len(b) - 2 and b[-n - 2] in (0xa1, 0xa2, 0xa3):
|
| 46 |
+
return b[:-n - 2]
|
| 47 |
+
return b
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def skeleton(b: bytes) -> bytes:
|
| 51 |
+
"""The opcodes of ``b`` with PUSH1..PUSH4 operands kept and PUSH5..PUSH32 operands dropped."""
|
| 52 |
+
out = bytearray(); i = 0
|
| 53 |
+
while i < len(b):
|
| 54 |
+
op = b[i]; out.append(op)
|
| 55 |
+
if 0x60 <= op <= 0x7f:
|
| 56 |
+
n = op - 0x5f
|
| 57 |
+
if n <= 4:
|
| 58 |
+
out += b[i + 1:i + 1 + n]
|
| 59 |
+
i += 1 + n
|
| 60 |
+
else:
|
| 61 |
+
i += 1
|
| 62 |
+
return bytes(out)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def template_id(code_hex: str | None) -> str | None:
|
| 66 |
+
"""The template of runtime code ``code_hex`` (0x-prefixed), None for no code."""
|
| 67 |
+
if not isinstance(code_hex, str) or len(code_hex) <= 2:
|
| 68 |
+
return None
|
| 69 |
+
return hashlib.sha256(skeleton(strip_meta(bytes.fromhex(code_hex[2:] if code_hex.startswith("0x") else code_hex)))).hexdigest()
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def known(template: str | None) -> bool:
|
| 73 |
+
return template in KNOWN_GOOD
|
fly_trader/rh/token_check.py
CHANGED
|
@@ -10,12 +10,20 @@ upgradeable and refused) and the dispatcher's selectors, and refuses a token who
|
|
| 10 |
AccessControl roles hold a power (``POWERS``), one with a built-in buy or sell tax (``TAXES``), one that is not 18 decimals,
|
| 11 |
and one whose name or symbol marks a major, a stablecoin or a tokenized stock. Functions are matched by selector: a power
|
| 12 |
under an unusual name is missed, which the floors and the creator history (rh/meta.py) still stand behind.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
"""
|
| 14 |
from __future__ import annotations
|
| 15 |
|
| 16 |
import re
|
| 17 |
|
| 18 |
-
from . import abi
|
| 19 |
|
| 20 |
POWERS: dict[str, tuple[str, ...]] = {
|
| 21 |
"mint": ("mint(address,uint256)", "mint(uint256)", "mintTo(address,uint256)", "grantMintRole(address)", "grantMintAndBurnRoles(address)",
|
|
@@ -103,6 +111,25 @@ def _str(v) -> str | None:
|
|
| 103 |
return None
|
| 104 |
|
| 105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
def check(rpc, token: str) -> dict:
|
| 107 |
"""The contract check of ``token`` (two batched RPC reads at most). ``ok`` False with ``reason`` when refused; an
|
| 108 |
unreadable token raises (the caller retries later)."""
|
|
@@ -128,4 +155,4 @@ def check(rpc, token: str) -> dict:
|
|
| 128 |
accepted, reason, powers = judge(sels, owner_live=owner_live, upgradeable=upgradeable, decimals=dec, symbol=sym, name=nm)
|
| 129 |
return {"ok": accepted, "reason": reason, "powers": powers, "decimals": dec, "symbol": sym, "name": nm, "supply_raw": supply, "impl": impl,
|
| 130 |
"owner": owner, "owner_live": owner_live, "upgradeable": upgradeable, "taxes": bool(sels & TAX_SEL), "roles": bool(sels & ROLE_SEL),
|
| 131 |
-
"code_len": (len(body) - 2) // 2, "n_selectors": len(sels)}
|
|
|
|
| 10 |
AccessControl roles hold a power (``POWERS``), one with a built-in buy or sell tax (``TAXES``), one that is not 18 decimals,
|
| 11 |
and one whose name or symbol marks a major, a stablecoin or a tokenized stock. Functions are matched by selector: a power
|
| 12 |
under an unusual name is missed, which the floors and the creator history (rh/meta.py) still stand behind.
|
| 13 |
+
|
| 14 |
+
``creator_can_move(rpc, token, creator, holder)`` tries what a selector cannot show: a power hidden inside a standard
|
| 15 |
+
function. It simulates ``transferFrom(holder, dead, 1)`` sent by the creator, who holds no allowance: a standard ERC-20
|
| 16 |
+
reverts. ANT (0x36c2861a, 2026-11-03) passed the selector check with no owner; 22 s after the wallet bought it, its creator
|
| 17 |
+
called transferFrom and burned the wallet's tokens. Its simulation succeeds; on 40 coins the check had passed it reverted on all.
|
| 18 |
+
|
| 19 |
+
``check`` also returns the code's template (rh/templates.py, a minimal proxy's implementation in its place): rh/v4_universe
|
| 20 |
+
refuses a coin whose template is not known good.
|
| 21 |
"""
|
| 22 |
from __future__ import annotations
|
| 23 |
|
| 24 |
import re
|
| 25 |
|
| 26 |
+
from . import abi, templates
|
| 27 |
|
| 28 |
POWERS: dict[str, tuple[str, ...]] = {
|
| 29 |
"mint": ("mint(address,uint256)", "mint(uint256)", "mintTo(address,uint256)", "grantMintRole(address)", "grantMintAndBurnRoles(address)",
|
|
|
|
| 111 |
return None
|
| 112 |
|
| 113 |
|
| 114 |
+
TRANSFER_FROM = abi.selector("transferFrom(address,address,uint256)").hex()
|
| 115 |
+
DEAD_ADDR = "000000000000000000000000000000000000dead"
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def creator_can_move(rpc, token: str, creator: str, holder: str) -> bool:
|
| 119 |
+
"""Whether ``creator`` moves ``holder``'s tokens without an allowance (a simulated ``transferFrom(holder, dead, 1)`` from
|
| 120 |
+
the creator does not revert). ``holder`` must hold the token (a v4 coin's PoolManager holds its pools' liquidity); an
|
| 121 |
+
unreadable answer raises (the caller retries later)."""
|
| 122 |
+
from ..chain.evm import EvmRpcError
|
| 123 |
+
data = "0x" + TRANSFER_FROM + "0" * 24 + holder.lower()[2:] + "0" * 24 + DEAD_ADDR + format(1, "064x")
|
| 124 |
+
try:
|
| 125 |
+
out = rpc.call("eth_call", [{"from": creator.lower(), "to": token.lower(), "data": data}, "latest"])
|
| 126 |
+
except EvmRpcError as e:
|
| 127 |
+
if "revert" in str(e).lower():
|
| 128 |
+
return False
|
| 129 |
+
raise
|
| 130 |
+
return isinstance(out, str) and (out in ("0x", "") or int(out, 16) != 0) # true, or a token that returns nothing
|
| 131 |
+
|
| 132 |
+
|
| 133 |
def check(rpc, token: str) -> dict:
|
| 134 |
"""The contract check of ``token`` (two batched RPC reads at most). ``ok`` False with ``reason`` when refused; an
|
| 135 |
unreadable token raises (the caller retries later)."""
|
|
|
|
| 155 |
accepted, reason, powers = judge(sels, owner_live=owner_live, upgradeable=upgradeable, decimals=dec, symbol=sym, name=nm)
|
| 156 |
return {"ok": accepted, "reason": reason, "powers": powers, "decimals": dec, "symbol": sym, "name": nm, "supply_raw": supply, "impl": impl,
|
| 157 |
"owner": owner, "owner_live": owner_live, "upgradeable": upgradeable, "taxes": bool(sels & TAX_SEL), "roles": bool(sels & ROLE_SEL),
|
| 158 |
+
"code_len": (len(body) - 2) // 2, "n_selectors": len(sels), "template": templates.template_id(body)}
|
fly_trader/rh/v4_facts.py
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""A plain v4 coin's launch facts (2026-11-02): what the universe's creator rules and the model's launch inputs read in
|
| 2 |
+
``rh_meta``. A coin without them never trades: rh/meta.build_v4 leaves them unknown and train/selector.in_universe refuses
|
| 3 |
+
an unknown launch. The definitions are the ones selector #791 trained on (2,469 coins of the v4 history), from the coin's
|
| 4 |
+
Transfer logs and code sizes:
|
| 5 |
+
|
| 6 |
+
- the launch block L: the first block in which a contract that ever held ``BIG`` of the supply (or the PoolManager) pays
|
| 7 |
+
tokens to anyone but another such holder, the mint aside. That is a launch-transaction buy on a factory's Uniswap v3 pool
|
| 8 |
+
or curve, or the first buy of a coin seeded by hand into a v4 pool;
|
| 9 |
+
- the holdings at L's end: the PoolManager, the zero and dead addresses and the token itself are nobody's; a contract that
|
| 10 |
+
paid at least ``MARKET_PAYEES`` addresses in the pulled window is a market (a pool, a curve); everything else is owned, a
|
| 11 |
+
contract that pays nobody included, so it fails closed. A wallet: code of at most ``WALLET_CODE`` bytes (an EIP-7702
|
| 12 |
+
delegation is 23, a clone 45);
|
| 13 |
+
- the creator: the sender of the mint transaction. dev_share = dev_hold_share = its share, bundle_share = the rest owned
|
| 14 |
+
(those holders go to rh_insiders), grad_hhi = the concentration of every holding but the markets';
|
| 15 |
+
- graduated_at = L's time (the age floor counts from it), ttg_min = mint to L, rq0 = the quote reserve in ETH at the first
|
| 16 |
+
swap of the coin's registered pools, curve_known = 1, its creator's history as rh/meta._history.
|
| 17 |
+
|
| 18 |
+
``fly-trader rh-v4-facts`` is a child of the stream at the lowest CPU priority (rh/stream.facts_apart), never in a minute's
|
| 19 |
+
path, its HyperSync calls in a slot of each minute behind live's (``_slot``) and the header endpoint, not the transaction
|
| 20 |
+
endpoint live orders use. Each run takes the coins that clear the floors now and near the universe's age, busiest first; the engine reads their
|
| 21 |
+
row within ``META_TTL_S``. A coin without a launch is tried again after ``RETRY_H``, and stays out until it has one.
|
| 22 |
+
"""
|
| 23 |
+
from __future__ import annotations
|
| 24 |
+
|
| 25 |
+
import fcntl
|
| 26 |
+
import json
|
| 27 |
+
import logging
|
| 28 |
+
import time
|
| 29 |
+
from collections import defaultdict
|
| 30 |
+
from datetime import datetime, timedelta, timezone
|
| 31 |
+
|
| 32 |
+
from .. import config
|
| 33 |
+
from ..db.connection import transaction
|
| 34 |
+
from . import hypersync, index, meta, prices, v4, v4_universe
|
| 35 |
+
from .rpc import logs_rpc
|
| 36 |
+
|
| 37 |
+
log = logging.getLogger(__name__)
|
| 38 |
+
ZERO = "0x" + "00" * 20
|
| 39 |
+
DEAD = {ZERO, "0x000000000000000000000000000000000000dead"}
|
| 40 |
+
WALLET_CODE = 100 # bytes of code a wallet can have
|
| 41 |
+
MARKET_PAYEES = 3 # distinct addresses a contract paid in the window, to count as a market
|
| 42 |
+
MIN_SHARE = 0.002 # a holding below this share of the supply is nobody's in particular
|
| 43 |
+
BIG = 0.05 # only an address that ever holds this share can be a pool or a curve, so only those are looked up
|
| 44 |
+
AFTER_INIT = 600 # blocks of Transfers past the first pool's Initialize (a minute)
|
| 45 |
+
AFTER_L = 300 # and past L
|
| 46 |
+
WINDOWS = (36_000, 360_000, 3_600_000) # blocks past a pool's start a later launch or first swap is looked for in (~1, 10, 100 h), never up to the head: a busy coin's whole history
|
| 47 |
+
EARLY_H = 1.0 # a coin is taken this long before the universe's 6 h
|
| 48 |
+
MAX_COINS = 20 # coins a run
|
| 49 |
+
RUN_S = 480.0 # no new coin is started after this long
|
| 50 |
+
RETRY_H = 24.0 # a coin that had no launch is tried again after this long
|
| 51 |
+
CODE_BATCH = 8
|
| 52 |
+
SLOT_S = (8.0, 20.0) # a HyperSync call starts this far into a minute: the minute that ended is written, its live orders out
|
| 53 |
+
SLOT_CALLS = 2 # at most this many a minute: since 2026-11-02 18:01 HyperSync answers 10 a minute, and the live tail takes the rest
|
| 54 |
+
SLOT_WAIT_S = 300.0 # a call that finds no slot this long raises: its coin is tried again next run
|
| 55 |
+
LAG_BLOCKS = 120 # the live index this far behind needs every call: no run
|
| 56 |
+
_SLOT = {"minute": None, "n": 0}
|
| 57 |
+
LOCK = config.DATA_DIR / ".rh_v4_facts.lock"
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
class Codes:
|
| 61 |
+
"""Code sizes, each address read once. An address that cannot be read raises: its coin is tried again next run."""
|
| 62 |
+
|
| 63 |
+
def __init__(self, rpc):
|
| 64 |
+
self.rpc = rpc; self.size: dict[str, int] = {ZERO: 0}
|
| 65 |
+
|
| 66 |
+
def need(self, addrs) -> None:
|
| 67 |
+
pm = config.V4_POOL_MANAGER.lower(); todo = sorted({a for a in addrs if a not in self.size and a != pm})
|
| 68 |
+
for i in range(0, len(todo), CODE_BATCH):
|
| 69 |
+
ch = todo[i:i + CODE_BATCH]
|
| 70 |
+
for a, v in zip(ch, self.rpc.batch([("eth_getCode", [a, "latest"]) for a in ch])):
|
| 71 |
+
if not isinstance(v, str):
|
| 72 |
+
raise RuntimeError(f"no code size for {a}: {v!r}"[:200])
|
| 73 |
+
self.size[a] = (len(v) - 2) // 2
|
| 74 |
+
|
| 75 |
+
def wallet(self, a: str) -> bool:
|
| 76 |
+
return a != config.V4_POOL_MANAGER.lower() and self.size.get(a, 0) <= WALLET_CODE
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def launch(token: str, rows: list[tuple], codes: Codes) -> dict | None:
|
| 80 |
+
"""L and the holdings at its end from a coin's Transfers in order, (block, tx, src, dst, value), all lower case; L is
|
| 81 |
+
None when none was found in ``rows``. None when nothing was minted."""
|
| 82 |
+
pm = config.V4_POOL_MANAGER.lower(); token = token.lower()
|
| 83 |
+
minted = sum(float(r[4]) for r in rows if r[2] == ZERO)
|
| 84 |
+
if minted <= 0:
|
| 85 |
+
return None
|
| 86 |
+
first = next(r for r in rows if r[2] == ZERO)
|
| 87 |
+
out = {"minted": minted, "mint_block": first[0], "mint_tx": first[1], "L": None}
|
| 88 |
+
payees: dict[str, set] = defaultdict(set)
|
| 89 |
+
for _b, _t, s, d, _v in rows:
|
| 90 |
+
if s != ZERO:
|
| 91 |
+
payees[s].add(d)
|
| 92 |
+
run: dict[str, float] = defaultdict(float); big = {pm}
|
| 93 |
+
for b, _t, s, d, v in rows:
|
| 94 |
+
v = float(v); run[s] -= v; run[d] += v
|
| 95 |
+
if s == ZERO or v <= 0 or d in DEAD or d == token:
|
| 96 |
+
continue
|
| 97 |
+
if run[d] / minted >= BIG and d not in big:
|
| 98 |
+
codes.need([d]); big.add(d)
|
| 99 |
+
if s in big and not codes.wallet(s) and d not in big:
|
| 100 |
+
out["L"] = b; break
|
| 101 |
+
if out["L"] is None:
|
| 102 |
+
return out
|
| 103 |
+
bal: dict[str, float] = defaultdict(float)
|
| 104 |
+
for b, _t, s, d, v in rows:
|
| 105 |
+
if b > out["L"]:
|
| 106 |
+
break
|
| 107 |
+
bal[s] -= float(v); bal[d] += float(v)
|
| 108 |
+
hold = {a: v for a, v in bal.items() if a != pm and a not in DEAD and a != token and v / minted >= MIN_SHARE}
|
| 109 |
+
codes.need(hold)
|
| 110 |
+
market = {a for a in hold if not codes.wallet(a) and len(payees[a]) >= MARKET_PAYEES}
|
| 111 |
+
owned = {a: v / minted for a, v in hold.items() if a not in market}
|
| 112 |
+
rest = [v for a, v in bal.items() if v > 0 and a != pm and a not in DEAD and a != token and a not in market]
|
| 113 |
+
total = sum(rest)
|
| 114 |
+
out.update(owned=owned, hhi=sum((v / total) ** 2 for v in rest) if rest else None)
|
| 115 |
+
return out
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def _transfers(rpc, token: str, lo: int, hi: int) -> list[tuple]:
|
| 119 |
+
return [(int(x["blockNumber"], 16), x["transactionHash"], "0x" + x["topics"][1][-40:].lower(), "0x" + x["topics"][2][-40:].lower(),
|
| 120 |
+
int(x["data"], 16) if x["data"] not in ("0x", "") else 0)
|
| 121 |
+
for x in index._order(index._get_logs(rpc, [token], lo, hi, [[index.TRANSFER]])) if len(x.get("topics") or ()) == 3]
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def find_launch(rpc, codes: Codes, token: str, init: int, head: int) -> dict | None:
|
| 125 |
+
"""``launch`` on the coin's Transfers from block 0 to a minute past its first pool (``init``), further out while no
|
| 126 |
+
launch shows, and to ``AFTER_L`` past L: the window the payee count and the history's holdings saw."""
|
| 127 |
+
hi = min(head, init + AFTER_INIT); rows = _transfers(rpc, token, 0, hi); f = launch(token, rows, codes)
|
| 128 |
+
for w in WINDOWS:
|
| 129 |
+
if f is None or f["L"] is not None or hi >= head:
|
| 130 |
+
break
|
| 131 |
+
to = min(head, init + w)
|
| 132 |
+
if to > hi:
|
| 133 |
+
rows += _transfers(rpc, token, hi + 1, to); hi = to; f = launch(token, rows, codes)
|
| 134 |
+
if f is not None and f["L"] is not None and f["L"] + AFTER_L > hi and hi < head:
|
| 135 |
+
to = min(head, f["L"] + AFTER_L); rows += _transfers(rpc, token, hi + 1, to); f = launch(token, rows, codes)
|
| 136 |
+
return f
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def first_swap(rpc, pools: list[str], lo: int, head: int) -> dict | None:
|
| 140 |
+
"""The first Swap of ``pools`` within ``WINDOWS`` of block ``lo``, or None: rq0 stays unknown."""
|
| 141 |
+
frm = lo
|
| 142 |
+
for w in WINDOWS:
|
| 143 |
+
to = min(head, lo + w)
|
| 144 |
+
if to >= frm:
|
| 145 |
+
logs = index._get_logs(rpc, [config.V4_POOL_MANAGER], frm, to, [[v4.TOPIC["Swap"]], pools])
|
| 146 |
+
if logs:
|
| 147 |
+
return v4.decode(index._order(logs)[0])
|
| 148 |
+
frm = to + 1
|
| 149 |
+
return None
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def block_time(rpc, block: int) -> datetime:
|
| 153 |
+
src = getattr(rpc, "blocks", rpc)
|
| 154 |
+
return datetime.fromtimestamp(int(src.call("eth_getBlockByNumber", [hex(block), False])["timestamp"], 16), timezone.utc)
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def facts(rpc, codes: Codes, c: dict, head: int) -> dict:
|
| 158 |
+
"""A coin's rh_meta facts from the chain, or {"why": ...} when it has no launch. ``c``: {"token", "first_block", "pools":
|
| 159 |
+
[{"pool_id", "init_block", "quote_asset", "token_is_0", "qdec"}]}. No database."""
|
| 160 |
+
token = c["token"].lower(); f = find_launch(rpc, codes, token, int(c["first_block"]), head)
|
| 161 |
+
if f is None:
|
| 162 |
+
return {"why": "nothing minted in its Transfers"}
|
| 163 |
+
if f["L"] is None:
|
| 164 |
+
return {"why": "no launch found"}
|
| 165 |
+
src = getattr(rpc, "blocks", rpc); tx = src.tx_by_hash(f["mint_tx"])
|
| 166 |
+
creator = ((tx or {}).get("from") or "").lower() or None
|
| 167 |
+
if creator is None:
|
| 168 |
+
raise RuntimeError(f"mint transaction {f['mint_tx']} of {token} not found")
|
| 169 |
+
g, created = block_time(rpc, f["L"]), block_time(rpc, f["mint_block"])
|
| 170 |
+
own = f["owned"]; dev = float(own.get(creator, 0.0)); total = sum(own.values()); supply = f["minted"] / 1e18
|
| 171 |
+
out = {"graduated_at": g, "create_ts": created, "creator": creator, "supply": supply, "ttg_min": max(0.0, (g - created).total_seconds() / 60.0), "dev_sol": 0.0,
|
| 172 |
+
"dev_tokens": dev * supply, "dev_share": dev, "bundle_share": max(0.0, total - dev), "dev_hold_share": dev, "dev_sold_frac": 0.0, "grad_hhi": f["hhi"],
|
| 173 |
+
"curve_known": 1.0, "owned": own, "rq0": None}
|
| 174 |
+
start = min((int(p["init_block"]) for p in c["pools"] if p.get("init_block") is not None), default=int(c["first_block"]))
|
| 175 |
+
s = first_swap(rpc, [p["pool_id"] for p in c["pools"]], start, head) if c["pools"] else None
|
| 176 |
+
if s is not None:
|
| 177 |
+
p = next(x for x in c["pools"] if x["pool_id"] == s["pool_id"])
|
| 178 |
+
out["first_swap"] = {"at": block_time(rpc, s["block"]), "quote_asset": p["quote_asset"],
|
| 179 |
+
"reserve": v4.quote_reserve(int(s["sqrt_price_x96"]), int(s["liquidity"]), bool(p["token_is_0"]), int(p["qdec"]))}
|
| 180 |
+
return out
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
FIELDS = ("graduated_at", "create_ts", "creator", "supply", "ttg_min", "dev_sol", "dev_tokens", "dev_share", "bundle_share", "dev_hold_share", "dev_sold_frac", "grad_hhi",
|
| 184 |
+
"curve_known", "rq0", "prior_launches", "prior_grads", "prior_known", "prior_rug_share", "prior_moon_share", "own_dd60", "own_max60")
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def write(conn, token: str, f: dict) -> bool:
|
| 188 |
+
"""``facts`` into the coin's rh_meta row and rh_insiders, with its creator's history and its first hour where the
|
| 189 |
+
minutes hold it. A row that has its facts stays as it is."""
|
| 190 |
+
row = {k: f.get(k) for k in FIELDS}; s = f.get("first_swap")
|
| 191 |
+
if s is not None:
|
| 192 |
+
base = prices.base_eth(conn, s["quote_asset"], s["at"])[0]
|
| 193 |
+
row["rq0"] = s["reserve"] * base if base is not None else None
|
| 194 |
+
row.update(meta._history(conn, f["creator"], f["create_ts"], f["graduated_at"], token))
|
| 195 |
+
o = meta.outcomes(conn, token, f["graduated_at"])
|
| 196 |
+
if o is not None:
|
| 197 |
+
row["own_dd60"], row["own_max60"] = o
|
| 198 |
+
n = conn.execute(f"UPDATE rh_meta SET {', '.join(k + ' = %s' for k in FIELDS)}, updated_at = now() WHERE mint = %s AND source = 'v4' AND ttg_min IS NULL",
|
| 199 |
+
[row.get(k) for k in FIELDS] + [token]).rowcount
|
| 200 |
+
if n:
|
| 201 |
+
for w in f["owned"]:
|
| 202 |
+
conn.execute("INSERT INTO rh_insiders (mint, wallet, kind) VALUES (%s, %s, %s) ON CONFLICT DO NOTHING", (token, w, "dev" if w == f["creator"] else "bundle"))
|
| 203 |
+
return bool(n)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
def note_failure(conn, token: str, why: str) -> None:
|
| 207 |
+
conn.execute("""UPDATE rh_v4_tokens SET detail = coalesce(detail, '{}'::jsonb) || jsonb_build_object('facts', jsonb_build_object('at', now(), 'why', %s::text)) WHERE token = %s""", (why, token))
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def due(conn, limit: int = MAX_COINS, now: datetime | None = None) -> list[dict]:
|
| 211 |
+
"""The plain coins without facts, busiest first: past the universe's age less ``EARLY_H``, near the floors over the last half hour. Their registered pools."""
|
| 212 |
+
now = now or datetime.now(timezone.utc); res, vol = v4_universe.floors()
|
| 213 |
+
rows = conn.execute("""WITH a AS (SELECT mint, max(resq_eth) AS resq, sum(buy_eth + sell_eth) AS vol FROM rh_minutes WHERE ts >= %(since)s GROUP BY 1)
|
| 214 |
+
SELECT m.mint AS token, v.first_block FROM a JOIN rh_meta m ON m.mint = a.mint JOIN rh_v4_tokens v ON v.token = m.mint
|
| 215 |
+
WHERE m.source = 'v4' AND m.ttg_min IS NULL AND a.resq >= %(res)s AND a.vol >= %(vol)s AND v.first_block IS NOT NULL AND m.graduated_at <= %(aged)s
|
| 216 |
+
AND (v.detail -> 'facts' IS NULL OR (v.detail -> 'facts' ->> 'at')::timestamptz <= %(retry)s)
|
| 217 |
+
ORDER BY a.vol DESC LIMIT %(n)s""",
|
| 218 |
+
{"since": now - timedelta(minutes=30), "res": res, "vol": vol, "aged": now - timedelta(hours=6.0 - EARLY_H), "retry": now - timedelta(hours=RETRY_H), "n": limit}).fetchall()
|
| 219 |
+
out = []
|
| 220 |
+
for r in rows:
|
| 221 |
+
ps = conn.execute("""SELECT p.pool_id, p.init_block, p.quote_asset, p.token_is_0, coalesce(a.decimals, 18) AS qdec FROM rh_pools p LEFT JOIN rh_assets a ON a.asset = p.quote_asset
|
| 222 |
+
WHERE p.token = %s AND p.kind = 'v4'""", (r["token"],)).fetchall()
|
| 223 |
+
if ps:
|
| 224 |
+
out.append({"token": r["token"], "first_block": int(r["first_block"]), "pools": [dict(p) for p in ps]})
|
| 225 |
+
return out
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
def live_trouble(conn) -> str | None:
|
| 229 |
+
"""Why a run waits for the next, or None: HyperSync failing other than by its 429 cap in the last 5 minutes (an outage:
|
| 230 |
+
live logs need every call), or the live index behind or silent. The cap's 429s come every minute; ``_slot`` keeps a
|
| 231 |
+
run's calls behind live's."""
|
| 232 |
+
n = int(conn.execute("""SELECT count(*) AS n FROM api_calls WHERE service = 'rh_hypersync' AND ts > now() - interval '5 minutes'
|
| 233 |
+
AND ok IS FALSE AND status IS DISTINCT FROM 429""").fetchone()["n"])
|
| 234 |
+
if n:
|
| 235 |
+
return f"{n} HyperSync failures in 5 minutes"
|
| 236 |
+
r = conn.execute("SELECT value FROM ui_settings WHERE key = 'rh_stream_status'").fetchone()
|
| 237 |
+
s = (r["value"] if isinstance(r["value"], dict) else json.loads(r["value"])) if r else {}
|
| 238 |
+
age = time.time() - datetime.fromisoformat(s["updated_at"]).timestamp() if s.get("updated_at") else None
|
| 239 |
+
if age is None or age > 180.0:
|
| 240 |
+
return "the stream is silent"
|
| 241 |
+
if s.get("mode") != "live" or (s.get("behind_blocks") or 0) > LAG_BLOCKS:
|
| 242 |
+
return f"the live index is {s.get('behind_blocks')} blocks behind"
|
| 243 |
+
return None
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
def _minute_written(end: int) -> bool:
|
| 247 |
+
"""The stream has written the minute ending at ``end`` (``flushed_through``: the newest written minute's start, as the engine reads it)."""
|
| 248 |
+
with transaction() as conn:
|
| 249 |
+
r = conn.execute("SELECT value->>'flushed_through' AS ft FROM ui_settings WHERE key = 'rh_stream_status'").fetchone()
|
| 250 |
+
return bool(r and r["ft"]) and datetime.fromisoformat(r["ft"]).timestamp() + 60.0 >= end
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
def _slot(clock=time.time, sleep=time.sleep) -> None:
|
| 254 |
+
"""Each HyperSync call of a run waits here (in place of hypersync._pace, in this process only): ``SLOT_S`` into a
|
| 255 |
+
minute, once the stream has written the minute that ended, ``SLOT_CALLS`` a minute at most. A 429's retry waits here
|
| 256 |
+
too, so for a later slot."""
|
| 257 |
+
t0 = clock()
|
| 258 |
+
while True:
|
| 259 |
+
t = clock(); m = int(t // 60); s = t - m * 60
|
| 260 |
+
if t - t0 > SLOT_WAIT_S:
|
| 261 |
+
raise TimeoutError(f"no HyperSync slot in {SLOT_WAIT_S:.0f} s")
|
| 262 |
+
if _SLOT["minute"] != m:
|
| 263 |
+
_SLOT.update(minute=m, n=0)
|
| 264 |
+
if s < SLOT_S[0]:
|
| 265 |
+
sleep(SLOT_S[0] - s)
|
| 266 |
+
elif s > SLOT_S[1] or _SLOT["n"] >= SLOT_CALLS:
|
| 267 |
+
sleep(60.0 - s + SLOT_S[0])
|
| 268 |
+
elif not _minute_written(m * 60):
|
| 269 |
+
sleep(1.0)
|
| 270 |
+
else:
|
| 271 |
+
_SLOT["n"] += 1
|
| 272 |
+
return
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
def run_once(limit: int = MAX_COINS) -> dict:
|
| 276 |
+
hypersync._pace = _slot # this process's HyperSync calls are all a run's: each takes the slot
|
| 277 |
+
out = {"due": 0, "written": 0, "no_launch": 0, "failed": 0}
|
| 278 |
+
with transaction() as conn:
|
| 279 |
+
why = live_trouble(conn)
|
| 280 |
+
if why:
|
| 281 |
+
log.info("rh v4 facts: no run, %s", why)
|
| 282 |
+
return {**out, "paused": why}
|
| 283 |
+
coins = due(conn, limit); cur = conn.execute("SELECT block FROM rh_scan WHERE name = %s", (index.CURSOR,)).fetchone()
|
| 284 |
+
out["due"] = len(coins)
|
| 285 |
+
if not coins or not cur:
|
| 286 |
+
return out
|
| 287 |
+
rpc = logs_rpc(); codes = Codes(getattr(rpc, "blocks", rpc)); head = int(cur["block"]); start = time.monotonic()
|
| 288 |
+
for c in coins:
|
| 289 |
+
t0 = time.monotonic()
|
| 290 |
+
if t0 - start > RUN_S:
|
| 291 |
+
break
|
| 292 |
+
try:
|
| 293 |
+
f = facts(rpc, codes, c, head)
|
| 294 |
+
with transaction() as conn:
|
| 295 |
+
if "why" in f:
|
| 296 |
+
note_failure(conn, c["token"], f["why"]); out["no_launch"] += 1
|
| 297 |
+
elif write(conn, c["token"], f):
|
| 298 |
+
out["written"] += 1
|
| 299 |
+
log.info("rh v4 facts %s: %s (%.1f s)", c["token"][:10], f.get("why") or {k: f.get(k) for k in ("dev_share", "bundle_share", "ttg_min")}, time.monotonic() - t0)
|
| 300 |
+
except Exception as e: # the next run tries it again: nothing was written
|
| 301 |
+
out["failed"] += 1; log.warning("rh v4 facts %s failed: %s: %s", c["token"][:10], type(e).__name__, str(e)[:200])
|
| 302 |
+
return out
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
def run_locked(limit: int = MAX_COINS) -> dict:
|
| 306 |
+
"""``run_once`` under a file lock, one run at a time (``fly-trader rh-v4-facts``)."""
|
| 307 |
+
with open(LOCK, "w") as lock:
|
| 308 |
+
try:
|
| 309 |
+
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
| 310 |
+
except BlockingIOError:
|
| 311 |
+
return {"busy": True}
|
| 312 |
+
return run_once(limit)
|
| 313 |
+
|
fly_trader/rh/v4_universe.py
CHANGED
|
@@ -16,8 +16,12 @@ outside the Pons pools (rh/index.step). Nothing below runs in front of a minute'
|
|
| 16 |
key (``classify``) and registered in ``rh_pools`` (kind 'pons_other' or 'v4', not yet ``indexed``), and a plain
|
| 17 |
coin is queued in ``rh_v4_tokens``;
|
| 18 |
- ``RH_V4_CHECKS_PER_SLOT`` plain coins have their contract checked (rh/token_check.py), the busiest first;
|
|
|
|
|
|
|
| 19 |
- a qualifying pool goes live: its coin's rh_meta row (rh/meta.write_v4) and ``indexed`` with ``live_from`` (the
|
| 20 |
next block the stream indexes) in one commit, so the next pass indexes its swaps with the Pons pools'.
|
|
|
|
|
|
|
| 21 |
3. ``backfill_main`` (``fly-trader rh-v4-backfill``, its own process): every Initialize since block 0 into
|
| 22 |
``rh_v4_keys``, then each live pool's swaps before ``live_from`` with its coin's minutes over them, and a plain
|
| 23 |
coin's first-hour outcomes once its history is complete.
|
|
@@ -36,7 +40,7 @@ from datetime import datetime, timezone
|
|
| 36 |
from .. import config
|
| 37 |
from ..db.connection import transaction
|
| 38 |
from ..chain.evm import EvmRpc, EvmRpcError
|
| 39 |
-
from . import token_check, v4
|
| 40 |
from .rpc import RhRpc, _rate_limited
|
| 41 |
|
| 42 |
log = logging.getLogger(__name__)
|
|
@@ -49,6 +53,7 @@ FORGET_S = 86400.0 # a pool that is no coin's pool (a hook, two quo
|
|
| 49 |
KEEP_SWAPS = 200_000 # swap logs kept between slots (~5 h at 2026-10-23's rate); older ones are dropped
|
| 50 |
SLOT_TIMEOUT_S = 5.0 # one RPC read of a slot
|
| 51 |
MAX_ATTEMPTS = 5 # contract checks of a coin before it is given up ('error')
|
|
|
|
| 52 |
HISTORY_CHUNK = 100_000 # backfill_main: blocks per history step (~3 h of chain)
|
| 53 |
KEYS_STEP = 2_000_000 # backfill_main: blocks per key-import step
|
| 54 |
_STASH: dict = {"inits": [], "swaps": []}
|
|
@@ -56,6 +61,7 @@ _ACT: dict[str, list] = {} # pool id -> [bucket, n, |amount0|, |amount1|, n
|
|
| 56 |
_KNOWN: set[str] = set() # pools found in rh_pools (Pons pools, registered pools)
|
| 57 |
_SEEN: dict[str, float] = {} # pools looked at and not registered -> when to look again
|
| 58 |
_RPC: dict = {"rpc": None}
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
class SlotOver(RuntimeError):
|
|
@@ -288,9 +294,13 @@ def refresh_activity(conn, now: float) -> None:
|
|
| 288 |
[(n, ts, pid) for pid, n, ts in vals])
|
| 289 |
|
| 290 |
|
|
|
|
|
|
|
|
|
|
| 291 |
def run_checks(conn, rpc, limit: int | None = None) -> dict:
|
| 292 |
-
"""The contract check (rh/token_check.check) of the busiest pending coins
|
| 293 |
-
first pool's initialization. A ``SlotOver`` ends the checks
|
|
|
|
| 294 |
limit = config.RH_V4_CHECKS_PER_SLOT if limit is None else limit
|
| 295 |
todo = conn.execute("""SELECT v.token, (SELECT k.init_tx FROM rh_v4_keys k WHERE k.pool_id = v.first_pool) AS init_tx,
|
| 296 |
(SELECT coalesce(sum(p.swaps_seen), 0) FROM rh_pools p WHERE p.token = v.token) AS swaps
|
|
@@ -302,6 +312,10 @@ def run_checks(conn, rpc, limit: int | None = None) -> dict:
|
|
| 302 |
r = token_check.check(rpc, t["token"])
|
| 303 |
tx = rpc.tx_by_hash(t["init_tx"]) if t["init_tx"] else None
|
| 304 |
creator = ((tx or {}).get("from") or "").lower() or None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
except SlotOver:
|
| 306 |
raise
|
| 307 |
except Exception as e:
|
|
@@ -317,6 +331,40 @@ def run_checks(conn, rpc, limit: int | None = None) -> dict:
|
|
| 317 |
return out
|
| 318 |
|
| 319 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
def go_live(conn, cursor: int) -> int:
|
| 321 |
"""Each registered pool that qualifies now (a graduated Pons coin's, or an accepted plain coin's) goes live: its
|
| 322 |
coin's rh_meta row and ``indexed`` with ``live_from`` = the next block the stream indexes, in this commit. Runs in
|
|
@@ -360,6 +408,14 @@ def slot(deadline: float, now: float | None = None) -> dict:
|
|
| 360 |
out["stopped"] = str(e)[:200]
|
| 361 |
except Exception as e:
|
| 362 |
out["error"] = str(e)[:200]; log.exception("rh v4 slot: contract checks failed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
try:
|
| 364 |
with transaction() as conn:
|
| 365 |
cur = conn.execute("SELECT block FROM rh_scan WHERE name = 'rh'").fetchone()
|
|
|
|
| 16 |
key (``classify``) and registered in ``rh_pools`` (kind 'pons_other' or 'v4', not yet ``indexed``), and a plain
|
| 17 |
coin is queued in ``rh_v4_tokens``;
|
| 18 |
- ``RH_V4_CHECKS_PER_SLOT`` plain coins have their contract checked (rh/token_check.py), the busiest first;
|
| 19 |
+
- an accepted coin whose contract matches no known-good template (rh/templates.py) is refused, and ``RETEMPLATE_PER_SLOT``
|
| 20 |
+
coins accepted before that check (2026-11-06) have their code read again, the ones trading now first;
|
| 21 |
- a qualifying pool goes live: its coin's rh_meta row (rh/meta.write_v4) and ``indexed`` with ``live_from`` (the
|
| 22 |
next block the stream indexes) in one commit, so the next pass indexes its swaps with the Pons pools'.
|
| 23 |
+
A plain coin's row knows no launch until rh/v4_facts.py finds it (a child process, near the universe's age): it trades
|
| 24 |
+
from then on.
|
| 25 |
3. ``backfill_main`` (``fly-trader rh-v4-backfill``, its own process): every Initialize since block 0 into
|
| 26 |
``rh_v4_keys``, then each live pool's swaps before ``live_from`` with its coin's minutes over them, and a plain
|
| 27 |
coin's first-hour outcomes once its history is complete.
|
|
|
|
| 40 |
from .. import config
|
| 41 |
from ..db.connection import transaction
|
| 42 |
from ..chain.evm import EvmRpc, EvmRpcError
|
| 43 |
+
from . import templates, token_check, v4
|
| 44 |
from .rpc import RhRpc, _rate_limited
|
| 45 |
|
| 46 |
log = logging.getLogger(__name__)
|
|
|
|
| 53 |
KEEP_SWAPS = 200_000 # swap logs kept between slots (~5 h at 2026-10-23's rate); older ones are dropped
|
| 54 |
SLOT_TIMEOUT_S = 5.0 # one RPC read of a slot
|
| 55 |
MAX_ATTEMPTS = 5 # contract checks of a coin before it is given up ('error')
|
| 56 |
+
RETEMPLATE_PER_SLOT = 25 # accepted coins whose template is read again a slot (one batch of codes, ~0.7 s)
|
| 57 |
HISTORY_CHUNK = 100_000 # backfill_main: blocks per history step (~3 h of chain)
|
| 58 |
KEYS_STEP = 2_000_000 # backfill_main: blocks per key-import step
|
| 59 |
_STASH: dict = {"inits": [], "swaps": []}
|
|
|
|
| 61 |
_KNOWN: set[str] = set() # pools found in rh_pools (Pons pools, registered pools)
|
| 62 |
_SEEN: dict[str, float] = {} # pools looked at and not registered -> when to look again
|
| 63 |
_RPC: dict = {"rpc": None}
|
| 64 |
+
_RETEMPLATED: dict = {"done": False} # every coin accepted before the template check has been read again
|
| 65 |
|
| 66 |
|
| 67 |
class SlotOver(RuntimeError):
|
|
|
|
| 294 |
[(n, ts, pid) for pid, n, ts in vals])
|
| 295 |
|
| 296 |
|
| 297 |
+
CREATOR_MOVES = "its creator can move holders' tokens without approval"
|
| 298 |
+
|
| 299 |
+
|
| 300 |
def run_checks(conn, rpc, limit: int | None = None) -> dict:
|
| 301 |
+
"""The contract check (rh/token_check.check) of the busiest pending coins, then the template (rh/templates.py), then the
|
| 302 |
+
creator probe; the creator is the sender of the coin's first pool's initialization. A ``SlotOver`` ends the checks
|
| 303 |
+
without counting an attempt."""
|
| 304 |
limit = config.RH_V4_CHECKS_PER_SLOT if limit is None else limit
|
| 305 |
todo = conn.execute("""SELECT v.token, (SELECT k.init_tx FROM rh_v4_keys k WHERE k.pool_id = v.first_pool) AS init_tx,
|
| 306 |
(SELECT coalesce(sum(p.swaps_seen), 0) FROM rh_pools p WHERE p.token = v.token) AS swaps
|
|
|
|
| 312 |
r = token_check.check(rpc, t["token"])
|
| 313 |
tx = rpc.tx_by_hash(t["init_tx"]) if t["init_tx"] else None
|
| 314 |
creator = ((tx or {}).get("from") or "").lower() or None
|
| 315 |
+
if r["ok"] and not templates.known(r.get("template")):
|
| 316 |
+
r = {**r, "ok": False, "reason": templates.DEVIATES}
|
| 317 |
+
if r["ok"] and creator and token_check.creator_can_move(rpc, t["token"], creator, config.V4_POOL_MANAGER):
|
| 318 |
+
r = {**r, "ok": False, "reason": CREATOR_MOVES}
|
| 319 |
except SlotOver:
|
| 320 |
raise
|
| 321 |
except Exception as e:
|
|
|
|
| 331 |
return out
|
| 332 |
|
| 333 |
|
| 334 |
+
def retemplate(conn, rpc, limit: int = RETEMPLATE_PER_SLOT) -> dict:
|
| 335 |
+
"""Coins accepted before the template check (2026-11-06; no ``template`` in their check's detail), the ones trading in
|
| 336 |
+
the last two hours first, then the newest: their code read again (one batch, a minimal proxy's implementation in a
|
| 337 |
+
second), the template noted, and a coin that deviates refused. A refused coin keeps its bars (an open position still
|
| 338 |
+
exits) and is never bought again (agent/minute_engine.refused). Once none is left (a coin accepted since carries its
|
| 339 |
+
template), no slot looks again until a restart."""
|
| 340 |
+
if _RETEMPLATED["done"]:
|
| 341 |
+
return {}
|
| 342 |
+
todo = conn.execute("""WITH act AS (SELECT lower(mint) AS token, max(ts) AS last FROM rh_minutes WHERE ts > now() - interval '2 hours' GROUP BY 1)
|
| 343 |
+
SELECT v.token, v.detail FROM rh_v4_tokens v LEFT JOIN act a ON a.token = v.token
|
| 344 |
+
WHERE v.status = 'ok' AND NOT (coalesce(v.detail, '{}'::jsonb) ? 'template')
|
| 345 |
+
ORDER BY a.last DESC NULLS LAST, v.first_ts DESC NULLS LAST LIMIT %s""", (limit,)).fetchall()
|
| 346 |
+
out = {"kept": 0, "refused": 0, "unread": 0}
|
| 347 |
+
if not todo:
|
| 348 |
+
_RETEMPLATED["done"] = True
|
| 349 |
+
return out
|
| 350 |
+
codes = rpc.batch([("eth_getCode", [t["token"], "latest"]) for t in todo])
|
| 351 |
+
impls = {i: token_check.clone_target(c) for i, c in enumerate(codes) if isinstance(c, str) and token_check.clone_target(c)}
|
| 352 |
+
if impls:
|
| 353 |
+
for i, c in zip(impls, rpc.batch([("eth_getCode", [a, "latest"]) for a in impls.values()])):
|
| 354 |
+
codes[i] = c if isinstance(c, str) else None
|
| 355 |
+
for t, code in zip(todo, codes):
|
| 356 |
+
if not isinstance(code, str):
|
| 357 |
+
out["unread"] += 1; continue # read again next slot
|
| 358 |
+
tpl = templates.template_id(code)
|
| 359 |
+
detail = json.dumps({**(t["detail"] or {}), "template": tpl}, default=str)
|
| 360 |
+
if templates.known(tpl):
|
| 361 |
+
conn.execute("UPDATE rh_v4_tokens SET detail = %s WHERE token = %s AND status = 'ok'", (detail, t["token"])); out["kept"] += 1
|
| 362 |
+
else:
|
| 363 |
+
conn.execute("UPDATE rh_v4_tokens SET status = 'refused', reason = %s, detail = %s, checked_at = now() WHERE token = %s AND status = 'ok'",
|
| 364 |
+
(templates.DEVIATES, detail, t["token"])); out["refused"] += 1
|
| 365 |
+
log.info("rh v4 coin %s: refused, %s", t["token"], templates.DEVIATES)
|
| 366 |
+
return out
|
| 367 |
+
|
| 368 |
def go_live(conn, cursor: int) -> int:
|
| 369 |
"""Each registered pool that qualifies now (a graduated Pons coin's, or an accepted plain coin's) goes live: its
|
| 370 |
coin's rh_meta row and ``indexed`` with ``live_from`` = the next block the stream indexes, in this commit. Runs in
|
|
|
|
| 408 |
out["stopped"] = str(e)[:200]
|
| 409 |
except Exception as e:
|
| 410 |
out["error"] = str(e)[:200]; log.exception("rh v4 slot: contract checks failed")
|
| 411 |
+
if "stopped" not in out:
|
| 412 |
+
try:
|
| 413 |
+
with transaction() as conn:
|
| 414 |
+
out["retemplate"] = retemplate(conn, rpc)
|
| 415 |
+
except SlotOver as e:
|
| 416 |
+
out["stopped"] = str(e)[:200]
|
| 417 |
+
except Exception as e:
|
| 418 |
+
out["error"] = str(e)[:200]; log.exception("rh v4 slot: template re-check failed")
|
| 419 |
try:
|
| 420 |
with transaction() as conn:
|
| 421 |
cur = conn.execute("SELECT block FROM rh_scan WHERE name = 'rh'").fetchone()
|
fly_trader/signer/core.py
CHANGED
|
@@ -109,12 +109,13 @@ class Signer:
|
|
| 109 |
mint_acc = f_mints.result()
|
| 110 |
progs = {m: (a["owner"] if a else TOKEN) for m, a in zip((in_m, out_m), mint_acc)}
|
| 111 |
addrs = [me] + f_spl.result() + f_t22.result()
|
| 112 |
-
for a in [policy.ata(me, m, progs[m]) for m in (in_m, out_m)] + chk.opens:
|
| 113 |
if a not in addrs:
|
| 114 |
addrs.append(a)
|
| 115 |
f_pre = _IO.submit(self.rpc.accounts, addrs)
|
| 116 |
f_sims = [_IO.submit(self.rpc.simulate, tx, addrs[i:i + SIM_MAX_ACCOUNTS]) for i in range(0, len(addrs), SIM_MAX_ACCOUNTS)] # in chunks: the RPC caps accounts per simulation
|
| 117 |
-
|
|
|
|
| 118 |
post_accs: list[dict | None] = []
|
| 119 |
for f in f_sims:
|
| 120 |
sim = f.result()
|
|
@@ -123,7 +124,8 @@ class Signer:
|
|
| 123 |
post_accs += [None if a is None else {"lamports": a["lamports"], "owner": Pubkey.from_string(a["owner"]), "data": base64.b64decode(a["data"][0])}
|
| 124 |
for a in sim["accounts"]]
|
| 125 |
post = self._snapshot(post_accs, addrs)
|
| 126 |
-
|
|
|
|
| 127 |
eff = policy.check_swap_effects(pre, post, in_mint=in_m, out_mint=out_m, in_amount=int(in_amount), min_out=int(min_out), slack_lamports=slack)
|
| 128 |
tok_acc = mint_acc[1] if buy else mint_acc[0]
|
| 129 |
prices = f_prices.result()
|
|
@@ -134,7 +136,7 @@ class Signer:
|
|
| 134 |
signed = VersionedTransaction.populate(vt.message, sigs)
|
| 135 |
sig = str(sigs[chk.signer_index])
|
| 136 |
self.ledger.record_swap("buy" if buy else "sell", in_mint, out_mint, int(in_amount), -eff["sol"], sig)
|
| 137 |
-
return {"tx": _b64(signed), "signature": sig, "simulated": eff, "value_ratio": ratio}
|
| 138 |
|
| 139 |
def close_atas(self, accounts: list[str]) -> list[dict]:
|
| 140 |
me = self.trading.pubkey()
|
|
|
|
| 109 |
mint_acc = f_mints.result()
|
| 110 |
progs = {m: (a["owner"] if a else TOKEN) for m, a in zip((in_m, out_m), mint_acc)}
|
| 111 |
addrs = [me] + f_spl.result() + f_t22.result()
|
| 112 |
+
for a in [policy.ata(me, m, progs[m]) for m in (in_m, out_m)] + chk.opens + chk.writable: # writable: accounts the route opens
|
| 113 |
if a not in addrs:
|
| 114 |
addrs.append(a)
|
| 115 |
f_pre = _IO.submit(self.rpc.accounts, addrs)
|
| 116 |
f_sims = [_IO.submit(self.rpc.simulate, tx, addrs[i:i + SIM_MAX_ACCOUNTS]) for i in range(0, len(addrs), SIM_MAX_ACCOUNTS)] # in chunks: the RPC caps accounts per simulation
|
| 117 |
+
pre_accs = f_pre.result()
|
| 118 |
+
pre = self._snapshot(pre_accs, addrs)
|
| 119 |
post_accs: list[dict | None] = []
|
| 120 |
for f in f_sims:
|
| 121 |
sim = f.result()
|
|
|
|
| 124 |
post_accs += [None if a is None else {"lamports": a["lamports"], "owner": Pubkey.from_string(a["owner"]), "data": base64.b64decode(a["data"][0])}
|
| 125 |
for a in sim["accounts"]]
|
| 126 |
post = self._snapshot(post_accs, addrs)
|
| 127 |
+
vault_rent = policy.rent_opened_for_others(pre_accs, post_accs) # a pool's fee vault opened at our expense
|
| 128 |
+
slack = policy.BASE_FEE_LAMPORTS * vt.message.header.num_required_signatures + chk.priority_lamports + 10_000 + vault_rent
|
| 129 |
eff = policy.check_swap_effects(pre, post, in_mint=in_m, out_mint=out_m, in_amount=int(in_amount), min_out=int(min_out), slack_lamports=slack)
|
| 130 |
tok_acc = mint_acc[1] if buy else mint_acc[0]
|
| 131 |
prices = f_prices.result()
|
|
|
|
| 136 |
signed = VersionedTransaction.populate(vt.message, sigs)
|
| 137 |
sig = str(sigs[chk.signer_index])
|
| 138 |
self.ledger.record_swap("buy" if buy else "sell", in_mint, out_mint, int(in_amount), -eff["sol"], sig)
|
| 139 |
+
return {"tx": _b64(signed), "signature": sig, "simulated": eff, "value_ratio": ratio, "vault_rent": vault_rent}
|
| 140 |
|
| 141 |
def close_atas(self, accounts: list[str]) -> list[dict]:
|
| 142 |
me = self.trading.pubkey()
|
fly_trader/signer/policy.py
CHANGED
|
@@ -9,7 +9,8 @@
|
|
| 9 |
accounts for its intermediate tokens hold their rent for the wallet). A buy may spend at most the ordered SOL (+ fees) and must deliver
|
| 10 |
the bought token to the trading wallet; a sell may spend at most the ordered tokens and must return SOL to it; no
|
| 11 |
other holding may shrink. So a route that pays someone else, drains another holding or overspends is refused
|
| 12 |
-
whatever the brain claimed.
|
|
|
|
| 13 |
|
| 14 |
Pure functions only: the RPC calls live in core.py, which passes their results in here.
|
| 15 |
"""
|
|
@@ -35,6 +36,8 @@ SWAP_PROGRAMS = frozenset({SYSTEM, COMPUTE_BUDGET, TOKEN, TOKEN_2022, ATA, JUPIT
|
|
| 35 |
EXCLUDE_ROUTERS = "jupiterz,dflow,okx"
|
| 36 |
ATA_RENT_LAMPORTS = 2_039_280
|
| 37 |
BASE_FEE_LAMPORTS = 5_000
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
class PolicyError(RuntimeError):
|
|
@@ -71,6 +74,7 @@ class SwapCheck:
|
|
| 71 |
priority_lamports: int
|
| 72 |
creates_atas: int
|
| 73 |
opens: list[Pubkey] = field(default_factory=list) # the token accounts its ATA instructions open for the trading wallet
|
|
|
|
| 74 |
|
| 75 |
|
| 76 |
def check_swap_static(tx: VersionedTransaction, trading: Pubkey, resolve_alt, max_priority_lamports: int) -> SwapCheck:
|
|
@@ -124,7 +128,18 @@ def check_swap_static(tx: VersionedTransaction, trading: Pubkey, resolve_alt, ma
|
|
| 124 |
prio = cu_limit * cu_price // 1_000_000
|
| 125 |
if prio > max_priority_lamports:
|
| 126 |
raise PolicyError(f"priority fee {prio} lamports is above the cap {max_priority_lamports}")
|
| 127 |
-
return SwapCheck(signer_index=idx, fee_payer=static[0], priority_lamports=prio, creates_atas=creates, opens=opens
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
|
| 130 |
def token_amount(data: bytes | None) -> int:
|
|
@@ -150,6 +165,25 @@ class Snapshot:
|
|
| 150 |
tokens: dict
|
| 151 |
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
def check_swap_effects(pre: Snapshot, post: Snapshot, *, in_mint: Pubkey, out_mint: Pubkey, in_amount: int, min_out: int,
|
| 154 |
slack_lamports: int) -> dict:
|
| 155 |
"""The balance changes a simulation produced, against what was ordered. Returns the deltas; raises PolicyError."""
|
|
|
|
| 9 |
accounts for its intermediate tokens hold their rent for the wallet). A buy may spend at most the ordered SOL (+ fees) and must deliver
|
| 10 |
the bought token to the trading wallet; a sell may spend at most the ordered tokens and must return SOL to it; no
|
| 11 |
other holding may shrink. So a route that pays someone else, drains another holding or overspends is refused
|
| 12 |
+
whatever the brain claimed. One cost to others is allowed: the rent of an empty token account the route opens inside
|
| 13 |
+
itself for a program's vault (a pool's fee vault opened at the buyer's expense; ``rent_opened_for_others``).
|
| 14 |
|
| 15 |
Pure functions only: the RPC calls live in core.py, which passes their results in here.
|
| 16 |
"""
|
|
|
|
| 36 |
EXCLUDE_ROUTERS = "jupiterz,dflow,okx"
|
| 37 |
ATA_RENT_LAMPORTS = 2_039_280
|
| 38 |
BASE_FEE_LAMPORTS = 5_000
|
| 39 |
+
RENT_PER_BYTE_MAX = 6_960 # lamports per byte (+128 of overhead) a rent exemption cost before 2026; it is lower now
|
| 40 |
+
OPENED_FOR_OTHERS_MAX = 2 # empty vault accounts a swap may open at the wallet's expense
|
| 41 |
|
| 42 |
|
| 43 |
class PolicyError(RuntimeError):
|
|
|
|
| 74 |
priority_lamports: int
|
| 75 |
creates_atas: int
|
| 76 |
opens: list[Pubkey] = field(default_factory=list) # the token accounts its ATA instructions open for the trading wallet
|
| 77 |
+
writable: list[Pubkey] = field(default_factory=list) # every account the transaction may write (lookup tables resolved)
|
| 78 |
|
| 79 |
|
| 80 |
def check_swap_static(tx: VersionedTransaction, trading: Pubkey, resolve_alt, max_priority_lamports: int) -> SwapCheck:
|
|
|
|
| 128 |
prio = cu_limit * cu_price // 1_000_000
|
| 129 |
if prio > max_priority_lamports:
|
| 130 |
raise PolicyError(f"priority fee {prio} lamports is above the cap {max_priority_lamports}")
|
| 131 |
+
return SwapCheck(signer_index=idx, fee_payer=static[0], priority_lamports=prio, creates_atas=creates, opens=opens,
|
| 132 |
+
writable=writable_keys(msg, keys))
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def writable_keys(msg, keys: list[Pubkey]) -> list[Pubkey]:
|
| 136 |
+
"""The accounts of ``keys`` (``account_keys`` order) the message may write: the writable signers, the writable
|
| 137 |
+
non-signers, then each lookup table's writable addresses."""
|
| 138 |
+
h = msg.header; ns = len(msg.account_keys)
|
| 139 |
+
n_sign, ro_sign, ro_unsigned = h.num_required_signatures, h.num_readonly_signed_accounts, h.num_readonly_unsigned_accounts
|
| 140 |
+
out = [k for i, k in enumerate(keys[:ns]) if i < n_sign - ro_sign or n_sign <= i < ns - ro_unsigned]
|
| 141 |
+
n_alt_w = sum(len(bytes(lk.writable_indexes)) for lk in getattr(msg, "address_table_lookups", []))
|
| 142 |
+
return out + list(keys[ns:ns + n_alt_w])
|
| 143 |
|
| 144 |
|
| 145 |
def token_amount(data: bytes | None) -> int:
|
|
|
|
| 165 |
tokens: dict
|
| 166 |
|
| 167 |
|
| 168 |
+
def rent_opened_for_others(pre_accounts: list[dict | None], post_accounts: list[dict | None]) -> int:
|
| 169 |
+
"""Lamports a swap put into token accounts it opened for a program's vault, in the simulation (``pre_accounts`` and
|
| 170 |
+
``post_accounts`` side by side, as ``Rpc.accounts`` reads them). Pump.fun's AMM opens a fee vault's empty wSOL
|
| 171 |
+
account at the buyer's expense: 34Ct2K, 2026-11-06, 1,488,440 lamports to an account of 5AmqLBHH (a program address),
|
| 172 |
+
and the signer refused the buy twice as an overspend. Counted: an account that did not exist, a token account whose
|
| 173 |
+
authority is a program address (off the curve: no wallet's), holding no tokens and no more lamports than the rent
|
| 174 |
+
exemption of its size; at most ``OPENED_FOR_OTHERS_MAX``. Rent to a wallet, or into an account that receives
|
| 175 |
+
tokens or SOL, is not."""
|
| 176 |
+
rents = []
|
| 177 |
+
for a0, a1 in zip(pre_accounts, post_accounts):
|
| 178 |
+
if (a0 and a0["lamports"]) or not a1 or a1["owner"] not in (TOKEN, TOKEN_2022) or len(a1["data"]) < 72:
|
| 179 |
+
continue
|
| 180 |
+
if token_owner(a1["data"]).is_on_curve() or token_amount(a1["data"]) != 0:
|
| 181 |
+
continue
|
| 182 |
+
if 0 < a1["lamports"] <= (128 + len(a1["data"])) * RENT_PER_BYTE_MAX:
|
| 183 |
+
rents.append(a1["lamports"])
|
| 184 |
+
return sum(sorted(rents)[:OPENED_FOR_OTHERS_MAX])
|
| 185 |
+
|
| 186 |
+
|
| 187 |
def check_swap_effects(pre: Snapshot, post: Snapshot, *, in_mint: Pubkey, out_mint: Pubkey, in_amount: int, min_out: int,
|
| 188 |
slack_lamports: int) -> dict:
|
| 189 |
"""The balance changes a simulation produced, against what was ordered. Returns the deltas; raises PolicyError."""
|
fly_trader/train/flow.py
CHANGED
|
@@ -11,7 +11,8 @@ index 10 = wallet without a score; None when the day has no table). The archive
|
|
| 11 |
|
| 12 |
``FlowWindow`` turns a mint's minutes into the derived model inputs (``FLOW_COLS`` + ``SKILL_COLS``), fed the same way
|
| 13 |
by ``mature.build_day`` and the live engine (``agent/minute_engine.py``); ``mkt_vol_1h`` is the market's total SOL volume
|
| 14 |
-
over the last hour (all mints), computed from the same minute rows in both paths.
|
|
|
|
| 15 |
"""
|
| 16 |
from __future__ import annotations
|
| 17 |
|
|
@@ -28,6 +29,10 @@ FLOW_COLS = ["top_sell_share_1m", "insider_sell_share_1m", "log_insider_sell_15m
|
|
| 28 |
"org_imb_5m", "org_imb_15m", "wash_share_15m", "log_org_vol_15m"]
|
| 29 |
SKILL_COLS = [f"skill_top{q}_15m" for q in range(1, 10)] + ["skill_known_15m"]
|
| 30 |
MARKET_COLS = ["mkt_vol_1h"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
RAW_COLS = ["n_buyers", "wash_sol", "wash_buy_sol", "top_sell_sol", "insider_sell_sol", "skill_buy"]
|
| 32 |
|
| 33 |
|
|
@@ -141,3 +146,37 @@ class MarketWindow:
|
|
| 141 |
|
| 142 |
def value(self, t_end: float) -> float:
|
| 143 |
return math.log1p(sum(v for t, v in self.q if t_end - WINDOW_S < t <= t_end))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
``FlowWindow`` turns a mint's minutes into the derived model inputs (``FLOW_COLS`` + ``SKILL_COLS``), fed the same way
|
| 13 |
by ``mature.build_day`` and the live engine (``agent/minute_engine.py``); ``mkt_vol_1h`` is the market's total SOL volume
|
| 14 |
+
over the last hour (all mints), computed from the same minute rows in both paths. ``BreadthWindow`` is the live market
|
| 15 |
+
breadth (agent/rising_clean.py), the study's series on ``features_mature`` rows fed minute by minute.
|
| 16 |
"""
|
| 17 |
from __future__ import annotations
|
| 18 |
|
|
|
|
| 29 |
"org_imb_5m", "org_imb_15m", "wash_share_15m", "log_org_vol_15m"]
|
| 30 |
SKILL_COLS = [f"skill_top{q}_15m" for q in range(1, 10)] + ["skill_known_15m"]
|
| 31 |
MARKET_COLS = ["mkt_vol_1h"]
|
| 32 |
+
BREADTH_MIN_RESQ = 10.0 # a coin counts towards the market's breadth with a pool of at least this many SOL
|
| 33 |
+
BREADTH_BUCKET_S = 300 # one 5-minute bucket of minute starts
|
| 34 |
+
BREADTH_LAG = 72 # 6 h of buckets between the two shares ``BreadthWindow.change`` compares
|
| 35 |
+
BREADTH_FILL = 12 # a bucket with no coin in it takes the last share at most this many buckets back
|
| 36 |
RAW_COLS = ["n_buyers", "wash_sol", "wash_buy_sol", "top_sell_sol", "insider_sell_sol", "skill_buy"]
|
| 37 |
|
| 38 |
|
|
|
|
| 146 |
|
| 147 |
def value(self, t_end: float) -> float:
|
| 148 |
return math.log1p(sum(v for t, v in self.q if t_end - WINDOW_S < t <= t_end))
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
class BreadthWindow:
|
| 152 |
+
"""The share of coins rising, per 5-minute bucket of minute starts: of the traded mint-minutes with a pool of at least
|
| 153 |
+
``BREADTH_MIN_RESQ``, those up on the hour (``ret_1h`` > 0). ``change`` is the winners study's ``up_chg_6h``
|
| 154 |
+
(data/scratch/winners_20261031/w_market.py): the last closed bucket's share minus the share 6 h before it."""
|
| 155 |
+
__slots__ = ("b",)
|
| 156 |
+
|
| 157 |
+
def __init__(self):
|
| 158 |
+
self.b: dict[int, list] = {} # bucket -> [mint-minutes counted, those up on the hour]
|
| 159 |
+
|
| 160 |
+
def add(self, t_start: float, resq: float | None, ret_1h: float) -> None:
|
| 161 |
+
if resq is None or resq < BREADTH_MIN_RESQ:
|
| 162 |
+
return
|
| 163 |
+
k = int(t_start // BREADTH_BUCKET_S); c = self.b.get(k)
|
| 164 |
+
if c is None:
|
| 165 |
+
c = self.b[k] = [0, 0]
|
| 166 |
+
for old in [x for x in self.b if x < k - BREADTH_LAG - BREADTH_FILL - 1]:
|
| 167 |
+
del self.b[old]
|
| 168 |
+
c[0] += 1; c[1] += bool(ret_1h > 0)
|
| 169 |
+
|
| 170 |
+
def _share(self, k: int) -> float | None:
|
| 171 |
+
for j in range(k, k - BREADTH_FILL - 1, -1):
|
| 172 |
+
c = self.b.get(j)
|
| 173 |
+
if c is not None:
|
| 174 |
+
return c[1] / c[0]
|
| 175 |
+
return None
|
| 176 |
+
|
| 177 |
+
def change(self, t_start: float) -> float | None:
|
| 178 |
+
"""For a row of the minute starting ``t_start``: the change in the share over 6 h, from buckets closed by then; None
|
| 179 |
+
when either share is unknown."""
|
| 180 |
+
k = int(t_start // BREADTH_BUCKET_S) - 1
|
| 181 |
+
now, then = self._share(k), self._share(k - BREADTH_LAG)
|
| 182 |
+
return None if now is None or then is None else now - then
|
fly_trader/ui/api/overview.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""Overview: what the system is doing in plain words, the book the money is in (live, else the selector's paper book), this minute's scoring, the race between the selector
|
| 2 |
-
and the plastic fly, and the open positions, each live one with its Sell Now (``overview.sell_now``). (The brain at the top of the page is ui/api/brain.py.)"""
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import csv
|
|
@@ -9,12 +9,15 @@ import re
|
|
| 9 |
from datetime import datetime, timedelta, timezone
|
| 10 |
|
| 11 |
from ... import config, markets
|
|
|
|
| 12 |
from ...agent.selector_session import pinned_snapshot
|
|
|
|
| 13 |
from ...db.queries import q, q1
|
|
|
|
| 14 |
from .. import runtime
|
| 15 |
from ..cache import ttl_cache
|
| 16 |
from ..hub import ActionError, action, topic
|
| 17 |
-
from ..state import ago, backtest_line, labels, market_info, market_param, pct, system_state
|
| 18 |
|
| 19 |
MAX_POINTS = 1500 # a 240 px line chart cannot show more; a month of minute marks is ~45,000 per book
|
| 20 |
|
|
@@ -85,10 +88,11 @@ def _series(rows: list[dict], books: list[str]) -> list[dict]:
|
|
| 85 |
def summary(params: dict) -> dict:
|
| 86 |
"""The narrative and the money's metrics — the live book while live money trades, else the selector's paper book:
|
| 87 |
wealth, realized P&L, closed (wins), open, drawdown, with sparklines (wealth and drawdown over the book's whole
|
| 88 |
-
history, realized P&L cumulative over the last 180 trades).
|
|
|
|
| 89 |
mk = market_param(params); s = system_state(mk); live = bool(s["live_money"]); BOOK = mk.live_book if live else mk.selector_book; u = mk.unit
|
| 90 |
-
|
| 91 |
-
cap =
|
| 92 |
w = q1("SELECT wealth, exposure, n_open, drawdown FROM wealth_marks WHERE book = %s ORDER BY ts DESC LIMIT 1", (BOOK,)) or {}
|
| 93 |
c = q1("SELECT count(*) AS n, sum(realized_sol) AS pnl, count(*) FILTER (WHERE realized_sol > 0) AS wins FROM positions WHERE book = %s AND status = 'closed'", (BOOK,)) or {}
|
| 94 |
spark = [float(r["wealth"]) for r in marks([BOOK], points=180) if r["wealth"] is not None] # the book since it started, thinned
|
|
@@ -96,11 +100,12 @@ def summary(params: dict) -> dict:
|
|
| 96 |
for x in spark: # drawdown below the running peak, on the same points
|
| 97 |
peak = max(peak, x); dd_spark.append(1 - x / peak if peak > 0 else 0.0)
|
| 98 |
trades = [float(r["realized_sol"] or 0) for r in q("SELECT realized_sol FROM positions WHERE book = %s AND status = 'closed' ORDER BY closed_at DESC NULLS LAST, id DESC LIMIT 180", (BOOK,))][::-1]
|
| 99 |
-
|
|
|
|
| 100 |
for r in trades: # cumulative realized P&L, ending at the total
|
| 101 |
base += r; pnl_spark.append(base)
|
| 102 |
return {"market": market_info(mk), "narrative": narrative(s), "marked": bool(w), "wealth": w.get("wealth"), "exposure": w.get("exposure"),
|
| 103 |
-
"n_open": int(w.get("n_open") or 0), "drawdown": w.get("drawdown"), "capital": cap, "closed": int(c.get("n") or 0), "pnl":
|
| 104 |
"wins": int(c.get("wins") or 0), "spark": spark, "pnl_spark": pnl_spark, "dd_spark": dd_spark, "live": live, "book": BOOK,
|
| 105 |
"label": "Live wealth" if live else "Paper wealth",
|
| 106 |
"wealth_help": (f"The bot wallet: {u} plus open positions net of exit costs; started at {cap:g} {u}." if live
|
|
@@ -208,6 +213,70 @@ def sell_now(body: dict) -> dict:
|
|
| 208 |
return {**out, "message": f"Selling {name}: sent, booked when it fills"}
|
| 209 |
|
| 210 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
# ---------------------------------------------------------------- trades by hour of day
|
| 212 |
def _utc_of(ts) -> datetime | None:
|
| 213 |
"""A timestamp in UTC: a datetime, or the text Postgres wrote into an archive ('2026-09-15 03:55:00+00')."""
|
|
|
|
| 1 |
"""Overview: what the system is doing in plain words, the book the money is in (live, else the selector's paper book), this minute's scoring, the race between the selector
|
| 2 |
+
and the plastic fly, the rising + clean test and its go-live, and the open positions, each live one with its Sell Now (``overview.sell_now``). (The brain at the top of the page is ui/api/brain.py.)"""
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
import csv
|
|
|
|
| 9 |
from datetime import datetime, timedelta, timezone
|
| 10 |
|
| 11 |
from ... import config, markets
|
| 12 |
+
from ...agent import rising_clean
|
| 13 |
from ...agent.selector_session import pinned_snapshot
|
| 14 |
+
from ...db.connection import transaction
|
| 15 |
from ...db.queries import q, q1
|
| 16 |
+
from ...ops import wallets
|
| 17 |
from .. import runtime
|
| 18 |
from ..cache import ttl_cache
|
| 19 |
from ..hub import ActionError, action, topic
|
| 20 |
+
from ..state import ago, backtest_line, labels, market_info, market_param, pct, setting, system_state
|
| 21 |
|
| 22 |
MAX_POINTS = 1500 # a 240 px line chart cannot show more; a month of minute marks is ~45,000 per book
|
| 23 |
|
|
|
|
| 88 |
def summary(params: dict) -> dict:
|
| 89 |
"""The narrative and the money's metrics — the live book while live money trades, else the selector's paper book:
|
| 90 |
wealth, realized P&L, closed (wins), open, drawdown, with sparklines (wealth and drawdown over the book's whole
|
| 91 |
+
history, realized P&L cumulative over the last 180 trades). A live book's capital and realized P&L are its wallet's
|
| 92 |
+
(ops/wallets.money), so they hold what the trade ledger never sees."""
|
| 93 |
mk = market_param(params); s = system_state(mk); live = bool(s["live_money"]); BOOK = mk.live_book if live else mk.selector_book; u = mk.unit
|
| 94 |
+
money = wallets.money(mk) if live else None
|
| 95 |
+
cap = money["start"] if money else mk.capital()
|
| 96 |
w = q1("SELECT wealth, exposure, n_open, drawdown FROM wealth_marks WHERE book = %s ORDER BY ts DESC LIMIT 1", (BOOK,)) or {}
|
| 97 |
c = q1("SELECT count(*) AS n, sum(realized_sol) AS pnl, count(*) FILTER (WHERE realized_sol > 0) AS wins FROM positions WHERE book = %s AND status = 'closed'", (BOOK,)) or {}
|
| 98 |
spark = [float(r["wealth"]) for r in marks([BOOK], points=180) if r["wealth"] is not None] # the book since it started, thinned
|
|
|
|
| 100 |
for x in spark: # drawdown below the running peak, on the same points
|
| 101 |
peak = max(peak, x); dd_spark.append(1 - x / peak if peak > 0 else 0.0)
|
| 102 |
trades = [float(r["realized_sol"] or 0) for r in q("SELECT realized_sol FROM positions WHERE book = %s AND status = 'closed' ORDER BY closed_at DESC NULLS LAST, id DESC LIMIT 180", (BOOK,))][::-1]
|
| 103 |
+
pnl = money["realized"] if money else c.get("pnl")
|
| 104 |
+
base = float(pnl or 0) - sum(trades); pnl_spark = []
|
| 105 |
for r in trades: # cumulative realized P&L, ending at the total
|
| 106 |
base += r; pnl_spark.append(base)
|
| 107 |
return {"market": market_info(mk), "narrative": narrative(s), "marked": bool(w), "wealth": w.get("wealth"), "exposure": w.get("exposure"),
|
| 108 |
+
"n_open": int(w.get("n_open") or 0), "drawdown": w.get("drawdown"), "capital": cap, "closed": int(c.get("n") or 0), "pnl": pnl,
|
| 109 |
"wins": int(c.get("wins") or 0), "spark": spark, "pnl_spark": pnl_spark, "dd_spark": dd_spark, "live": live, "book": BOOK,
|
| 110 |
"label": "Live wealth" if live else "Paper wealth",
|
| 111 |
"wealth_help": (f"The bot wallet: {u} plus open positions net of exit costs; started at {cap:g} {u}." if live
|
|
|
|
| 213 |
return {**out, "message": f"Selling {name}: sent, booked when it fills"}
|
| 214 |
|
| 215 |
|
| 216 |
+
# ---------------------------------------------------------------- the rising + clean test (agent/rising_clean.py)
|
| 217 |
+
GO_LIVE_PAST_S = 120.0 # a go-live this far in the past is a typing mistake, not "now"
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
@topic("overview.rising", every=30)
|
| 221 |
+
def rising(params: dict) -> dict:
|
| 222 |
+
"""The paper test on Solana: the test book against the selector's since it started, its doubled trades and the rest,
|
| 223 |
+
the market's breadth now and the go-live."""
|
| 224 |
+
mk = market_param(params); observer = isinstance(runtime.sup(), runtime.Observer)
|
| 225 |
+
if mk.chain != "sol":
|
| 226 |
+
return {"hidden": True}
|
| 227 |
+
t, _ = setting(rising_clean.KEY)
|
| 228 |
+
if not t:
|
| 229 |
+
return {"hidden": False, "running": False, "days": rising_clean.TEST_DAYS, "observer": observer}
|
| 230 |
+
since = datetime.fromisoformat(t["started_at"]); books = [rising_clean.BOOK, mk.selector_book]
|
| 231 |
+
rows = q("""SELECT p.book, COALESCE((d.detail->>'stake_x')::float, 1) > 1 AS doubled, count(*) FILTER (WHERE p.status = 'closed') AS closed,
|
| 232 |
+
count(*) FILTER (WHERE p.status = 'open') AS open, COALESCE(sum(p.realized_sol) FILTER (WHERE p.status = 'closed'), 0) AS pnl,
|
| 233 |
+
COALESCE(sum(p.cost_sol) FILTER (WHERE p.status = 'closed'), 0) AS cost
|
| 234 |
+
FROM positions p LEFT JOIN decisions d ON d.id = p.entry_decision_id WHERE p.book = ANY(%s) AND p.opened_at >= %s GROUP BY 1, 2""", (books, since))
|
| 235 |
+
|
| 236 |
+
def stat(pick) -> dict:
|
| 237 |
+
got = [r for r in rows if pick(r)]; cost = sum(float(r["cost"]) for r in got); pnl = sum(float(r["pnl"]) for r in got)
|
| 238 |
+
return {"closed": sum(int(r["closed"]) for r in got), "open": sum(int(r["open"]) for r in got), "pnl": pnl, "mean": pnl / cost if cost else None}
|
| 239 |
+
|
| 240 |
+
series = []
|
| 241 |
+
for s_ in _series([r for r in marks(books, days=math.ceil((datetime.now(timezone.utc) - since).total_seconds() / 86400) + 1) if r["ts"] >= since], books):
|
| 242 |
+
w0 = s_["data"][0][1]; series.append({"name": s_["name"], "data": [[ts, w - w0] for ts, w in s_["data"]]}) # P&L since the test began
|
| 243 |
+
st, _ = setting(rising_clean.STATUS_KEY); chg = st.get("breadth_chg")
|
| 244 |
+
return {"hidden": False, "running": True, "observer": observer, "unit": mk.unit, "started_at": t["started_at"], "go_live_at": t.get("go_live_at"),
|
| 245 |
+
"live_since": t.get("live_since"), "held": t.get("held"), "breadth_chg": chg, "rise": rising_clean.RISE_6H, "rising": None if chg is None else chg >= rising_clean.RISE_6H,
|
| 246 |
+
"book": stat(lambda r: r["book"] == rising_clean.BOOK), "selector": stat(lambda r: r["book"] == mk.selector_book),
|
| 247 |
+
"doubled": stat(lambda r: r["book"] == rising_clean.BOOK and r["doubled"]), "rest": stat(lambda r: r["book"] == rising_clean.BOOK and not r["doubled"]), "series": series}
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
@action("overview.rising_start")
|
| 251 |
+
def rising_start(body: dict) -> dict:
|
| 252 |
+
with transaction() as conn:
|
| 253 |
+
if rising_clean.state(conn) is not None:
|
| 254 |
+
raise ActionError("The rising + clean test has already started.")
|
| 255 |
+
v = rising_clean.start(conn)
|
| 256 |
+
return {"message": "Rising + clean test started: its book joins within ten minutes", "go_live_at": v["go_live_at"]}
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
@action("overview.rising_go_live")
|
| 260 |
+
def rising_go_live(body: dict) -> dict:
|
| 261 |
+
"""The go-live moves to ``at`` (an ISO time with its zone), or is cleared (``at`` null). The wallet goes back to its plain
|
| 262 |
+
stakes until the new time passes and the test is judged."""
|
| 263 |
+
at = body.get("at")
|
| 264 |
+
if at is not None:
|
| 265 |
+
try:
|
| 266 |
+
at = datetime.fromisoformat(at)
|
| 267 |
+
except (TypeError, ValueError):
|
| 268 |
+
raise ActionError("at must be an ISO time", 400)
|
| 269 |
+
if at.tzinfo is None:
|
| 270 |
+
raise ActionError("at must carry its time zone", 400)
|
| 271 |
+
if at.timestamp() < datetime.now(timezone.utc).timestamp() - GO_LIVE_PAST_S:
|
| 272 |
+
raise ActionError("That time has passed. Pick one from now on.")
|
| 273 |
+
with transaction() as conn:
|
| 274 |
+
if rising_clean.state(conn) is None:
|
| 275 |
+
raise ActionError("The rising + clean test has not started.")
|
| 276 |
+
rising_clean.set_go_live(conn, at)
|
| 277 |
+
return {"message": "Go-live cleared: the wallet keeps its plain stakes" if at is None else "Go-live moved"}
|
| 278 |
+
|
| 279 |
+
|
| 280 |
# ---------------------------------------------------------------- trades by hour of day
|
| 281 |
def _utc_of(ts) -> datetime | None:
|
| 282 |
"""A timestamp in UTC: a datetime, or the text Postgres wrote into an archive ('2026-09-15 03:55:00+00')."""
|
fly_trader/ui/api/trades.py
CHANGED
|
@@ -4,12 +4,12 @@ from __future__ import annotations
|
|
| 4 |
|
| 5 |
from ... import markets
|
| 6 |
from ...db.queries import q, q1
|
| 7 |
-
from ...ops.wallets import explorer
|
| 8 |
from ..hub import TopicError, topic
|
| 9 |
from ..state import system_state, labels, market_param
|
| 10 |
|
| 11 |
# The decision log's "Show" pills → the decision kinds each covers (the selector's, the fly's and the older generic kinds).
|
| 12 |
-
KINDS = {"Bought": ("selector_enter", "fly_enter", "enter"), "Sold": ("selector_exit", "fly_exit", "exit"), "Not taken": ("blocked",)}
|
| 13 |
ACTION = {k: p for p, ks in KINDS.items() for k in ks}
|
| 14 |
LIMIT = 300
|
| 15 |
|
|
@@ -46,8 +46,9 @@ def _thin(points: list, n: int = 180) -> list:
|
|
| 46 |
@topic("trades.summary", every=15)
|
| 47 |
def summary(params: dict) -> dict:
|
| 48 |
"""The book's results at a glance: the closed-trade metrics, the open positions, realized P&L cumulated trade by
|
| 49 |
-
trade and per day, and how the trades exited and which strategy took them.
|
| 50 |
-
|
|
|
|
| 51 |
c = q1("SELECT count(*) AS n, sum(realized_sol) AS pnl, avg(realized_sol / NULLIF(cost_sol, 0)) AS avg_ret, "
|
| 52 |
"count(*) FILTER (WHERE realized_sol > 0) AS wins FROM positions WHERE book = %s AND status = 'closed'", (book,)) or {}
|
| 53 |
o = q1("SELECT count(*) AS n, COALESCE(sum(cost_sol), 0) AS cost FROM positions WHERE book = %s AND status = 'open'", (book,)) or {}
|
|
@@ -62,7 +63,7 @@ def summary(params: dict) -> dict:
|
|
| 62 |
strategies = q("SELECT COALESCE(strategy, '') AS strategy, count(*) AS n, sum(realized_sol) AS pnl, count(*) FILTER (WHERE realized_sol > 0) AS wins "
|
| 63 |
"FROM positions WHERE book = %s AND status = 'closed' GROUP BY 1 ORDER BY 2 DESC, 1", (book,))
|
| 64 |
return {"chain": mk.chain, "book": book, "unit": mk.unit,
|
| 65 |
-
"metrics": {"n": n, "pnl": c.get("pnl"), "avg_ret": c.get("avg_ret"), "wins": wins, "win_rate": wins / n if n else None,
|
| 66 |
"open": int(o.get("n") or 0), "open_cost": float(o.get("cost") or 0)},
|
| 67 |
"cum": _thin(cum), "daily": [{"day": d["day"].isoformat(), "n": d["n"], "pnl": float(d["pnl"] or 0), "wins": d["wins"]} for d in daily],
|
| 68 |
"exits": [{"kind": e["kind"], "n": e["n"], "pnl": float(e["pnl"] or 0)} for e in exits],
|
|
|
|
| 4 |
|
| 5 |
from ... import markets
|
| 6 |
from ...db.queries import q, q1
|
| 7 |
+
from ...ops.wallets import explorer, money
|
| 8 |
from ..hub import TopicError, topic
|
| 9 |
from ..state import system_state, labels, market_param
|
| 10 |
|
| 11 |
# The decision log's "Show" pills → the decision kinds each covers (the selector's, the fly's and the older generic kinds).
|
| 12 |
+
KINDS = {"Bought": ("selector_enter", "fly_enter", "rising_enter", "enter"), "Sold": ("selector_exit", "fly_exit", "rising_exit", "exit"), "Not taken": ("blocked",)}
|
| 13 |
ACTION = {k: p for p, ks in KINDS.items() for k in ks}
|
| 14 |
LIMIT = 300
|
| 15 |
|
|
|
|
| 46 |
@topic("trades.summary", every=15)
|
| 47 |
def summary(params: dict) -> dict:
|
| 48 |
"""The book's results at a glance: the closed-trade metrics, the open positions, realized P&L cumulated trade by
|
| 49 |
+
trade and per day, and how the trades exited and which strategy took them. The live book's ``realized`` is its
|
| 50 |
+
wallet's (ops/wallets.money); ``pnl`` stays the closed trades'."""
|
| 51 |
+
mk = market_param(params); book = _book(params, mk); w = money(mk) if book == mk.live_book else None
|
| 52 |
c = q1("SELECT count(*) AS n, sum(realized_sol) AS pnl, avg(realized_sol / NULLIF(cost_sol, 0)) AS avg_ret, "
|
| 53 |
"count(*) FILTER (WHERE realized_sol > 0) AS wins FROM positions WHERE book = %s AND status = 'closed'", (book,)) or {}
|
| 54 |
o = q1("SELECT count(*) AS n, COALESCE(sum(cost_sol), 0) AS cost FROM positions WHERE book = %s AND status = 'open'", (book,)) or {}
|
|
|
|
| 63 |
strategies = q("SELECT COALESCE(strategy, '') AS strategy, count(*) AS n, sum(realized_sol) AS pnl, count(*) FILTER (WHERE realized_sol > 0) AS wins "
|
| 64 |
"FROM positions WHERE book = %s AND status = 'closed' GROUP BY 1 ORDER BY 2 DESC, 1", (book,))
|
| 65 |
return {"chain": mk.chain, "book": book, "unit": mk.unit,
|
| 66 |
+
"metrics": {"n": n, "pnl": c.get("pnl"), "realized": w["realized"] if w else c.get("pnl"), "avg_ret": c.get("avg_ret"), "wins": wins, "win_rate": wins / n if n else None,
|
| 67 |
"open": int(o.get("n") or 0), "open_cost": float(o.get("cost") or 0)},
|
| 68 |
"cum": _thin(cum), "daily": [{"day": d["day"].isoformat(), "n": d["n"], "pnl": float(d["pnl"] or 0), "wins": d["wins"]} for d in daily],
|
| 69 |
"exits": [{"kind": e["kind"], "n": e["n"], "pnl": float(e["pnl"] or 0)} for e in exits],
|
fly_trader/ui/static/console/assets/index-BxxFsYRV.js
DELETED
|
The diff for this file is too large to render.
See raw diff
|
|
|