Coding help - page 791

 
AmirHossein Yk #: But it returns only 10 or -1!!

What is “it?”
          Be precise and informative about your problem.

Your OnCalcuate calculates the trend, but you do nothing with it.  You must return a bar index, not trend.

 
William Roeder #:

What is “it?”
          Be precise and informative about your problem.

Your OnCalcuate calculates the trend, but you do nothing with it.  You must return a bar index, not trend.

I try to get the zigzag highs & lows by collecting_zz_values() , then classify highs at zzhigh and lows at zzlow.After the classification , remain empty arrays at each one(zzlow & zzhigh) ,so by DellArrayElement() I remove empty arrays.

Finally  if the new high , lower than previous high & new low, lower than previous low,  we have the bearish trend (trend==1)

and  if the new high , higher than previous high & new low, higher than previous low,  we have the bullish trend (trend==2) .

So if not, we cant detect the trend (trend==0).


Thanks a million. 

//+------------------------------------------------------------------+
//|                                                      ZZTrend.mq5 |
//|                                                      AmirHossein |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "AmirHossein"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#resource "\Indicators\Examples\ZigZag.ex5"
//--- input parameters
int      Depth=20;
int      Deviation=2;
int      Backstep=3;
int      collect_values=5;
double zz_values[];
datetime zz_times[];
double zz_handle;
double zzlow[];
double zzhigh[];
//datetime zzlowtime[];
//datetime zzhightime[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   zz_handle=iCustom(_Symbol,PERIOD_CURRENT,"::Indicators\Examples\ZigZag.ex5",Depth,Deviation,Backstep);
   ArrayResize(zz_values,collect_values,0);
   //ArrayResize(zz_times,collect_values,0);
   ArrayResize(zzlow,collect_values,0);
   //ArrayResize(zzlowtime,collect_values,0);
   ArrayResize(zzhigh,collect_values,0);
   //ArrayResize(zzhightime,collect_values,0);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
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[])
  {
  
   collecting_zz_values(); //get the zz highs & lows
//----------------highs & lows classification-----------------//
   
   for(int a=0;a<collect_values;a++)
     {
      int b=a+1;
      if(zz_values[a]<zz_values[b])
        {
         zzlow[a]=zz_values[a];
         //zzlowtime[a]=zz_times[a];
        }
      else
         if(zz_values[a]>zz_values[b])
           {
            zzhigh[a]=zz_values[a];
            //zzhightime[a]=zz_times[a];
           }
     }
//----------------deleting empty arrays-----------------//
   for(int i=0;i!=ArraySize(zzlow);i++)
     {
      if((zzlow[i] == 0.0))
        {
         DellArrayElement(i,zzlow);
        }
     }
   for(int h=0;h!=ArraySize(zzhigh);h++)
     {
      if((zzhigh[h] == 0.0))
        {
         DellArrayElement(h,zzhigh);
        }
     }
//------------------Trend Ditection--------------------//H,H|L,L
//    1 - Down trend
//    2 - Up trend
//    0 - trend is not defined
   int    trend;
   if(zzhigh[0]<zzhigh[1])
     {
      if(zzlow[0]<zzlow[1])
         trend=1;
      else
         trend=0;
     }
   if(zzlow[0]>zzlow[1])
     {
      if(zzhigh[0]>zzhigh[1])
         trend=2;
      else
         trend=0;
     }


//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void collecting_zz_values()
  {
   int collected_values=0;
   int i=1;
   double zz_array[];
   while(collected_values<collect_values)
     {
      CopyBuffer(zz_handle,0,i,1,zz_array);
      if(zz_array[0]!=0.0)
        {
         zz_values[collected_values]=zz_array[0];
         // zz_times[collected_values]=iTime(_Symbol,PERIOD_CURRENT,i);
         collected_values++;
        }
      i++;
     }
  }
//+------------------------------------------------------------------+
void DellArrayElement(int shift, double &arr[])
  {
   int arrSize = ArraySize(arr);
   if(shift > arrSize)
      return;
   if(shift <= arrSize)
      ArrayCopy(arr, arr, shift, shift+1);
//arrSize=arrSize-2;
   ArrayResize(arr,arrSize-1);
  }
///+------------------------------------------------------------------+

//+------------------------------------------------------------------+
 
AmirHossein Yk #:

I try to get the zigzag highs & lows by collecting_zz_values() , then classify highs at zzhigh and lows at zzlow.After the classification , remain empty arrays at each one(zzlow & zzhigh) ,so by DellArrayElement() I remove empty arrays.

Finally  if the new high , lower than previous high & new low, lower than previous low,  we have the bearish trend (trend==1)

and  if the new high , higher than previous high & new low, higher than previous low,  we have the bullish trend (trend==2) .

So if not, we cant detect the trend (trend==0).


Thanks a million. 

I want that this indicator code , return the trend.

 

Can someone correct this indicator : 

//+------------------------------------------------------------------+
//|                                         CDUR_Indicators.mq5      |
//|                        Copyright 2024, MetaTrader 5 User         |
//|                                       https://www.example.com    |
//+------------------------------------------------------------------+
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
// Paramètres des périodes
input int DEMA1PeriodUTC = 9;
input int DEMA2PeriodUTC = 19;
input int DEMA3PeriodUTC = 6;
input int WilderAveragePeriodUTC = 5;
input int DEMA1PeriodUTS = 45;
input int DEMA2PeriodUTS = 95;
input int DEMA3PeriodUTS = 6;
input int WilderAveragePeriodUTS = 8;
// Buffers
double CDURUTC_Buffer[];
double CDURUTS_Buffer[];
// Déclaration des buffers de l'indicateur
//+------------------------------------------------------------------+
int OnInit()
{
    // Initialiser les buffers
    SetIndexBuffer(0, CDURUTC_Buffer);
    PlotIndexSetString(0, PLOT_LABEL, "CDUR UTC");
    SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2, clrBlue);
    
    SetIndexBuffer(1, CDURUTS_Buffer);
    PlotIndexSetString(1, PLOT_LABEL, "CDUR UTS");
    SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2, clrRed);
    
    return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Fonction pour calculer l'indicateur sur les ticks                |
//+------------------------------------------------------------------+
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[])
{
    if (rates_total < DEMA2PeriodUTS) return 0; // Vérifier si les données sont suffisantes
    for (int i = prev_calculated; i < rates_total; i++)
    {
        // Calcul CDUR UTC
        double z1 = iMA(NULL, 0, DEMA1PeriodUTC, 0, MODE_EMA, PRICE_CLOSE, i);
        double z2 = iMA(NULL, 0, DEMA2PeriodUTC, 0, MODE_EMA, PRICE_CLOSE, i);
        double e = z1 - z2;
        double z3 = iMAOnArray(e, rates_total, DEMA3PeriodUTC, 0, MODE_EMA, i);
        double f = z3;
        double hausse = MathMax(0, f - f[i+1]);
        double baisse = MathMax(0, f[i+1] - f);
        double mmHausse = WilderAverage(hausse, WilderAveragePeriodUTC, i);
        double mmBaisse = WilderAverage(baisse, WilderAveragePeriodUTC, i);
        double RSUTC = mmHausse / mmBaisse;
        CDURUTC_Buffer[i] = 100 - (100 / (1 + RSUTC));
        // Calcul CDUR UTS
        double z1S = iMA(NULL, 0, DEMA1PeriodUTS, 0, MODE_EMA, PRICE_CLOSE, i);
        double z2S = iMA(NULL, 0, DEMA2PeriodUTS, 0, MODE_EMA, PRICE_CLOSE, i);
        double eS = z1S - z2S;
        double z3S = iMAOnArray(eS, rates_total, DEMA3PeriodUTS, 0, MODE_EMA, i);
        double fS = z3S;
        double hausseS = MathMax(0, fS - fS[i+1]);
        double baisseS = MathMax(0, fS[i+1] - fS);
        double mmHausseS = WilderAverage(hausseS, WilderAveragePeriodUTS, i);
        double mmBaisseS = WilderAverage(baisseS, WilderAveragePeriodUTS, i);
        double RSUTS = mmHausseS / mmBaisseS;
        CDURUTS_Buffer[i] = 100 - (100 / (1 + RSUTS));
    }
    return rates_total;
}
//+------------------------------------------------------------------+
//| Fonction pour calculer la Wilder Average                         |
//+------------------------------------------------------------------+
double WilderAverage(double value, int period, int index)
{
    double avg = value;
    for (int i = 1; i < period; i++)
    {
        avg += value / period;
    }
    return avg;
}
//+------------------------------------------------------------------+
 
Fabien Mutter #: Can someone correct this indicator :
    for (int i = prev_calculated; i < rates_total; i++)

After the initial run (where prev_calculated is zero), prev_calculated equals rates_total, and your loop does nothing.
          How to do your lookbacks correctly #9#14 & #19 (2016)

 
thank you very much
but I am not familiar with programming on MT5 
so what should I do?
thanks in advance

 
Fabien Mutter #: but I am not familiar with programming on MT5  so what should I do?

If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.
          I need HEEEELP, please, it's URGENT...really ! - General - MQL5 programming forum (2017)

Hire someone on Freelance.

 
thank you for your reply.
this forum is strictly useless in itself

 
Fabien Mutter #:
thank you very much
but I am not familiar with programming on MT5 
so what should I do?
thanks in advance

Try replacing:

    if (rates_total < DEMA2PeriodUTS) return 0; // Vérifier si les données sont suffisantes
    for (int i = prev_calculated; i < rates_total; i++)

with:

for(int i = ratestotal - DEMA2PeriodUTS - 1; i >= 0; i--)

and replace:

    return rates_total;

with:

return rates_total - 1;

and replace:

    for (int i = 1; i < period; i++)

with:

for(int i = period; i >= 1; i--)

and then insert into OnInit(), or OnCalculate():

ArraySetAsSeries(CDURUTC_Buffer, true);
ArraySetAsSeries(CDURUTS_Buffer, true);

The idea is to count "backward" without the need for prev_calculated.

By the way you'll also need an indicator handle, an ArraySetAsSeries, and a CoppyBuffer for each of your iMA calls. (You could share a single indicator handle among all 4 calls but the code is easier to follow if each call has its own.)
 

Hello,

I'm looking for help and hope I'm at the right place in the forum.


I'm trying to modify a deal (#2 here below, volume 0.12) with PositionModify function of CTrade class :

trade.PositionModify(Ticket, newStopLoss, PositionGetDouble(POSITION_TP))

but I don't know why, the server is modifying all open positions (volume 0.3) and not only the 'ticketed' one.

The "bug" does not seem to be in the Trade.mqh because when I force the m_request.volume to 0.12, it does not help.

It looks like the bug is in the server side or in the configuration of the server...

Any help would be appreciated.

Thanks,


Rose


2025.03.08 20:22:20.696 2022.03.09 06:00:00   market buy 0.12 EURJPY sl: 125.848 tp: 129.248 (126.438 / 126.448 / 126.438)

2025.03.08 20:22:20.696 2022.03.09 06:00:00   deal #2 buy 0.12 EURJPY at 126.448 done (based on order #2)

2025.03.08 20:22:20.697 2022.03.09 06:00:00   deal performed [#2 buy 0.12 EURJPY at 126.448]

2025.03.08 20:22:20.697 2022.03.09 06:00:00   order performed buy 0.12 at 126.448 [#2 buy 0.12 EURJPY at 126.448]

2025.03.08 20:22:20.704 2022.03.09 06:00:00   CTrade::OrderSend: market buy 0.12 EURJPY sl: 125.848 tp: 129.248 [done at 126.448]

2025.03.08 20:22:20.704 2022.03.09 06:00:00   Open MagicNumber : 20250201

2025.03.08 20:22:20.704 2022.03.09 06:00:00   market buy 0.18 EURJPY sl: 125.948 tp: 130.548 (126.438 / 126.448 / 126.438)

2025.03.08 20:22:20.704 2022.03.09 06:00:00   deal #3 buy 0.18 EURJPY at 126.448 done (based on order #3)

2025.03.08 20:22:20.704 2022.03.09 06:00:00   deal performed [#3 buy 0.18 EURJPY at 126.448]

2025.03.08 20:22:20.704 2022.03.09 06:00:00   order performed buy 0.18 at 126.448 [#3 buy 0.18 EURJPY at 126.448]

2025.03.08 20:22:20.707 2022.03.09 06:00:00   CTrade::OrderSend: market buy 0.18 EURJPY sl: 125.948 tp: 130.548 [done at 126.448]

2025.03.08 20:22:20.707 2022.03.09 06:00:00   Open MagicNumber : 20250301

2025.03.08 20:22:20.709 2022.03.09 12:00:00   position modified [#2 buy 0.3 EURJPY 126.448 sl: 125.951 tp: 130.548]

2025.03.08 20:22:20.712 2022.03.09 12:00:00   CTrade::OrderSend: modify EURJPY (sl: 125.951, tp: 130.548) [done]