Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 66

 
Roger:

What you called Trailing is in fact not, it is calculated in a different way and its behaviour can be illogical.


Even though our concepts of the terms do not coincide, but you do understand me, don't you?

Solved the problem, it was here:

double getLots(double newSL) {
   int opnTime = 0; // время открытия трейда для цикла пересчета позиций
   double lotSum = 0; 
   for (int i = 0; i <= OrdersTotal()-1; i++) {
      OrderSelect(i, SELECT_BY_POS);     
      if ((OrderOpenTime() > opnTime) && (OrderType() == OP_BUY) || (OrderType() == OP_SELL)) { 
         opnTime = OrderOpenTime(); 
         if (OrderType() == OP_BUY)    { lotSum += OrderLots() * (newSL - OrderOpenPrice()) / Point; }
         if (OrderType() == OP_SELL)   { lotSum -= OrderLots() * (newSL - OrderOpenPrice()) / Point; }
      }
   }   
   return(lotSum);
}

This function did not take into account all open trades, so I had to change the loop conditions. Now it looks like this:

double AcountProfitEx(double Price) {
   double   ProfitSum   = 0;
   for (int i = OrdersTotal()-1; i >= 0; i--) {
      OrderSelect(i, SELECT_BY_POS);
      if(OrderType() == OP_BUY)
      ProfitSum -= (OrderOpenPrice() - Price) * OrderLots() / Point;
      if(OrderType() == OP_SELL)
      ProfitSum += (OrderOpenPrice() - Price) * OrderLots() / Point;
   }
   return (ProfitSum);
}

This function calculates the sum of profits of all open trades at the price level specified to it. However, there is a quoted opinion that it does not take into account the spread, and I agree with it. The author of this opinion suggested this solution:

double getLots(double newSL)
{
   double TickValue, delta;
   double lotSum;
   string SymbolName;
   
   SymbolName = Symbol();
   TickValue = MarketInfo( SymbolName, MODE_TICKVALUE) / Point;
   delta = ( newSL - Bid ) * TickValue;

   lotSum = 0.0; 
   for (int i = 0; i <= OrdersTotal()-1; i++)
   {
      OrderSelect(i, SELECT_BY_POS);     
      if ( OrderSymbol() == SymbolName )
      { 
         if (OrderType() == OP_BUY)    { lotSum += OrderProfit() + OrderLots() * delta; }
         if (OrderType() == OP_SELL)   { lotSum += OrderProfit() - OrderLots() * delta; }
      }
   }   
   return(lotSum);
}

But I confess to be honest, I don't understand him as he writes:

The spread is not accounted for. This can be corrected by counting the result from the current price.
I don't understand the algorithm he suggested, how is the spread taken into account? Can someone please explain?
 
Mepkypuu:

But, to be honest, I don't understand him as he writes:

I do not seem to understand his algorithm; how the spread is considered there? Can someone explain?

OrderProfit() counts for short positions at the current Ask, this is where the value of the current spread is taken into account. If the spread does not change, when the price changes from the current Bid to newSL the one-lot profit of the position (either Buy or Sell, with an appropriate sign) will change by the value of delta, which is written in the operator:

delta = ( newSL - Bid ) * TickValue;

In brief, OrderProfit() counts everything including spreads. We fix the result for a certain moment ( the current price) using OrderProfit(). From here on, all we have to do is to monitor price changes.

 
Mislaid:

OrderProfit() is counted for short positions at the current Ask, this is where the current spread values are taken into account. If the spread does not change, when the price changes from the current Bid to newSL the one-lot profit of the position (either Buy or Sell, with the corresponding sign) will change by the value of delta, which is written in the statement:

delta = ( newSL - Bid ) * TickValue;

In brief, OrderProfit() counts everything including spreads. We fix the result for a certain moment ( the current price) using OrderProfit(). From here on, all we have to do is to monitor the price change.

I think I am beginning to understand, but perhaps it is more correct:

if (OrderType() == OP_BUY)    { lotSum += OrderProfit() + OrderLots() * ((newSL - Bid) / Point * MarketInfo( SymbolName, MODE_TICKVALUE)); }
if (OrderType() == OP_SELL)   { lotSum += OrderProfit() - OrderLots() * ((newSL - Ask) / Point * MarketInfo( SymbolName, MODE_TICKVALUE)); }

OrderProfit for short positions is calculated from Ask price, isn't it?

By the way, MarketInfo( SymbolName, MODE_TICKVALUE) returns 329.02 for EURUSD currency pair, that's why this function does not work properly for me.

 

So far I've decided to go for a trick, i.e. MarketInfo(SymbolName, MODE_TICKVALUE) to count differently:

double GetTickValue(string CurrentQuote) {
   string AccountCurr = AccountCurrency();
   string BaseCurr = StringSubstr(CurrentQuote,0,3);
   string CurrentCurr = StringSubstr(CurrentQuote,3,3);
   
   if (CurrentCurr == AccountCurr)  
      return (MarketInfo(CurrentQuote, MODE_LOTSIZE) * MarketInfo(CurrentQuote, MODE_TICKSIZE));
   if (BaseCurr == AccountCurr)
      return (MarketInfo(CurrentQuote, MODE_LOTSIZE) * MarketInfo(CurrentQuote, MODE_TICKSIZE) / MarketInfo(CurrentQuote, MODE_BID));
   if ((CurrentCurr != AccountCurr) && (BaseCurr != AccountCurr))  
      return (MarketInfo(CurrentQuote, MODE_LOTSIZE) * MarketInfo(CurrentQuote, MODE_TICKSIZE) * MarketInfo(StringConcatenate(BaseCurr,AccountCurr), MODE_BID) / MarketInfo(CurrentQuote, MODE_BID));
}
 
Mepkypuu:

So far I've decided to go for a trick, i.e. MarketInfo(SymbolName, MODE_TICKVALUE) to count differently:

The way it was written is correct. By how much the Bid is shifted, by how much the Ask is shifted, with the spread unchanged.
 
Mislaid:
The way it was written is correct. How much the Bid is shifted, the Ask is shifted by the same amount, if the spread is unchanged.

From personal experience, the spread is usually unchanged, and it is quite strong.) During sharp moves I have seen the spread increase from 8 to 80 points on a five-digit spread.
 
Is it possible to code(reliably) a double top?
 
001:
Can a double top be coded(reliably)?
It is possible.
 

When testing an Expert Advisor in the journal an error pops up

2013.08.07 12:35:41 2012.06.06 05:29 Puria - 1.452 - SQ EURUSD,M30: Error 4002 (array index - out of range)

2013.08.07 12:35:41 2012.06.06 05:29 Puria - 1.452 - SQ EURUSD,M30: Attempting to open a Buy order. Waiting for an answer.

Accordingly, the orders are not opened. How to eliminate the error? What are the reasons?

I would be glad to help.

 
alexey1979621:

When testing an EA, an error pops up in the log

2013.08.07 12:35:41 2012.06.06 05:29 Puria - 1.452 - SQ EURUSD,M30: Error 4002 (array index - out of range)

2013.08.07 12:35:41 2012.06.06 05:29 Puria - 1.452 - SQ EURUSD,M30: Attempting to open a Buy order. Waiting for an answer.

Accordingly, the orders are not opened. How to eliminate the error? What are the reasons?

I would be glad to help.

Not much will be clear from the terminal messages alone, not much will help you if you don't post the EA code. Somewhere in your array you are writing a non-existent batch of data, as one of the assumptions, but guessing is not my profile.
Reason: