Use Case: Quant Analytics with pandas¶
This use case builds a Python analysis that takes a price history from 3forge Enterprise into pandas and writes volatility figures back.
Prerequisites¶
- Python 3.14 with the
pyami-3forgepackage. See the Python client. - A client certificate the server accepts, with
client.crt.pem,client.key.pem, and the server's3forge.pubin the working directory. See Connecting and Securing a Client.
How it fits together¶
The program works in 4 steps:
- Drop any earlier
QuantPricesandQuantVolatilitytables, then upload a toy history of 60 trading days for 3 symbols withsql_insert, which createsQuantPrices. - Read the history into a DataFrame with
pandas.read_sql_query, through the SQLAlchemy dialect. - Compute the statistics in pandas and upload them with
sql_insert, which createsQuantVolatility. - Read back the most volatile symbols with the query builder.
The day is stored as an integer, such as 20260102. A pandas datetime column would be stored as text; see Data that does not upload as expected.
1. Create the connection helper¶
The program connects through a helper that opens only the connections it asks for.
-
Save the following code as
connect.pyin the working directory: -
Confirm that the helper imports: run
python -c "import connect", and the command returns with no output.
2. Write the analysis¶
The analysis generates the history, then does the 4 steps above in order.
-
Save the following code as
analytics.py, besideconnect.py. The program runs in stage 3:
3. Run it¶
This stage runs the analysis and checks what it uploads and reads back.
- Run the program:
python analytics.py. -
Check that it prints the 2 uploads, then the symbols whose volatility is above 15%:
The history is generated from a fixed seed, so the figures are the same on every run.
Result¶
The program moved data from 3forge Enterprise into pandas and back: 180 prices out, 3 statistics in, read back with a filter. It drops both tables before it exits.