help with sar EA

 

hi

here is my sar EA .... i want it execute once every new bar buy or sell

 


//---- Trades Limits
extern double    TakeProfit     = 500;  
extern double    StopLoss       = 420;    
extern double    TrailingStop   = 0;   
extern bool      SmartClose     = false; 

//---- Hour Trades

extern bool      UseHourTrade   = false;
extern int       FromHourTrade  = 8;
extern int       ToHourTrade    = 20; 

//---- Money Monagement

extern string    MM_Parameters  = "Money Management";
extern int       MaxTrades      = 1;
extern double    Lots           = 0.01;
extern bool      MM             = false, 
                 AccountIsMicro = true;
extern int       Risk           = 10;

//---- Global variables
int              ID             = 1098701;
int              ID2            = 1098702;
string           eaComment      = "sar_test";

//+------------------------------------------------------------------+
//|    Initialation function                                         |
//+------------------------------------------------------------------+

int init()
   {
    return(0);
   }

//+------------------------------------------------------------------+
//|    deinitialization function                                     |
//+------------------------------------------------------------------+

void deinit() 
    {
     Comment("bye");
    }
int orderscnt()
   {
    int cnt=0;
    for(int i =0;i<OrdersTotal();i++)
       {
        if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
          {
           if(OrderSymbol()==Symbol() && ID==OrderMagicNumber())
             {
              cnt++;
             }
          }
       }
     return(cnt);
   }

//+------------------------------------------------------------------+
//|    Start function                                                |
//+------------------------------------------------------------------+

int start()
   {        
    if(TakeProfit<10)
      {
       Print("TakeProfit less than 10");
          return(0);
      }  
    if(Bars<100)
      {
       Print("bars less than 100");
          return(0);  
      }
    if(MM) Lots = subLotSize();
    
    double sl, tp;
    int cnt, total;
    
    if (UseHourTrade)
      {
       if(!(Hour()>=FromHourTrade&&Hour()<=ToHourTrade))
         {
          Comment("Non-Trading Hours!");
               return(0);
         }
      }
 
    double   ali1, open,saqr1,open1;
   
   
       ali1= iSAR(NULL, 0, 0.02, 0.2,0);
       
       open= iOpen(Symbol(),0,0);
        
        saqr1 = iSAR(NULL, 0, 0.02, 0.2,0);
        
        open1 = iOpen(Symbol(),0,1);
       
    total=OrdersTotal();
    if(total<1) 
      {    
       if(AccountFreeMargin()<(1000*Lots))
         {
          Print("We have no money. Free Margin = ",
          AccountFreeMargin());
                         return(0);  
         } 
       if (ali1<open )   //---- Buy
         {
          if(orderscnt()<MaxTrades)
            {
             if(StopLoss==0)
               {sl=0;TrailingStop=0;}
             else
               {sl=Ask-StopLoss*Point;}
             if(TakeProfit==0)
               {tp=0;}
             else
               {tp=Ask+TakeProfit*Point;}
                OrderSend(Symbol(),OP_BUY,Lots,Ask,3,sl,tp,eaComment,ID,12,Green);
                PlaySound("Alert.wav");
               }
            }    
            
            if (saqr1<open1)   //---- Buy
         {
          if(orderscnt()<MaxTrades)
            {
             if(StopLoss==0)
               {sl=0;TrailingStop=0;}
             else
               {sl=Ask-StopLoss*Point;}
             if(TakeProfit==0)
               {tp=0;}
             else
               {tp=Ask+TakeProfit*Point;}
                OrderSend(Symbol(),OP_BUY,Lots,Ask,3,sl,tp,eaComment,ID,12,Green);
                PlaySound("Alert.wav");
               }
            }              
                      
       if (ali1>open )   //---- Sell
         {
          if(orderscnt()<MaxTrades)
            {
             if(StopLoss==0)
               {sl=0;TrailingStop=0;}
             else
               {sl=Bid+StopLoss*Point;}
             if(TakeProfit==0)
               {tp=0;}
             else
               {tp=Bid-TakeProfit*Point;}
                OrderSend(Symbol(),OP_SELL,Lots,Bid,3,sl,tp,eaComment,ID2,12,Red); 
                PlaySound("Alert.wav");
               }
            }
            
            if (saqr1 > open1)   //---- Sell
         {
          if(orderscnt()<MaxTrades)
            {
             if(StopLoss==0)
               {sl=0;TrailingStop=0;}
             else
               {sl=Bid+StopLoss*Point;}
             if(TakeProfit==0)
               {tp=0;}
             else
               {tp=Bid-TakeProfit*Point;}
                OrderSend(Symbol(),OP_SELL,Lots,Bid,3,sl,tp,eaComment,ID2,12,Red); 
                PlaySound("Alert.wav");
               }
            }
            
            
            
        return(0);
      }
//---------- Trailing Stop 
for(cnt=0;cnt<total;cnt++)
  {               
   OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
   if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) 
     {
      if(OrderType()==OP_BUY)
        {
         if(TrailingStop>0)  
           {                 
            if(Bid-OrderOpenPrice()>Point*TrailingStop)
              {
               if(OrderStopLoss()<Bid-Point*TrailingStop)
                 {
                  OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()+Point*TrailingStop,OrderTakeProfit(),0,Green);
                           return(0);
                 }
              }
           }
   else
     {
      if(TrailingStop>0)  
        {                 
         if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
           {
            if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
              {
               OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()-Point*TrailingStop,OrderTakeProfit(),0,Red);
                        return(0);
              }
           }
        }
     }
        }
     }
  }
//---------- Smart Close
if (SmartClose)
  {      
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) 
        {
         if(OrderType()==OP_BUY)
           {
            if(ali1<open)   //---- Cloes Buy
              {
               OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
                       return(0);
              }
           }
         else
           {
            if(ali1>open)   //---- Close Sell
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
                       return(0);
              }
           }
        }
     }
  }
    return(0);
   }
   
//+------------------------------------------------------------------+
//|    Money Management                                              |
//+------------------------------------------------------------------+
  
double subLotSize()
    {
     double lotMM = MathCeil(AccountFreeMargin() *  Risk / 1000) / 100; 
          if(AccountIsMicro==false)
            {
             if(lotMM < 0.1)                  lotMM = Lots;
             if((lotMM > 0.5) && (lotMM < 1)) lotMM = 0.5;
             if(lotMM > 1.0)                  lotMM = MathCeil(lotMM);
             if(lotMM > 100)                  lotMM = 100;
            }
          else
            {
             if(lotMM < 0.01)                 lotMM = Lots;
             if(lotMM > 1.0)                  lotMM = MathCeil(lotMM);
             if(lotMM > 100)                  lotMM = 100;
            }
           return (lotMM);
    }
      
//------------------------------------------------------------------------------------- The End.
 
Checkout the Newbar function Here.
 
The problem with the newbar function is that it can be called only once per tick and fails there after.
int start(){
   static datetime Time0;
   if (Time0 == Time[0]) return(0); Time0 = Time[0];
   // once per bar...
Reason: