Crypto strategy research, backtesting, signal monitoring, and live-trading tooling.
This repository is a Python workspace for testing trading ideas against Binance candle data, running a local FastAPI backtest panel, and operating strategy-specific live coordinators against supported exchange adapters. It is intended for research and controlled live testing, not unattended production trading.
- Backtesting for
engulfing,ema_avwap_pullback,pinbar_magic_v3,stochastic_fsm, andstrong_trend_stair. - Binance candle ingestion with PostgreSQL-backed storage.
- A local web control panel for submitting and monitoring backtests.
- Live trading coordinators for
heiken_ashi,ema_avwap_pullback,pinbar_magic_v3, andstrong_trend_stair. - Bitunix futures adapter code plus helper scripts for positions, orders, and TP/SL management.
- CCXT-backed Weex spot and USDT-perpetual adapter support.
- Telegram signal notification for live engulfing-pattern scans.
- Experimental market-structure tools for pivots, BOS/CHOCH, and liquidity zones.
Live trading code can place real orders when configured with live credentials and live mode. Treat every config file under configs/ as sensitive once copied from an example template.
- Do not commit real API keys, Telegram tokens, or account identifiers.
- Prefer exchange testnet or dry-run flows before any live run.
- Review strategy settings, leverage, position sizing, and margin mode before starting a coordinator.
- Local
.envfiles can be read by CLI defaults and may appear in--helpoutput, so avoid sharing terminal logs from credentialed machines. - This repo does not provide high-availability monitoring, automatic key rotation, alert escalation, or operational guardrails expected in production systems.
- Python 3.10 or newer.
- PostgreSQL for candle storage.
pipand a virtual environment.- Node/npm only for JavaScript linting or future web asset work; the current web UI is plain checked-in static assets.
Install Python dependencies:
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -r requirements.txtIf your shell does not expose python after activating the virtual environment, use python3 in the commands below.
.
|-- backtest/ # Backtest engine, reports, plotting, and strategies
|-- candle_downloader/ # Binance candle client, downloader, and PostgreSQL store
|-- cmd/ # Python module entrypoints
|-- configs/ # Live-trading .env templates and config notes
|-- experiments/ # Pivot, liquidity-zone, BOS/CHOCH, and CSV helpers
|-- live_trading/ # Coordinators, exchange adapters, position management
|-- scripts/ # Launchers, candle utilities, and Bitunix helper CLIs
|-- signal_notifier/ # Telegram signal scanner and notifier
|-- tests/ # Unit tests
|-- web/ui/ # Static browser UI
`-- webserver/ # FastAPI app and backtest job manager
Candle storage is PostgreSQL-only.
createdb scalp_test
export CANDLE_DB_HOST=localhost
export CANDLE_DB_PORT=5432
export CANDLE_DB_USER=postgres
export CANDLE_DB_PASSWORD=postgres
export CANDLE_DB_NAME=scalp_testSupported database environment variables:
CANDLE_DATABASE_URLorDATABASE_URLCANDLE_DB_HOST,CANDLE_DB_PORT,CANDLE_DB_USER,CANDLE_DB_PASSWORD,CANDLE_DB_NAMECANDLE_DB_SSLMODE,CANDLE_DB_MIN_POOL_SIZE,CANDLE_DB_MAX_POOL_SIZE- Matching
POSTGRES_*aliases CANDLE_DB_ENV_FILEorPOSTGRES_ENV_FILEpointing to an env file
The candles table and index are created automatically by the storage layer.
Backtests download missing Binance candles into PostgreSQL, load the requested range, run the selected strategy, write statistics JSON, and optionally save an interactive Plotly chart.
Basic example:
python -m cmd.backtest.main \
--strategy pinbar_magic_v3 \
--symbol BTCUSDT \
--timeframe 1h \
--start 2025-01-01T00:00:00Z \
--end 2025-02-01T00:00:00Z \
--initial-capital 10000 \
--store-kind postgres \
--stats-output results/pinbar_magic_v3_stats.json \
--plot-output results/pinbar_magic_v3.htmlSupported strategy names:
engulfingema_avwap_pullbackpinbar_magic_v3stochastic_fsmstrong_trend_stair
Useful flags:
--override-downloadredownloads candles even when local rows exist.--store-path path/to/db.envloads PostgreSQL settings from an env file.--http-proxy,--https-proxy, or--proxyroute Binance requests through a proxy.--show-plotopens the generated Plotly chart in a browser.--no-stochasticand--no-equityhide chart subplots.
Strategy settings can be supplied with CLI flags or environment variables. Run a strategy-specific help command for the exact options:
python -m cmd.backtest.main --strategy engulfing --help
python -m cmd.backtest.main --strategy ema_avwap_pullback --help
python -m cmd.backtest.main --strategy pinbar_magic_v3 --help
python -m cmd.backtest.main --strategy stochastic_fsm --help
python -m cmd.backtest.main --strategy strong_trend_stair --helpEMA + AVWAP backtests use the same setup, sizing, EMA, expiry, and
protective-stop rules as live trading. Their default execution contract is
--entry-mode close --exit-mode close: every entry signal and AVWAP target is
decided from a completed candle, which is the only like-for-like comparison
available from ordinary OHLCV history. Their environment counterparts are
STRATEGY_ENTRY_MODE, STRATEGY_EXIT_MODE, and STRATEGY_EXIT_BAND; see
configs/backtest.ema_avwap_pullback.env.example for a complete example.
live remains available for scenario analysis, but it is clearly marked in
the result metadata as an OHLC approximation: a forming-candle AVWAP and the
sequence of live quotes cannot be reconstructed from a bar's final OHLCV.
Rigid stops and trailing protection remain simulated over the documented
deterministic OHLC path in either target mode, because production enforces
those protections live.
Start the local FastAPI panel:
./scripts/run_web.sh --localThen open:
http://127.0.0.1:9092
--local binds to 127.0.0.1:9092, disables HTTPS enforcement, and trusts localhost. Without --local, configure these environment variables as needed:
WEB_HOSTWEB_PORTWEB_LOG_LEVELWEB_FORCE_HTTPSWEB_TRUSTED_HOSTSWEB_ALLOWED_ORIGINS
Main API surfaces:
POST /api/backtestsGET /api/backtests/{job_id}GET /api/backtests/{job_id}/resultWS /ws/backtests/{job_id}
Additional experiment servers have dedicated launch scripts:
./scripts/run_pivot_server.sh
./scripts/run_bos_choch_server.sh
./scripts/run_liquidity_zone_server.shDownload candles directly to CSV:
python scripts/download_candles_to_csv.py \
--symbol BTCUSDT \
--timeframe 15m \
--start 2025-01-01T00:00:00Z \
--end 2025-01-07T00:00:00Z \
--output data/BTCUSDT-15m.csvCheck PostgreSQL candle completeness and optionally redownload missing data:
python scripts/check_missing_candles.py \
--db-kind postgres \
--pg-host localhost \
--pg-port 5432 \
--pg-user postgres \
--pg-password postgres \
--pg-db scalp_test \
--symbol BTCUSDT \
--timeframe 15m \
--start-date 2025-01-01T00:00:00Z \
--end-date 2025-02-01T00:00:00Z \
--redownload-from-binanceCreate a strategy config from a template:
cp configs/live_trading.heiken_ashi.env.example configs/live_trading.heiken_ashi.env
cp configs/live_trading.ema_avwap_pullback.env.example configs/live_trading.ema_avwap_pullback.env
cp configs/live_trading.pinbar_magic_v3.env.example configs/live_trading.pinbar_magic_v3.env
cp configs/live_trading.strong_trend_stair.env.example configs/live_trading.strong_trend_stair.envEdit only the strategy file you plan to run. See configs/README.md for the full variable list and resolution order.
Run through the generic launcher:
CONFIG_FILE=configs/live_trading.pinbar_magic_v3.env ./scripts/run_live_trading.shOr run a strategy module directly:
python -m cmd.live_trading.heiken_ashi_main --config-file configs/live_trading.heiken_ashi.env
python -m cmd.live_trading.ema_avwap_pullback_main --config-file configs/live_trading.ema_avwap_pullback.env
python -m cmd.live_trading.pinbar_magic_v3_main --config-file configs/live_trading.pinbar_magic_v3.env
python -m cmd.live_trading.strong_trend_stair_main --config-file configs/live_trading.strong_trend_stair.envRun EMA + AVWAP for multiple independent symbols from one shared config:
scripts/run_ema_avwap_pullback_live_multi.sh \
--config-file configs/live_trading.ema_avwap_pullback.env \
--symbols ETHUSDT,BTCUSDT,SOLUSDT \
--mode blockingUse --mode async to start each symbol in the background with separate state,
positions DB, kline DB, and log files under ./data/ema_avwap_pullback/<SYMBOL>/
and ./logs/ema_avwap_pullback/<SYMBOL>/.
Config precedence for live trading is:
- CLI arguments
- OS environment variables
- Strategy config file
- Built-in defaults
Common live-trading settings include:
STRATEGY_NAMEEXCHANGETRADING_MODEAPI_KEY,API_SECRET,API_PASSPHRASETESTNETLEVERAGEPOSITION_SIZE_USDTMAX_CONCURRENT_POSITIONSMARGIN_MODESTATE_FILE,POSITIONS_DB,KLINES_DB,LOG_FILETELEGRAM_ENABLED,TELEGRAM_BOT_TOKEN,TELEGRAM_CHAT_ID
Use --help on a specific module before changing live settings:
python -m cmd.live_trading.ema_avwap_pullback_main --help
python -m cmd.live_trading.pinbar_magic_v3_main --helpEMA + AVWAP defaults to EXCHANGE=bitunix. In that mode all symbols except
ZECUSDT execute on Bitunix; ZECUSDT is automatically routed to Weex because
Bitunix does not list it. Configure WEEX_API_KEY, WEEX_API_SECRET, and
WEEX_API_PASSPHRASE for this fallback. Set EXCHANGE=weex to execute every
configured symbol on Weex. EMA + AVWAP requires TRADING_MODE=futures so its
native protective-stop safety policy can be enforced. CCXT's Weex sandbox
supports swap markets only; direct Weex spot trading requires mainnet.
Monitor Binance symbols for engulfing signals and send Telegram notifications:
python -m cmd.signal_notifier.main \
--timeframe 15m \
--top-n 100 \
--telegram-token "$TELEGRAM_BOT_TOKEN" \
--telegram-chat-id "$TELEGRAM_CHAT_ID"Use --dry-run to log signals without sending Telegram messages:
python -m cmd.signal_notifier.main --timeframe 15m --symbols BTCUSDT,ETHUSDT --dry-runThese scripts use BITUNIX_API_KEY and BITUNIX_API_SECRET unless --key and --secret are provided.
export BITUNIX_API_KEY=...
export BITUNIX_API_SECRET=...Common helpers:
scripts/bitunix_list_positions.pyscripts/bitunix_place_position_tpsl.pyscripts/bitunix_modify_position_tpsl.pyscripts/bitunix_update_stop_loss.pyscripts/bitunix_order_smoke_test.py
Example:
python scripts/bitunix_list_positions.py --symbol BTCUSDTPlace position-level TP/SL:
python scripts/bitunix_place_position_tpsl.py \
--symbol BTCUSDT \
--position-id 123456 \
--sl-price 27000 \
--sl-stop-type MARK_PRICERun the unit test suite:
python -m pytestRun the type checker:
ty check --exit-zero-on-warningRun npm tooling:
npm install
npm exec eslint .For the Debian deployment at jzbe.jazebeh.ir:15443, use the checked-in
end-to-end deployer and guide in deploy/debian/README.md.
It creates a systemd service, configures Nginx TLS and WebSocket proxying,
creates or validates the PostgreSQL candle store, and requires HTTP Basic
Authentication before exposing the panel.
The older files in deploy/systemd/ and deploy/nginx/ are example templates
for other hosts; do not apply them unchanged to this deployment.
This project is licensed under the terms in LICENSE.