this is my complete project. have to divide it into 2 phases. for the first phase i need the MVP. i need good quality code. if necessary make use of design patterns to organize code but the core of he logic and the trasnfromer should be top class. build this mvp. no threading needed or now. no sql database. alot of daignostics for the tesor flow model
PROject description:
focus and explain the working of the model
ubse biannce websockets for data
crypto scalping bot 2
AAVE/USD, AVAX/USD, BAT/USD,
Build a transformer model that predicts the price of change of 15 crypto currencies
the model uses 5 second data and an input sequence of 200 columns and 80-100 time steps or rows
the model generates 4 target columns for 10-18 rows
calculate market environments using daily data and 5 minute data
market conditions, leg data and percent change classification are described below
there is also a comprehensive list of technical indicators being used for input feature enhancements to the model
The project will be trained with sliding window/ walk forward training one day at a time with testing
First we need to understand market conditions
To determine nine distinct market environments based on combinations of volatility (high, medium, low) and trend direction (bullish, neutral, bearish), you can use a combination of technical indicators and statistical thresholds. These indicators and metrics can give insights into market dynamics and help define the specific conditions.
Here's a breakdown of how to algorithmically determine these conditions:
1. Volatility Classification (High, Medium, Low)
Volatility refers to how much the price fluctuates within a certain period. You can measure volatility using these methods:
a. Average True Range (ATR)
The ATR measures market volatility by averaging the true range over a certain period (e.g., 14 periods).
Formula: ATR is the moving average of the true range, where the true range is the maximum of the following:
Current high - current low
Absolute value of current high - previous close
Absolute value of current low - previous close
Algorithm:
High Volatility: If today's ATR is significantly higher than a moving average of ATR (e.g., 20% above a 14-period ATR average).
: If today's ATR is within ±10% of the ATR moving average.
Assistant
Medium Volatility
Low Volatility: If today's ATR is significantly lower than the moving average (e.g., 20% below the 14-period ATR).
b. Bollinger Bands Width (BB Width)
Bollinger Bands use standard deviation to define volatility. The width of the bands can be used to measure market volatility.
Formula:
Upper Band = 20-period moving average + 2 * standard deviation
Lower Band = 20-period moving average - 2 * standard deviation
Band Width = (Upper Band - Lower Band) / Moving Average
Algorithm:
High Volatility: If the Band Width is significantly wider than its average over a period (e.g., 20% above the 20-period moving average of Band Width).
Medium Volatility: If the Band Width is within ±10% of the moving average.
Low Volatility: If the Band Width is significantly narrower than the moving average (e.g., 20% below).
c. Realized Volatility (RV)
Formula:
Take the standard deviation of daily returns over a defined window (e.g., 20 days).
Algorithm:
High Volatility: If realized volatility is in the top 20% of values over the last month.
Medium Volatility: If realized volatility is within the middle 60% range.
Low Volatility: If realized volatility is in the bottom 20%.
Market trend can be determined using a variety of momentum and trend-following indicators.
a. Moving Averages (Simple or Exponential)
Look at the relationship between short-term and long-term moving averages (e.g., 50-day vs. 200-day).
Algorithm:
Bullish: If the short-term moving average (e.g., 50-period) is above the long-term moving average (e.g., 200-period), the market is considered in an uptrend.
Neutral: If the short-term and long-term moving averages are close to each other or crossing.
Bearish: If the short-term moving average is below the long-term moving average.
b. Relative Strength Index (RSI)
RSI measures the speed and change of price movements to indicate overbought or oversold conditions.
Algorithm:
Bullish: If RSI is above 60 (signaling strength).
Neutral: If RSI is between 40 and 60 (indicating balanced pressure).
Bearish: If RSI is below 40 (signaling weakness or oversold conditions).
c. MACD (Moving Average Convergence Divergence)
MACD measures the distance between short-term and long-term exponential moving averages.
Algorithm:
Bullish: If the MACD line is above the signal line and positive.
Neutral: If the MACD line is close to the signal line and near zero.
Bearish: If the MACD line is below the signal line and negative.
d. Trend Strength Indicator: Average Directional Index (ADX)
ADX measures the strength of a trend.
Algorithm:
ADX above 25 indicates a strong trend (bullish or bearish).
ADX below 25 indicates a weak trend or range-bound market (neutral).
3. Combining Volatility and Trend for 9 Market Environments
Once you have classified volatility and trend, you can create nine distinct market environments:
Volatility
Bullish
Neutral
Bearish
High
High Vol/Bullish
High Vol/Neutral
High Vol/Bearish
Medium
Med Vol/Bullish
Med Vol/Neutral
Med Vol/Bearish
Low
Low Vol/Bullish
Low Vol/Neutral
Low Vol/Bearish
Example of Defining Market Environment:
High Volatility/Bullish:
ATR or Bollinger Band width is significantly higher than average, and short-term moving average is above the long-term moving average.
RSI is above 60, MACD line is above the signal line.
Medium Volatility/Neutral:
ATR or Bollinger Band width is close to average, and the short-term moving average is close to or crossing the long-term moving average.
RSI is between 40 and 60, MACD line is near zero.
Low Volatility/Bearish:
ATR or Bollinger Band width is lower than average, and short-term moving average is below the long-term moving average.
RSI is below 40, MACD line is below the signal line.
4. Implementation Plan
Use the above indicators to calculate the volatility and trend classification for the past day or trading session.
use all the indicators and create a voting mechanism where each indicator votes for a market condition
the condition with the most votes is considered the actual
use the market condition data to optimize trading logic and bot deployment
Combine the volatility and trend indicators for each of the last day’s data points (5-second intervals, in your case).
Output the resulting market environment label for each time step to build a continuous understanding of the market conditions.
This approach will give you a consistent way to label market conditions in real-time. By storing these labels, you can assess how different market regimes perform for your trading models and algorithms.
Leg data explained
Key Variables:
Leg Magnitude (current_leg_change): This represents the percent change in price during the leg, ranging between 1 (for a 100% decrease) and 1 (for a 100% increase).
Leg Length (current_leg_length): This counts the number of time steps (e.g., 5-second intervals) during which the price has been moving in a consistent direction.
The previous leg stores similar information but pertains to the leg completed when the price direction changes.
Revised Algorithm Breakdown
1. Initialize Variables
At the start of the time series:
Current Leg Percent Change (current_leg_change): Tracks the cumulative percent change for the ongoing leg.
Current Leg Length (current_leg_length): Tracks how many time steps the current leg has lasted.
Previous Leg Percent Change (previous_leg_change): Cumulative percent change for the previous leg.
Previous Leg Length (previous_leg_length): Number of time steps for the previous leg.
2. Calculate Percent Change:
For each new time step, calculate the percent change of the price:
This will yield a value between 1 and 1 (with positive values indicating an increase and negative values indicating a decrease).
3. Determine Continuation or New Leg:
If the current price movement is in the same direction (positive or negative percent change):
Continue the current leg:
Add the percent_delta to current_leg_change (accumulating the magnitude of the leg).
Increment current_leg_length by 1.
If the price movement changes direction (i.e., from a positive to a negative percent change or vice versa):
Update the previous leg:
Set previous_leg_change = current_leg_change.
Set previous_leg_length = current_leg_length.
Start a new leg:
Set current_leg_change = percent_delta (initialize the new leg with the percent change of this candle).
Set current_leg_length = 1 (new leg starts with the first time step).
4. Algorithm Example
python
python
Copy code
# Initialize leg tracking variables
previous_leg_change = 0
previous_leg_length = 0
current_leg_change = 0
current_leg_length = 0
for t in range(1, len(prices)): # prices is the list of closing prices
# Calculate the percent change between the current and previous price
percent_delta = (prices[t] - prices[t-1]) / prices[t-1]
# Check if this is the first leg
if current_leg_length == 0:
current_leg_change = percent_delta
current_leg_length = 1
else:
# Check if the price movement continues in the same direction
if (current_leg_change > 0 and percent_delta > 0) or (current_leg_change < 0 and percent_delta < 0):
# Continue current leg
current_leg_change += percent_delta # Accumulate the percent change
current_leg_length += 1 # Increment leg length
else:
# Direction changed, finalize the previous leg
previous_leg_change = current_leg_change
previous_leg_length = current_leg_length
# Start a new current leg
current_leg_change = percent_delta
current_leg_length = 1 # Reset leg length to 1 for the new leg
# Store or print the results for each time step
print(f"Time Step {t}:")
print(f"Previous Leg: Percent Change={previous_leg_change:.4f}, Length={previous_leg_length}")
print(f"Current Leg: Percent Change={current_leg_change:.4f}, Length={current_leg_length}")
Example Walkthrough
Let’s walk through the algorithm with some sample price data:
Time Step
Close Price
Percent Change
Current Leg Change
Current Leg Length
Previous Leg Change
Previous Leg Length
1
100
-
-
-
-
-
2
102
0.02
0.02
1
-
-
3
104
0.0196
0.0396
2
-
-
4
103
-0.0096
-0.0096
1
0.0396
2
5
101
-0.0194
-0.0290
2
0.0396
2
6
100
-0.0099
-0.0389
3
0.0396
2
7
101
0.01
0.01
1
-0.0389
3
Explanation:
Step 1 & 2: Price goes from 100 to 102, a +2% change, starting a new leg.
Current leg: 1 time step, +2% change.
Step 3: Price continues upward to 104, a +1.96% change, so the current leg continues.
Current leg: 2 time steps, total +3.96% change.
Step 4: Price reverses to 103, a 0.96% change. The current leg ends, and a new leg starts.
Previous leg: 2 time steps, +3.96% change.
New current leg: 1 time step, -0.96% change.
Step 5: Price drops further to 101, a 1.94% change, continuing the current (down) leg.
Current leg: 2 time steps, -2.90% change.
Step 6: Price continues to fall to 100, a 0.99% change.
Current leg: 3 time steps, -3.89% change.
Step 7: Price rises back to 101, a +1% change, starting a new leg.
Previous leg: 3 time steps, -3.89% change.
New current leg: 1 time step, +1% change.
Summary of Key Outputs:
At each time step, you now have:
Previous Leg:
Percent Change: The total percent price movement during the previous leg.
Length: The number of time steps during which the previous leg persisted.
Current Leg:
Percent Change: The accumulated percent price movement for the current leg.
Length: The number of time steps the current leg has lasted so far.
By tracking both percent change and leg length, you can dynamically capture the magnitude and duration of each directional price movement at every step in your time series, and the previous leg becomes available for reference once a price reversal occurs.
Discrete classification of percent change
To implement a percent change classification mechanism that groups continuous percent changes into discrete "buckets," we can design the process in several steps. The goal is to define meaningful percent change categories that simplify the model's task of predicting directional movements, while preserving enough granularity to capture market dynamics. We'll use statistical analysis to define these levels and categorize them effectively.
Step-by-Step Process:
1. Define the Classification Labels
You suggested 7 classifications that can represent the magnitude and direction of percent changes. These classifications can be broken down as:
Down a Lot
Down Moderate
Down a Little
No Change
Up a Little
Up Moderate
Up a Lot
Each classification will represent a range of percent changes between time steps. This will help the model generalize well without needing to predict exact percent values, allowing some margin for error.
2. Gather Percent Change Data
To effectively determine the boundaries for each classification, we need to statistically analyze the distribution of percent changes in your time series data.
Calculate Percent Changes between each time step:
Percent change between time steps:percent_change(t)=price(t−1)price(t)−price(t−1)
percent_change(t)=price(t)−price(t−1)price(t−1)
Create a Histogram of the percent change data:
A histogram will show how frequently different percent changes occur in the data.
This can help identify natural boundaries for the different percent change classifications.
3. Determine Boundaries with Statistical Insights
To assign reasonable boundaries to the percent change categories, we will use the histogram and some statistical techniques to define how each category should be segmented.
Key Methods:
Percentile-based Binning:
One approach is to use percentiles to define the boundaries. This ensures that the classification boundaries reflect the natural distribution of the data, preventing categories from being too rare or too common.
For example:
The bottom 5% of percent changes could represent Down a Lot.
The 5th to 20th percentile could represent Down Moderate.
The 20th to 40th percentile could represent Down a Little.
The 40th to 60th percentile could represent No Change.
The 60th to 80th percentile could represent Up a Little.
The 80th to 95th percentile could represent Up Moderate.
The top 5% of percent changes could represent Up a Lot.
Histogram-based Binning:
Alternatively, you can analyze the shape of the histogram directly.
Look for natural gaps or clusters in the histogram where percent changes seem to be more or less common.
These gaps can help define ranges that group frequent changes together, while separating rare extreme movements into their own categories.
4. Fine-tuning the Percent Change Levels
Once you have the histogram and percentiles, you can fine-tune the thresholds for each bucket to ensure that each class captures meaningful ranges of percent changes and doesn’t overly cluster or sparsely distribute the data.
For example, after examining the histogram, the boundaries might look like this:
Down a Lot: Percent change less than 3% (extreme downward moves).
Down Moderate: Percent change between 1.5% and 3%.
Down a Little: Percent change between 0.5% and 1.5%.
No Change: Percent change between 0.5% and +0.5%.
Up a Little: Percent change between +0.5% and +1.5%.
Up Moderate: Percent change between +1.5% and +3%.
Up a Lot: Percent change greater than +3% (strong upward moves).
5. Implementing the Classification in the Model
Now that we have clear classifications, these can be represented as discrete labels for the model. Each time step’s percent change will be categorized into one of the 7 buckets:
If the percent change is between -1.5% and -0.5%, it gets classified as "Down a Little".
If the percent change is greater than +3%, it gets classified as "Up a Lot", etc.
Example Code to Create the Bins and Labels
python
python
Copy code
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Assuming you have a DataFrame df with a column 'price'
df['percent_change'] = df['price'].pct_change()
# Step 1: Remove NaNs from percent change (from pct_change)
df.dropna(subset=['percent_change'], inplace=True)
# Step 2: Plot a histogram of percent changes
plt.hist(df['percent_change'], bins=100)
plt.title('Histogram of Percent Changes')
plt.xlabel('Percent Change')
plt.ylabel('Frequency')
plt.show()
# Step 3: Define percentiles (or you can visually examine histogram)
percentiles = np.percentile(df['percent_change'], [5, 20, 40, 60, 80, 95])
# Step 4: Define classification based on percentiles or chosen bins
def classify_percent_change(x):
if x < percentiles[0]:
return 'Down a Lot'
elif x < percentiles[1]:
return 'Down Moderate'
elif x < percentiles[2]:
return 'Down a Little'
elif x < percentiles[3]:
return 'No Change'
elif x < percentiles[4]:
return 'Up a Little'
elif x < percentiles[5]:
return 'Up Moderate'
else:
return 'Up a Lot'
# Apply classification
df['percent_change_classification'] = df['percent_change'].apply(classify_percent_change)
# View the resulting classifications
print(df[['percent_change', 'percent_change_classification']].head())
6. Benefits of This Approach
Flexibility: By basing the classification on the data’s percentiles or the histogram, the categories adapt to different market conditions and volatility levels.
Simplicity for the Model: The model now has fewer discrete outputs to predict, making it easier to handle slight inaccuracies in the predictions without too much penalty.
Statistical Rigor: Using the actual data distribution helps ensure that the classification reflects how the market behaves, rather than arbitrarily choosing ranges.
7. Integrating into the Transformer Model
The discrete percent change classifications will be part of the output sequence that the model predicts. Instead of outputting raw percent changes, the model will predict one of the 7 categories. During training, the actual percent changes are converted into their respective classification categories using the above binning system, and the model’s task is to predict the correct bucket.
Outputs:
Price Prediction: A normalized predicted price or percent change.
Discrete Percent Change Classification: One of the 7 categories (e.g., "Up Moderate") and confidence score.
Leg Prediction: Current leg
Summary
Percent Change Classifications: We discretize the continuous percent changes into 7 meaningful buckets based on the data distribution.
Statistical Insights: Using a histogram or percentile-based binning ensures that these classifications are relevant and provide clear distinctions for the model.
Prediction Task: The model is tasked with predicting these classifications, allowing some margin of error for more stable performance.
Model development
1. Data Pipeline Construction
1.1 Data Sources
Historical Price Data:
Use the Polygon API to fetch historical price data for all the currencies available on Alpaca.
Market Condition Data:
Fetch relevant data like SPY (S&P 500 ETF), VIX (Volatility Index), and additional indicators such as volume, and volatility measures.
Retrieve technical indicators like RSI, MACD, Donchian Channels, Heikin Ashi Candles, Nadaraya-Watson Envelope, Money Flow Multiplier, and ATR.
1.2 Feature Engineering and Normalization
Calculate Percent Change between consecutive price points and derive the Leg Data as described (magnitude of percent change and leg length).
Current Leg Data: Percent change and the number of time steps since the price started moving in the current direction.
Previous Leg Data: Percent change and length of the last completed leg when price movement changed direction.
Market Conditions:
Define market states (bullish, neutral, bearish; high, medium, low volatility) using historical analysis of key metrics (e.g., VIX, SPY correlation with price).
Each time step should include the identified market condition, either as binary inputs (bullish/bearish, high/low volatility) or categorical labels.
Percent Change Classification:
Implement the 7-level classification system for percent change at each time step.
The labels are:
Down a lot
Down moderate
Down a little
No change
Up a little
Up moderate
Up a lot
These classifications will be a target column in the model to help with prediction.
1.3 Data Preprocessing
Normalize all inputs for each currency's time series (price, technical indicators, leg data, market conditions) to be within a uniform range, so the model can process them efficiently.
Chunk the time-series data into 5-second time steps.
Example: If a sequence has data points every 5 seconds, group 120 time steps (10 minutes) as a single input sequence for training the model.
Prepare two variations of the input sequence structure:
Group by asset (each currency and its associated data points within each time step).
Group by data point (for each time step, group together the same data point across different assets).
1.4 Database Storage
Store the processed features in a time-series database (like InfluxDB or TimescaleDB) for efficient retrieval during training and testing.
2. Model Architecture: Transformer Model
2.1 Input Data Format
The Transformer will take in sequences of time steps, with each step containing:
Price data (OHLC, Heikin Ashi).
Leg data (current and previous leg’s percent change and length).
Percent change classification (as a target output).
Percent Change Classification (using the 7-level classification system).
Leg Data Predictions:
Predict the current leg's direction (percent change) and its length.
Predict the next 18 time steps for each asset, focusing on whether the current leg will continue or reverse.
normalized predicted price:
just predict the close price for the predicted time step
Confidence Score:
Include a confidence score for the discrete percent change classification to assess how certain the model is of the market movement.
3. Training and Testing Strategy
3.1 Walk-Forward Training
Step 1: Initial Training
Start by training the model on 20 days of historical data using the full set of features (price, technical indicators, market conditions, etc.).
Use the walk-forward method where you train on the first chunk (20 days), then test on the next few days.
Step 2: Walk Forward Testing
After testing on the next 1-3 days, fine-tune the model with the data from the most recent day.
Test again on the following day, and repeat the process, simulating how the model would be updated daily during live deployment.
This allows the model to remain adaptive to recent market behavior and improve its short-term predictions.
3.2 Rolling Training on 100-Day Windows
Use another training regime where the model is retrained every few days on the last 100 days of data instead of fine-tuning it daily.
This helps the model capture longer-term patterns in the data without overfitting to very recent, short-term movements.
3.3 Backtesting and Saving Results
During testing, store the generated predicted price data and market condition predictions for backtesting.
Save these results to assess how well the model's predictions align with actual market movements.
The backtest data can later be used to test different trading strategies based on model signals.
4. Using the Model for Trading Signals
4.1 Model-Based Trading Logic
The model will be run every 15 seconds during live deployment to make predictions.
4.2 Triggering Trades Based on Predicted Percent Change
Use the predicted percent change classification to determine the direction of the next trade:
Down a Lot/Moderate: Place a sell order if the confidence score is high.
Up a Lot/Moderate: Place a buy order.
No Change: Stay in the current position or hold.
4.3 Leg Data and Market Condition Analysis
Use the leg data predictions to detect potential trend reversals or continuation patterns:
If the model predicts a continuation of the current leg with a high confidence score, maintain the current trade.
If the model predicts a leg reversal (a switch from bullish to bearish), close or reverse the position.
Analyze market conditions (bullish, bearish, or neutral) alongside leg data:
In a bullish market, favor buy signals and trade conservatively on predicted down movements.
In a bearish market, prioritize sell signals.
In a neutral market, only trade on strong directional confidence from the percent change classification.
4.4 Risk Management
Implement stop-loss or take-profit orders based on the model's confidence score:
Higher confidence scores can allow for larger trades or a wider stop-loss range.
Lower confidence should trigger smaller trade sizes or tighter risk parameters.
4.5 Backtesting Trading Strategies
Backtest multiple trading strategies using the stored model predictions and assess profitability and risk.
Example strategies to test:
Trend-following strategy: Trade in the direction of the predicted leg continuation.
Mean-reversion strategy: Trade against predicted large upward or downward moves in neutral market conditions.
Summary of Key Components for the Developer
Data Pipeline: Gather historical price data, compute technical indicators, and preprocess them.
Model Inputs: Price, leg data, market conditions, percent change classification, and technical indicators.
Model Outputs: Predicted price change, percent change classification, leg data predictions, and confidence scores.
Training and Testing:
Walk-forward training with daily fine-tuning.
Rolling 100-day window retraining.
Trading Logic: Use predictions to trigger trades, manage risk, and backtest strategies.
This approach will integrate a robust prediction mechanism based on market conditions, technical indicators, and advanced time-series analysis to facilitate intelligent trading decisions.
Example table
Let's create an example table that represents a time series dataset for predicting cryptocurrency price movements, including both the inputs and targets. Each row will represent a single time step, and each column will represent a feature, including technical indicators, leg data, market conditions, and targets like price change and percent change classification.
Example Table Structure
Time Step: The timestamp or index for each row (not explicitly shown but implied).
Inputs: The model inputs at each time step.
Targets: The columns the model will predict.
Time Step
Open
High
Low
Close
Heikin Ashi Close
RSI
MACD Line
Donchian Upper
Donchian Lower
ATR
Leg Percent Change (Current)
Leg Length (Current)
Leg Percent Change (Previous)
Leg Length (Previous)
Market Condition
SPY
VIX
Nadaraya-Watson
Percent Change
Percent Change Class
Target: Predicted Price
Target: Percent Change Class
Target: Leg Direction
Target: Leg Length
00:00:00
1.00
1.02
0.98
1.01
1.005
55
0.002
1.02
0.98
0.05
-0.5%
20
+1.5%
50
Bullish
420
18
1.01
+0.1%
Up a Little
1.012
Up a Little
+0.05
5
00:00:05
1.01
1.03
0.99
1.02
1.015
58
0.003
1.03
0.99
0.06
+0.5%
25
-0.5%
20
Bullish
421
17
1.02
+0.15%
Up Moderate
1.025
Up Moderate
+0.10
10
00:00:10
1.02
1.05
1.00
1.03
1.025
60
0.005
1.04
1.00
0.07
+1.0%
30
-0.5%
20
Bullish
422
16
1.03
+1.0%
Up a Lot
1.038
Up a Lot
+0.15
15
00:00:15
1.03
1.06
1.01
1.04
1.035
62
0.008
1.05
1.01
0.08
+1.5%
35
-1.0%
30
Bullish
423
15
1.04
+1.5%
Up a Lot
1.045
Up a Lot
+0.20
20
00:00:20
1.04
1.08
1.02
1.05
1.045
65
0.010
1.06
1.02
0.09
+2.0%
40
-1.0%
30
Bullish
424
14
1.05
+2.0%
Up a Lot
1.056
Up a Lot
+0.25
25
Explanation of Each Column:
Inputs:
Price Data:
Open, High, Low, Close: Standard OHLC values for the currency at each time step.
Heikin Ashi Close: The Heikin Ashi transformation of the closing price for smoothing price movements.
MACD Line: Moving Average Convergence Divergence, a trend-following momentum indicator.
Donchian Upper, Donchian Lower: Upper and lower bounds of Donchian channels, which track volatility.
ATR (Average True Range): Measures volatility, indicating how much an asset’s price moves, on average, during a set time period.
Nadaraya-Watson Envelope: A smoothed moving average that adapts to volatility.
Leg Data:
Leg Percent Change (Current): The percent change for the current price leg (magnitude and direction).
Leg Length (Current): The number of time steps the current leg has persisted in the same direction.
Leg Percent Change (Previous): The percent change for the previous leg (magnitude and direction).
Leg Length (Previous): The length of the previous leg before the direction changed.
Market Conditions:
Market Condition: Bullish, neutral, or bearish, based on overall market analysis using SPY, VIX, and other metrics.
SPY, VIX: S&P 500 ETF and Volatility Index data as additional inputs that reflect market sentiment and volatility.
Percent Change:
Percent Change: The raw percent change in price from the previous time step.
Percent Change Class: A discrete classification of percent change (one of 7 classes: Down a Lot, Down Moderate, Down a Little, No Change, Up a Little, Up Moderate, Up a Lot).
Targets:
Target: Predicted Price: The predicted normalized price for the next time step.
Target: Percent Change Class: The predicted classification of percent change for the next time step (from the 7-level discrete classification).
Target: Leg Direction: The predicted percent change for the current leg (which includes both direction and magnitude).
Target: Leg Length: The predicted length of the current leg for the next 18 time steps.
Example Time Step Explanation:
For the row at time step 00:00:00:
Inputs:
The close price is 1.01.
The current leg is showing a 0.5% percent change over the last 20 time steps, meaning the price has been moving downward slightly.
The previous leg had a +1.5% percent change over 50 time steps, so the prior move was bullish.
The market is currently bullish, as indicated by external factors such as SPY and VIX.
Targets:
The model should predict that the next price will be 1.012 (a slight increase).
The next percent change will fall into the Up a Little classification (between +0.1% and +0.5%).
The leg is expected to continue moving upward, with a leg direction percent change of +0.05% over the next 5 time steps.
Certainly! Breaking down a comprehensive project into manageable phases is essential for systematic development and ensuring timely delivery. Below, I outline how to divide your Crypto Scalping Bot 2 project into Phase 1 (MVP) and Phase 2, followed by a detailed plan and high-quality code implementation for the MVP. The MVP will focus on establishing the foundational components necessary for the bot to function, ensuring core logic and the transformer model are robust and maintainable.
Project Phases Overview
Phase 1: Minimum Viable Product (MVP)
Objective: Develop a functional prototype that demonstrates the core capabilities of the crypto scalping bot, including data collection, preprocessing, feature engineering, and initial model training with diagnostics.
Key Components:
Data Pipeline Construction
Fetch real-time and historical crypto price data using Binance WebSockets.
Calculate basic technical indicators and leg data.
Feature Engineering
Implement market condition classification (volatility and trend).
Discrete percent change classification.
Model Development
Build a Transformer-based model using TensorFlow.
Train the model on historical data with initial diagnostics.
Diagnostics and Logging
Implement comprehensive diagnostics for model performance.
Set up logging for data processing and model training stages.
Modular Code Structure
Organize code using design patterns for maintainability and scalability.
Ensure core logic and transformer model are encapsulated in top-level classes.
Phase 2: Advanced Features and Optimization
Objective: Enhance the MVP by integrating advanced features, optimizing the model, and preparing for live deployment.
Key Components:
Advanced Feature Engineering
Incorporate additional technical indicators and market condition factors.
Refine leg data tracking with more sophisticated algorithms.
Model Enhancement
Optimize the Transformer model architecture.
Implement walk-forward and rolling window training strategies.
Backtesting Framework
Develop a robust backtesting system to evaluate trading strategies based on model predictions.
Trading Signal Execution
Implement trading logic to execute buy/sell orders based on model outputs.
Integrate risk management strategies.
Deployment Preparations
Set up a live trading environment with necessary safeguards.
Implement real-time diagnostics and monitoring.
Phase 1: MVP Detailed Plan and Implementation
1. Code Structure and Organization
To ensure maintainability and scalability, we'll adopt the Modular Architecture design pattern. The project will be organized into distinct modules, each responsible for a specific functionality. Here's an overview of the proposed structure:
First, create a virtual environment and install the required packages.
bash
# Create virtual environment
python -m venv venv
# Activate virtual environment
# On Windows
venv\Scripts\activate
# On Unix or MacOS
source venv/bin/activate
# Install required packages
pip install tensorflow numpy pandas matplotlib ta websockets asyncio
2.2. Configuration Management
Centralize configuration parameters for easy maintenance and scalability.
python
# utils/config.py
import os
class Config:
# Data Collection
BINANCE_WS_URL = "wss://stream.binance.com:9443/ws"
CRYPTO_PAIRS = ["AAVEUSDT", "AVAXUSDT", "BATUSDT"] # Add more pairs as needed
KLINE_INTERVAL = "5s" # 5-second intervals
# Data Storage
DATA_DIR = os.path.join(os.getcwd(), "data")
if not os.path.exists(DATA_DIR):
os.makedirs(DATA_DIR)
PRICE_DATA_FILE = os.path.join(DATA_DIR, "price_data.csv")
# Feature Engineering
SEQUENCE_LENGTH = 80 # Time steps
FEATURES = ["open", "high", "low", "close", "volume", "rsi", "macd", "atr"] # Extend as needed
# Model Training
EPOCHS = 10
BATCH_SIZE = 32
VALIDATION_SPLIT = 0.2
# Logging
LOG_FILE = os.path.join(DATA_DIR, "bot.log")
2.3. Logging Utility
Implement a logging mechanism to track the bot's operations and diagnose issues.
python
# utils/logger.py
import logging
from config import Config
def get_logger(name=__name__):
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
# Create handlers
fh = logging.FileHandler(Config.LOG_FILE)
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# Create formatters and add to handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# Add handlers to the logger
if not logger.handlers:
logger.addHandler(fh)
logger.addHandler(ch)
return logger
2.4. Data Collection Module
Use Binance WebSockets to fetch real-time price data. For the MVP, we'll collect data for a few crypto pairs and store them locally.
python
Notes:
The DataCollector class connects to Binance WebSockets and subscribes to multiple crypto pairs' kline (candlestick) data.
Data is appended to a Pandas DataFrame and periodically saved to a CSV file.
Logging is implemented to track subscriptions and data collection progress.
2.5. Data Preprocessing Module
Process the collected price data to compute technical indicators and prepare it for model training.
python
# data/data_preprocessor.py
import pandas as pd
import ta
from utils.config import Config
from utils.logger import get_logger
logger = get_logger("DataPreprocessor")
class DataPreprocessor:
def __init__(self, filename):
self.filename = filename
self.data = pd.read_csv(self.filename, parse_dates=['timestamp'])
self.processed_data = pd.DataFrame()
def preprocess(self):
logger.info("Starting preprocessing of data.")
# Sort data by timestamp and pair
self.data.sort_values(by=['pair', 'timestamp'], inplace=True)
self.data.reset_index(drop=True, inplace=True)
# Calculate technical indicators for each pair
pairs = self.data['pair'].unique()
processed_list = []
for pair in pairs:
df = self.data[self.data['pair'] == pair].copy()
df.set_index('timestamp', inplace=True)
# Calculate RSI
df['rsi'] = ta.momentum.RSIIndicator(close=df['close'], window=14).rsi()
# Calculate MACD
macd = ta.trend.MACD(close=df['close'])
df['macd'] = macd.macd()
df['macd_signal'] = macd.macd_signal()
df['macd_diff'] = macd.macd_diff()
# Calculate ATR
df['atr'] = ta.volatility.AverageTrueRange(high=df['high'], low=df['low'], close=df['close'], window=14).average_true_range()
# Handle NaN values
df.fillna(method='bfill', inplace=True)
df.fillna(method='ffill', inplace=True)
processed_list.append(df.reset_index())
self.processed_data = pd.concat(processed_list, ignore_index=True)
self.processed_data.to_csv(Config.DATA_DIR + "/processed_data.csv", index=False)
logger.info("Preprocessing completed and data saved to processed_data.csv.")
if __name__ == "__main__":
preprocessor = DataPreprocessor(Config.PRICE_DATA_FILE)
preprocessor.preprocess()
Notes:
The DataPreprocessor class reads raw price data, sorts it, and computes technical indicators using the ta library.
Technical indicators computed include RSI, MACD, and ATR.
The preprocessed data is saved to processed_data.csv for further processing.
2.6. Feature Engineering Module
Implement market condition classification, leg data tracking, and discrete percent change classification.
python
Notes:
The FeatureEngineer class extends the preprocessed data by adding:
Volatility Classification based on ATR.
Trend Classification using moving averages.
Market Condition by combining volatility and trend.
Leg Data that tracks current and previous legs' percent changes and lengths.
Percent Change Classification into discrete categories based on percentiles.
The engineered features are saved to featured_data.csv.
2.7. Transformer Model Module
Build and train the Transformer model using TensorFlow, including diagnostics.
python
Notes:
The TransformerModel class handles data preparation, model building, training, evaluation, and saving.
Data Preparation:
Features are selected and encoded, including one-hot encoding of categorical market conditions.
Target labels (percent_change_class) are label-encoded.
Features are scaled using StandardScaler.
For the MVP, sequences are treated as individual samples. Future phases can implement proper sequence handling for the Transformer.
Model Architecture:
A simplified Transformer architecture is implemented, suitable for the MVP.
Multi-Head Attention and Feed-Forward networks are incorporated.
Diagnostics:
Training and validation accuracy and loss are plotted and saved as training_diagnostics.png.
Final evaluation metrics are logged.
Model Saving:
The trained model is saved to transformer_model.h5 for future use.
2.8. Diagnostics Module
Although basic diagnostics are included in the TransformerModel, we can create a separate module for extended diagnostics if needed. For the MVP, we'll keep diagnostics integrated within the model training process.
2.9. Main Execution Script
Coordinate the execution of data collection, preprocessing, feature engineering, and model training.
python
# main.py
import argparse
import pandas as pd
from data.data_collector import DataCollector
from data.data_preprocessor import DataPreprocessor
from data.feature_engineer import FeatureEngineer
from models.transformer_model import TransformerModel
from utils.config import Config
from utils.logger import get_logger
import asyncio
logger = get_logger("Main")
def collect_data():
collector = DataCollector(Config.CRYPTO_PAIRS, Config.KLINE_INTERVAL, Config.PRICE_DATA_FILE)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(collector.collect())
except KeyboardInterrupt:
logger.info("Data collection stopped by user.")
def preprocess_and_engineer():
# Preprocess data
preprocessor = DataPreprocessor(Config.PRICE_DATA_FILE)
preprocessor.preprocess()
# Feature engineering
engineer = FeatureEngineer(Config.DATA_DIR + "/processed_data.csv")
engineer.preprocess_features()
def train_model():
model = TransformerModel(Config.DATA_DIR + "/featured_data.csv")
model.run()
def main():
parser = argparse.ArgumentParser(description="Crypto Scalping Bot MVP")
parser.add_argument('--collect', action='store_true', help="Start data collection")
parser.add_argument('--preprocess', action='store_true', help="Preprocess and feature engineer data")
parser.add_argument('--train', action='store_true', help="Train the Transformer model")
parser.add_argument('--all', action='store_true', help="Run all steps sequentially")
args = parser.parse_args()
if args.all:
logger.info("Running all steps: Data Collection, Preprocessing, Feature Engineering, and Model Training.")
collect_data()
preprocess_and_engineer()
train_model()
else:
if args.collect:
collect_data()
if args.preprocess:
preprocess_and_engineer()
if args.train:
train_model()
if __name__ == "__main__":
main()
Usage:
Collect Data:
bash
python main.py --collect
Press Ctrl+C to stop data collection.
Preprocess and Feature Engineering:
bash
python main.py --preprocess
Train Model:
bash
python main.py --train
Run All Steps Sequentially:
bash
python main.py --all
Notes:
The main.py script provides a command-line interface to execute different parts of the MVP.
For the MVP, it's recommended to:
Collect sufficient data (e.g., for a few hours or days).
Preprocess and engineer features once adequate data is collected.
Train the Transformer model on the prepared dataset.
Quality Assurance and Best Practices
To ensure high-quality code and maintainability:
Modular Design:
Each component (data collection, preprocessing, feature engineering, modeling) is encapsulated in its own module.
Logging:
Comprehensive logging is implemented across all modules to aid in diagnostics and debugging.
Configuration Management:
Centralized configurations make it easy to manage and update parameters without altering the core codebase.
Documentation:
Clear inline comments and structured code enhance readability.
A README.md file should be created to provide an overview, setup instructions, and usage guidelines.
Error Handling:
Exception handling ensures that the bot can recover gracefully from errors, especially during data collection.
Version Control:
Use Git to track changes, collaborate, and manage different versions of the project.
Conclusion
The Phase 1 MVP establishes the essential framework for the Crypto Scalping Bot, covering data acquisition, preprocessing, feature engineering, and initial model training with diagnostics. This foundation enables you to validate the core functionalities and gather insights to inform further development in Phase 2. The modular and well-structured code ensures scalability and ease of maintenance as you incorporate more advanced features and optimizations.
Feel free to expand upon this MVP by enhancing feature engineering, refining the model architecture, and integrating more sophisticated diagnostics as you progress to Phase 2.
Not yet included in a published release.
# data/data_collector.py
import asyncio
import json
import pandas as pd
import websockets
from datetime import datetime
from utils.config import Config
from utils.logger import get_logger
logger = get_logger("DataCollector")
class DataCollector:
def __init__(self, pairs, interval, filename):
self.pairs = [pair.lower() for pair in pairs]
self.interval = interval
self.filename = filename
self.columns = ["timestamp", "pair", "open", "high", "low", "close", "volume"]
self.data = pd.DataFrame(columns=self.columns)
async def subscribe(self, ws, pair):
subscribe_message = {
"method": "SUBSCRIBE",
"params": [
f"{pair}@kline_{self.interval}"
],
"id": 1
}
await ws.send(json.dumps(subscribe_message))
logger.info(f"Subscribed to {pair}@kline_{self.interval}")
async def collect(self):
uri = Config.BINANCE_WS_URL
async with websockets.connect(uri) as ws:
# Subscribe to all pairs
for pair in self.pairs:
await self.subscribe(ws, pair)
while True:
try:
message = await ws.recv()
data = json.loads(message)
if 'k' in data:
kline = data['k']
if kline['x']: # Only take data when kline is closed
timestamp = datetime.fromtimestamp(kline['t'] / 1000)
pair = data['s']
open_price = float(kline['o'])
high = float(kline['h'])
low = float(kline['l'])
close = float(kline['c'])
volume = float(kline['v'])
new_row = {
"timestamp": timestamp,
"pair": pair,
"open": open_price,
"high": high,
"low": low,
"close": close,
"volume": volume
}
self.data = self.data.append(new_row, ignore_index=True)
logger.debug(f"Collected data for {pair} at {timestamp}")
# Save periodically
if len(self.data) % 100 == 0:
self.save_data()
except Exception as e:
logger.error(f"Error in data collection: {e}")
await asyncio.sleep(5) # Wait before reconnecting
def save_data(self):
self.data.to_csv(self.filename, mode='a', header=not pd.io.common.file_exists(self.filename), index=False)
logger.info(f"Saved {len(self.data)} rows to {self.filename}")
self.data = pd.DataFrame(columns=self.columns)
if __name__ == "__main__":
collector = DataCollector(Config.CRYPTO_PAIRS, Config.KLINE_INTERVAL, Config.PRICE_DATA_FILE)
loop = asyncio.get_event_loop()
loop.run_until_complete(collector.collect())
# data/feature_engineer.py
import pandas as pd
import numpy as np
from utils.config import Config
from utils.logger import get_logger
logger = get_logger("FeatureEngineer")
class FeatureEngineer:
def __init__(self, filename):
self.filename = filename
self.data = pd.read_csv(self.filename, parse_dates=['timestamp'])
self.featured_data = pd.DataFrame()
def calculate_volatility(self, df):
# ATR-based volatility classification
df['atr_avg'] = df['atr'].rolling(window=14).mean()
df['volatility'] = np.where(df['atr'] > 1.2 * df['atr_avg'], 'High',
np.where(df['atr'] < 0.8 * df['atr_avg'], 'Low', 'Medium'))
return df
def calculate_trend(self, df):
# Moving Averages
df['ma_short'] = df['close'].rolling(window=50).mean()
df['ma_long'] = df['close'].rolling(window=200).mean()
# Trend Classification
df['trend'] = np.where(df['ma_short'] > df['ma_long'], 'Bullish',
np.where(df['ma_short'] < df['ma_long'], 'Bearish', 'Neutral'))
return df
def combine_market_conditions(self, df):
df['market_condition'] = df['volatility'] + "/" + df['trend']
return df
def compute_leg_data(self, df):
df['percent_change'] = df['close'].pct_change()
df['percent_change'].fillna(0, inplace=True)
# Initialize leg data columns
df['current_leg_change'] = 0.0
df['current_leg_length'] = 0
df['previous_leg_change'] = 0.0
df['previous_leg_length'] = 0
current_leg_change = 0.0
current_leg_length = 0
previous_leg_change = 0.0
previous_leg_length = 0
for idx, row in df.iterrows():
percent_delta = row['percent_change']
if current_leg_length == 0:
current_leg_change = percent_delta
current_leg_length = 1
else:
if (current_leg_change > 0 and percent_delta > 0) or (current_leg_change < 0 and percent_delta < 0):
current_leg_change += percent_delta
current_leg_length += 1
else:
previous_leg_change = current_leg_change
previous_leg_length = current_leg_length
current_leg_change = percent_delta
current_leg_length = 1
df.at[idx, 'current_leg_change'] = current_leg_change
df.at[idx, 'current_leg_length'] = current_leg_length
df.at[idx, 'previous_leg_change'] = previous_leg_change
df.at[idx, 'previous_leg_length'] = previous_leg_length
return df
def classify_percent_change(self, df):
# Define classification boundaries based on percentiles
percentiles = df['percent_change'].quantile([0.05, 0.2, 0.4, 0.6, 0.8, 0.95]).values
def classify(x):
if x < percentiles[0]:
return 'Down a Lot'
elif x < percentiles[1]:
return 'Down Moderate'
elif x < percentiles[2]:
return 'Down a Little'
elif x < percentiles[3]:
return 'No Change'
elif x < percentiles[4]:
return 'Up a Little'
elif x < percentiles[5]:
return 'Up Moderate'
else:
return 'Up a Lot'
df['percent_change_class'] = df['percent_change'].apply(classify)
return df
def preprocess_features(self):
logger.info("Starting feature engineering.")
# Apply volatility classification
self.data = self.calculate_volatility(self.data)
# Apply trend classification
self.data = self.calculate_trend(self.data)
# Combine market conditions
self.data = self.combine_market_conditions(self.data)
# Compute leg data
self.data = self.compute_leg_data(self.data)
# Classify percent changes
self.data = self.classify_percent_change(self.data)
# Drop rows with NaN values (from rolling calculations)
self.data.dropna(inplace=True)
self.data.reset_index(drop=True, inplace=True)
self.featured_data = self.data
self.featured_data.to_csv(Config.DATA_DIR + "/featured_data.csv", index=False)
logger.info("Feature engineering completed and data saved to featured_data.csv.")
if __name__ == "__main__":
engineer = FeatureEngineer(Config.DATA_DIR + "/processed_data.csv")
engineer.preprocess_features()
# models/transformer_model.py
import tensorflow as tf
from tensorflow.keras import layers, models
import pandas as pd
import numpy as np
from utils.config import Config
from utils.logger import get_logger
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
logger = get_logger("TransformerModel")
class TransformerModel:
def __init__(self, data_file):
self.data = pd.read_csv(data_file, parse_dates=['timestamp'])
self.model = None
self.label_encoder = LabelEncoder()
self.scaler = StandardScaler()
def prepare_data(self):
logger.info("Preparing data for model training.")
# Select features
features = ['open', 'high', 'low', 'close', 'volume', 'rsi', 'macd', 'atr',
'current_leg_change', 'current_leg_length',
'previous_leg_change', 'previous_leg_length',
'market_condition']
target = ['percent_change_class']
# Encode categorical features
self.data = pd.get_dummies(self.data, columns=['market_condition'], drop_first=True)
X = self.data[features + [col for col in self.data.columns if col.startswith('market_condition_')]]
y = self.data[target[0]]
# Encode target labels
y_encoded = self.label_encoder.fit_transform(y)
# Scale features
X_scaled = self.scaler.fit_transform(X)
# Reshape for Transformer [batch_size, time_steps, features]
# For MVP, we'll simplify by treating each time step as a separate sample
# Future phases can implement proper sequence handling
self.X = X_scaled
self.y = y_encoded
# Split into training and testing
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
self.X, self.y, test_size=Config.VALIDATION_SPLIT, random_state=42, stratify=y_encoded
)
def build_model(self):
logger.info("Building Transformer model.")
# Define input shape
input_shape = (self.X_train.shape[1], )
inputs = layers.Input(shape=input_shape)
# Add Dense layer to project inputs
x = layers.Dense(64, activation='relu')(inputs)
x = layers.Dropout(0.2)(x)
# Transformer Block
# For MVP, we'll implement a simplified Transformer block
# Reshape to 3D for Transformer (batch_size, time_steps, features)
x = layers.Reshape((1, 64))(x) # Treat as sequence length=1
# Multi-Head Attention
attn_output = layers.MultiHeadAttention(num_heads=4, key_dim=16)(x, x)
attn_output = layers.Dropout(0.2)(attn_output)
x = layers.Add()([x, attn_output])
x = layers.LayerNormalization(epsilon=1e-6)(x)
# Feed Forward Network
ffn = layers.Dense(128, activation='relu')(x)
ffn = layers.Dense(64, activation='relu')(ffn)
x = layers.Add()([x, ffn])
x = layers.LayerNormalization(epsilon=1e-6)(x)
# Flatten and output
x = layers.Flatten()(x)
x = layers.Dense(64, activation='relu')(x)
outputs = layers.Dense(len(self.label_encoder.classes_), activation='softmax')(x)
# Compile model
self.model = models.Model(inputs=inputs, outputs=outputs)
self.model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
logger.info("Transformer model built and compiled.")
def train(self):
logger.info("Starting model training.")
history = self.model.fit(
self.X_train, self.y_train,
epochs=Config.EPOCHS,
batch_size=Config.BATCH_SIZE,
validation_data=(self.X_test, self.y_test)
)
logger.info("Model training completed.")
self.evaluate(history)
def evaluate(self, history):
logger.info("Evaluating model performance.")
# Plot training & validation accuracy and loss
import matplotlib.pyplot as plt
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(len(acc))
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.savefig(Config.DATA_DIR + "/training_diagnostics.png")
plt.close()
# Log final evaluation metrics
loss, accuracy = self.model.evaluate(self.X_test, self.y_test, verbose=0)
logger.info(f"Test Loss: {loss:.4f}, Test Accuracy: {accuracy:.4f}")
def save_model(self):
model_path = Config.DATA_DIR + "/transformer_model.h5"
self.model.save(model_path)
logger.info(f"Model saved to {model_path}.")
def run(self):
self.prepare_data()
self.build_model()
self.train()
self.save_model()
if __name__ == "__main__":
tm = TransformerModel(Config.DATA_DIR + "/featured_data.csv")
tm.run()