Examples Gallery¶
Complete, copy-paste examples for common StreamForge use cases.
Quick Navigation¶
-
Get started with simple streaming examples
-
Save data to PostgreSQL, Kafka and CSV
-
Transform and enrich your data
-
Aggregation, backfilling, and multi-exchange
By Use Case¶
I want to...¶
...get started quickly¶
...save data to CSV¶
...save data to PostgreSQL¶
...stream to Kafka¶
...rename database columns¶
...aggregate 1m to 5m, 15m, 1h¶
...load historical data¶
...compare prices across exchanges¶
By Complexity¶
Beginner ⭐¶
Start here if you're new to StreamForge:
- Hello World - Simplest possible example
- CSV Output - Save to file
- Multiple Symbols - Track multiple assets
Intermediate ⭐⭐¶
Once you understand the basics:
- PostgreSQL - Database integration
- Upsert Patterns - Handle duplicates
- Transformers - Modify data
- Aggregation - Multiple timeframes
Advanced ⭐⭐⭐¶
For complex use cases:
- Backfilling - Historical data
- Multi-Exchange - Merge exchanges
- Custom Emitters - Build your own
Source Code¶
All examples are available in the GitHub repository:
Example Categories¶
Basic Streaming¶
Simple examples to get you started:
- Hello World
- CSV Output
- Multiple Symbols
- Different Timeframes
- Different Exchanges
Database Output¶
Save data to databases and streaming platforms:
- PostgreSQL Basic
- PostgreSQL with Upsert
- Kafka Streaming
- Multiple Outputs
Data Transformation¶
Modify and enrich your data:
- Rename Fields
- Add Computed Fields
- Filter Data
- Custom Transformers
Advanced Patterns¶
Complex real-world patterns:
- Multi-Timeframe Aggregation
- Historical Backfilling
- Multi-Exchange Merging
- Arbitrage Detection
- Production Patterns
Running Examples¶
Prerequisites¶
Install StreamForge:
For database examples, you'll also need:
# PostgreSQL
pip install streamforge # Already includes asyncpg
# For examples with additional dependencies
pip install -r examples/requirements.txt
Running an Example¶
# 1. Copy the example code
# 2. Save to a Python file
python my_example.py
# Or download from GitHub
git clone https://github.com/paulobueno90/streamforge.git
cd streamforge/examples
python 01_basic/hello_world.py
Example Template¶
Use this template for your own scripts:
"""
My StreamForge Script
=====================
Description: What this script does
Prerequisites:
- streamforge installed
- (any other requirements)
Run:
python my_script.py
"""
import asyncio
import streamforge as sf
async def main():
"""Main function"""
# 1. Configure stream
stream = sf.DataInput(
type="kline",
symbols=["BTCUSDT"],
timeframe="1m"
)
# 2. Create runner
runner = sf.BinanceRunner(stream_input=stream)
# 3. Add emitter(s)
runner.register_emitter(sf.Logger(prefix="StreamForge"))
# 4. Run!
await runner.run()
if __name__ == "__main__":
asyncio.run(main())
Tips & Tricks¶
Start with Logger¶
Always test with Logger
first:
Test with Short Periods¶
Use timeouts for testing:
import asyncio
async def main():
runner = sf.BinanceRunner(stream_input=stream)
runner.register_emitter(sf.Logger())
# Run for 30 seconds only
try:
await asyncio.wait_for(runner.run(), timeout=30)
except asyncio.TimeoutError:
print("Test complete!")
asyncio.run(main())
Getting Help¶
If you need help with examples:
- Check the User Guide
- Read Core Concepts
- See API Reference
- Ask on GitHub Discussions
- Report issues on GitHub Issues
Next Steps¶
Ready to dive in?
- Basic Streaming → - Start here
- Database Output → - Save your data
- Data Transformation → - Customize output
- Advanced Patterns → - Complex use cases