Order repeating on Non true condetion also. - page 2

 
DEEPAK SONI #:

Ea on Engulfing pattern, incorrect opening of more order on false condetions also.

Made an ea on Engulfing pattern. 
Suppose it enters sell trade on Bear engulfing , after taking profit or sl ( trade closed) after few candles it again enters bear trade on Not proper condition . 
I set up orderstotal = 0 condetion also.
Still it enters incorrectly next trade on. Previous correct condetion. Please see the attachment to be more clear.

I would like to know the way we can avoid this by any code reference. Thank you
Yes exactly, I tried testing your code sir, but it wasn't opening any order, i don't know what's wrong with your code . It doesn't show any error while compiling but at time of backtest it doesn't give any orders. 
I managed to take trades on my code but still there are error even after putting Bars total and Orderstotal==0 .
Is there any way to fix False condetion orders

Did you try the code with the addition of the latest Engulfing check functions ?

 

added trailing stop loss into this..

i would like to add one more condetion :  When stop loss hits , it should take reverse trade with the same previous candle low.

as i added trailing stop losss. so now am confused to add reverse trade.


would be  really gratefull if you can sort me out for this,

waiting for your kind gesture 

 
Lorentzos Roussos #:

try this one :

i would like to add one more condetion :  When stop loss hits , it should take reverse trade with the same previous candle low.

as i added trailing stop losss. so now am confused to add reverse trade.


would be  really gratefull if you can sort me out for this,

waiting for your kind gesture

Reversing: The holy grail or a dangerous delusion?
Reversing: The holy grail or a dangerous delusion?
  • www.mql5.com
In this article, we will study the reverse martingale technique and will try to understand whether it is worth using, as well as whether it can help improve your trading strategy. We will create an Expert Advisor to operate on historic data and to check what indicators are best suitable for the reversing technique. We will also check whether it can be used without any indicator as an independent trading system. In addition, we will check if reversing can turn a loss-making trading system into a profitable one.
 
DEEPAK SONI #:

i would like to add one more condetion :  When stop loss hits , it should take reverse trade with the same previous candle low.

as i added trailing stop losss. so now am confused to add reverse trade.


would be  really gratefull if you can sort me out for this,

waiting for your kind gesture

Well done , sharing the code would help however.

 
Lorentzos Roussos #:

Well done , sharing the code would help however.

this is code , i added trailing stop loss to minimize drawdown levels. 

i want to add Reverse trade when default stop loss or when the trade ends up in loss.. 

// Constants
#include <Trade\Trade.mqh>
const int BULL_ENGULFING = -1;
const int BEAR_ENGULFING = 1;
const double PIP_SIZE = _Point;
ulong Tickets;
datetime barStamp=0;
// Global variables
CTrade trade;

int OnInit(){
barStamp=0;
return(INIT_SUCCEEDED);
}

// Function to check for bullish engulfing pattern
bool IsBullEngulfing(bool &managed_to_check_it)
{
    managed_to_check_it=false;
    ResetLastError();
    int error_collection=0;
    int prev = 2;
    double prevOpen = iOpen(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError();
    double prevClose = iClose(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError();
    double prevLow = iLow(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError();
    double prevHigh = iHigh(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError();

    double currentOpen = iOpen(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError();
    double currentClose = iClose(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError();
    double currentLow = iLow(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError();
    double currentHigh = iHigh(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError();

    //if no errors 
    if(error_collection==0){
    managed_to_check_it=true;
    // Check if the current candle is bullish and engulfs the previous candle
    /*
    bool isBullish = (currentOpen < currentClose) && (currentClose > prevOpen) && (currentOpen <= prevClose);
    bool isEngulfing = currentHigh > prevHigh;
    return(isBullish && isEngulfing);
    */
    //is the previous bar bearish ? 
      if(prevOpen>prevClose){
        //and is the current bar bullish ? 
          if(currentClose>currentOpen){
            //is the close of the previous above the current open 
              if(prevClose>=currentOpen){
                //is the open of the previous below the current close 
                  if(prevOpen<=currentClose){
                    if(currentHigh>prevHigh){
                    return(true);
                    }
                }
            }
        }
     }   
    }
return(false);
}

// Function to check for bearish engulfing pattern
bool IsBearEngulfing(bool &managed_to_check_it)
{
    managed_to_check_it=false;
    ResetLastError();
    int error_collection=0;
    int prev = 2;
    double prevOpen = iOpen(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError();
    double prevClose = iClose(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError();
    double prevLow = iLow(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError();
    double prevHigh = iHigh(_Symbol, _Period, prev);error_collection+=GetLastError();ResetLastError();

    double currentOpen = iOpen(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError();
    double currentClose = iClose(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError();
    double currentLow = iLow(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError();
    double currentHigh = iHigh(_Symbol, _Period, 1);error_collection+=GetLastError();ResetLastError();
    double stopLoss = currentHigh;
    
    //if no errors 
    if(error_collection==0){
    managed_to_check_it=true;
    /*
    // Check if the current candle is bearish and engulfs the previous candle
    bool isBearish = (currentOpen > currentClose) && (currentClose < prevOpen) && (currentOpen >= prevClose);
    bool isEngulfing = currentLow < prevLow;
    return(isBearish && isEngulfing);
    */
    //is the previous bar bullish ? 
      if(prevOpen<prevClose){
        //and is the current bar bearish ? 
          if(currentClose<currentOpen){
            //is the open of the previous above the current close 
              if(prevOpen>=currentClose){
                //is the close of the previous below the current open 
                  if(prevClose<=currentOpen){
                   // lower low
                      if(currentLow<prevLow){
                    return(true);
                    }
                }
            }
        }
       } 
    }
return(false);
 
}

// Function to send a buy order

// Function to send a sell order


// The OnTick event handler
void OnTick()
{
ResetLastError();
datetime now=iTime(_Symbol,_Period,0);
//check on new bars only
if(GetLastError()==0&&now>barStamp){
//create 2 indicators for the check completing succesfully
  bool isBull=false,isBear=false,bullChecked=false,bearChecked=false;
  //we want both checks to fire otherwise we do not register the new bar timestamp
    isBull=IsBullEngulfing(bullChecked);
    isBear=IsBearEngulfing(bearChecked);
    //if both checked succefussvufully :P
    if(bullChecked&&bearChecked)
    {
    //then we log the new bar timestamp 
      barStamp=now;
    //and then we check for opening the trades    
      if(isBull){
                // Retrieve the current Ask and Bid prices
                double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
                double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
            
                // Calculate the risk based on 0.4% of the stop loss distance
                double stopLoss = iLow(_Symbol,_Period, 1);
                double riskAmount =  (ask - stopLoss) / _Point;
                double riskPercentage = 0.02;  // 4% risk
                
               double accountCapital = AccountInfoDouble(ACCOUNT_BALANCE);
               
                double takeProfitreward = ask - stopLoss ;
                double takeProfitrewards = takeProfitreward * 1.40;
                double takeProfits =NormalizeDouble((ask + takeProfitrewards),_Digits);
                
                double tradeQuantity = AccountInfoDouble(ACCOUNT_BALANCE);
                double tradeqty = tradeQuantity * riskPercentage;
                double qty = tradeqty / riskAmount;
                
                //Tick value for one lot (movement of 1 point (_Point) is equal to what with one lot)
                  double tvol_buy=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
                  /*You know you can lose "risk percentage" by "risk amount"
                  in other words , the 4% will be lost in distance ask->StopLoss
                  so you want to "estimate" the lot size 
                  So you know how much one point for one lot costs and you know how many points your loss would be
                  so you can project the lot
                  you will also have to check many other things , lot size limit , max total volume , if the asset is tradable
                  if you have enough margin etc.*/
                    double tick_value_for_your_lot=tradeqty/riskAmount;
                    qty=NormalizeDouble(tick_value_for_your_lot/tvol_buy,2);
                    
                   if(PositionsTotal()==0 && OrdersTotal()==0) {
                   trade.SetExpertMagicNumber(22000);          
                   trade.Buy(qty,_Symbol,ask,stopLoss,takeProfits,"Bull Engulfing");
                   }  
        Comment("Bull("+TimeToString(now,TIME_DATE|TIME_MINUTES|TIME_SECONDS));      
        }  
        if(isBear){
                     // Retrieve the current Ask and Bid prices
                     
                   double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
                   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
               
                   // Calculate the risk based on 0.4% of the stop loss distance
                   double stopLoss = iHigh(_Symbol, PERIOD_CURRENT, 1);
                   double riskAmount = (stopLoss - bid) / _Point;  // Assuming a short trade
                   double riskPercentage = 0.04;  // 4% risk
                   double accountCapital = AccountInfoDouble(ACCOUNT_BALANCE);
                   
                   double takeProfitreward = stopLoss - bid;
                   double takeProfitrewards = takeProfitreward * 1.40 ;
                   double takeProfits =  NormalizeDouble((bid - takeProfitrewards),_Digits);
                   
                   
                   double tradeQuantity = AccountInfoDouble(ACCOUNT_BALANCE);
                   double tradeqty = tradeQuantity * riskPercentage;
                   double qty = tradeqty / riskAmount;
                   
                  
                  
                   double tvol_sell=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
                  /*You know you can lose "risk percentage" by "risk amount"
                  in other words , the 4% will be lost in distance ask->StopLoss
                  so you want to "estimate" the lot size 
                  So you know how much one point for one lot costs and you know how many points your loss would be
                  so you can project the lot
                  you will also have to check many other things , lot size limit , max total volume , if the asset is tradable
                  if you have enough margin etc.*/
                    double tick_value_for_your_lot=tradeqty/riskAmount;
                    qty=NormalizeDouble(tick_value_for_your_lot/tvol_sell,2);
                   
                   if (PositionsTotal()==0 && OrdersTotal()==0){
                   
                   trade.SetExpertMagicNumber(45000);
                   
                       trade.Sell(qty,_Symbol,bid,stopLoss,takeProfits,"bear Engulfing");
                             
                   } 
        Comment("Bear("+TimeToString(now,TIME_DATE|TIME_MINUTES|TIME_SECONDS));          
        } 
       }


     }

  }
for(int i=PositionsTotal()-1; i>=0; i--) {
    
      ulong PosTicket = PositionGetTicket(i);
     if(PositionSelectByTicket(PosTicket)){
      double posSL = PositionGetDouble(POSITION_SL);
      double posTP = PositionGetDouble(POSITION_TP);
      
      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY){
      
      double bullbuy = iLow(_Symbol,PERIOD_CURRENT,4);
      bullbuy = NormalizeDouble(bullbuy,_Digits); 
      
      if(bullbuy>posSL) {
          if(trade.PositionModify(PosTicket,bullbuy,posTP)){
          
           Print("Buy trailing Stop loss");
           Comment("bull stop loss trailing");
          
           }
         
    
      }  
    
      
     else if (PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL){
     
      double bearsell = iHigh(_Symbol,PERIOD_CURRENT,4);
      bearsell = NormalizeDouble(bearsell,_Digits); 
      
      if(bearsell<posSL) {
          if(trade.PositionModify(PosTicket,bearsell,posTP))
          {
          Print("Sell trailing stop loss");
          Comment("bear stop loss trailed");
          
     
            }
          }
     
        }
     }
 }