VIX Futures Gridbot
GPT4 Generated Code Summary
This code is for processing financial data and backtesting a trading strategy using a grid system. Here's a summary of its components and functionality:
Data Processing:
Reads data from 'VIX_History.csv', extracting the last field from each line (presumably closing prices) and storing it in
data_close
.Applies settings from an external 'settings' module to determine the subset of data to use.
Utility Functions:
wholenums
: Returns a list of whole numbers between two given numbers.affordable_shares
: Calculates the number of shares that can be bought within an acceptable risk limit.
Trading Strategy Implementation:
Defines lists for buy signals, sell signals, and combined buy/sell signals.
Uses settings from the 'settings' module to create a range of price limits (
limit
) for triggering buy/sell orders.The
limits_hit
function detects when the price crosses these limits and records corresponding buy or sell signals.
Backtesting and Profit Calculation:
print_backtest_results
: Calculates and prints the grid profit, unrealized profit/loss, and total profit from the trading strategy.Buys and sells are tracked, and profit is calculated based on the difference between buy and sell prices, adjusted for the number of shares.
Plotting:
Visualizes the trading strategy by plotting the closing price data, buy/sell signals, and price limits.
Uses Matplotlib to create the plot, with buy signals in green, sell signals in red, and price limits in orange.
Execution:
The script executes the trading strategy over the data, plots the results, and then prints the backtest results.
The code is structured to backtest a grid trading strategy where buy and sell orders are triggered when the price crosses predefined limits. It then visualizes the strategy's performance and calculates the profitability.