condition based on a previous condition ... How ?? - page 2

 
walidabou: so what's the difference between this code and my own code ??
Your code was A && B meaning both must be true at the same time.
His code is CrossCondition was seen and now ChicospanCondition is seen.
If you still can't see it. Print out your variables, and find out why.
 

GumRai:


//Globalscope
enum condition{ None,CrossedUp,CrossedDown };
condition CrossCondition=None;
  if(Close[2] < kijun line and Close[1] > kijun line)
     CrossCondition=CrossedUp;  
  
  if(CrossCondition==CrossedUp && ChicospanCondition)
     {
     Place Order; 
     CrossCondition=None;
     } 

The above is pseudo code amd will allow only 1 trade per cross

You should also include code to check only when a new bar opens


walidabou:
Thank you ,, but i didn't understand the code much ,, could you explain more if you don't mind
walidabou:
but this code mean that the both two conditions must be true in sametime ,, or not ??

No,

CrossCondition is a global variable that is initialized to None.

 if(Close[2] < kijun line and Close[1] > kijun line)
     CrossCondition=CrossedUp;  

When a cross is detected CrossCondition is set to CrossedUp

 if(CrossCondition==CrossedUp && ChicospanCondition)
     {
     Place Order; 
     CrossCondition=None;
     } 

In the same or following bars, CrossCondition remains set to CrossedUp until the Chicospan condition is met. Then a trade is placed and CrossCondition is set to None so that no more trades are placed on the same cross. Then the EA will check for a new cross.

Obviously you will also need code for the opposite cross.

 
thank you very much ,, but after creating the code and making a backtest ,, it opens only one order ,, and then ,,   it doesn't open any more orders when new cross happens.
 
walidabou:
thank you very much ,, but after creating the code and making a backtest ,, it opens only one order ,, and then ,,   it doesn't open any more orders when new cross happens.
You need to show your code
 
//+------------------------------------------------------------------+
//|                                                        Kijun.mq4 |
//|                                                      |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int Lot = 1.0;
int MagicNum = 232242;

double KijunH4_2 = iIchimoku(NULL, 15, 9, 26, 52, MODE_KIJUNSEN, 2);
double ChicouH4_2 = iIchimoku(NULL, 15, 9, 26, 52, MODE_CHIKOUSPAN, 2);
double KijunH4_1 = iIchimoku(NULL, 15, 9, 26, 52, MODE_KIJUNSEN, 1);
double ChicouH4_1 = iIchimoku(NULL, 15, 9, 26, 52, MODE_CHIKOUSPAN, 1);
double SpanB_H4 = iIchimoku(NULL, 15, 9, 26, 52, MODE_SENKOUSPANB, 0);
double SpanA_H4 = iIchimoku(NULL, 15, 9, 26, 52, MODE_SENKOUSPANA, 0);

double KijunH4_0 = iIchimoku(NULL, 15, 9, 26, 52, MODE_KIJUNSEN, 0);

//---------------------------------------------------------------------------

double Kijun_M30 = iIchimoku(NULL, 30, 9, 26, 52, MODE_KIJUNSEN, 0);
double Tenken_M30 = iIchimoku(NULL, 30, 9, 26, 52, MODE_TENKANSEN, 0);
double Chicou_M30 = iIchimoku(NULL, 30, 9, 26, 52, MODE_CHIKOUSPAN, 1);
double SpanB_M30 = iIchimoku(NULL, 30, 9, 26, 52, MODE_SENKOUSPANB, 0);
double SpanA_M30 = iIchimoku(NULL, 30, 9, 26, 52, MODE_SENKOUSPANA, 0);

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- 
         static int FirstSignal = 0;
          if(Close[2] < KijunH4_2 && Close[1] > KijunH4_1)
 {
  FirstSignal = 1;
 }
 
         if(FirstSignal == 1 && Close[1] > High[27] && OrdersTotal() <= 0)
 {
  OrderSend(Symbol(), OP_BUY, Lot, Ask, 3, (KijunH4_0)-10*Point, Bid+30*Point, NULL, MagicNum, 0, clrBlue); 
  FirstSignal = 0;
 }
 
         if(Close[2] > KijunH4_2 && Close[1] < KijunH4_1)
 {
  FirstSignal = -1;
 }
          if(FirstSignal == -1 && Close[1] < Low[27] && OrdersTotal() <= 0)
 {
  OrderSend(Symbol(), OP_SELL, Lot, Bid, 3, (KijunH4_0)-10*Point, Bid-30*Point, NULL, MagicNum, 0, clrRed); 
  FirstSignal = 0;
 }  
     
  }
//+------------------------------------------------------------------+
 
walidabou: , but after creating the code and making a backtest ,, it opens only one order ,, and then ,,   it doesn't open any more orders when new cross happens.
  1. double KijunH4_2 = iIchimoku(NULL, 15, 9, 26, 52, MODE_KIJUNSEN, 2);
    double ChicouH4_2 = iIchimoku(NULL, 15, 9, 26, 52, MODE_CHIKOUSPAN, 2);
    Of course it doesn't because nothing is changing. Update your variables in OnTick, as previously said.
  2. OrderSend(Symbol(), OP_SELL, Lot, Bid, 3, (KijunH4_0)-10*Point, Bid-30*Point, NULL, MagicNum, 0, clrRed); 
    Check your return codes and find out why. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
 

with this code i added a third condition which is (the price is above Kijun and tenken and span A and span b   on M30 Timeframe) and fourth condition which is (Close[1] > Close[27]   on M30 Timeframe)

It is right code ??


//+------------------------------------------------------------------+
//|                                                        Kijun.mq4 |
//|                                                      |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+

#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int Lot = 1.0;
int MagicNum = 232242;


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


double KijunH4_2 = iIchimoku(NULL, 240, 9, 26, 52, MODE_KIJUNSEN, 2);
double ChicouH4_2 = iIchimoku(NULL, 240, 9, 26, 52, MODE_CHIKOUSPAN, 2);
double KijunH4_1 = iIchimoku(NULL, 240, 9, 26, 52, MODE_KIJUNSEN, 1);
double ChicouH4_1 = iIchimoku(NULL, 240, 9, 26, 52, MODE_CHIKOUSPAN, 1);
double SpanB_H4 = iIchimoku(NULL, 240, 9, 26, 52, MODE_SENKOUSPANB, 0);
double SpanA_H4 = iIchimoku(NULL, 240, 9, 26, 52, MODE_SENKOUSPANA, 0);

double KijunH4_0 = iIchimoku(NULL, 240, 9, 26, 52, MODE_KIJUNSEN, 0);

//---------------------------------------------------------------------------

double Kijun_M30 = iIchimoku(NULL, 30, 9, 26, 52, MODE_KIJUNSEN, 0);
double Tenken_M30 = iIchimoku(NULL, 30, 9, 26, 52, MODE_TENKANSEN, 0);
double Chicou_M30 = iIchimoku(NULL, 30, 9, 26, 52, MODE_CHIKOUSPAN, 1);
double SpanB_M30 = iIchimoku(NULL, 30, 9, 26, 52, MODE_SENKOUSPANB, 26);
double SpanA_M30 = iIchimoku(NULL, 30, 9, 26, 52, MODE_SENKOUSPANA, 26);



         static int FirstSignal = 0, SecondSignal = 0, ThirdSignal = 0;
   
 
      if(Close[2] < KijunH4_2 && Close[1] > KijunH4_1)
 {
         FirstSignal = 1;
 }
  
      if(FirstSignal == 1 && Close[1] > High[27])
      {
         SecondSignal = 2;
      }
     
      if(SecondSignal == 2 && Ask > Kijun_M30 && Ask > Tenken_M30 && Ask > SpanA_M30 && Ask > SpanB_M30)
      {
         ThirdSignal = 3;
      }
      
      if(ThirdSignal == 3 && iClose(Symbol(), 30, 1) > iClose(Symbol(), 30, 27) && OrdersTotal() <= 0)
      {
         OrderSend(Symbol(), OP_BUY, 1.0, Ask+6*Point, 3, (KijunH4_0)-10*Point, Bid+50*Point, NULL, MagicNum, 0, clrBlue);
      
        FirstSignal = 0;
      }
      
      
      if(Close[2] > KijunH4_2 && Close[1] < KijunH4_1)
 {
         FirstSignal = -1;
 }
  
      if(FirstSignal == -1 && Close[1] < High[27])
      {
         SecondSignal = -2;
      }
     
      if(SecondSignal == -2 && Ask < Kijun_M30 && Ask < Tenken_M30 && Ask < SpanA_M30 && Ask < SpanB_M30)
      {
         ThirdSignal = -3;
      }
        
        if(ThirdSignal == -3 && iClose(Symbol(), 30, 1) < iClose(Symbol(), 30, 27) && OrdersTotal() <= 0)
      {
         OrderSend(Symbol(), OP_SELL, 1.0, Bid-6*Point, 3, (KijunH4_0)+10*Point, Ask-50*Point, NULL, MagicNum, 0, clrRed);
      
         FirstSignal= 0;
         
      }
  }
//+------------------------------------------------------------------+