Learning and writing together in MQL5 - page 24

 

What does this function do that is clear to everyone?

int OpenSell(double volume,int slippage=10,string comment="Open Short EUR/USD (Sell)",int magic0=102406)

{

MqlTradeRequest my_trade;

MqlTradeResult my_trade_result;

my_trade.action=TRADE_ACTION_DEAL;

my_trade.symbol=Symbol();

my_trade.volume=NormalizeDouble(volume,1);

my_trade.price=NormalizeDouble(Bid,_Digits);

my_trade.sl=NormalizeDouble(Bid+StopLoss*_Point,_Digits);

my_trade.tp=NormalizeDouble(Bid-TakeProfit*_Point,_Digits);

my_trade.deviation=slippage;

my_trade.type=ORDER_TYPE_SELL;

my_trade.type_filling=ORDER_FILLING_AON;

my_trade.comment=comment;

my_trade.magic=magic;


ResetLastError();

if(OrderSend(my_trade,my_trade_result))

{

time_oc=TimeLocal();

md=0;

Print(" Operation result code - ", my_trade_result.retcode);

}

else

{

Print("Operation result code - ",my_trade_result.retcode);

Print("Order open error = ",GetLastError());

}

return(0);

}

In the OnTick() procedure

...

OpenSell(Lots,10, "EUR/USD (Sell)",102406);

...

At the very beginning:

double StopLoss=250,

TakeProfit=1400;

extern double Lots = 0.1;

Why the hell is $220 in the test results?

<DATE> <BALANCE> <EQUITY> <MARGIN LEVEL

01,01,2010 0 :00:00 10000 10000 0

11,01,2010 2 :57:00 9779,29 9841,92 6827

15,01,2010 16:00:00 9829,8 9941,9 6857

22,01,2010 14:32:00 10124,1 10135,9 7167

26,01,2010 2 :14:00 10048,6 10048,6 0

26,01,2010 4 :06:00 10048,6 10038,4 7099

27,01,2010 21:12:00 10188,5 10188,5 0

29,01,2010 2 :16:00 10188,5 10308 7346

29,01,2010 7 :14:00 10188,5 10277,4 7324

04,02,2010 22:05:00 10418,63 10480,83 7592


If I'm wrong, poke me in the nose.

I will not let you see the whole EA. The function on Buy is similar...

I close position with opposite order.

 

All right. I've been shouting my head off. Just learning the language. If I offended anyone, I apologize. I will work and look for the problem. I have probably done it myself, but I really want to learn how to make good EAs using mql5. It is a very good system, but . I have problems with it so far.

Although I have 20 Expert Advisors on mql4 without any problems.

 
Khomtchenko:
...
It would be better to give the transaction history for the period in question, rather than these unknown figures. Then perhaps it will be clearer where the loss came from.
 

I've started to study the "File operations" section little by little. Do you know if there are any tutorials on how to work with files in MQL5?

 

What is the correct way to delete all orders with a certain mode?

I have two functions for this, check_orders checks for orders with a certain mode, and remove_sl deletes them:

void remove_sl()
     {
      int counter01;
      ulong counter02;
      while(check_orders()==true)
        {
         for(counter01=0; counter01<OrdersTotal(); counter01++)
           {
            counter02=OrderGetTicket(counter01);
            if(OrderGetInteger(ORDER_MAGIC)!=magick)
               continue;
            remove_request.action=TRADE_ACTION_REMOVE; remove_request.order=counter02;
            if(OrderCheck(start_request,check_result)==true)
               OrderSend(start_request,trade_result);
           }
        }
     }



bool check_orders()
     {
      int counter01;
      bool order_exist=false;
      for(counter01=0; counter01<OrdersTotal(); counter01++)
        {
         OrderGetTicket(counter01);
         if(OrderGetInteger(ORDER_MAGIC)==magick)
           {
            order_exist=true;
            break;
           }
        }
      return(order_exist);
 

The problem is that in EA's journal these lines appear:

2011.05.11 21:40:19 Trades '726238' : failed cancel order #4375237 buy 0.00 at 0.00000 [Invalid request]
I.e. there are redundant requests to the trade server to delete the order the request to delete that has already been sent.

 

I think I have a problem with closing and opening positions.

Somehow, instead of having a position for 0.1 lot, I actually have several lots. Apparently the positions of one symbol overlap and the volume of the position grows. This is the only way to explain the above mentioned problems with large stop values and big difference when testing. It means that I am analyzing the presence of open trades incorrectly or closing the trails incorrectly.

This is the procedure I use to determine if there is an open position by a symbol:

int Total()

{

count=0;

for (i=0; i<=PositionsTotal(); i++)

{

if (PositionGetSymbol(i)==_Symbol) {count++;}

}

return(count);

}

Of course, the function returns either 0 or 1.

I use the following entry to open orders

if (Total()<1)

{

if (# some check on indicators#) OpenBuy(Lots,10, "EUR/USD (Buy)",102406);

if (#some check on indicators#) OpenSell(Lots,10, "EUR/USD (Sell)",102406);

}

I open buy and sell it looks like:

int OpenSell(double volume,int slippage=10,string comment="Open Short EUR/USD (Sell)",int magic0=102406)

{

MqlTradeRequest my_trade;

MqlTradeResult my_trade_result;

my_trade.action=TRADE_ACTION_DEAL;

my_trade.symbol=Symbol();

my_trade.volume=NormalizeDouble(volume,1);

my_trade.price=NormalizeDouble(Bid,_Digits);

my_trade.sl=NormalizeDouble(Bid+StopLoss*_Point,_Digits);

my_trade.tp=NormalizeDouble(Bid-TakeProfit*_Point,_Digits);

my_trade.deviation=slippage;

my_trade.type=ORDER_TYPE_SELL;

my_trade.type_filling=ORDER_FILLING_AON;

my_trade.comment=comment;

my_trade.magic=magic;


ResetLastError();

if(OrderSend(my_trade,my_trade_result))

{

time_oc=TimeLocal();

md=0;

Print(" Operation result code - ", my_trade_result.retcode);

}

else

{

Print("Operation result code - ",my_trade_result.retcode);

Print("Order open error = ",GetLastError());

}

return(0);

}

I close trades in the main procedure in the same way:

if (Total()>0) TryToClose();

TryToClose is like this:

int TryToClose()
{
for (i=0; i<=PositionsTotal(); i++)
{
if (PositionGetSymbol(i)==_Symbol)
{
if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
if(# some indicator check#) OpenSell(Lots,10, "EUR/USD (Sell)",102406)
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
if(# some indicator check#) OpenBuy(Lots,10, "EUR/USD (Buy)",102406);
}
}
return(0);

}

The indicator checks that if there should be a Buy position and there is a Sell position, then we do a Buy.

Where I made a mistake. Why am I getting enlarged positions.

 
Khomtchenko:

Insert the code via the SRC button

Будет читабельнее
Do you know the difference between an MT4 order and an MT5 order ?
 

In mql4 orders are either trade or pending.

In mql5 an order is a pending trade. Entry into the market is an opening of a position. When the order is triggered, a position is opened. There can be only one position for one symbol, and there can be as many orders as we want.

Спасибо за подсказку
 

Everyone keeps hinting at me to be stupid. I may be dumb. Well, find my mistakes so I can correct them.

 
Khomtchenko:

I'm all sort of hinting at stupidity. I may be stupid. Well, find my mistakes so that I can correct them.

I'm not hinting at anything, I just ran by and asked what is most often the reason, I don't know your level of training.

I've been here a long time, I've already formed an opinion about many people (everyone is strong in his own way), I don't know you yet, so I'm not going to hint at anything.


I don't see the big picture of the strategy. Judging by the way you're going through the positions, the strategy is multi-currency.

But you've prescribed only one tool, if the tool is one, why cycle through the positions?

If you know the instrument, you don't need to look through all the positions to find them, you just need to use this function:

bool  PositionSelect(
   string  symbol     // имя инструмента
   );