Analyzing Bars backwards in EA - page 2

 
FMIC:

There is no OnStart() in EA's but I get what you meant. However, the OP even in response to your comments said and I quote:

Yes, I meant OnTick(). You are still attaching too much significance to "while EA running". It doesn't necessarily, or even most plausibly mean, "while the EA is executing its OnTick()", rather than "while the EA is attached to a chart, i.e. during its entire lifetime".

We need more information from paranoyakX.

 
jjc:

Yes, I meant OnTick(). You are still attaching too much significance to "while EA running". It doesn't necessarily, or even most plausibly mean, "while the EA is executing its OnTick()", rather than "while the EA is attached to a chart, i.e. during its entire lifetime".

We need more information from paranoyakX.

Hi guys,

 let me clarify this. My code is looking for a very basic pattern on previous bars and will decide if an order will be opened but opening new order will be check only when a new bar comes up. what I mean I am waiting for the last bar close, then check the pattern if it is ok I will open the order. so opening an order is happening at new bar's opening. as @jjc said, I am holding basic pattern information at global variables, like when my pattern started and ended, what the highest and lowest value is, etc. 

Somehow if I have to re-run EA, I want to find these basic information again (pattern start and end bar, highest, lowest prices etc) and fill my global variables again, that's why I need this. I will use start and end info to decide when to close my order. so it is enough if a new bar does not come up at OnInit, while EA running, it is not a problem.

 
jjc:

Yes, I meant OnTick(). You are still attaching too much significance to "while EA running". It doesn't necessarily, or even most plausibly mean, "while the EA is executing its OnTick()", rather than "while the EA is attached to a chart, i.e. during its entire lifetime".

We need more information from paranoyakX.

Yes! You are correct! From the latest information from the OP, he is definitely running everything in the OnInit() and not on the OnTick(). I assumed incorrectly that the OP was doing things a little more correctly. It never occurred to me that all his logic was in the OnInit().
 
paranoyakX:

Hi guys,

 let me clarify this. My code is looking for a very basic pattern on previous bars and will decide if an order will be opened but opening new order will be check only when a new bar comes up. what I mean I am waiting for the last bar close, then check the pattern if it is ok I will open the order. so opening an order is happening at new bar's opening. as @jjc said, I am holding basic pattern information at global variables, like when my pattern started and ended, what the highest and lowest value is, etc. 

Somehow if I have to re-run EA, I want to find these basic information again (pattern start and end bar, highest, lowest prices etc) and fill my global variables again, that's why I need this. I will use start and end info to decide when to close my order. so it is enough if a new bar does not come up at OnInit, while EA running, it is not a problem.

You should NOT be doing all of that in the OnInit(). You should be doing all that logic (including recovery) in the OnTick(). This is important! Doing it in OnInit() will cause you further problems which you have not foreseen and will leave your EA in a state of "initialising" while you are doing all of that logic. So, do it right! Do only your initialisation (such as variables, external parameter checks, etc.) in OnInit() and everything else in the OnTick().
 
FMIC:
You should NOT be doing all of that in the OnInit(). You should be doing all that logic (including recovery) in the OnTick(). This is important! Doing it in OnInit() will cause you further problems which you have not foreseen and will leave your EA in a state of "initialising" while you are doing all of that logic. So, do it right! Do only your initialisation (such as variables, external parameter checks, etc.) in OnInit() and everything else in the OnTick().

No no no! I couldn't explain my selft sorry, I am running everything in the OnTick. but simply I checked if this tick is belong to a new bar and do my staff. What I run in OnInit is, re-find the orders and the pattern that I calculated before, recalculate it. I said that this is running at OnInit, find my pattern that currently opened order is belong to.

 I hope I can explain. 

 
paranoyakX:

No no no! I couldn't explain my selft sorry, I am running everything in the OnTick. but simply I checked if this tick is belong to a new bar and do my staff. What I run in OnInit is, re-find the orders and the pattern that I calculated before, recalculate it. I said that this is running at OnInit, find my pattern that currently opened order is belong to.

 I hope I can explain. 

Yes, I understood that in your post! What I am saying is DON'T do that in the OnInit(). You should find your orders and patterns and all that calculation in the OnTick().

Just define a local static variable in OnTick() and do all your checking and pattern definition, and then set the variable to false.

void OnTick()
{
   static bool FirstOnTick = true;

   if( FirstOnTick )
   {
      // Check Orders, Patterns, whatever

      FirstOnTick = false;
   }

   // Here you do your normal OnTick handling
}
 
FMIC:

Yes, I understood that in your post! What I am saying is DON'T do that in the OnInit(). You should find your orders and patterns and all that calculation in the OnTick().

Just define a local static variable in OnTick() and do all your checking and pattern definition, and then set the variable to false.

Sorry then, I misunderstood you before. Why I do not do that OnInit ? Is not it more convenient ? When I do that, for every tick after the first one, I will be running and if statement that wont be true every. I thought that unnecessary load on the code.

is not the reason OnInit exists, to initialize something like my global variables ? 

 
paranoyakX:

I hope I can explain. 

I think that we need to see some code to be certain of what you are talking about. For example, it is not clear whether you are looking for the historic pattern which generated an existing open trade because (a) that tells you how to manage the open trade, where/when to close it etc, or (b) just in order to draw some markers on the chart explaining to yourself why the existing trade was opened.

I generally agree with FMIC that you should try to do as much as possible in OnTick(), and to keep as little state as possible in global variables.

 
paranoyakX:

Sorry then, I misunderstood you before. Why I do not do that OnInit ? Is not it more convenient ? When I do that, for every tick after the first one, I will be running and if statement that wont be true every. I thought that unnecessary load on the code.

is not the reason OnInit exists, to initialize something like my global variables ? 

No, it is not correct to do that in the OnInit() because you will be holding up the Initialisation execution and no, the static variables will maintain its state so you will only be doing the checks "once", not on every OnTick(). The "if( FirstOnTick )" is very quick and has very little load, especially in comparrison to all your other code that is running in the OnTick().
 

thanks for the advice, here is a very simple version of my code, this is not my real code but I hope this will be more clear.

 as I said this is not the real code, finding pattern etc is just an example. the reason I opened this thread is DetectExistingPattern() function

//+------------------------------------------------------------------+
//|                                                          Mustafa |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Mustafa"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int MagicNumber= 100;


int OnInit()
  {
//---
   DetectExistingPattern();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+


int LastBar=0;
double HighestValue, LowestValue;

void OnTick()
  {
//---
    MyStart();

  }


void MyStart(){
    
    CloseOpenedOrders();

    if (LastBar == Bars) return;
    LastBar = Bars;

    if (!CheckForExistedOrders()) {       
       
       if (CheckTrendPattern()){
           PlaceOrders();
       };
    };
};

bool CheckForExistedOrders(){
  for(int TradeNumber = OrdersTotal(); TradeNumber >= 0; TradeNumber--){
    if ( (OrderSelect(TradeNumber, SELECT_BY_POS, MODE_TRADES)) && (OrderMagicNumber() == MagicNumber )) return true;
  };
  return false;

};


bool CheckTrendPattern(){
 
  for(int i=10; i>=1; i--) {
    if (High[i]>High[i-1])
      return false;
  };
  HighestValue = High[10];
  LowestValue  = Low[1];
  
  return true;
};



void PlaceOrders(){  
    int OrderResult = OrderSend(Symbol(), OP_BUY, 1, Ask, 5, 0, 0, "", MagicNumber);
}
//+------------------------------------------------------------------+

void CloseOpenedOrders(){
  // I will use HighestValue and LowestValue here, so I need to re calculate them.
};

void DetectExistingPattern() {
  // Find HighestValue and LowestValue again, this part is the reason why I opened the thread!!! I want to run this in OnInıt()
  // I will find opened order and find the previos pattern that cause me to open that order.
};
Reason: