Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1483

 
klycko DEAL_ENTRY_OUT modifier.
But how to use it, I can't understand.
void  OnTradeTransaction(
   const MqlTradeTransaction&    trans,   // структура торговой транзакции
   const MqlTradeRequest&        request, // структура запроса
   const MqlTradeResult&         result   // структура ответа
)
  {
   if(trans.type==TRADE_TRANSACTION_DEAL_ADD)
     {
      if(HistoryDealSelect(trans.deal) && HistoryDealGetInteger(trans.deal,DEAL_ENTRY)==DEAL_ENTRY_OUT)
        {

           //---

        }

Regards, Vladimir.

 
Alexey Viktorov #:

You're in the right direction. But not attentive enough

Hi Alexei, it's great that you support beginners learning a programming language and don't get tired of people like us. Thank you very much!

What is the main problem of beginners. I will only speak for myself. Yes, indeed, I don't always understand how to apply the variety of possibilities offered by the MQL5 programming language. Without basic education or practice of programming in top-level languages, it is very hard to get into this topic. Only forum members like you help me. I realise that many people are tired (and sometimes do not have enough time or patience) of constantly prompting a newcomer who has suddenly appeared on the forum.

I would like to express my gratitude once again to all those who respond to our dilettantish questions and wish - God grant you all health, long life, good luck and prosperity!!!!

Regards, Vladimir.

 

Good day to all.
Please, tell me how to add the Accelerator Oscillator indicator to give a signal for SELL on the red colour and a signal for BUY on the green colour, on the closing of the last bar.

Thanks

 
makssub Accelerator Oscillator indicator to give a signal for SELL on the red colour and a signal for BUY on the green colour, on the closing of the last bar.

Thanks

Connect the indicator to the Expert Advisor and get its data through CopyBuffer(). The colour buffer has index 1, where 0 is green and 1 is red.
Документация по MQL5: Технические индикаторы / iAC
Документация по MQL5: Технические индикаторы / iAC
  • www.mql5.com
iAC - Технические индикаторы - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Artyom Trishkin #:
Connect the indicator to the Expert Advisor and get its data through CopyBuffer(). The colour buffer has index 1, where value 0 is green, 1 is red.

If you don't mind. Can you give me an example?

MQL5 is hard for me after MQL4.

 
makssub #:

If it's not too much trouble. Can you give me an example?

MQL5 is hard for me after MQL4.

The examples are in the links in my post above
 

Hello.

There is an ADX indicator in the codebase. It contains this piece of code

//--- set draw begin
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtADXPeriod<<1);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtADXPeriod);
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtADXPeriod);

If

ExtADXPeriod=14

it turns out that ExtADXPeriod<<1 is equal to value 9.

Why do we need such an entry then? Can't it be written without bit shifts?

ExtADXPeriod<<1

full indicator code below


//+------------------------------------------------------------------+
//|                                                          ADX.mq5 |
//|                             Copyright 2000-2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright   "Copyright 2000-2023, MetaQuotes Ltd."
#property link        "https://www.mql5.com"
#property description "Average Directional Movement Index"
#include <MovingAverages.mqh>

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_plots   3
#property indicator_type1   DRAW_LINE
#property indicator_color1  LightSeaGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
#property indicator_type2   DRAW_LINE
#property indicator_color2  YellowGreen
#property indicator_style2  STYLE_DOT
#property indicator_width2  1
#property indicator_type3   DRAW_LINE
#property indicator_color3  Wheat
#property indicator_style3  STYLE_DOT
#property indicator_width3  1
#property indicator_label1  "ADX"
#property indicator_label2  "+DI"
#property indicator_label3  "-DI"
//--- input parameters
input int InpPeriodADX=14; // Period ADX
//--- indicator buffers
double    ExtADXBuffer[];
double    ExtPDIBuffer[];
double    ExtNDIBuffer[];
double    ExtPDBuffer[];
double    ExtNDBuffer[];
double    ExtTmpBuffer[];

int       ExtADXPeriod;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- check for input parameters
   if(InpPeriodADX>=100 || InpPeriodADX<=0)
     {
      ExtADXPeriod=14;
      PrintFormat("Incorrect value for input variable Period_ADX=%d. Indicator will use value=%d for calculations.",InpPeriodADX,ExtADXPeriod);
     }
   else
      ExtADXPeriod=InpPeriodADX;
//--- indicator buffers
   SetIndexBuffer(0,ExtADXBuffer);
   SetIndexBuffer(1,ExtPDIBuffer);
   SetIndexBuffer(2,ExtNDIBuffer);
   SetIndexBuffer(3,ExtPDBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(4,ExtNDBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,ExtTmpBuffer,INDICATOR_CALCULATIONS);
//--- indicator digits
   IndicatorSetInteger(INDICATOR_DIGITS,2);
//--- set draw begin
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtADXPeriod<<1);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtADXPeriod);
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtADXPeriod);
//--- indicator short name
   string short_name="ADX("+string(ExtADXPeriod)+")";
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   PlotIndexSetString(0,PLOT_LABEL,short_name);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//--- checking for bars count
   if(rates_total<ExtADXPeriod)
      return(0);
//--- detect start position
   int start;
   if(prev_calculated>1)
      start=prev_calculated-1;
   else
     {
      start=1;
      ExtPDIBuffer[0]=0.0;
      ExtNDIBuffer[0]=0.0;
      ExtADXBuffer[0]=0.0;
     }
//--- main cycle
   for(int i=start; i<rates_total && !IsStopped(); i++)
     {
      //--- get some data
      double high_price=high[i];
      double prev_high =high[i-1];
      double low_price =low[i];
      double prev_low  =low[i-1];
      double prev_close=close[i-1];
      //--- fill main positive and main negative buffers
      double tmp_pos=high_price-prev_high;
      double tmp_neg=prev_low-low_price;
      if(tmp_pos<0.0)
         tmp_pos=0.0;
      if(tmp_neg<0.0)
         tmp_neg=0.0;
      if(tmp_pos>tmp_neg)
         tmp_neg=0.0;
      else
        {
         if(tmp_pos<tmp_neg)
            tmp_pos=0.0;
         else
           {
            tmp_pos=0.0;
            tmp_neg=0.0;
           }
        }
      //--- define TR
      double tr=MathMax(MathMax(MathAbs(high_price-low_price),MathAbs(high_price-prev_close)),MathAbs(low_price-prev_close));
      if(tr!=0.0)
        {
         ExtPDBuffer[i]=100.0*tmp_pos/tr;
         ExtNDBuffer[i]=100.0*tmp_neg/tr;
        }
      else
        {
         ExtPDBuffer[i]=0.0;
         ExtNDBuffer[i]=0.0;
        }
      //--- fill smoothed positive and negative buffers
      ExtPDIBuffer[i]=ExponentialMA(i,ExtADXPeriod,ExtPDIBuffer[i-1],ExtPDBuffer);
      ExtNDIBuffer[i]=ExponentialMA(i,ExtADXPeriod,ExtNDIBuffer[i-1],ExtNDBuffer);
      //--- fill ADXTmp buffer
      double tmp=ExtPDIBuffer[i]+ExtNDIBuffer[i];
      if(tmp!=0.0)
         tmp=100.0*MathAbs((ExtPDIBuffer[i]-ExtNDIBuffer[i])/tmp);
      else
         tmp=0.0;
      ExtTmpBuffer[i]=tmp;
      //--- fill smoothed ADX buffer
      ExtADXBuffer[i]=ExponentialMA(i,ExtADXPeriod,ExtADXBuffer[i-1],ExtTmpBuffer);
     }
//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }
//+------------------------------------------------------------------+
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2023.11.30
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Novichokkk #:

If

ExtADXPeriod=14

thenExtADXPeriod<<1 is equal to value 9

is equal to 28 (14<<1 is like 14*2, as long as there are enough digits)

Don't pay attention, anyway the compiler will calculate const expressions at compilation and they won't get into the code. Only their result

it could be premature optimisation or the author wanted to show that he is cool.

 
Maxim Kuznetsov #:

is equal to 28 (14<<1 is like 14*2, as long as there are enough digits)

don't pay attention, anyway the compiler will count known const expressions at compilation and they won't get into the code. Only their result

it could be premature optimisation or the author wanted to show that he is cool.

1-Why 28?

14 is binary. It's 1110.

Shift one bit to the left, it's 0111, convert back to decimal, it's 9. 1*1+1+1*2+1*4=7 (wrong not 9).


2-I still would like a concrete example in this case, what is more correct to insert in such a construction instead ofExtADXPeriod<<1?

 
Novichokkk #:

14 is binary. It's 1110.

Shift one bit to the left to 0111,

we have different "lefts" :-)

1110<<1 := 11100

Reason: