Discussing the article: "Using PatchTST Machine Learning Algorithm for Predicting Next 24 Hours of Price Action" - page 2

 
I often find that the predicted results of this model are not quite consistent with the actual situation. I haven't made any changes to the code of this model. Could you please give me some guidance? Thank you.
 
Thomas Sawyer #:
I often find that the predicted results of this model are not quite consistent with the actual situation. I haven't made any changes to the code of this model. Could you please give me some guidance? Thank you.

Thank you for sharing your experience with the model. You raise a valid point about prediction consistency. The PatchTST model works best when integrated into a comprehensive trading approach that considers multiple market factors. Here's how I recommend using the model's predictions more effectively:

  1. Time Window Optimization:
  • Focus on trading during peak hours (6:00 AM to 10:00 AM US Central Time). 
  • Use the model's predictions primarily during these hours when market movements are more predictable
  • Pay special attention to deviations around previous daily and weekly highs/lows. These are your primary supply and demand zones. 
  1. Model Integration Strategy:
  • Use the predictions as part of a broader analysis, not as standalone signals
  • Look for Fair Value Gaps (FVGs) in the predicted price ranges. I have given the code for an indicator that I use for FVGs in MQL5 below. 
  • Combine predictions with technical patterns like flags, wedges, and horizontal consolidations. 
  • Consider the predictions in context of daily, weekly, and monthly price locations
  1. Risk Management:
  • Implement wider stops (e.g., 10-point or 100 pips stop-loss for Gold, 50 pips for EURUSD, 65 pips of USDJPY, 60 pips of GBPUSD, 30 pips for AUDUSD/NZDUSD, 40 pips for USDCAD, 0.80 points Oil, 25 points US500, 75 points NQ, 200 points US30)
  • Use modest take-profits for partial (i.e., 1:1 risk reward for first partial (70% of the initial position), leave a runner (30% of initial position)
  • Scale positions based on market conditions - good trades with lots of confluence, 2 - 3x base position size. 
  • Avoid trading during high-impact news events
  1. Context Building:
  • Analyze market structure across multiple timeframes: utilize 5 minutes and 15 minutes - there is likely an optimal candle/bar/closing price to enter within the 1 hour where the model predicts entry in your trading direction. 
  • Consider current market state (trending/ranging/consolidating/choppy/reversing) - utilize this information to pre-plan your optimal trading hour. If what you anticipate is not playing out, look for the next available opportunity that the model is giving you. 
  • Look for pattern confirmations in the predicted price ranges
  • Focus on directional bias and major support/resistance levels
  1. Entry Refinement:
  • Wait for structural confirmations before entering trades. Structures to know the best: double tops/bottoms, wedges, bull/bear flags, MTR tops/bottoms, climaxes, especially around key support and resistance areas/FVGs. 
  • Do not enter if you anticipate a trend channel. Trend channels are your worst enemies. Even if you find a trend channel on a higher timeframe like 4 hours or one day, DO NOT COUNTERTREND! 
  • Look for consolidation patterns within predicted ranges - play every reversal. This model really shines with reversals. 
  • Consider scaling into positions rather than taking full-size entries. 25% initial size - scale in as price goes in your favor up to full position size. Do not scale-in against your position, i.e., if the position goes against you. 

Some Additional Personal Observations: 

  • You have to be looking and anticipating certain things with this model: Reversals around key areas are the most profitable. 
    • So say the model predicts a change of color for a particular bar or time-frame. That's the time to start paying attention. Look for 1 extra confluence prior to entry. If you don't get that confluence, wait until you get it, even if the color has shifted, the trade may still work. My point is, this is a good "timing" model from the standpoint of "when" you need to start caring and paying attention. 
  • This model does give a lot of false positives, but a few big wins wipe out all bad losses. 
  • Start with 1 pair and add another pair every week. Scale your models up to 10 pairs total.
  • Your will get 2 - 3 big trades every week. 
  • Anticipate a weekly gain of about 0.5% - 1.5%. You are risking about 1.0% - 2.5% every week across all the different pairs. In other words, you trade small, stay diversified across multiple uncorrelated instruments, and focus on specific time-ranges. You will get around 3 - 6 instruments right consistently and that will make most of your money. So don't get hung up on any one trade or over analyze any one data point. 
  • If the predicted change is around a news event, skip it and focus on another pair or wait for the next change of color signal. 
  • Trend channels are your worst enemy - if you even suspect them - forget about the pair for that day. Alternatively, do the opposite of what the model is telling you because otherwise you will get caught up counter-trending lose. 
  • Trading this model is "uncomfortable" - you really don't ever trust it and it always feels like "this trade will never work, it's so counterintuitive," but there in lies the beauty. It makes you do what you don't want to do, which is the right thing to do (the thing you don't want to do, but you should do because it's the right thing to do in that situation). 
  • Concept of Inversions Occurs ~20 - 30% of the time: this is when the model is giving you one color (red or green), but the market seems to always be doing the opposite. This is an inversion scenario - nothing is wrong with the model and nothing is wrong with your trading either. You just have to realize an inversion has occurred and start doing the opposite or skip the pair altogether until the expectations re-align. This generally takes around 6 - 10 sessions (around 2 - 4 days) for an inversion to fix itself. Easiest way to identify an inversion with this model is to use the python script I gave you for the model predictions - pull the data for sequentially for the past 6 sessions (so don't pull the latest data - do a lookback). See if the predictions are matching the actual. If they are not - inversion has occurred. 

The model's predictions should be used as one component of your analysis rather than the sole decision-maker. By incorporating these elements, you can potentially improve the consistency of your trading results when using the PatchTST model.

I hope this helps.

Fair Value Gap (FVG) Script that I mentioned (these gaps work very much like supply and demand zones, in my experience): 

#property copyright "© ShashankRai1"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

input bool ShowMidpoint = false; // Show Midpoint Line
input color UpFVGColor = clrGreen;  // Up FVG Color
input color DownFVGColor = clrRed;  // Down FVG Color

int OnInit()
{
    IndicatorSetString(INDICATOR_SHORTNAME, "Show FVG");
    return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason)
{
    ObjectsDeleteAll(0, "FVG_");
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
    int start;
    if(prev_calculated == 0)
    {
        start = rates_total - 1;  // Process all available data
    }
    else
    {
        start = prev_calculated - 1;  // Process only new bars
    }

    for (int i = start; i >= 2; i--)  // Ensure we have at least 3 bars
    {
        drawFVG(i, rates_total, time, open, high, low, close);
    }

    return(rates_total);
}

void drawFVG(int index, int total, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[])
{
    if (index < 2 || index >= total) return; // Ensure we have enough bars

    if (close[index - 1] > open[index - 1] && high[index - 2] < low[index])
    {
        // Up close candle condition and gap exists
        string boxName = StringFormat("FVG_Box_Up_%d", index);
        if(ObjectCreate(0, boxName, OBJ_RECTANGLE, 0, time[index - 2], high[index - 2], time[index], low[index]))
        {
            ObjectSetInteger(0, boxName, OBJPROP_COLOR, UpFVGColor);
            ObjectSetInteger(0, boxName, OBJPROP_BGCOLOR, UpFVGColor);
            ObjectSetInteger(0, boxName, OBJPROP_BACK, true);
            ObjectSetInteger(0, boxName, OBJPROP_WIDTH, 1);
            ObjectSetInteger(0, boxName, OBJPROP_FILL, true);
            Print("Created Up FVG box at index ", index);
        }
        else
        {
            Print("Failed to create Up FVG box at index ", index, ". Error: ", GetLastError());
        }

        if (ShowMidpoint)
        {
            string lineName = StringFormat("FVG_Line_Up_%d", index);
            double midpoint = (high[index - 2] + low[index]) / 2;
            if(ObjectCreate(0, lineName, OBJ_TREND, 0, time[index - 2], midpoint, time[index], midpoint))
            {
                ObjectSetInteger(0, lineName, OBJPROP_COLOR, UpFVGColor);
                ObjectSetInteger(0, lineName, OBJPROP_STYLE, STYLE_DASH);
                ObjectSetInteger(0, lineName, OBJPROP_WIDTH, 1);
                Print("Created Up FVG midline at index ", index);
            }
            else
            {
                Print("Failed to create Up FVG midline at index ", index, ". Error: ", GetLastError());
            }
        }
    }
    else if (close[index - 1] < open[index - 1] && low[index - 2] > high[index])
    {
        // Down close candle condition and gap exists
        string boxName = StringFormat("FVG_Box_Down_%d", index);
        if(ObjectCreate(0, boxName, OBJ_RECTANGLE, 0, time[index - 2], low[index - 2], time[index], high[index]))
        {
            ObjectSetInteger(0, boxName, OBJPROP_COLOR, DownFVGColor);
            ObjectSetInteger(0, boxName, OBJPROP_BGCOLOR, DownFVGColor);
            ObjectSetInteger(0, boxName, OBJPROP_BACK, true);
            ObjectSetInteger(0, boxName, OBJPROP_WIDTH, 1);
            ObjectSetInteger(0, boxName, OBJPROP_FILL, true);
            Print("Created Down FVG box at index ", index);
        }
        else
        {
            Print("Failed to create Down FVG box at index ", index, ". Error: ", GetLastError());
        }

        if (ShowMidpoint)
        {
            string lineName = StringFormat("FVG_Line_Down_%d", index);
            double midpoint = (high[index] + low[index - 2]) / 2;
            if(ObjectCreate(0, lineName, OBJ_TREND, 0, time[index - 2], midpoint, time[index], midpoint))
            {
                ObjectSetInteger(0, lineName, OBJPROP_COLOR, DownFVGColor);
                ObjectSetInteger(0, lineName, OBJPROP_STYLE, STYLE_DASH);
                ObjectSetInteger(0, lineName, OBJPROP_WIDTH, 1);
                Print("Created Down FVG midline at index ", index);
            }
            else
            {
                Print("Failed to create Down FVG midline at index ", index, ". Error: ", GetLastError());
            }
        }
    }
}
 
Shashank Rai #:

Thank you for your interest! Yes, those changes to the parameters would work in principle, but there are a few important considerations when switching to M1 data:

1. Data Volume: Training with 10080 minutes (1 week) of M1 data means handling significantly more data points than with H1. This will:

  • Increase training time substantially
  • Require more memory
  • Potentially need GPU acceleration for efficient training

2. Model Architecture Adjustments: In Step 8 of model training and Step 4 of prediction code, you might want to adjust other parameters to accommodate the larger input sequence:

3. Prediction Quality: While you'll get more granular predictions, be aware that M1 data typically contains more noise. You might want to experiment with different sequence lengths and prediction windows to find the optimal balance.

Thanks for the insight. My computer is reasonably capable, with 256GB and 64 physical cores. It could do with a better GPU though.

Once I've updated the GPU, I will try the updated config settings.

 
Shashank Rai #:

Thank you for sharing your experience with the model. You raise a valid point about prediction consistency. The PatchTST model works best when integrated into a comprehensive trading approach that considers multiple market factors. Here's how I recommend using the model's predictions more effectively:

  1. Time Window Optimization:
  • Focus on trading during peak hours (6:00 AM to 10:00 AM US Central Time). 
  • Use the model's predictions primarily during these hours when market movements are more predictable
  • Pay special attention to deviations around previous daily and weekly highs/lows. These are your primary supply and demand zones. 
  1. Model Integration Strategy:
  • Use the predictions as part of a broader analysis, not as standalone signals
  • Look for Fair Value Gaps (FVGs) in the predicted price ranges. I have given the code for an indicator that I use for FVGs in MQL5 below. 
  • Combine predictions with technical patterns like flags, wedges, and horizontal consolidations. 
  • Consider the predictions in context of daily, weekly, and monthly price locations
  1. Risk Management:
  • Implement wider stops (e.g., 10-point or 100 pips stop-loss for Gold, 50 pips for EURUSD, 65 pips of USDJPY, 60 pips of GBPUSD, 30 pips for AUDUSD/NZDUSD, 40 pips for USDCAD, 0.80 points Oil, 25 points US500, 75 points NQ, 200 points US30)
  • Use modest take-profits for partial (i.e., 1:1 risk reward for first partial (70% of the initial position), leave a runner (30% of initial position)
  • Scale positions based on market conditions - good trades with lots of confluence, 2 - 3x base position size. 
  • Avoid trading during high-impact news events
  1. Context Building:
  • Analyze market structure across multiple timeframes: utilize 5 minutes and 15 minutes - there is likely an optimal candle/bar/closing price to enter within the 1 hour where the model predicts entry in your trading direction. 
  • Consider current market state (trending/ranging/consolidating/choppy/reversing) - utilize this information to pre-plan your optimal trading hour. If what you anticipate is not playing out, look for the next available opportunity that the model is giving you. 
  • Look for pattern confirmations in the predicted price ranges
  • Focus on directional bias and major support/resistance levels
  1. Entry Refinement:
  • Wait for structural confirmations before entering trades. Structures to know the best: double tops/bottoms, wedges, bull/bear flags, MTR tops/bottoms, climaxes, especially around key support and resistance areas/FVGs. 
  • Do not enter if you anticipate a trend channel. Trend channels are your worst enemies. Even if you find a trend channel on a higher timeframe like 4 hours or one day, DO NOT COUNTERTREND! 
  • Look for consolidation patterns within predicted ranges - play every reversal. This model really shines with reversals. 
  • Consider scaling into positions rather than taking full-size entries. 25% initial size - scale in as price goes in your favor up to full position size. Do not scale-in against your position, i.e., if the position goes against you. 

Some Additional Personal Observations: 

  • You have to be looking and anticipating certain things with this model: Reversals around key areas are the most profitable. 
    • So say the model predicts a change of color for a particular bar or time-frame. That's the time to start paying attention. Look for 1 extra confluence prior to entry. If you don't get that confluence, wait until you get it, even if the color has shifted, the trade may still work. My point is, this is a good "timing" model from the standpoint of "when" you need to start caring and paying attention. 
  • This model does give a lot of false positives, but a few big wins wipe out all bad losses. 
  • Start with 1 pair and add another pair every week. Scale your models up to 10 pairs total.
  • Your will get 2 - 3 big trades every week. 
  • Anticipate a weekly gain of about 0.5% - 1.5%. You are risking about 1.0% - 2.5% every week across all the different pairs. In other words, you trade small, stay diversified across multiple uncorrelated instruments, and focus on specific time-ranges. You will get around 3 - 6 instruments right consistently and that will make most of your money. So don't get hung up on any one trade or over analyze any one data point. 
  • If the predicted change is around a news event, skip it and focus on another pair or wait for the next change of color signal. 
  • Trend channels are your worst enemy - if you even suspect them - forget about the pair for that day. Alternatively, do the opposite of what the model is telling you because otherwise you will get caught up counter-trending lose. 
  • Trading this model is "uncomfortable" - you really don't ever trust it and it always feels like "this trade will never work, it's so counterintuitive," but there in lies the beauty. It makes you do what you don't want to do, which is the right thing to do (the thing you don't want to do, but you should do because it's the right thing to do in that situation). 
  • Concept of Inversions Occurs ~20 - 30% of the time: this is when the model is giving you one color (red or green), but the market seems to always be doing the opposite. This is an inversion scenario - nothing is wrong with the model and nothing is wrong with your trading either. You just have to realize an inversion has occurred and start doing the opposite or skip the pair altogether until the expectations re-align. This generally takes around 6 - 10 sessions (around 2 - 4 days) for an inversion to fix itself. Easiest way to identify an inversion with this model is to use the python script I gave you for the model predictions - pull the data for sequentially for the past 6 sessions (so don't pull the latest data - do a lookback). See if the predictions are matching the actual. If they are not - inversion has occurred. 

The model's predictions should be used as one component of your analysis rather than the sole decision-maker. By incorporating these elements, you can potentially improve the consistency of your trading results when using the PatchTST model.

I hope this helps.

Fair Value Gap (FVG) Script that I mentioned (these gaps work very much like supply and demand zones, in my experience): 

Thank you very much for your patient answer and selfless sharing. I have never seen such detailed and professional answers before. I will read your article repeatedly. These knowledge are particularly valuable to me. Best wishes to you.
 
Thomas Sawyer #:
Thank you very much for your patient answer and selfless sharing. I have never seen such detailed and professional answers before. I will read your article repeatedly. These knowledge are particularly valuable to me. Best wishes to you.

Thank you. Your kind words mean a lot!! Please reach out if you need any more assistance!