Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 222

 

Please help. I can not understand the for() loop, all the time after the update, because of the offset(iMA(NULL,PERIOD_M1,1,1,MODE_SMA,PRICE_OPEN,i)) The indicator redraws!


//+------------------------------------------------------------------+
//|                                                         help.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+

#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property strict
#include <MovingAverages.mqh>
//--- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 2
#property   indicator_color1  clrSilver
#property   indicator_color2  clrRed
#property   indicator_width1  2

//--- indicator parameters
input int SignalSMA=8;            // Signal SMA Period
//--- indicator buffers
double    ExtBuffer[];
double    ExtSignalBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   IndicatorDigits(Digits+1);
//--- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_LINE);
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtBuffer);
   SetIndexBuffer(1,ExtSignalBuffer);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
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 i,limit;
//--- last counted bar will be recounted
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit++;
//--- counted in the 1-st buffer
   for(i=0;i<limit;i++)
      ExtBuffer[i]=(
                    iMA(NULL,PERIOD_CURRENT,1,0,MODE_SMA,PRICE_HIGH,i)
                    +iMA(NULL,PERIOD_M1,1,1,MODE_SMA,PRICE_OPEN,i)
                    );
//--- signal line counted in the 2-nd buffer
   SimpleMAOnBuffer(rates_total,prev_calculated,0,SignalSMA,ExtBuffer,ExtSignalBuffer);
//--- done
   return(rates_total);
  }
//+------------------------------------------------------------------+

Many thanks in advance.

 
Alexey Viktorov:

In my opinion, the approach is not logical at all. Why define the day of the week? What difference does it make what day it is, if the condition should say "do not open more than xxx orders today"?

It seems more logical to me to count the orders opened today and specify the corresponding condition.

there is no date of order opening.


If you know, please write how to do it)
I do not understand how to make sure that on any given day no more than n-th number of orders are opened during the whole day.

 
cripple:

Please help. I can not understand the for() loop, all the time after the update, because of the offset(iMA(NULL,PERIOD_M1,1,1,MODE_SMA,PRICE_OPEN,i)) The indicator redraws!


I thank you in advance.

The MAs have different TFs. You need to sort of fit the higher timeframe into the M1 TF, i.e. count the MAs twice with different numbers of ticks. In this case one and the same value of the older period will be added to different values of the younger one.

If you look through i you get, for example, 10 candles of period D1 and 10 M1. Logically something is wrong....

Another thing, if the indicator is set on M1, it will work without re-brisings most likely

 
Renat Akhtyamov:

the MAs have different TFs. you have to sort of fit the higher timeframe into the M1 TF, i.e. count the MAs twice with different numbers of ticks.

By going through i you are now taking for example 10 candles of period D1 and 10 M1. Logically something is wrong....

Yes you're right, but my mind is still not enough to understand how to make M1 count correctly
 
cripple:
Yes you are right, but my mind is still not enough to figure out how to get M1 to count correctly

Besides, I need to synchronize M1 with a higher timeframe, because 1 bar of M5 will not necessarily correspond to 5 candles of M1, it may be 4 or 1.

 
cripple:
Yes you are right, but my mind is still not enough to understand how to make M1 count correctly

Try

int  Bars(
   string           symbol_name,     // имя символа
   ENUM_TIMEFRAMES  timeframe,       // период
   datetime         start_time,      // с какой даты
   datetime         stop_time        // по какую дату
   );

time of the i-th bar and put the resulting bar number instead of i.

 
Could you please tell me if you can use the keyboard to scroll through open pairs in the mt4 terminal?
 
LRA:
Dear novikov433!!! Teach you programming, or write you a free Expert Advisor or both!!! In exchange, teach me how loss-making orders are translated into no-loss. You can use a simple example. I give my wife an order (order): buy a bucket of potatoes early in the morning at the market, and by 10 o'clock (fundamental analysis) the price goes up - sell. But sometimes a truckload of potatoes arrives at 10:30 (news). And the price (on the news) instantly goes down and it lasts till the end of the day, or even for the whole week. I place a Stop Loss - if the price falls by 10 rubles, I sell as soon as I can (at the market price). How to change the order, to avoid losses. If this variant is interesting - send us your e-mail.
The problem is that the market went slightly to the required side and then went back, so you need to close the deal at breakeven and open it for a reverse. i got addicted to it as soon as i start. once again i got convinced that it is bullshit to trade with hands. how do i understand why i need to write so many components in my code?
 
Alexey Viktorov:

Try

the time of the i-th bar and put the resulting bar number instead of i.


Have you thought of something like this?
//+------------------------------------------------------------------+
//|                                                         help.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+

#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "Moving Averages Convergence/Divergence"
#property strict
#include <MovingAverages.mqh>
//--- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 2
#property   indicator_color1  clrSilver
#property   indicator_color2  clrRed
#property   indicator_width1  2

//--- indicator parameters
input int SignalSMA=8;            // Signal SMA Period
//--- indicator buffers
double    ExtBuffer[];
double    ExtSignalBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   IndicatorDigits(Digits+1);
//--- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_LINE);
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtBuffer);
   SetIndexBuffer(1,ExtSignalBuffer);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
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 i,limit;
//--- last counted bar will be recounted
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit++;
//--- counted in the 1-st buffer
   for(i=0;i<limit;i++)
     {
      int bars=iBarShift(Symbol(),PERIOD_M1,iTime(Symbol(),PERIOD_CURRENT,i),false);
      ExtBuffer[i]=(
                    iMA(NULL,PERIOD_CURRENT,1,0,MODE_SMA,PRICE_HIGH,i)
                    +iMA(NULL,PERIOD_M1,1,1,MODE_SMA,PRICE_OPEN,bars)
                    );
      Print(bars);
     }
//--- signal line counted in the 2-nd buffer
   SimpleMAOnBuffer(rates_total,prev_calculated,0,SignalSMA,ExtBuffer,ExtSignalBuffer);
//--- done
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
ax00071:
Thank you for your attention )) I am a sucker ... ... at closing I had the condition to close the deal at 22:00 on Friday, without any additional conditions to check the deal type. The deal itself closed a couple of hours earlier. Well, when 22:00 p.m. came, the Expert Advisor started sending orders to close an order that was already closed ... .
You should hardly call yourself a plant. If you have managed to locate, understand and correct such an error, you are getting closer to the level of a programmer!