THIS IS MY PROJECT DESCRITION THIS HAS TO BE MADE IN PYTHON. LETS START BUILDING THIS BY FOCUSING ON CORES : # crypto scalping bot 2

AAVE/USD, AVAX/USD, BAT/USD,

- Build a transformer model that predicts the price of change of 15 crypto currencies
- create database for storing price information and feature enhancements
- 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
- trading strategies will have to be implemented
- different trading strategies in different market environments
- 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
- model signal generations from testing will be saved to a database to be used in backtesting trading strategies using those signals
- experiment with ideal stop loss and take profit in different market environments
- deploy the trading bot on my home server

## 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).
    - **Medium Volatility**: If today's ATR is within ±10% of the ATR moving average.
    - **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%.

### 2. **Market Trend Classification (Bullish, Neutral, Bearish)**

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:

1. **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.
2. **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.
3. **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:

1. **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).
2. **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:

- **Percent Change (`percent_delta`)**:
    - `percent_delta = (current_price - previous_price) / previous_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**:

1. **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.
2. **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:

1. **Step 1 & 2**: Price goes from 100 to 102, a **+2%** change, starting a **new leg**.
    - Current leg: 1 time step, +2% change.
2. **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.
3. **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.
4. **Step 5**: Price drops further to 101, a **1.94%** change, continuing the **current (down) leg**.
    - Current leg: 2 time steps, -2.90% change.
5. **Step 6**: Price continues to fall to 100, a **0.99%** change.
    - Current leg: 3 time steps, -3.89% change.
6. **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:

1. **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.
2. **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:

1. **Down a Lot**
2. **Down Moderate**
3. **Down a Little**
4. **No Change**
5. **Up a Little**
6. **Up Moderate**
7. **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.

1. **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)
        
2. **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:**

1. **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**.
2. **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:
    1. Group by **asset** (each currency and its associated data points within each time step).
    2. 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).
    - **Market conditions** (bullish/bearish, volatility level).
    - **Technical indicators** (RSI, MACD, ATR, etc.).
    - **SPY, VIX data**.

### 2.2 **Model Outputs**

For each input sequence, the model should output:

1. **Predicted Price Change**:
    - A normalized prediction of the next price.
    - **Percent Change Classification** (using the 7-level classification system).
2. **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.
3. normalized predicted price:
    - just predict the close price for the predicted time step
4. **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

1. **Data Pipeline**: Gather historical price data, compute technical indicators, and preprocess them.
2. **Model Inputs**: Price, leg data, market conditions, percent change classification, and technical indicators.
3. **Model Outputs**: Predicted price change, percent change classification, leg data predictions, and confidence scores.
4. **Training and Testing**:
    - **Walk-forward training** with daily fine-tuning.
    - **Rolling 100-day window retraining**.
5. **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:**

1. **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.
2. **Technical Indicators**:
    - **RSI (Relative Strength Index)**: Measures momentum, indicating overbought or oversold conditions.
    - **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.
3. **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.
4. **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.
5. **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:**

1. **Target: Predicted Price**: The predicted normalized price for the next time step.
2. **Target: Percent Change Class**: The predicted classification of percent change for the next time step (from the 7-level discrete classification).
3. **Target: Leg Direction**: The predicted percent change for the current leg (which includes both direction and magnitude).
4. **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.

### Summary:

This example table is a simplified version of the real dataset you would generate. It includes all the necessary inputs (price data, technical indicators, leg data, market conditions, etc.) and targets (price prediction, percent change classification, leg direction, etc.) for training and testing the transformer model. Each row represents a snapshot of the market at a specific time step, and the model will use this data to predict future price movements and classify percent changes.

This example is only one part there would actually be 15 times more columns,  all of these technical indicators for 15 crypto currencies

## Trading strategy development and backtesting

experiment with deploying the bot only in certain market environments

using percent change classification buy when the percent change is over a certain threshold

set a take profit at the predicted threshold

when take profit is hit sell 3/4 of position

when take profit is hit set stop loss to initial entry and take profit for remaining position at current price

set a stop loss depending on the market environment and ATR

if holding and signal is buy then hold

if holding and signal is sell then sell all

using the leg predictions buy if the percent change is positive

set a take profit at the predicted percent change

set a stop loss based on market environment and ATR

## UI

UI was created by [bolt.new](http://bolt.new/) it just needs to be integrated with the backend

database tables the serve frontend:

Trading bot info: max num positions, total bot allocation, trade sizes for different assets, returns over previous day.

recent trades: time, price, amount, number of coins, return if a sell, bot that placed trade

bots that are deployed:  to paper or live, bot name