dont trade buy if last deal was buy closed in loss

 

Hi all!


I try to create a code to check if the last buy position (what is closed) if it closed ik loss or in profit.

i Try to create a bool function so before the trade started , the EA is sure that the last buy position is not closed in loss (false).

if its closed in loss it return true and then i would like my EA dont open the trade. 

I read a lot and saw a lot of example but still cant get it working.

i will show my code maybe somebody figure out whats wrong with my code.

i test also the sell type cause ctrade reverse the closed position (a sell trade end with a buy ticket)

even with youtube tutorials i can t make it work!

Hope someone can help me a little hand 

Btw im a newbie , but you already figure that out 🧐

------------------------------------------ first code try   

 bool LastBuyPositioninLoss(){
  
  
  
  HistorySelect(0,TimeCurrent());
  int TotalDeals = HistoryDealsTotal();
  
  for(int i=0; i<TotalDeals; i++){
  long DealType = HistoryDealGetInteger(i,DEAL_TYPE);
   if(DealType== DEAL_TYPE_BUY){
      double Profit = HistoryDealGetDouble(i,DEAL_PROFIT);
         if(Profit <= 0){
         return true;
         }
   
   return false;
   }
   
 return false;
  
  }
  
  
  
  
  return false;
  }
  
  
  ---------------------------------------- second code try
 

bool CheckBuyDealClosedinLoss()
{
    int totalDeals = HistoryDealsTotal();
    
    if (totalDeals > 0)
    {
        ulong lastDealTicket = HistoryDealGetTicket(totalDeals - 1);
        
        ENUM_DEAL_TYPE lastDealType = (ENUM_DEAL_TYPE)HistoryDealGetInteger(lastDealTicket, DEAL_TYPE);
        long DealMagic = HistoryDealGetInteger(lastDealTicket, DEAL_MAGIC);
        double lastDealProfit = HistoryDealGetDouble(lastDealTicket, DEAL_PROFIT);
        
        // Ckeck if buy is true
        if (lastDealType == DEAL_TYPE_BUY)
        { if (DealMagic == InpMagicNumber)
        {
             // Check if cloed in loss
            if (lastDealProfit > 0)
            {
                return true;} 
                return false;} 
                return false;}
                return false; }
    
 return false;  
}
 
muntje:
dont trade buy if last deal was buy closed in loss

Then, on the first loss, you stop trading forever.

 
William Roeder #:

Then, on the first loss, you stop trading forever.

Well not specially cause if the next trade is a sell trade, after that it will trade again right. Its only to check the last trade. Last trade buy is closed in loss? If true , only sell trades are allowed. After the sell trade it has to be possible to trade buys again
 

Try this. I did not compile or test.

double LatestBuyPnL()
{
   if(HistorySelect(0, TimeCurrent()==false) return 0;
   for(int i=HistoryDealsTotal()-1;i>=0;i--) // scan deals from latest to earliest
   {
      ulong ticket = HistoryDealGetTicket(i);
      if(HistoryDealGetInteger(ticket, DEAL_TYPE) == DEAL_TYPE_SELL && HistoryDealGetInteger(ticket, DEAL_ENTRY) == DEAL_ENTRY_OUT)
      {
         //You have a deal out which is sell ==> So the original position was a buy
         return HistoryDealGetDouble(ticket, DEAL_PROFIT);
      }
   }
   return 0;
}
 

use global boolean flags

bool last_sell_deal_win = false;
bool last_buy_deal_win = false;




    if (!PositionSelect(Symbol())){
        
      HistorySelect(TimeCurrent()-3600, TimeCurrent());

         for (int i = 0; i < HistoryDealsTotal(); i++){
         
             ulong ticket = HistoryDealGetTicket(i);      
             ulong last_ticket = HistoryDealGetTicket(HistoryDealsTotal() - i - 1);  // if HistoryDealsTotal is greater than 1 you might want to use this instead of "ticket" variable


             if (ticket > 0){
             
                double dealProfit = HistoryDealGetDouble(ticket, DEAL_PROFIT);
                ulong dealTime = HistoryDealGetInteger(ticket, DEAL_TIME);
                ulong dealType = HistoryDealGetInteger(ticket, DEAL_TYPE);
         
                  
                if(dealType == DEAL_TYPE_SELL && dealProfit > 0){
                  
                  last_sell_deal_win = true;
                }
                else if(dealType == DEAL_TYPE_SELL && dealProfit < -1){
                
                  last_sell_deal_win = false;
                }
                
                if(dealType == DEAL_TYPE_BUY && dealProfit > 0){
                
                  last_buy_deal_win = true;
                }
                else if(dealType == DEAL_TYPE_BUY && dealProfit < -1){
                
                  last_buy_deal_win = false;
                }
                                
             }           
             else{
                 Print("Failed to get ticket for index ", i);
             }
         }       
      }   



if(mySellTradeSignal && last_sell_deal_win){
        Sell(); // it is allowed to make a new sell position
}

if(myBuyTradeSignal && last_buy_deal_win){
        Buy(); // it is allowed to make a new buy position
}


If both flags become false, you have to reset them so the EA can continue to place trades

if(!last_sell_deal_win && !last_buy_deal_win){
        last_sell_deal_win = true; //reset the flag
        last_buy_deal_win = true; //reset the flag
}
 
Conor Mcnamara #:

use global boolean flags







If both flags become false, you have to reset them so the EA can continue to place trades

Thanks! I add the script and its working for me ! Thanks!!

 
Yashar Seyyedin #:

Try this. I did not compile or test.

Thanks!!!!