Useful features from KimIV - page 45

 

An example of using the GetPriceDiffInPoint() function.

Suppose we need to calculate the value in pips and the direction of rate movement between the second and first bars on the current chart. Suppose that the current chart is EURUSD H1. Call the GetPriceDiffInPoint() function with default parameters:

Message(GetPriceDiffInPoint());

Here is the result:


The GetPriceDiffInPoint() function has returned -23 points. Let's analyze how it was obtained. For this purpose let's draw vertical lines through the hour bars 2, 1 and 0:


Let's switch to a smaller time frame, for example, M5 and mark the interval of bars 2 and 1 with a single rectangle on it:

Now we can clearly see that the first in the highlighted interval were two equal highs of 1.4283, and only after them, in the middle of the second hour was the low of 1.4260. Hence we conclude that during the last two hours EURUSD decreased and the value of this decrease was (1.4283 - 1.4260) / 0.0001 = 23 pips.


P.S. Attached is the script for testing the GetPriceDiffInPoint() function.

 

Awesome selection, isn't there a function to close an order (buy or sell) on the first successful close of a bar on any TF...

 

Can I ask what checks are made before orders are opened?

If they are collected in a separate function, please give me a link. If not, a list can be given.

 
SK. писал (а) >>
Can I ask what checks are made before orders are opened?

Sergei, I can't think whether your question is for me or not?

 
KimIV писал (а) >>

...

The functions, opening, closing and modifying when handling errors use the Sleep command. I've done some research and recommend replacing it with one of these. The point is that the value of delay is a recommendation, and does not have to be strictly that value. Quite often, you can perform actions with an order right away (without a delay). But if you have a requote, then I do not like the fact that BC can calculate when and what type of operation I will perform.

int start()
  {
//----
   Sleep_RND(20000);  
//----
   return(0);
  }
//+------------------------------------------------------------------+
void Sleep_RND(int _Pausa)
  {
   MathSrand(TimeLocal());
   MathRand();
   Sleep(MathRound(_Pausa*MathRand()/32767+_Pausa/5));
   return;
  }
 
KimIV писал (а) >>

Sergei, I don't know whether your question is for me or not.

Yes, Igor, it is a question for you.

The point is this. I looked at the opening function, there are no checks there. Apparently they are concentrated in some other function. If you have such a function, please give me the link. If you don't see such a function, you may write a list of checks right here. You may do it slowly.

 

No, Sergei, I don't do any more checks. Or rather, I do, but I don't put them in separate functions. That is, I put some of them... Geez, I'll give you an example:

extern int StopLoss=30;
extern int TakeProfit=50;

double ll=GetSizeLot();
double po, pp, sl, tp;
if (ExistPositions(NULL, OP_BUY, Magic)) {
  po=PriceOpenLastPos(NULL, OP_BUY, Magic);
  if (!ExistOrders   (NULL, OP_SELLSTOP, Magic+1)
  &&  !ExistPositions(NULL, OP_SELL    , Magic+1)) {
    pp=po-offSet*Point;
    if (StopLoss>0) sl=pp+StopLoss*Point; else sl=0;
    if (TakeProfit>0) tp=pp-TakeProfit*Point; else tp=0;
    SetOrder(NULL, OP_SELLSTOP, ll, pp, sl, tp, Magic+3);
  }
}

What do we see here?

1. We calculate the lot to be traded.

2. If there is a Buy position with a specified magic number, its open price is defined po.

If there is no SellStop order and Sell position with a Magik by one unit larger than the specified one, we set the SellStop order at the price by offSet points lower than the opening price of Buy position.

4. In the interval we carry out calculation of price levels sl and tp.

 
KimIV писал (а) >>

4. In between, calculate price levels sl and tp.

That's what I mean. We have to take into account the existing constraints, which, generally speaking, change all the time.

Is there some function that analyzes all possible constraints?

Same for modification.

--

(I have 16 functions - 3 for each good order (OP,SL,TP) and 2 for each market order (SL,TP),

and then some more analyzing combinations of permissions).

 
SK. писал (а) >>
Is there any function that analyses all sorts of restrictions?
Not yet... somehow I didn't have to. But thanks to you, I've thought about it and felt I should! >> Thank you!
 

GetTypePrice() function

Returns the name of the price type. The function accepts only one optional parameter. Valid values: PRICE_CLOSE, PRICE_OPEN, PRICE_HIGH, PRICE_LOW, PRICE_MEDIAN, PRICE_TYPICAL, PRICE_WEIGHTED. The default value is 0 - PRICE_CLOSE.

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 01.09.2005                                                     |
//|  Описание : Возвращает наименование типа цены.                             |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    Applied_Price - тип цены                                                |
//+----------------------------------------------------------------------------+
string GetTypePrice(int Applied_Price=0) {
  switch (Applied_Price) {
    case PRICE_CLOSE   : return("Close");
    case PRICE_OPEN    : return("Open");
    case PRICE_HIGH    : return("High");
    case PRICE_LOW     : return("Low");
    case PRICE_MEDIAN  : return("Median");
    case PRICE_TYPICAL : return("Typical");
    case PRICE_WEIGHTED: return("Weighted");
    default            : return("Unknown Type Price");
  }
}
This function is useful for commenting or logging the work of indicators, scripts and Expert Advisors, in particular, for displaying the explanations of price constants' values.