Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1096

 
EgorKim:

I have the time and price of points A and B

How do I know the time and price of point C? It's in the middle between A and B.

I can't find an example anywhere


//+------------------------------------------------------------------+
//| Уравнение прямой                                                 |
//+------------------------------------------------------------------+
double EquationDirect(const int left_bar,const double left_price,const int right_bar,const double right_price,const int bar_to_search) 
  {
   return(right_bar==left_bar ? left_price : (right_price-left_price)/(right_bar-left_bar)*(bar_to_search-left_bar)+left_price);
  }
//+------------------------------------------------------------------+
 
EgorKim:

I have the time and price of points A and B

How do I know the time and price of point C? It's in the middle between A and B.

I can't find an example anywhere.


Time C = Time B - ((Time B - Time A) / 2)

Price at Time C

 
Сергей Таболин:

Time C = Time B - ((Time B - Time A) / 2)

Price by Time C

Checked it out, sometimes I get a weekend)

Is there any way to find out the centre in the line itself?

Here is my line

void SetLow(double price_1,double price_2,datetime time_1,datetime time_2)
  {
   ObjectCreate(0,"Low",OBJ_TREND,0,time_2,price_2,time_1,price_1);
   ObjectSetInteger(0,"Low",OBJPROP_COLOR,clrAqua);
   ObjectSetInteger(0,"Low",OBJPROP_WIDTH,2);
   ObjectSetInteger(0,"Low",OBJPROP_STYLE,STYLE_DOT);
   ObjectSetInteger(0,"Low",OBJPROP_BACK,false);
   ObjectSetInteger(0,"Low",OBJPROP_RAY_RIGHT,true);
  }
 
Igor Makanu:

how do you find out the ticket of the last order opened? it doesn't work that way:

What exactly do you want to know? We need to clearly distinguish between the terms ORDER, ORDER and POSITION.

Second: You have immediately caused confusion by naming the objects of the CTrade class "order_1" " "order_2". They used to call CTrade class objects "m_trade_1" and "m_trade_2". It doesn't affect the usability, but it adds a lot of confusion.

Third: thePositionClose method won't work only because you pass to it a position ticket equal to zero (and you initialize them with zero in OnInit() ) - Such ideal conditions, when the first position has a ticket "0", can only be obtained in the tester.

Fourth: TheBuy method

Note

Successful completion of the Buy(...) method does not always mean successful trade operation. One should check the result of the trade request (return code of the trade server) by callingResultRetcode() method as well asthe value returned byResultDeal() method.


ResultDeal() method

Gets the READ ticket.

 
Vladimir Karputov:

What exactly do you want to know? You need to clearly distinguish between the terms ORDER, ORDER and POSITION.

Second, you have immediately caused confusion by naming objects of the CTrade class with the words "order_1" and "order_2". They used to call CTrade class objects "m_trade_1" and "m_trade_2". It doesn't affect the usability, but it adds a lot of confusion.

Third: thePositionClose method won't work only because you pass to it a position ticket equal to zero (and you initialize them with zero in OnInit() ) - Such ideal conditions, when the first position has a ticket "0", can only be obtained in the tester.

Fourth: TheBuy method

Note

Successful completion of the Buy(...) method does not always mean successful trade operation. One should check the result of the trade request execution ( return code of the trade server) by calling ResultRetcode() method as well asthe value returned byResultDeal() method.


ResultDeal() method

Gets the RECEIVED ticket.

Thank you! Figured it out already

in general, the problem was from the abundance of information, gossip and opportunities.... was sure that I wouldn't be able to catch a ticket in Result(), because the OnTradeTransaction() event is remembered every once in a while ... and it's on!

i am currently working on it with MT5 tester, i am just looking for reproducible examples, i am not talking about the functional code - just a tester for now

thanks again for your help!

this code can get the tickets of the last orders in the tester:

//+------------------------------------------------------------------+
//|                                                   tst_CTrade.mq5 |
//|                                                            IgorM |
//|                              https://www.mql5.com/ru/users/igorm |
//+------------------------------------------------------------------+
#property copyright "IgorM"
#property link      "https://www.mql5.com/ru/users/igorm"
#property version   "1.00"

input double Lot_1 = 0.10;
input double Lot_2 = 0.20;
input ulong  Magic1 = 12345;
input ulong  Magic2 = 67890;
#include<Trade\Trade.mqh>
 CTrade order_1,order_2;
ulong   ticket_1,ticket_2;
//_______________________________________________________________________ 
class CNewbar
  {
private:
   datetime          mnewtime;
   ENUM_TIMEFRAMES   mperiod;
public:
                     CNewbar()                        { mperiod=PERIOD_CURRENT;  mnewtime=TimeCurrent(); }
                     CNewbar(ENUM_TIMEFRAMES period)  { mperiod=period;          mnewtime=TimeCurrent(); }
   bool              NewBar(){ datetime t=iTime(NULL,mperiod,0); if(mnewtime<t){ mnewtime=t; return(true); } return(false);  }
  };
//_______________________________________________________________________ 

CNewbar Bar_H1(PERIOD_H1);
CNewbar Bar_M30(PERIOD_M30);
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   order_1.SetExpertMagicNumber(Magic1);
   order_2.SetExpertMagicNumber(Magic2);
   ticket_1 = 0;
   ticket_2 = 0;
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(Bar_H1.NewBar())
     {
      order_1.PositionClose(ticket_1);
      if(order_1.Buy(Lot_1))
        {
         ticket_1=order_1.ResultOrder();
         Print("NewBar H1, ticket_1 = ",ticket_1);
        }
     }

   if(Bar_M30.NewBar())
     {
      order_2.PositionClose(ticket_2);
      if(order_2.Sell(Lot_2))
        {
         ticket_2=order_2.ResultOrder();
         Print("NewBar M30, ticket_2 = ",ticket_2);
        }
     }
  }
//+------------------------------------------------------------------+
 
Igor Makanu:

Thank you! Already figured it out.

in general the problem was from the abundance of information, gossip and possibilities.... was sure that I couldn't catch the ticket in Result(), because the OnTradeTransaction() event is remembered every once in a while ... and it's on!

i am currently working on it with MT5 tester, i am just looking for reproducible examples, i am not talking about the functional code - just a tester for now

thanks again for your help!

Such code can get the ticks of the last orders in the tester:

Yes using ResultOrder will get the order ticket.

 
Vladimir Karputov:

Yes, with ResultOrder we can get a ticket for the order.

Ok, now I don't have any problems with it, but another problem has appeared when using CTrade: I have a memorized order ticket and I can close it using PositionClose(m_ticket)

In MQL4, it was very easy to find out if the order is closed or not. I selected it using OrderSelect() and checked if(OrderCloseTime()>0) - if the close time is not equal to 0, the order is closed

How do I know if an order is closed using SB CTrade ?

 
Igor Makanu:

Ok, now I don't have any problems with it, but another problem has appeared when using CTrade: I have a memorized order ticket and I can close it using PositionClose(m_ticket)

In MQL4, it was very easy to find out if the order is closed or not. I selected it using OrderSelect() and checked if(OrderCloseTime()>0) - if the close time is not equal to 0, the order is closed

how do I know that the order is closed using SB CTrade ?

Forget about the order! There is a position!

I specifically asked what do you want: an ORDER, a TAKE OR a POSITION? Do not confuse the terminology. After the trade order is executed, the POSITION appears, not the ORDER!


Therefore, first describe in words what you are doing and what you want to obtain. Instead of an ORDER, use "BUY trade order" and "SELL trade order". This results in a "BUY" or "SELL" position.

 
Igor Makanu:

OK, now I don't have any problems with it, but another problem has appeared when using CTrade: I have a memorized order ticket and I can close it using PositionClose(m_ticket)

In MQL4, it was quite simple: I selected the order via OrderSelect() and checked if(OrderCloseTime()>0) - if the close time is not equal to 0, the order is closed

How do I know if an order is closed using SB CTrade ?

Leaving aside the fact that a position ticket usually corresponds to an order ticket, but not absolutely always, it is possible to use the PositionSelectByTicket function without SB

Документация по MQL5: Торговые функции / PositionSelectByTicket
Документация по MQL5: Торговые функции / PositionSelectByTicket
  • www.mql5.com
Выбирает открытую позицию для дальнейшей работы с ней по указанному тикету. Возвращает true при успешном завершении функции. Возвращает false при неудачном завершении функции. Чтобы получить информацию об ошибке, необходимо вызвать функцию GetLastError(). Функция PositionSelectByTicket() копирует данные о позиции в программное окружение, и...
 
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

Can I substitute my own calculation formula in this indicator calculation code?

For example open/2

Reason: