Problem with Parabolic EA

 

hi all

Please help, i have a problem with my PSAR EA. The problem is it opens properly as i want it to, problem is when it closes it opens another trade. I would like for it to open a trade & close THE TRADE that's all. I know there's something missing. I tried searching on the internet but no luck.

#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int MagicNumber=55;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int CountTrades()
 {
  int Count=0;
  for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
  {
   if(OrderSelect(Loop,SELECT_BY_POS))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUY||OrderType()==OP_SELL)
   Count++;
  }
  
  return(Count);
 } 
 
void Buy()
 {
  double PSAR=iSAR(Symbol(),NULL,0.02,0.2,0);
 
  if(PSAR<Bid)
   {
    int BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"JackBuda",MagicNumber,0,clrBlue);
   }
 } 
 
void Sell()
 { 
  double PSAR=iSAR(Symbol(),NULL,0.02,0.2,0);
  
  if(PSAR>Ask)
   {
    int SellTrade=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"JackBuda",MagicNumber,0,clrRed);//20
   }
 }  
  
void OnTick()
 {
  int LimitOrders=(int)AccountInfoInteger(ACCOUNT_LIMIT_ORDERS);
  double CloseBar=iClose(NULL,0,1);
  
  double InProfit=OrderLots()*50;
  
  for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
   {
    bool OSProfit=OrderSelect(Loop,SELECT_BY_POS,MODE_TRADES);
   } 
   
  if(AccountBalance()>10&&CountTrades()<1){Buy();}
  
  if(AccountBalance()>10&&CountTrades()<1){Sell();}
  
  for(int Loop2=OrdersTotal()-1;Loop2>=0;Loop2--)
  {
   if(OrderSelect(Loop2,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   { 
    bool CloseTrade=false;
    int Type=OrderType(); 
    double Profit=OrderLots()*100;
    switch(Type)
    {
     case OP_BUY:if(OrderProfit()>=Profit)CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);break;
     case OP_SELL:if(OrderProfit()>=Profit)CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);break;                       
    }
   }
  } 
   
 }

 I tried the following

if(AccountBalance()>10&&CountTrades()<1){Buy();}
else if(OrderProfit()>InProfit&&CloseBar>0&&OrdersTotal()>0)return;

if(AccountBalance()>10&&CountTrades()<1){Sell();}
else if(OrderProfit()>InProfit&&0<CloseBar&&OrdersTotal()>0)return;

But it would open a trade & not close at all & that's not what i want.

 Your help will be appreciated.
 
search for Trade Per Bar
 
Ahmet Metin Yilmaz:
search for Trade Per Bar

I'll do that, since you mentioned "trade per bar" would this function fix my problem or not?

bool NewCandle()
 {
  static int BarsOnChart=0;
  if(Bars==BarsOnChart)
  return(false);
  BarsOnChart=Bars;
  return(true);
 }  
 
Jack Buda:

I'll do that, since you mentioned "trade per bar" would this function fix my problem or not?

no you can find in this website just search

 
Ahmet Metin Yilmaz:

no you can find in this website just search

I tried looking for "trade per bar" & this what i found. It still doesn't work

#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int MagicNumber=55;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int CountTrades()
 {
  int Count=0;
  for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
  {
   if(OrderSelect(Loop,SELECT_BY_POS))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUY||OrderType()==OP_SELL)
   Count++;
  }
  
  return(Count);
 } 
 
void Buy()
 {
  double PSAR=iSAR(Symbol(),NULL,0.02,0.2,0);
 
  if(PSAR<Bid)
   {
    int BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"JackBuda",MagicNumber,0,clrBlue);
   }
 } 
 
void Sell()
 { 
  double PSAR=iSAR(Symbol(),NULL,0.02,0.2,0);
  
  if(PSAR>Ask)
   {
    int SellTrade=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"JackBuda",MagicNumber,0,clrRed);//20
   }
 }  
  
void OnTick()
 {
  int BarsCount=0;
  if(Bars>BarsCount)
  {
   for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
    {
     bool OSProfit=OrderSelect(Loop,SELECT_BY_POS,MODE_TRADES);
    } 
   
   if(AccountBalance()>10&&CountTrades()<1){Buy();}
  
   if(AccountBalance()>10&&CountTrades()<1){Sell();}
  
   for(int Loop2=OrdersTotal()-1;Loop2>=0;Loop2--)
   {
    if(OrderSelect(Loop2,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    { 
     bool CloseTrade=false;
     int Type=OrderType(); 
     double Profit=OrderLots()*100;
     switch(Type)
     {
      case OP_BUY:if(OrderProfit()>=Profit)CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);break;
      case OP_SELL:if(OrderProfit()>=Profit)CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);break;                       
     }
    }
   } 
   BarsCount=Bars;
  }
  
 }
 

Hi Jack,

I have add an simple OrderFlag to your code - now it should work as you want to.

#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int MagicNumber=55;
int OrderFlag = 0;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int CountTrades()
 {
  int Count=0;
  for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
  {
   if(OrderSelect(Loop,SELECT_BY_POS))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   if(OrderType()==OP_BUY||OrderType()==OP_SELL)
   Count++;
  }
  
  return(Count);
 } 
 
void Buy()
 {
  double PSAR=iSAR(Symbol(),NULL,0.02,0.2,0);
 
  if(PSAR<Bid)
   {
    if (OrderFlag != 1)
    {
      OrderFlag = 1;
      int BuyTrade=OrderSend(Symbol(),OP_BUY,0.01,Ask,0,0,0,"JackBuda",MagicNumber,0,clrBlue);
    }
   }
 } 
 
void Sell()
 { 
  double PSAR=iSAR(Symbol(),NULL,0.02,0.2,0);
  
  if(PSAR>Ask)
   {
    if (OrderFlag != -1)
    {
      OrderFlag = -1;
      int SellTrade=OrderSend(Symbol(),OP_SELL,0.01,Bid,0,0,0,"JackBuda",MagicNumber,0,clrRed);//20
    }
   }
 }  
  
void OnTick()
 {
  int BarsCount=0;
  if(Bars>BarsCount)
  {
   for(int Loop=OrdersTotal()-1;Loop>=0;Loop--)
    {
     bool OSProfit=OrderSelect(Loop,SELECT_BY_POS,MODE_TRADES);
    } 
   
   if(AccountBalance()>10&&CountTrades()<1){Buy();}
  
   if(AccountBalance()>10&&CountTrades()<1){Sell();}
  
   for(int Loop2=OrdersTotal()-1;Loop2>=0;Loop2--)
   {
    if(OrderSelect(Loop2,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    { 
     bool CloseTrade=false;
     int Type=OrderType(); 
     double Profit=OrderLots()*100;
     switch(Type)
     {
      case OP_BUY:if(OrderProfit()>=Profit)CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);break;
      case OP_SELL:if(OrderProfit()>=Profit)CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);break;                       
     }
    }
   } 
   BarsCount=Bars;
  }
  
 }

Best regards

 
Werner Klehr:

Hi Jack,

I have add an simple OrderFlag to your code - now it should work as you want to.

Best regards

Hi Werner

I have tried the "OrderFlag" as you have shown but no changes. :(

The picture below, i want the ea to open a trade & close that's it, i don't want it to open more trades. With the picture below i used the following code :

if(OrdersTotal()>0)return;


 

The code "OrdersTotal" i used will not close at all. So i would like a way just to open one trade & close in one trend that's it.

 
Jack Buda:

Hi Werner

I have tried the "OrderFlag" as you have shown but no changes. :(

The picture below, i want the ea to open a trade & close that's it, i don't want it to open more trades. With the picture below i used the following code :


 

The code "OrdersTotal" i used will not close at all. So i would like a way just to open one trade & close in one trend that's it.

Hi Jack,

one question: do you want one trade per bar or one trade per parabolic wave?
My solution handles one trade per parabolic wave.

Best regards

 
Werner Klehr:

Hi Jack,

one question: do you want one trade per bar or one trade per parabolic wave?
My solution handles one trade per parabolic wave.

Best regards

I would like one trade per bar. Since you mentioned your solution handles one trade per parabolic wave, I tried it & its not the one i want. I hope one trade per bar will be the solution.

 
 

Hi Jack,

here is the code if you want to do something on a new bar:

datetime LastBarTime;

...

void OnTick()
 {
   bool NewBarFlag = false;

   if (Time[0] != LastBarTime)
   {
        NewBarFlag = true;
        LastBarTime = Time[0];
   }

...

   if (NewBarFlag == true)
   {
      if(AccountBalance()>10&&CountTrades()<1){Buy();}
      if(AccountBalance()>10&&CountTrades()<1){Sell();}
   }

I hope, this helps you.

Best regards

 
Werner Klehr:

Hi Jack,

one question: do you want one trade per bar or one trade per parabolic wave?
My solution handles one trade per parabolic wave.

Best regards

this is what i would i like