2.8 KiB
Trade Analysis Tools
This directory contains tools for analyzing backtest results from Freqtrade.
tradestocsv.py
Converts Freqtrade backtest JSON results to CSV and enhanced JSON formats with position adjustment information.
Features:
- Converts backtest results to CSV format for easy analysis
- Enhances JSON output with detailed position entry information
- Calculates average entry prices, initial entry costs, and adjustment costs
- Handles both real order data and fallback scenarios
Fields Added:
entry_orders_count: Number of entry orders for the tradeadjustments_count: Number of position adjustments (entry_orders_count - 1)avg_entry_price: Average price across all entry ordersinitial_entry_cost: Cost of the initial entry ordertotal_adjustment_cost: Total cost of all adjustment ordersentries: Detailed array of all entry orders with:order_index: Order sequence number (0 for initial)timestamp: Order execution time (millisecond timestamp)price: Order execution priceamount: Order quantitycost: Order total costorder_type: 'initial' or 'adjustment'
Usage:
cd tools
python tradestocsv.py
# Run analysis script (including timing analysis)
python analyze_entry_timing.py
The script will automatically:
- Find the largest backtest result JSON file in the
../resultdirectory - Convert it to both CSV and enhanced JSON formats
- Save the outputs to
../result/backtest_trades.csvand../result/backtest_trades.json
Environment Variables:
STRATEGY_NAME: Strategy name to analyze (default: 'FreqaiPrimer')
Output Format:
The CSV and JSON files will contain all original trade information plus the additional fields listed above for comprehensive trade analysis.
Explanation of Position Adjustment Timing Issues
Analysis reveals the following characteristics in current backtest data:
- Only two orders per trade: One entry order and one exit order
- Identical open and close times: All trades have exactly the same
open_dateandclose_date - No actual position adjustments: The
adjustments_countfield is always 0, indicating no position adjustment behavior
This means there are no actual position adjustment time intervals to analyze in the current data. If position adjustment timing strategy analysis is needed, the corresponding position adjustment functionality should be enabled in backtesting to generate data containing multiple entry operations.
Timestamp handling notes:
- The script prioritizes using the
order_filled_timestampfield from orders - If not available, it falls back to the
timestampfield - Finally falls back to the trade's
open_datefield - All timestamps are stored in millisecond format for precise time calculations