Useful features from KimIV - page 20

 

GetTypeLastClosePos() function.

This function returns the type of the last closed position or -1. A more accurate selection of positions to be taken into account is defined by external parameters:

  • sy - Name of market instrument. If you specify this parameter, the function will consider only positions of the specified instrument. The default value "" means any market instrument. NULL means the current instrument.
  • mn - position identifier, MagicNumber. Default value -1 means any identifier.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает тип последней закрытой позиции или -1               |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
int GetTypeLastClosePos(string sy="", int mn=-1) {
  datetime t;
  int      i, k=OrdersHistoryTotal(), r=-1;

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if ((OrderSymbol()==sy || sy=="") && (mn<0 || OrderMagicNumber()==mn)) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (t<OrderCloseTime()) {
            t=OrderCloseTime();
            r=OrderType();
          }
        }
      }
    }
  }
  return(r);
}
ZS. Attached is a script to test GetTypeLastClosePos() function.
 

GetTypeLastOpenPos() function.

This function returns the type of the last open position or -1. A more accurate selection of positions to be taken into account is specified by external parameters:

  • sy - Name of market instrument. If you set this parameter, the function will consider only positions of the specified instrument. The default value "" means any market instrument. NULL means the current instrument.
  • mn - position identifier, MagicNumber. Default value -1 means any identifier.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает тип последней открытой позиции или -1               |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
int GetTypeLastOpenPos(string sy="", int mn=-1) {
  datetime t;
  int      i, k=OrdersTotal(), r=-1;

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if ((OrderSymbol()==sy || sy=="") && (mn<0 || OrderMagicNumber()==mn)) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (t<OrderOpenTime()) {
            t=OrderOpenTime();
            r=OrderType();
          }
        }
      }
    }
  }
  return(r);
}
ZS. Attached is a script to test GetTypeLastOpenPos() function.
 
KimIV:

OpenPosition() function for online.

      if (err==128 || err==142 || err==143) {
        Sleep(1000*66.666);
        if (ExistPositions(sy, op, mn, ot)) {
          if (UseSound) PlaySound(NameFileSound); break;
        }
      

What do error codes 142 and 143 mean.

Thank you.

 
zennon:

What do error codes 142 and 143 mean?

142 - The order has been queued up.

143 - The order has been accepted by the dealer for execution.

For more information , see here.

 

The doc advises to treat them as error 128

if possible, a word or two about this from your point of view or position...

 
kombat:

The doc advises to treat them as error 128

If possible, a word or two about this from your point of view or position...

My point of view is expressed in the code given by Andrei two posts above.

 

Sorry... saw it, of course...

It's a little unclear to me.

Sleep(1000*66.666);

Other than that, I don't know... sleep...

But if and if further, not so much.

i.e. sleep, then if there is a position, if we play a march.

-

OK, if it's a lamer question, sorry again, and can be unanswered.

 
kombat писал (а):
OK, if it's a lamer question, I'm sorry again, and I'll leave it at that.

nah... let's sort it out... ...a showdown deepens understanding... I just don't understand what you don't understand. Put the question in simpler and more conventional terms. Are you confused by the hibernation interval? Why exactly 66 seconds?

 
KimIV:
Why exactly 66 seconds?

No, not that! There's no question about that... absolutely!

But the further logical-sequential action in this fragment is not so good.

Let's say we get an error code, we sleep, then we get a boolt, Spoin's Waltz plays if this feature is on...

-

(damn, message tails are disappearing, I have to repeat myself :(((

-

Much more interested in how to continue what has been started!

I.e. further trading gestures: either the function will chug

Either the function will chug until it opens, or it will give control to another one...

In the first case it is not clear how to "loop", to call "itself" or what?

 

Hmm... I still don't get it. I'll shoot at random. All of the following will apply to errors 128, 142 and 143.

If error 128 is not handled in any way, you risk getting two identical open positions instead of one. In two years of trading on two real accounts I have had only one such case. Duplicate positions occur because the trading server, for some reason, returned a message to the terminal about the expiration of the order execution time. The terminal was not denied the service. The trade may still be executed. Its execution has just been delayed for an "unacceptably" long time. If you send another request after this message, both the delayed request and the new one may be executed. To avoid this situation, the developers advise you to pause for at least one minute and check if the request the trading server has sent the delayed message about has been executed. If YES, we have reached our goal - the position is opened, if NO, we can repeat the request. This is how my code handles errors 128, 142 and 143.