Indicators: Find Swing Highs Swing Lows - page 2

 
I'll suggest you get a VPS if you're not always on pc. 
 

here's another (maybe better) way to optimize, when the buffers are very custom:


   int start;
   int max_bars = 1000;
  
   if(prev_calculated == 0)
      start = InpRangeBars;
   else
      start = MathMin(rates_total-max_bars, prev_calculated-1);
  
   for(int i = MathMax(InpRangeBars, start); i < rates_total - InpRangeBars - 1 && !IsStopped(); i++)
     {       
             // the indicator code           
     }
 

hi guys, i have convert this indicator to an Expert but it doesnt work on backtest and it doesnt trade:

Can anyone Fix this problem? 

//+------------------------------------------------------------------+

//|                                  Find Swing High_SwingLow.mq5    |

//|                                  Copyright 2024, MetaQuotes Ltd. |

//|                                  https://www.mql5.com            |

//+------------------------------------------------------------------+

input double LotSize = 0.25;           // Lot size for trades

input double StopLoss = 50;            // Stop loss in points

input double TakeProfit = 100;         // Take profit in points

input int InpRangeBars = 12;           // Number of bars to check for swings



#include <Trade\Trade.mqh> // Include the Trade library



CTrade trade; // Create an instance of the CTrade class



// Function to get the signal from your custom indicator

double GetIndicatorSignal()

{

    // Replace "Find Swing High_SwingLow" with the name of your indicator

    // Assuming it returns 1 for buy, -1 for sell, and 0 for no action

    double signal = iCustom(_Symbol, 0, "Meghdad Profit");

    return signal;

}



// Function to open a buy position

void OpenBuy(double lotSize)

{

    double askPrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

    if (trade.Buy(lotSize, _Symbol, askPrice))

    {

        Print("Buy order placed at price: ", askPrice);

    }

    else

    {

        Print("Error placing buy order: ", GetLastError());

    }

}



// Function to open a sell position

void OpenSell(double lotSize)

{

    double bidPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);

    if (trade.Sell(lotSize, _Symbol, bidPrice))

    {

        Print("Sell order placed at price: ", bidPrice);

    }

    else

    {

        Print("Error placing sell order: ", GetLastError());

    }

}



// Main function that runs on each tick

void OnTick()

{

    double SwingHigh = iCustom(NULL, 0, "Find Swing High_SwingLow", InpRangeBars, 0, 1);

    double SwingLow = iCustom(NULL, 0, "Find Swing High_SwingLow", InpRangeBars, 1, 1);



    // Check for existing positions

    bool hasPosition = false;

    for (int i = PositionsTotal() - 1; i >= 0; i--)

    {

        if (PositionGetSymbol(i) == Symbol())

        {

            hasPosition = true;

            break;

        }

    }



    static double previousSignal = 0; // To keep track of the last signal

    double currentSignal = GetIndicatorSignal(); // Get the current signal



    // Buy condition based on SwingLow and currentSignal

    if (SwingLow != EMPTY_VALUE && !hasPosition && currentSignal == 1 && previousSignal != 1)

    {

        OpenBuy(LotSize); // Use input LotSize

    }



    // Sell condition based on SwingHigh and currentSignal

    if (SwingHigh != EMPTY_VALUE && !hasPosition && currentSignal == -1 && previousSignal != -1)

    {

        OpenSell(LotSize); // Use input LotSize

    }



    previousSignal = currentSignal; // Update previous signal

}



//+------------------------------------------------------------------+    

 
i hope this is useful in the long run still
 
Quick Hitz #:

Hi,


Thanks looking forward to the EA for this indicator. Cus I'm not on the pc all the time and I can't see it on my mobile so would be great. 

So far the arrows are oke sometimes it shows sell but the market is buying this is based on previous timecharts.

How do you recommend to apply this indicator at its best?

Fractal kind
 
Conor Mcnamara #:

here's another (maybe better) way to optimize, when the buffers are very custom:


Thank you