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

 
Artyom Trishkin #:

Dobro. Let me put it in simple Russian terms.

You're going fishing.

  1. You open the pantry at home, scratch your head and close it.
  2. You go fishing and you need a fishing rod.
  3. You go home to the storeroom to get a fishing rod.
  4. Went fishing, put in your fishing rod and caught a fish, and you need a net
  5. Went home to the storeroom for the net
  6. Went fishing, picked up the fish you had caught and floated on the hook with the net, and you needed a container for storing the fish you had caught
  7. Go home for ..... Shall I continue?

Or you can take everything you need from the pantry (OnInit) straight away, fish without running around and, when you get home, put everything in the pantry and refrigerator (OnDeinit).

You may have known that here on the forum. You just need to listen and hear what you are told sometimes.

Otherwise you get a question, get an answer, say "bullshit" and do as you think.

You must first think over the question, ask questions, and then start editing.

Are you aware that the simplest thing in programming is printing code? And the lion's share of development is thinking over the logic.

That's a ridiculous analogy. In that case let's make all variables, class objects etc. global and not create them on demand or maybe a graphical object is something else (certainly not a fishing rod or a net :)) ? So my approach is quite rational and justified, it's just that mql or metatrader screws up.

 
MakarFX #:
This way the signal will be missed

Either I'm being obtuse, or there is no condition if there are open orders AND there is a signal and the bar is already new.

If the signals are long, more than a bar multiple, it will also work on the first tick of the bar.

The only question is when to open an order, when the signal appears or at the first tick of the next bar.
 
Valeriy Yastremskiy #:

Either I'm being obtuse, or there is no condition if there are open orders AND there is a signal and the bar is already new.

If the signals are long, more than a bar multiple, it will also work on the first tick of the bar.

The only question is when the order should be opened, at the moment of the signal or at the first tick of the next bar.
I ask you to open one order per bar
 
законопослушный гражданин Sleep(), but Makar said it would be better not to stop the process.


The code now looks like this:

Before opening, check if there is an open position on this candle. If not, open it.

 
законопослушный гражданин #:

In principle, an open order would normally close on the ticks of an already open bar. MakarFX and Artem have correctly noted. Therefore, the right thing to do would be to do this:

void OnTick()
  {
  datetime cTime;
  static datetime time = 0;
  int nOrders;

  cTime = iTime(NULL, PERIOD_CURRENT, 0);

  nOrders = CountOrders();

  if (time != cTime && nOrders == 0)
    time = cTime;
  else
    return;

// Получим значение индикатора
   dMA = iMA(Symbol(), 0,PeriodMA, MovingShift, MODE_SMA, PRICE_CLOSE, 0); // MODE_SMA - простое усреднение , значение 0. PRICE_CLOSE- цена закрытия, значение 0.

// Если нет открытых ордеров, то входим в условие
      if(nOrders == 0) // теперь это условие можно убрать
     {
// Если появился сигнал на покупку, то откроем ордер на покупку
      if(bSignalBuy() == true)
         vOrderOpenBuy();

// Если появился сигнал на продажу, то откроем ордер на продажу
      if(bSignalSell() == true)
         vOrderOpenSell();
     }
   }
 
Mihail Matkovskij #:

In principle, an open order would normally close on the ticks of an already open bar. MakarFX and Artem have correctly noted. Therefore, the right thing to do is this:

Artem is right, it should be like this

//+-----------------------------------------------------------------------------------------------+
void OnTick()
  {
// Получим значение индикатора
   dMA = iMA(Symbol(), 0,PeriodMA, MovingShift, MODE_SMA, PRICE_CLOSE, 0); // MODE_SMA - простое усреднение , значение 0. PRICE_CLOSE- цена закрытия, значение 0.

// Если нет открытых ордеров и появился сигнал на покупку, то откроем ордер на покупку
   if(CountOrders()==0&&bSignalBuy())
     {
      vOrderOpenBuy();
     }
// Если нет открытых ордеров и появился сигнал на продажу, то откроем ордер на продажу
   if(CountOrders()==0&&bSignalSell())
     {
      vOrderOpenSell();
     }
   }
 
MakarFX #:

You're right, Artem, it should be like this.

The result will be the same here. Only the code is more complicated. You are writing the same code in two different functions. And it will be less readable. Although, everyone writes as he likes or feels comfortable...

 

There's a lot going on here.

An open position has an opening time. The open time can be compared to the time of a candle - to find out if the time of opening the position is inside the candle (and this is the current candle).

And why do you always find out the number of orders in all the examples? And if there are zero orders, then you open them. This greatly limits the possibilities.

Generally, we should only focus on the number of orders/positions for a very narrow strategy direction.

 
Mihail Matkovskij #:

The result here will be the same. Only the code is more confusing. You are writing the same code in two different functions. And it will be less readable. Although, everyone writes as he likes or feels comfortable...

Your code

  if (time != cTime && nOrders == 0)
    time = cTime;

(A bar has opened and no order has been placed)

checked for a signal - no signal

... we wait for the next bar.

You repeated the same mistake as last time

P.S.

this is

 time = cTime;
must be carried out after the order has been opened
 
MakarFX #:

You're right, Artem, it should be like this

In your code, if one position is open, another one will not open by the opposite signal. I.e., first of all, when the signal comes in, you need to check whether there is an opposite position and close it.

But again, this all greatly limits the possibilities to improve the strategies.

We should avoid binding to the presence/absence of orders (especially in four - pending orders (which is correct) and positions (which is wrong)).

No, it is correct and necessary to have the number of orders and positions by their types at hand. But it is wrong to rely on the absence of any orders only for the signals.

In general, the correct way is to have functions to count orders and positions (one function that fills the structure when the number of orders and positions changes), functions to open/close positions, functions to set orders and alarm functions. Functions of various trawls and getting data from indicators. Plus - ability to compare the data of a position (open or closed) with some values.

Any strategy can be built out of this set.