[ARCHIVE] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 3. - page 546

 

The EA has the following block (Event Counter):

if (isCloseLastPosByStop()==True) //If the last order was closed by Stop

{
N=N+1;
Alert(N, " moose");
} else N=0;

QUESTION: how to write the data into a file (Excel) instead of displaying it on the screen?

 
Dimka-novitsek:

Good afternoon, could you tell me how it can be, I select an order in one loop, I go through which order is last and remember it so ticket = OrderTicket();

In this case let's say it's number two. A bit later if (ticket>-1){
OrderSelect(ticket,SELECT_BY_TICKET,MODE_HISTORY);

and i often get error 4105 - order not selected!!!


And that's because the order hasn't been closed yet. It is not in MODE_HISTORY, it is still in MODE_TRADES.
 

for(  i=0;i<=OrdersHistoryTotal();i++){
    OrderSelect(i ,SELECT_BY_POS,MODE_HISTORY);
     if (OrderMagicNumber( )== magic) { if(OrderSymbol()== Symbol()) {





 current = OrderOpenTime();  Alert ( " current = OrderOpenTime(); ",  current);Alert ( " max ", max );
      
      if (current > max) 
      {                    
         max = current;      
         ticket = OrderTicket();   Alert ( " ticket = OrderTicket();  ", ticket );
      }}}}


       if (ticket>-1){
      OrderSelect( ticket,SELECT_BY_TICKET,MODE_HISTORY);Alert ("SELL Select error HISTORYticket ", GetLastError( )  ) ;

No way! Why not? There were no open orders at the time of the overshoot! Maybe I don't understand.

The search cycle is also on history! That's where I assign it to the int ticket variable!

And it swears, excuse me, 2012.02.04 11:28:47 2011.12.06 16:35 GMT EURUSD,M30: Alert: SELL Select error HISTORYticket 4105

When it gets from MODE_HISTORY to MODE_TRADES?

And most importantly, I select the same order on the history with the help of order select, and it is selected first, otherwise I wouldn't be able to work with it! There's no logic !!!!!! I'm shocked...




 
Dimka-novitsek:

No way! Why not? There were no open orders at the time of the overshoot! Maybe I don't understand.

The search cycle is also on history! That's where I assign it to the int ticket variable!

And it swears, excuse me, 2012.02.04 11:28:47 2011.12.06 16:35 GMT EURUSD,M30: Alert: SELL Select error HISTORYticket 4105

When it goes from MODE_HISTORY to MODE_TRADES ?


ticket=-1;
for(  i=0;i<OrdersHistoryTotal();i++){
    if (OrderSelect(i ,SELECT_BY_POS,MODE_HISTORY));{
     if (OrderMagicNumber( )== magic) { if(OrderSymbol()== Symbol()) {





 current = OrderOpenTime();  Alert ( " current = OrderOpenTime(); ",  current);Alert ( " max ", max );
      
      if (current > max) 
      {                    
         max = current;      
         ticket = OrderTicket();   Alert ( " ticket = OrderTicket();  ", ticket );
      }}}}}


       if (ticket>-1){
      OrderSelect( ticket,SELECT_BY_TICKETJ ,MODE_HISTORY);Alert ("SELL Select error HISTORYticket ", GetLastError( )  ) ;
Added a line at the beginning and corrected the next two lines of code.
 
Thank you!!!
 

help insert a stop loss into an EA

please

Files:
my_1.mq4  10 kb
 
Please share the function for calculating the maximum allowable lot, taking into account the open positions, i.e. equity. The standard function calculates the risk, but if there are trades, there is not enough money to open them
 
T-G:
Please share the function for calculating the maximum allowable lot, taking into account the open positions, i.e. equity. The standard function calculates the risk, but if there are trades, there is not enough money to open them


In the simplest case it is like this:

double Klots = 0.1; //коэфф. риска (определяет желаемый размер лота от максимально возможного в данный момент.)

Lots=NormalizeDouble(Klots*AccountFreeMargin()/MarketInfo(Symbol(),MODE_MARGINREQUIRED),2);Lots=MathMax(MarketInfo(Symbol(),MODE_MINLOT),Lots);
 

Guys, can you advise on this question?

Here's a script to find the maximum size of the zig-zag backlash from crenfx - see here:

#property show_inputs

extern int MinPips = 100;
extern datetime StartTime = D'2011.03.24';
extern datetime EndTime = D'2011.12.31';

#define MAX_POINTS 10000

// Заполняет массив размерами колен ЗигЗага с условием колена >= MinPips пунктов
int GetZigZagData( int MinPips, datetime& StartTime, datetime& EndTime, int& Data[] )
{
  bool FlagUP = TRUE;
  int Pos = iBarShift(Symbol(), Period(), StartTime);
  int PosEnd = iBarShift(Symbol(), Period(), EndTime);
  int Max = High[Pos] / Point + 0.1;
  int Min = Low[Pos] / Point + 0.1;
  int Count = 0;
  int PriceHigh, PriceLow;
 
  StartTime = Time[Pos];
  EndTime = Time[PosEnd];
  
  ArrayResize(Data, MAX_POINTS);

  Pos--;
  
  while (Pos >= PosEnd)
  {
    PriceHigh = High[Pos] / Point + 0.1;
    PriceLow = Low[Pos] / Point + 0.1;   

    if (FlagUP)
    {
      if (PriceHigh > Max)
        Max = PriceHigh;
      else if (Max - PriceLow >= MinPips)
      {
        Data[Count] = Max - Min;
        Count++;
        
        FlagUP = FALSE;
        Min = PriceLow;
      }
    }
    else
    {
      if (PriceLow < Min)
        Min = PriceLow;
      else if (PriceHigh - Min >= MinPips)
      {
        Data[Count] = Max - Min;
        Count++;
        
        FlagUP = TRUE;
        Max = PriceHigh;
      }
    }
    
    Pos--;
  }
  
  ArrayResize(Data, Count);
    
  return(Count);
}

void start()
{
  int ZigZagData[];
  int Amount = GetZigZagData(MinPips, StartTime, EndTime, ZigZagData);
  
  ArraySort(ZigZagData);
  
  Print("На интервале " + TimeToStr(StartTime) + " - " + TimeToStr(EndTime) +
        " максимальное безоткатное (> " + MinPips +
        " пунктов) движение " + ZigZagData[Amount - 1] + " пунктов.");
        
  return;
}

I throw it on the chart of the instrument on M5 - it works fine with the default timeframe

extern datetime StartTime = D'2011.03.24';
extern datetime EndTime = D'2011.12.31';

it is the maximum depth of history for the symbol in the Alpari office when loading it with F2 - I checked it, i.e. it looks like the content of the "Experts" tab of the client terminal - it seems to be true:

When using the same script design in my EA on the same time interval on the same M5 chart period it doesn't count correctly, i.e.

it calculates quite different (smaller than in the script) numbers or 0. What may be the error or it may be some kind of history inconsistency? Thank you.

I.e., I start in the tester with owl after the estimated time for finding a failsafe, in theory all is normal and the numbers at the end of the script and owl should be the same... What could be the error?

 

This is how I charge this script to my owl:

extern string A3 = "Расчет безотката";
//РАСЧЕТ БЕЗОТКАТА ДЛЯ РАССТАВЛЕНИЯ ОРДЕРОВ С ЗАДАННЫМ ШАГОМ
extern int MinPips = 100;
extern datetime StartTime = D'2011.03.24';
extern datetime EndTime = D'2011.12.31';
#define MAX_POINTS 1000000

//extern double grid = 1000;          // размер безотката в пунктах



//+------------------------------------------------------------------+
//| Старт робота                                                     |
//+------------------------------------------------------------------+

int init()
{   
       
  //------------------     ШАГ СЕТКИ   ---------------------------------------------------------------------------------------

 //Pipstep = grid/Max_Iteration;
 //Pipstep = NormalizeDouble(Pipstep,0);  
 // СЧИТАЕМ БЕЗОТКАТ
    int ZigZagData[];
    int Amount = GetZigZagData(MinPips, StartTime, EndTime, ZigZagData);
  
    ArraySort(ZigZagData);
  
    Print("На интервале " + TimeToStr(StartTime) + " - " + TimeToStr(EndTime) +
        " максимальное безоткатное (> " + MinPips +
        " пунктов) движение " + ZigZagData[Amount - 1] + " пунктов.");        

  
   
    return (0);
}

...
...

// Заполняет массив размерами колен ЗигЗага с условием колена >= MinPips пунктов
int GetZigZagData( int MinPips, datetime& StartTime, datetime& EndTime, int& Data[] )
{
  bool FlagUP = TRUE;
  int Pos = iBarShift(Symbol(), Period(), StartTime);
  int PosEnd = iBarShift(Symbol(), Period(), EndTime);
  int Max = High[Pos] / Point + 0.1;
  int Min = Low[Pos] / Point + 0.1;
  int Count = 0;
  int PriceHigh, PriceLow;
 
  StartTime = Time[Pos];
  EndTime = Time[PosEnd];
  
  ArrayResize(Data, MAX_POINTS);

  Pos--;
  
  while (Pos >= PosEnd)
  {
    PriceHigh = High[Pos] / Point + 0.1;
    PriceLow = Low[Pos] / Point + 0.1;   

    if (FlagUP)
    {
      if (PriceHigh > Max)
        Max = PriceHigh;
      else if (Max - PriceLow >= MinPips)
      {
        Data[Count] = Max - Min;
        Count++;
        
        FlagUP = FALSE;
        Min = PriceLow;
      }
    }
    else
    {
      if (PriceLow < Min)
        Min = PriceLow;
      else if (PriceHigh - Min >= MinPips)
      {
        Data[Count] = Max - Min;
        Count++;
        
        FlagUP = TRUE;
        Max = PriceHigh;
      }
    }
    
    Pos--;
  }
  
  ArrayResize(Data, Count);
    
  return(Count);
}

It seems to be all the same - what could be the mistake?