Questions from Beginners MQL5 MT5 MetaTrader 5 - page 721

 
sile:

Hello.

Please help.

Signal when indicator lines are crossed on the first bar.

The position is opened correctly on the first bar if the shift ma_shiftT = 0 and ma_shiftD = 0. If the shift ma_shiftT = -1 and ma_shiftD = 1, the position is opened on the third or fourth bar.

What should I do to open a position if it is crossed on the first bar?


input int      ma_shiftT=-1;
input int      ma_shiftD=1;
int iTEMAHandle;   // хэндл индикатора iTEMA
int iDEMAHandle;    // хэндл индикатора
double ma1Val[]; // динамические массивы для хранения численных значений  для каждого бара
double maVal[];  // 

//+------------------------------------------------------------------+
int OnInit()
  {
//---
m_symbol.Name(Symbol());  
//--- Получить хэндл индикатора iTEMA
iTEMAHandle=iTEMA(_Symbol,PERIOD_CURRENT,30,ma_shiftT,PRICE_CLOSE);;
//---Получить хэндл индикатора
iDEMAHandle=iDEMA(_Symbol,PERIOD_CURRENT,30,ma_shiftD,PRICE_CLOSE);;
  
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- освобождаем хэндлы индикаторов
   IndicatorRelease(iTEMAHandle);

   IndicatorRelease(iDEMAHandle);
   }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  
  //--- массив значений индикатора
   ArraySetAsSeries(ma1Val,true);

//--- массив значений индикатора
   ArraySetAsSeries(maVal,true);
  
  if(CopyRates(_Symbol,_Period,0,5,mrate)<0)
     {
      Alert("Ошибка копирования исторических данных - ошибка:",GetLastError(),"!!");
      return;
     }
//====
  if(CopyBuffer(iTEMAHandle,0,0,5,ma1Val)<0)
     {
      Alert("Ошибка копирования буферов индикатора TEMA - номер ошибки:",GetLastError(),"!!");
      return;
     }
   if(CopyBuffer(iDEMAHandle,0,0,5,maVal)<0)
     {
      Alert("Ошибка копирования буферов индикатора DEMA - номер ошибки:",GetLastError());
      return;
     }

   bool Sell_Condition_3=(maVal[2]<=ma1Val[2]);
   bool Sell_Condition_4=(maVal[1]>ma1Val[1]);
  
   if(Sell_Condition_3 && Sell_Condition_4)
   {
   // открываю позицию SELL
   }
  }

First: Why copy 5 indicator values if you only use one?

Second: The position of indicator lines does not indicate their crossover.

It is sufficient to copy 2 values, starting from the first one.

CopyBuffer(iTEMAHandle, 0, 1, 2, ma1Val)

CopyBuffer(iDEMAHandle, 0, 1, 2, maVal)

The only way to determine whether there is an intersection is to compare the position of the lines on the second bar and the first. FOR THE TASK AT HAND.

It is necessary to consider the direction of array indexing. In this case we will get

if(maVal[0] <= ma1Val[0] && maVal[1] > ma1Val[1])

If the condition is met, the fact of intersection is obvious. The direction of intersection can be determined by yourself.


ps; Oh, right... I didn't look carefully at the definition of the fact of intersection. Well in any case extra values are not needed. They are probably what threw you off.

 
sile:

Hello.

Please help.

Signal when indicator lines are crossed on the first bar.

The position opens correctly on the first bar if the shift ma_shiftT = 0 and ma_shiftD = 0. If the shift ma_shiftT = -1 and ma_shiftD = 1, the position opens on the third or fourth bar.

What should I do to open a position when it crosses on the first bar?


input int      ma_shiftT=-1;
input int      ma_shiftD=1;
int iTEMAHandle;   // хэндл индикатора iTEMA
int iDEMAHandle;    // хэндл индикатора
double ma1Val[]; // динамические массивы для хранения численных значений  для каждого бара
double maVal[];  // 

//+------------------------------------------------------------------+
int OnInit()
  {
//---
m_symbol.Name(Symbol());  
//--- Получить хэндл индикатора iTEMA
iTEMAHandle=iTEMA(_Symbol,PERIOD_CURRENT,30,ma_shiftT,PRICE_CLOSE);;
//---Получить хэндл индикатора
iDEMAHandle=iDEMA(_Symbol,PERIOD_CURRENT,30,ma_shiftD,PRICE_CLOSE);;
  
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- освобождаем хэндлы индикаторов
   IndicatorRelease(iTEMAHandle);

   IndicatorRelease(iDEMAHandle);
   }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  
  //--- массив значений индикатора
   ArraySetAsSeries(ma1Val,true);

//--- массив значений индикатора
   ArraySetAsSeries(maVal,true);
  
  if(CopyRates(_Symbol,_Period,0,5,mrate)<0)
     {
      Alert("Ошибка копирования исторических данных - ошибка:",GetLastError(),"!!");
      return;
     }
//====
  if(CopyBuffer(iTEMAHandle,0,0,5,ma1Val)<0)
     {
      Alert("Ошибка копирования буферов индикатора TEMA - номер ошибки:",GetLastError(),"!!");
      return;
     }
   if(CopyBuffer(iDEMAHandle,0,0,5,maVal)<0)
     {
      Alert("Ошибка копирования буферов индикатора DEMA - номер ошибки:",GetLastError());
      return;
     }

   bool Sell_Condition_3=(maVal[2]<=ma1Val[2]);
   bool Sell_Condition_4=(maVal[1]>ma1Val[1]);
  
   if(Sell_Condition_3 && Sell_Condition_4)
   {
   // открываю позицию SELL
   }
  }

I have attached an example to help you - in this example I am not copying multiple values, but one at a time. By comparing what my example returns and what your example returns, you should see - how indexing occurs in the arrays in your example (i.e. you should understand where an element from bar 1 is stored in the array and where an element from bar 2 is stored).

How to work with my example:

I created and saved a chart template specifically for the example settings with the same name as the example - "Two iMA Shift minus shift plus.tpl". Save this template in \MQL5\Profiles\Templates folder of your terminal. Now when you run the tester, the tester will pick up this template and you will have such a nice picture in the tester:

Untitled

When it detects an intersection, the tester will stop at the directive

//---
   if(ma_one_1<ma_two_1 && ma_one_2>ma_two_2)
     {
      string text="ma_one_1 = "+DoubleToString(ma_one_1,Digits()+1)+"\n"+
                  "ma_two_1 = "+DoubleToString(ma_two_1,Digits()+1)+"\n"+
                  "ma_one_2 = "+DoubleToString(ma_one_2,Digits()+1)+"\n"+
                  "ma_two_2 = "+DoubleToString(ma_two_2,Digits()+1);
      Comment(text);
      DebugBreak();
     }

and you will be able in the tester, using "Crosshair" to move over the bars and see the values of the indicators on bar 1 and bar 2 (pre-include the "Data window").

 

Hello!

I have a question about the spread in FOREX.

When the spread widens is ASK raised, BID lowered or do they both move symmetrically in different directions?

 
Sergey Zhukov:

Hello!

I have a question about the spread in FOREX.

When the spread widens, does it ASK go up, BID goes down or do they both move symmetrically in different directions?

It may be either way. One thing remains the same - the spread is the difference between the Ask price and the Bid price.
 
Vladimir Karputov:
It can be anything you like. One thing remains the same - the spread is the difference between the Ask price and the Bid price.
Did I understand correctly that when the spread starts narrowing, they can move towards each other at the same time?
 
Sergey Zhukov:
Did I understand correctly that when the spread starts to narrow, they can move towards each other at the same time?

"One thing remains the same - the spread is the difference between the Ask price and the Bid price."

There are no other laws.

 
Vladimir Karputov:

"One thing remains the same - the spread is the difference between the Ask price and the Bid price."

There are no other laws.

What do you think, if let's say I have a buy and the spread starts to widen, Ask goes up and Bid goes down, should I close the position or wait for Ask to go down?
 
Sergey Zhukov:
What do you think, if I have a buy and the spread starts to widen, Ask goes up and Bid goes down, should I close the position or wait until Ask goes down?

A buy (BUY position) is closed at the Bid price.

Another thing you are probably asking is "what to do in periods of ultra-high volatility?". The answer is that everyone decides differently.

 
Vladimir Karputov:

A buy (BUY position) is closed at the Bid price.

Another thing you are probably asking is "what to do in periods of ultra-high volatility?". The answer is that everyone decides differently.

Yes, that's right, it's the ultra-high volatility I meant, I'll try searching the forum, thanks.
 
Vladimir Karputov:

A buy (BUY position) is closed at the Bid price.

Another thing you are probably asking is "what to do in periods of ultra-high volatility?". The answer is that everyone decides differently.

Yeah, well... Where to find such a broker... Can you give me a link?
Reason: