Useful features from KimIV - page 7

 

Well, since there are no questions, let's continue...

ModifyOrder() function.

Function ModifyOrder is designed to change one or more price levels of one pre-selected order. Here a pending order is understood as a Limit or Stop order, as well as a market Buy or Sell order, i.e. a position. Using the ModifyOrder function you can modify two price levels StopLoss and TakeProfit for the position, while for the pending order the OpenPrice setting price can be changed as well. The modified price levels are passed to the ModifyOrder function as parameters. If any of the parameters is negative, the corresponding price level will not be changed.

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 28.11.2006                                                     |
//|  Описание : Модификация одного предварительно выбранного ордера.           |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    pp - цена установки ордера                                              |
//|    sl - ценовой уровень стопа                                              |
//|    tp - ценовой уровень тейка                                              |
//|    cl - цвет значка модификации                                            |
//+----------------------------------------------------------------------------+
void ModifyOrder(double pp=-1, double sl=0, double tp=0, color cl=CLR_NONE) {
  bool   fm;
  double op, pa, pb, os, ot;
  int    dg=MarketInfo(OrderSymbol(), MODE_DIGITS), er, it;
 
  if (pp<=0) pp=OrderOpenPrice();
  if (sl<0 ) sl=OrderStopLoss();
  if (tp<0 ) tp=OrderTakeProfit();
  
  pp=NormalizeDouble(pp, dg);
  sl=NormalizeDouble(sl, dg);
  tp=NormalizeDouble(tp, dg);
  op=NormalizeDouble(OrderOpenPrice() , dg);
  os=NormalizeDouble(OrderStopLoss()  , dg);
  ot=NormalizeDouble(OrderTakeProfit(), dg);
 
  if (pp!=op || sl!=os || tp!=ot) {
    for (it=1; it<=NumberOfTry; it++) {
      if (!IsTesting() && (!IsExpertEnabled() || IsStopped())) break;
      while (!IsTradeAllowed()) Sleep(5000);
      RefreshRates();
      fm=OrderModify(OrderTicket(), pp, sl, tp, 0, cl);
      if (fm) {
        if (UseSound) PlaySound(NameFileSound); break;
      } else {
        er=GetLastError();
        pa=MarketInfo(OrderSymbol(), MODE_ASK);
        pb=MarketInfo(OrderSymbol(), MODE_BID);
        Print("Error(",er,") modifying order: ",ErrorDescription(er),", try ",it);
        Print("Ask=",pa,"  Bid=",pb,"  sy=",OrderSymbol(),
              "  op="+GetNameOP(OrderType()),"  pp=",pp,"  sl=",sl,"  tp=",tp);
        Sleep(1000*10);
      }
    }
  }
}
 

Examples of how to use the ModifyOrder() function.

I decided to give the very first examples that I've been asked many times before. This is opening of positions in terms of market order execution Market Watch. It is when we cannot simultaneously give an order to open a position at the market price and attach a pending order to it. Such an opening at Market Watch should be performed in two stages: first, we open a position, and then we attach a pending order to it, i.e. we set StopLoss and TakeProfit price levels.

1. Buy 0.1 lot of the current symbol and set a stop of 30 points

int ti=OpenPosition(NULL, OP_BUY, 0.1);
if (OrderSelect(ti, SELECT_BY_TICKET))
  ModifyOrder(-1, Ask-30*Point, -1, clModifyBuy);

2. Sell 0.15 lot of the current instrument and set SL=45, TP=99

int ti=OpenPosition(NULL, OP_SELL, 0.15);
if (OrderSelect(ti, SELECT_BY_TICKET))
  ModifyOrder(-1, Bid+45*Point, Bid-99*Point, clModifySell);
A working script with examples is included in the trailer.
Files:
 
Igor, do you have a close order function (both just Close and CloseBy)?
 
Lukyanov:
Igor, do you have a function to close orders (both just Close and CloseBy)?
Close is there, CloseBy is not.
 
Close in alphabetical order should be before ModifyOrder ;-)
 
Lukyanov:
Close in alphabetical order should be before ModifyOrder ;-)
Yes, that's right... Open first, then Modify, and finally Close ;-)
 

Makes sense... ;)

Would you consider it cheeky to do a little, one-post, libation on the subject of "Transmission to function" ?
So far it's a dark logical-ideological forest for me...
(no, it isn't...).

 
kombat писал (а):
Would you consider it cheeky to have a little one-post education on the subject of "Transmission to function"?
No, I wouldn't. As far as my functions are concerned, any questions are acceptable at any time. I'll try to answer all of them...
 

Thank you...
And of course there's no rush... there's no rush.
The market won't run away from us... :)))

 
kombat писал (а):
Thank you...
And of course there's no rush... there's no rush.
The market won't run away from us... :)))
I was actually expecting questions from you. Specific questions, directly indicating what you need to clarify about the use of my functions.