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

 

I can't finish the Expert Advisor. The essence is simple - first I draw one rectangle, which is a buy trigger, in case the price gets in its area, then I draw the second - when the price gets there, the deal should be closed.

But the opposite happens - the price starts to open and close within the area. What am I doing wrong?


//+------------------------------------------------------------------+

//| RECTANGLES OP_BUY.mq4 |

//+------------------------------------------------------------------+

#property copyright ""

#property link ""

#property version "1.00"

#property strict


extern int Magic = 777701;

extern double Lot = 0.01;

extern int Slippage = 3;



int ticket;

int OrderOfSymbol;




//+------------------------------------------------------------------+

//| Expert initialisation function |

//+------------------------------------------------------------------+

int OnInit()

{

//---

return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+

//| Expert deinitialization function |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

//---

}

//+------------------------------------------------------------------+

//| expert tick function |

//+------------------------------------------------------------------+

void OnTick()

{

//---

//Check for a rectangle:

if(ObjectsTotal(OBJ_RECTANGLE)==0)

{Comment("Draw a rectangle to open a buy trade!");}


//Get the rectangle's NAME:

for(int n=0;n<ObjectsTotal();n++)

{

string name=ObjectName(n);

if(ObjectType(name)==OBJ_RECTANGLE)

{

datetime t1a = (datetime) ObjectGet(name,OBJPROP_TIME1);

datetime t2a = (datetime) ObjectGet(name,OBJPROP_TIME2);


double p1a=NormalizeDouble(ObjectGet(name,OBJPROP_PRICE1),Digits);

double p2a=NormalizeDouble(ObjectGet(name,OBJPROP_PRICE2),Digits);

OrderOfSymbol=CounterOrderTradeType(-1);

if (OrderOfSymbol<1)

if (Close[1]>Open[1])

if(TimeCurrent()>t1a && TimeCurrent()< t2a &&

Bid < p1a && Bid > p2a )

{

ticket = OrderSend(Symbol(),OP_BUY, Lot, Ask, Slippage, 0, 0, "open buy order", Magic, 0, Blue);

}

}

}

//Check if there is already a secondrectangle:

if(ObjectsTotal(OBJ_RECTANGLE)==1)

{Comment("Draw a second rectangle to close the buy trade!");}


//Get the rectangle's NAME:

for(int n=0;n<ObjectsTotal();n++)

{

string name=ObjectName(n);

if(ObjectType(name)==OBJ_RECTANGLE)

{

datetime t1b = (datetime) ObjectGet(name,OBJPROP_TIME1);

datetime t2b = (datetime) ObjectGet(name,OBJPROP_TIME2);


double p1b=NormalizeDouble(ObjectGet(name,OBJPROP_PRICE1),Digits);

double p2b=NormalizeDouble(ObjectGet(name,OBJPROP_PRICE2),Digits);


if(TimeCurrent()>t1b && TimeCurrent()< t2b &&

Bid < p1b && Bid > p2b )

CloseBuyPositions1();

}

}

//Check to see if there is already a 2nd rectangle:

if(ObjectsTotal(OBJ_CHANNEL)==2)

{Comment("Buy order open and close areas are set - trade is on!");}


}

//+------------------------------------------------------------------+

void CloseBuyPositions1()

{

for(int i = OrdersTotal() - 1; i >= 0; i--)

if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

if(OrderMagicNumber() == Magic)

if(OrderSymbol() == Symbol())

{



if(OrderType()==OP_BUY)

{

if(OrderClose(OrderTicket(), OrderLots(), Bid, 3, NULL)){Print("Order Close");}

}

}

}

//+------------------------------------------------------------------+

//| Order counting |

//+------------------------------------------------------------------+

int CounterOrderTradeType(ENUM_ORDER_TYPE order_type)

{

int cnt=0;

//----

for(int pos=OrdersTotal()-1;pos>=0;pos--)

{

if(OrderSelect(pos, SELECT_BY_POS, MODE_TRADES)==false) continue;

if(OrderSymbol()!=_Symbol) continue;

if(order_type == OrderType() || order_type == -1) cnt++;

}

//----

return(cnt);

}

 
Порт-моне тв:

There's a function for posting code like this, it will make it easier for everyone to read your code to help.

 
MakarFX:

There's a function for posting code like this, it would make it easier for everyone to read your code to help.

It won't help him. Blank lines are only read by people like him.

 

Порт-моне тв:

What am I doing wrong?

There is no uniqueness in the name of the square, you have any square as a condition for buying and selling

 
Порт-моне тв:

This will also help you


 

Please advise how to correct the error

//+------------------------------------------------------------------+
#property copyright   "Copyright 2020,"
#property version     "1.0"
#property strict

//--- Inputs
int   side_position                    = 1;
input ENUM_BASE_CORNER  CornerInfo     = CORNER_RIGHT_UPPER; 
input ENUM_BASE_CORNER  CornerInfo1    = CORNER_LEFT_UPPER; 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   if(side_position == 2)
   {
   CornerInfo  = CORNER_RIGHT_LOWER; 
   CornerInfo1 = CORNER_LEFT_LOWER;
   }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+

Here's the error

'CornerInfo' - constant cannot be modified      Wa.mq4  22      1
'CornerInfo1' - constant cannot be modified     Wa.mq4  23      1
2 errors, 0 warnings            3       1
 
Forallf:

Thank you!

You're welcome )

MakarFX:

Could you please tell me how to correct the error?

Here is an error

Input variables cannot be changed.

You can do it like this:

input ENUM_BASE_CORNER  CornerInfo     = CORNER_RIGHT_UPPER; 

ENUM_BASE_CORNER  MyCornerInfo     = CornerInfo; 


.......

if(side_position == 2)
   {
   MyCornerInfo  = CORNER_RIGHT_LOWER; 



 
Aleksei Stepanenko:

You're welcome )

input variables cannot be changed.

You can do it like this:

Thanks. Got it.

 
MakarFX:

This will help you too.


It didn't. And I realised that the square values are not taken from two as they should be, but from the last one. I don't know how to beat that. Wouldn't I have figured it out on my own. I'm not fluent in mql, that's why I'm asking you here and you're offering me combs.

 
Порт-моне тв:

didn't help. And I realised that the square values are not taken from two as they should be, but from the last one. I don't know how to beat that. I wouldn't have guessed it myself. I'm not fluent in mql, that's why I'm asking you, and you're offering me combs.

I'm offering you combs to make your code easy to read for those who you address (no malice here).

As for your squares, in order to properly identify them you need to understand their origin, i.e. you draw them or an advisor

Reason: