Questions from Beginners MQL5 MT5 MetaTrader 5 - page 463

 

How to get the position volume to 0 (zero)? (FORTS, Otkrytie-Broker, Real)

We have the following code:

#property strict
long     gTicks=0;
int      Step=0;
//=====
void OnTick()
{
   gTicks++;
   PositionSelect(_Symbol);
   //-----
   {if((gTicks>1000)&&(Step==0))
   {
      Print("OPEN>> *** VOLUME=",PositionGetDouble(POSITION_VOLUME),
                        " *** ID=",PositionGetInteger(POSITION_IDENTIFIER),
                        " *** TYPE=",EnumToString((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)),
                        " *** OrdersTotal()=",OrdersTotal());
      MqlTradeRequest request={0};                                   //Обнулим записи запроса
      MqlTradeResult result={0};                                     //Обнулим записи ответа
      request.action=TRADE_ACTION_PENDING;                           //Отложенный ордер
      request.symbol=_Symbol;                                        //Инструмент
      request.price=SymbolInfoDouble(_Symbol,SYMBOL_BID);            //Прайс (цена) исполнения ордера
      request.type=ORDER_TYPE_SELL_STOP;                             //Тип ордера
      request.type_filling=ORDER_FILLING_RETURN;                     //Разрешить исполнять частями (ORDER_FILLING_RETURN)
      request.type_time=ORDER_TIME_SPECIFIED_DAY;                    //В очереди до экспирации
      request.expiration=
         (datetime)SymbolInfoInteger(_Symbol,SYMBOL_EXPIRATION_TIME);//Время истечения фьючерсного контракта
      request.volume=1;                                              //Объем
      Print("OPEN OrderSend=",OrderSend(request,result));
      Print("OPEN Retcode=",result.retcode);
      Print("OPEN Order=",result.order);
      Print("OPEN Deal=",result.deal);
      Print("OPEN OrdersTotal()=",OrdersTotal());
      Print("OPEN Volume=",PositionGetDouble(POSITION_VOLUME));
      Step=1;
      return;
   }}//if((gTicks>1000)&&(Step==0))
   //-----
   {if((gTicks>2000)&&(Step==1))
   {
   Print("CLOSE>> *** VOLUME=",PositionGetDouble(POSITION_VOLUME),
                     " *** ID=",PositionGetInteger(POSITION_IDENTIFIER),
                     " *** TYPE=",EnumToString((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)),
                     " *** OrdersTotal()=",OrdersTotal());
      MqlTradeRequest request={0};                                   //Обнулим записи запроса
      MqlTradeResult result={0};                                     //Обнулим записи ответа
      request.action=TRADE_ACTION_DEAL;                              //Отложенный ордер
      request.symbol=_Symbol;                                        //Инструмент
      request.price=SymbolInfoDouble(_Symbol,SYMBOL_ASK);            //Прайс (цена) исполнения ордера
      request.type=ORDER_TYPE_BUY;                                   //тип ордера
      request.type_filling=ORDER_FILLING_FOK;                        //Исполнять только в полном объёме
      request.type_time=ORDER_TIME_DAY;                              //В очереди до снятия
      request.volume=1;                                              //Объем Правильно
      Print("CLOSE OrderSend=",OrderSend(request,result));
      Print("CLOSE Retcode=",result.retcode);
      Print("CLOSE Order=",result.order);
      Print("CLOSE Deal=",result.deal);
      Print("CLOSE OrdersTotal()=",OrdersTotal());
      Print("CLOSE Volume=",PositionGetDouble(POSITION_VOLUME));
      Step=2;
      return;
   }}//if((gTicks>2000)&&(Step==1))        
   //-----   
   {if((gTicks>3000)&&(Step==2))
   {
      Print("INFO>> *** VOLUME=",PositionGetDouble(POSITION_VOLUME),
                        " *** ID=",PositionGetInteger(POSITION_IDENTIFIER),
                        " *** TYPE=",EnumToString((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)),
                        " *** OrdersTotal()=",OrdersTotal());
      Step=3;
      return;
   }}//if((gTicks>3000)&&(Step==2))
   {if((gTicks>4000)&&(Step==3))
   {
      ExpertRemove();
   }}//if((gTicks>4000)&&(Step==3))
}//OnTick()

That is, we open a position with an order, close it with a reverse order, and look at the position volume as a result.

We expect 0 (zero) and have 1 (one). Logs below (beginning below).

2015.10.27 16:28:11.476 2015.10.26 10:05:08   ExpertRemove() function called
2015.10.27 16:28:11.465 2015.10.26 10:03:14   INFO>> *** VOLUME=1.0 *** ID=2 *** TYPE=POSITION_TYPE_SELL *** OrdersTotal()=0
2015.10.27 16:28:11.450 2015.10.26 10:01:47   CLOSE Volume=1.0
2015.10.27 16:28:11.450 2015.10.26 10:01:47   CLOSE OrdersTotal()=0
2015.10.27 16:28:11.450 2015.10.26 10:01:47   CLOSE Deal=3
2015.10.27 16:28:11.450 2015.10.26 10:01:47   CLOSE Order=3
2015.10.27 16:28:11.450 2015.10.26 10:01:47   CLOSE Retcode=10009
2015.10.27 16:28:11.450 2015.10.26 10:01:47   CLOSE OrderSend=true
2015.10.27 16:28:11.449 2015.10.26 10:01:47   order performed buy 1.00 at 9249 [#3  buy 1.00 SBRF-12.15 at 9249]
2015.10.27 16:28:11.449 2015.10.26 10:01:47   deal performed [#3  buy 1.00 SBRF-12.15 at 9249]
2015.10.27 16:28:11.449 2015.10.26 10:01:47   deal #3  buy 1.00 SBRF-12.15 at 9249 done (based on order #3)
2015.10.27 16:28:11.449 2015.10.26 10:01:47   exchange buy 1.00 SBRF-12.15 at 9249 (9242 / 9249 / 9242)
2015.10.27 16:28:11.449 2015.10.26 10:01:47   CLOSE>> *** VOLUME=1.0 *** ID=2 *** TYPE=POSITION_TYPE_SELL *** OrdersTotal()=0
2015.10.27 16:28:11.422 2015.10.26 10:00:57   order performed sell 1.00 at 9205 [#2  sell stop 1.00 SBRF-12.15 at 9205]
2015.10.27 16:28:11.422 2015.10.26 10:00:57   deal performed [#2  sell 1.00 SBRF-12.15 at 9205]
2015.10.27 16:28:11.422 2015.10.26 10:00:57   deal #2  sell 1.00 SBRF-12.15 at 9205 done (based on order #2)
2015.10.27 16:28:11.422 2015.10.26 10:00:57   order [#2  sell stop 1.00 SBRF-12.15 at 9205] triggered
2015.10.27 16:28:11.422 2015.10.26 10:00:57   OPEN Volume=0.0
2015.10.27 16:28:11.422 2015.10.26 10:00:57   OPEN OrdersTotal()=1
2015.10.27 16:28:11.422 2015.10.26 10:00:57   OPEN Deal=0
2015.10.27 16:28:11.422 2015.10.26 10:00:57   OPEN Order=2
2015.10.27 16:28:11.422 2015.10.26 10:00:57   OPEN Retcode=10009
2015.10.27 16:28:11.422 2015.10.26 10:00:57   OPEN OrderSend=true
2015.10.27 16:28:11.422 2015.10.26 10:00:57   sell stop 1.00 SBRF-12.15 at 9205 (9205 / 9227 / 9205)
2015.10.27 16:28:11.422 2015.10.26 10:00:57   OPEN>> *** VOLUME=0.0 *** ID=0 *** TYPE=POSITION_TYPE_BUY *** OrdersTotal()=0
2015.10.27 16:28:11.344 SBRF-12.15,M1: testing of Experts\Projects\CoinAge5\Helper_v01\mq5\Tst\TST006_Open_Close_Positions_001.ex5 from 2015.10.26 00:00 to 2015.10.27 00:00 started

What is the reason?

 
Yury Kirillov:

How to get the position volume to 0 (zero)? (FORTS, Otkrytie-Broker, Real)

We have the following code:

That is, we open a position with an order, close it with a reverse order, and look at the position volume as a result.

We expect 0 (zero) and have 1 (one). Logs below (beginning below).

What is the reason?

Thank you for your attention! Got an explanation in a neighbouring thread. :-)
 
Alexey Viktorov:
Exactly right. When I wrote this formula, my SL was not defined by a predefined value, but was calculated as the difference between the order open price and some level, so I had to multiply the risk amount by _Point
Then you should divide, not multiply.
 

Hello all! I can't cope with one problem... Please help!!! There was an Expert Advisor with Martingale (2SS), I reworked almost everything - now it also opens by Trend. There is a block counting accumulated profit of separately closed orders and reset to "0" - when the whole series was closed, and in particular the 1st open order. Now this 1st order may close at any time... And the accumulated profit is nullified. TASK: Hold this flag (series openings) until ALL orders are closed after this flag has "appeared". In the source code, it looked like this:

  if(OrderSelect(TicketB[totb-1],SELECT_BY_TICKET)) TimeB=OrderOpenTime();
  if(OrderSelect(TicketS[tots-1],SELECT_BY_TICKET)) TimeS=OrderOpenTime();
.......//...........//...........//............//............//........
         if(!OrderSelect(k,SELECT_BY_POS,MODE_HISTORY)) break;
         if((OrderOpenTime()<TimeB || totb==0) && (OrderOpenTime()<TimeS || tots==0)) break;
         if(OrderSymbol()==Symbol())
           {
            if((OrderMagicNumber()==magicbuy || OrderMagicNumber()==magicbuyTrEnd) && OrderType()==OP_BUY  && OrderOpenTime()>TimeB) ProfitBuyN  += OrderProfit()+OrderSwap()+OrderCommission();
            if((OrderMagicNumber()==magicsell || OrderMagicNumber()==magicsellTrEnd) && OrderType()==OP_SELL && OrderOpenTime()>TimeS) ProfitSellN += OrderProfit()+OrderSwap()+OrderCommission();
           }

Thank you in advance!

 
Artyom Trishkin:
Then divide, not multiply.
You did not look at my variant carefully, I did not multiply the stop, although it is indeed the right variant, but I multiplied the money, which after 5-6 years seems unreasonable, but the result is correct. I have not returned to this variant all these years, I have hardly found an Expert Advisor where this is done. By the time I found it, you had already written two posts :)))
 
Alexey Viktorov:
You did not look carefully at my variant, I did not multiplied the stop, although it is indeed the right variant, and multiplied the money, which after 5-6 years seems unreasonable, but the result is correct. I have not returned to this variant all these years, I have hardly found an Expert Advisor where this is done. By the time I found it, you had already written two posts :)))

And from a smartphone ;)

It's strange, of course. If I wrote stop value in pips, it is 300 (in his example). He multiplied it by _Point. As a result, at five-digit quotes the stop value in pips is 300*0.00001=0.003

Ok. If the difference between the necessary close price and the open price is equal to 0.003 (in the price), why did he multiply it and get 0.00000003 points? If he had divided it, he would have gotten 300 as he should.

In fact, I answered from my smartphone, without even realizing that I was answering you and not the original questioner ;)

 
Artyom Trishkin:

And from a smartphone ;)

It's strange, of course. If I wrote stop value in pips, it is 300 (in his example). He multiplied it by _Point. As a result, at five-digit quotes the stop value in pips is 300*0.00001=0.003

Ok. If the difference between the necessary close price and the open price is equal to 0.003 (in the price), why did he multiply it and get 0.00000003 points? If he had divided it, he would have got 300 as it is supposed to be.

In fact, I was answering from my smartphone without even realizing that I was answering you and not the questioner at first ;)

And now I have already had dinner and I do not care what he gets. :)))

The main thing is that we understood each other... :)))))))))))))))))))

 
Alexey Viktorov:

And now I've had my dinner and I don't care what he gets. :)))

The important thing is that you and I understand each other... :)))))))))))))))))))

I'm very glad you understand each other)) I, who am very young, do not know what to divide and multiply. Please help me with the information, I'd be happy to have a link to an article, a simple explanation on my fingers, I'm grateful for any help.
 
Alexey Viktorov:

Now I've had my dinner and I don't care what he gets. :)))

The main thing is that you and I understand each other... :)))))))))))))))))))

I think I've got it, gentlemen)))

double Test=100/(300*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE));

That's how it works, thank you all)

 
PabloEs:

I think I've got it, gentlemen))

double Test=100/(300*SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE));

That's how it works, thank you all)

See, I had dinner and you got it done in 11 minutes. :)))