Adaptive Bars Limits for EA

9 March 2024, 13:16
Rajesh Kumar Nait
0
57

I came across a problem, My EA is very different. Unlike indicators which are loaded instantly like Moving average using iCustom, my EA is different.

It looks for Highs and Lows and do calculations further analyzing waves legs. As a noob, I first implemented EA using ZigZag which is the first solution which a newbie comes across when he learns to code EA
but that solution is not perfect, because high lows may be found in less than 50 bars sometime and when market is volatile it may be found in more than 500 bars.

You never know when market is going to be volatile and when market can be not volatile that is why any input field like

input int barLimit = 50; // Default value, user can change

is never an optimal solution for EA.

So How to solve this problem?

An EA Coder should use adaptive bar limit methods. There are many algorithm to achieve this. I have my own personal algorithm for this but I am going to share one example

For example, you could calculate the average true range (ATR) and use it to determine the required lookback period.
// Calculate ATR
double atrValue = iATR(_Symbol, _Period, 14, 0);

// Determine dynamic bar limit based on ATR
int dynamicBarLimit = MathMax(50, NormalizeDouble(atrValue * 2, 0));

In this example, the dynamicBarLimit is set to a minimum of 50 bars or twice the ATR, whichever is greater. 

then you can find high and low

double highestHigh = iHigh(_Symbol, _Period, iHighest(_Symbol, _Period, MODE_HIGH, dynamicBarLimit, 0));
double lowestLow = iLow(_Symbol, _Period, iLowest(_Symbol, _Period, MODE_LOW, dynamicBarLimit, 0));
Once you have the dynamically determined highs and lows, you can use them as reference points for setting take-profit levels in your trading strategy.
// Example: Use the highest high as TP level
double takeProfitLevel = highestHigh + (ATR * 1.5); //  multiplier 

The Idea of using ATR came to my mind when I was writing an algorithm based on Renko where I found Native Renko vs ATR based Renko was much more effecient as it was also analysing the market volatility.

To analyse ATR Period dynamically

// Function to find optimal ATR period
int findOptimalATRPeriod(int iterations) {
    double minATR = 1000000; // Some large initial value
    int optimalPeriod = 14; // Default ATR period

    for (int i = 2; i <= iterations; i++) {
        double atrValue = iATR(_Symbol, _Period, i, 0);
        if (atrValue < minATR) {
            minATR = atrValue;
            optimalPeriod = i;
        }
    }

    return optimalPeriod;
}

// Determine dynamic ATR period
int dynamicATRPeriod = findOptimalATRPeriod(50); // Analyze ATR over the last 50 periods

This function iterates through different ATR periods and selects the one with the lowest ATR value.

// Determine dynamic bar limit based on dynamic ATR period
int dynamicBarLimit = MathMax(50, NormalizeDouble(iATR(_Symbol, _Period, dynamicATRPeriod, 0) * 2, 0));
Now, your EA will dynamically determine both the ATR period and the bar limit based on market conditions


Share it with friends: