Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1097

 
Vladimir Karputov:

After execution of a trade order, a POSITION appears, not an ORDER!

There are servers where you can reproduce such a situation

PositionGetTicket(0) = 73401069 
PositionSelect(_Symbol) = true 
OrderGetTicket(0) = 73401069 
PositionsTotal() = 1
OrdersTotal() = 1, ORDER_STATE_STARTED


I.e. we have the following order in the terminal at the same time

#73401069 2019.06.08 21:42:42 buy 1.00 BO Volatility 100 Index 0.5150 0.0000 0.0000 0.5150 0.00 0.00 0.00 Hello! 0


and the same position from it

#73401069 2019.06.08 21:42:42 buy 1.00 BO Volatility 100 Index 0.5150 0.0000 0.0000 0.5150 0.00 0.00 0.00 Hello! 0
 
Vladimir Karputov:

So first describe in words what you are doing and what you want to get. Use "BUY trade order" and "SELL trade order" instead of ORDER. The result is a "BUY position" or a "SELL position".

OK, let's simplify our discussion to examples in code. Here is the code written in MQL4 - I'm interested in tester version, i.e. minimum checks, etc:

//+------------------------------------------------------------------+
//|                                                        tst__.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int ticket1=-1;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   ticket1=-1;
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(ticket1<0) ticket1=OrderSend(_Symbol,OP_BUY,0.1,Ask,30,NormalizeDouble(Ask-100*_Point,_Digits),NormalizeDouble(Ask+100*_Point,_Digits));

   if(OrderSelect(ticket1,SELECT_BY_TICKET))
     {
      if(OrderCloseTime()>0)
        {
         int cmd=OrderType()==OP_BUY ? OP_SELL : OP_BUY;
         double tp = cmd==OP_BUY ? NormalizeDouble(Ask+100*_Point,_Digits) : NormalizeDouble(Bid-100*_Point,_Digits);
         double sl = cmd==OP_BUY ? NormalizeDouble(Ask-100*_Point,_Digits) : NormalizeDouble(Bid+100*_Point,_Digits);
         ticket1=OrderSend(_Symbol,cmd,0.1,Ask,30,sl,tp);
        }
     }
  }
//+------------------------------------------------------------------+

The essence of TS: when you open a Buy order at first start and remember the ticket of the order (work on the ticket), then check if the order is closed - see what type of order was and put the opposite order and remember the ticket and so on round

I need a similar example to be reproduced in MQL5 using SB CTrade

 
Igor Makanu:

OK, let's simplify our discussion to examples in the code, here is the code written in MQL4 - I am interested in the version for tester, i.e. minimum checks, etc:

The essence of TS: when you open a Buy order at first start and remember the ticket of the order (work on the ticket), then check whether the order is closed - see what type of order and put the opposite order and remember the ticket and so on round

I need a similar example to be reproduced in MQL5 using SB CTrade

Here's where the little drummer will say what he thinks of you))))
 
Alexey Viktorov:
Now the little drummer will tell you what he thinks of you))))

let it speak, but we need to know if the ticket number is open or closed, if it is closed, we need to know what typeof trade order the closed position had

(in bold - it is an order anyway! - because in the TC header account)

 
Igor Makanu:

OK, let's simplify our discussion to examples in code. I wrote a code in MQL4 - I'm interested in Tester version, i.e. minimum of checks, etc:

The second OrderSend contains an error. And normalization is unnecessary. In the forum (and not only) code is easier to read when it is shorter.
void OnTick()
  {
   static int ticket1 = -1;
   
   if(ticket1<0) ticket1=OrderSend(_Symbol,OP_BUY,0.1,Ask,30,Ask-100*_Point,Ask+100*_Point);

   if(OrderSelect(ticket1,SELECT_BY_TICKET) && OrderCloseTime())
    {
     int cmd=1-OrderType();
     double open = cmd ? Bid : Ask;
     double tp = open - (cmd ? 1 : -1) * 100 * _Point;
     double sl = open + (cmd ? 1 : -1) * 100 * _Point;
     ticket1=OrderSend(_Symbol,cmd,0.1,open,30,sl,tp);
    }
  }

I need a similar example to be reproduced in MQL5 using CTrade SB

I need a similar example to be reproduced in MQL5 using CTrade SB.

 

So, the basics are thePOSITION_IDENTIFIER, but not the position ticket. This is important precisely because of the netting:

POSITION_IDENTIFIER

The position identifier is a unique number, which is assigned to each newly opened position and does not change throughout its lifetime. It corresponds to the ticket of the order with which the position was opened.

The position identifier is specified in each order (ORDER_POSITION_ID) and each trade (DEAL_POSITION_ID) which opened, changed or closed it. Use this property to search for orders and trades related to the position.

When a position is reversed in netting mode (a single in/out trade), the POSITION_IDENTIFIER identifier of the position is not changed. However, POSITION_TICKET is changed to the order ticket which resulted in the reversal. In the hedging mode there is no position reversal.

long


So, we have to keep track of the position identifier (POSITION_IDENTIFIER).


Now, a more accurate task: at first run, we open a BUY POSITION and memorize it (WARNING: we should memorize the position ID, not the ticket). If the position has been closed, we open an opposite position: e.g. once upon a time there was a BUY position, then it was closed, which means that we open a SELL position right away.


Now it will be much easier to solve the problem.

 
Vladimir Karputov:

Now it will be much easier to solve the problem.

When writing the example, look a little ahead.

 
fxsaber:
The second OrderSend contains an error. I don't need normalization. On the forum (and not only) it's easier to read the code when it's shorter.

Yes, I see, I wrote the code on the fly - ran it in the tester - it works, copied it to the forum


fxsaber:

You'll have to try a bit harder.

That's why I asked for help on the forum! - I decided to estimate capabilities of SB CTrade - I took an elementary task as an example and so far the result has been negative!


Vladimir Karputov:

Now it will become much easier to solve the problem.

I know you as an active member of the forum. Could you reproduce my, or rather the correctedfxsaber code, using CTrade Server under MQL5?

 
Igor Makanu:

Yes, I see, I wrote the code on the fly - ran it in the tester - it works, copied it to the forum

When I start writing an EA, I always start with a blank page (I recommend it). Probably most often start with a header with On-functions.

 
Igor Makanu:

That's why I asked for help on the forum! - I decided to assess CTrade's capabilities - I took an elementary task as an example and so far the result has been negative!

There is an even shorter example.

forum on trading, automated trading systems and trading strategies testing

Features of mql5 language, intricacies and tricks

fxsaber, 2018.02.15 11:48

OrderCloseBy(OrderSend(_Symbol, OP_BUY, 1, Ask, 0, 0, 0), OrderSend(_Symbol, OP_SELL, 1, Bid, 0, 0, 0));

It's hard to deal with in MT5 if not in the tester.