Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1426

 
MrBrooklin #:

Look at this code:

Regards, Vladimir.
Unfortunately, the next two days I can't check it, but please accept my thanks!
 
vitaliy zamkovoy #:
Unfortunately, I will not be able to check it for the next two days, but please accept my thanks!

Thank you, but please note that this is not a ready code, but only the direction of my thought. It may be wrong. You need to check it. You may consider another option - remove OnTimer and try the same code in OnTick(). In short, try it.

Regards, Vladimir.

 
vitaliy zamkovoy current price and compare it to some constant with the subsequent opening of an order - how do you record it?

Record the current price and time. Compare the time with the current time on each tick, if it is more than 20 seconds remember the second price. Calculate the difference between the first price and the second price. Compare the obtained result with a constant and make a decision to open an order.

 
Valeriy Yastremskiy #:

Remember the current price and time. Compare the time with the current time on each tick, if more than 20 seconds remember the second price. Calculate the difference between the first price and the second one. Compare the obtained result with a constant and make a decision to open an order.

Do I understand you correctly: you need to remember the current price every second to compare it with the current price when each value of this series is removed for 20 seconds?

The comparison should be continuous.

 
vitaliy zamkovoy #:

Do I understand you correctly: you need to remember the current price every second, so that when each value of this series is deleted for 20 seconds, you can compare it with the current one?

The comparison should be continuous.

ticks do not go evenly, the question is in the algorithm then, if it is necessary to check all ticks, then all ticks should be memorised and compared. If it is possible less often according to the algorithm, then a timer and after a second remember the price and after 20 seconds also remember and compare. Or rather you should remember all prices or prices in a second within 20 seconds and compare with a new tick.
There can be 1 tick per second, there can be one in 5 seconds, and there can be 100 in one second. You have to decide how to act when there is one tick per 5 seconds and when there are 100 ticks per second.

 
Valeriy Yastremskiy #:

ticks are not uniform, the question is in the algorithm then, if you need to check all ticks, then all ticks should be memorised and compared. If it is possible to be less frequent according to the algorithm, then a timer and after a second remember the price and after 20 seconds also remember and compare. Or rather you should remember all prices or prices in a second within 20 seconds and compare with a new tick.
There can be 1 tick per second, there can be one in 5 seconds, and there can be 100 in one second. You have to decide how to act when there is one tick per 5 seconds and when there are 100 ticks per second.

It's not a question of choosing an option, it's a question of the difficulty for me to write it down.... I'm here for the first time today. I'm a total amateur.

I just want to try out my idea...

 
vitaliy zamkovoy #:

It's not a question of choosing an option, it's a question of the difficulty for me to write it down..... This is my first time here today. Total amateur.

I just want to try out my idea.

The algorithm must be precise, and understanding of the conditions of its application. You can't write code any other way. The code is the realisation of one algorithm of actions, it cannot be inaccurate.

And here it is better to write it yourself first, and perhaps you will correct mistakes.

You understand the signs of equals, more is less.

 

Ring buffer:

Take a buffer - a large enough static array of "price, time" structures (or two separate arrays).

We take two variables - "pointers" (indices) to the head and tail of the buffer.

On each tick we add an element to the head and advance the pointer.

Compare the time with the element in the tail, if necessary - move the pointer.

Compare the price with the element in the tail.

If some pointer has reached the end of the array, we reset this pointer to zero.

 
JRandomTrader static array of "price, time" structures (or two separate arrays).

Take two variables - "pointers" (indices) to the head and tail of the buffer.

On each tick we add an element to the head and move the pointer.

Compare the time with the element in the tail, if necessary - move the pointer.

Compare the price with the element in the tail.

If some pointer has reached the end of the array - reset this pointer to zero.

👍
 
Valeriy Yastremskiy #:

The algorithm must be precise, and an understanding of the conditions of its application. It is impossible to write code otherwise. The code is the realisation of one algorithm of actions, it cannot be inaccurate.

And here it is better to write it yourself first, and perhaps errors will be corrected.

You understand the signs of equals, more is less.

Hello, Valery! I completely agree with you. To write a correct code, you need an exact condition for opening a position. This code works too:

//+------------------------------------------------------------------+
//|                                                         Test.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//ВХОДНОЙ ПАРАМЕТР
input ushort Constanta=50; //Константа
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Tick function                                                    |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double open_0=iOpen(_Symbol,PERIOD_CURRENT,0);
   if(open_0-SymbolInfoDouble(_Symbol,SYMBOL_BID)>Constanta*_Point)
      Print("Цена прошла вниз расстояние больше чем Константа, поэтому нужно открыть позицию SELL!");
   if(SymbolInfoDouble(_Symbol,SYMBOL_BID)-open_0>Constanta*_Point)
      Print("Цена прошла вверх расстояние больше чем Константа, поэтому нужно открыть позицию BUY!");
   Sleep(20000);
  }
//+------------------------------------------------------------------+

but the whole question is whether it should work as Vitaly intended? Frankly speaking, I don't quite understand why once every 20 seconds (or even once every 10, 5 or 1 second), we need to check the price difference. What will it give in terms of determining the direction of further price movement?

Regards, Vladimir.

Reason: