EA which will take only one trade at a time ?

 

Hello members,

Looking for a help for making an EA.

I have Ea which opens buy order if last candle closes above Trade level.

It opens sell order if last candle closes below the trade level.

Suppose I run this on an hourly chart, then every time this condition satisfies it opens buy or sell order on each hour. But I want only one trade to open.

There should not be any new order until current buy/sell order get closed?

How can I do that?

Plus in the code below..it takes only buy order no sell order.. 

  // Orders-----------------------------in start function
      if(TimeCurrent() == Dat_DtTm)
      {
       double T1 = L1;
       
       //---Buy Orders//------
    
       if(Close[1]> T1 ) BC();
        
       //---Sell Order //---------
        
       if(Close[1]< T1 ) SC();    
        
      }

//------------------------------------------------------------
void BC()
{
       double BuyStopLoss = Ask - (StopLoss * CalcPoint1);
       double BuyTakeProfit = Ask + (TakeProfit *  CalcPoint1);
       BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green); 
       return;
}

void SC()
{
       double SellStopLoss = Bid - (StopLoss * CalcPoint1);
       double SellTakeProfit = Bid + (TakeProfit *  CalcPoint1);
       SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Order",MagicNumber,0,Red); 
       return;
}

void OnTick(){
   static datetime timeCur;
   datetime timePre = timeCur; timeCur=Time[0]; 
   bool isNewBar = timeCur != timePre;
   
   if(isNewBar){ 
     timePre=timeCur;  //Do what you want at each new bar // Once per bar
     BC();
     SC();
    } 
     
    return; //every tick
   
 
       double SellStopLoss = Bid - (StopLoss * CalcPoint1);
       double SellTakeProfit = Bid + (TakeProfit *  CalcPoint1);

Swap the + and -

 
GumRai:

Swap the + and -



Thank you for rectifying it.

I changed it but now its creating one buy & one sell order on each bar from same level?

 
 bool can_buy=true;
 bool can_sell=true;
 for(int x=OrdersTotal()-1;x>=0;x--)
    {
    if(OrderSelect(x,SELECT_BY_POS) && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
          {
          if(OrderType()==OP_BUY)
             can_buy=false;
          if(OrderType()==OP_SELL)
             can_sell=false;
          }
    }
  
  if(can_buy)
     {
     //Check whether to place order
     }
     
  if(can_sell)
     {
     //Check whether to place order
     }

It is not clear what you want. The above code or modification of it should help you.


  // Orders-----------------------------in start function

Do you have both start() and OnTick in your code??

 
cashcube: But I want only one trade to open. There should not be any new order until current buy/sell order get closed? How can I do that?
Then you must check if there is already an open order. Search "OrderSelect loop."
 
GumRai:

It is not clear what you want. The above code or modification of it should help you.


Do you have both start() and OnTick in your code??



Thank GumRai for sharing the code. I will apply it today..

But yes I have both start & Ontick function in my code..I really confuse about the ontick function...Previously I added buy sell orders inside the start() function..but was creating buy sell order on each tick...That's why I created separated ontick function .

 
I don't know what conflicts may occur by having both. In my opinion it is best just to use OnTick
 
GumRai:
I don't know what conflicts may occur by having both. In my opinion it is best just to use OnTick

In the start function it need to run tick by tick cause it fetches data from a csv file.

I am here sharing the whole code..please help me to make it working...Currently it stuck after taking one trade.

int init()
{

int CalcDigits = (int)MarketInfo(Symbol(),MODE_DIGITS);

if(CalcDigits == 2 || CalcDigits == 4) CalcSlippage = SlippagePips;
else if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10;      


if(CalcDigits == 2 || CalcDigits == 3) CalcPoint1 = 0.01;
else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint1 = 0.0001;
      
UsePoint = CalcPoint;
UseSlippage = (int) CalcSlippage; 
return (0);
}
//-------------------------------------------------------------------
int start()                            // Spec. function start()
  {
//--------------------------------------------------------------- 2 --
   int Handle;                          // File descriptor
   string File_Name="H.csv",          // Name of the file            
          D1,                           // Data           
          Str_DtTm;                    // Date and time of the event (line)
   datetime Dat_DtTm;                  // Date and time of the event (date)
   double  DD1;      // Data Converted 
//--------------------------------------------------------------- 3 --
   Handle=FileOpen(File_Name,FILE_CSV|FILE_READ,",");// File opening
   if(Handle<0)                        // File opening fails
     {
      if(GetLastError()==4103)         // If the file does not exist,..
         Alert("No file named ",File_Name);//.. inform trader
      else                             // If any other error occurs..
         Alert("Error while opening file ",File_Name);//..this message
      PlaySound("Bzrrr.wav");          // Sound accompaniment
      return(0);                          // Exit start()      
     }
//--------------------------------------------------------------- 4 --
   while(FileIsEnding(Handle)==false)  // While the file pointer..
     {                                 // ..is not at the end of the file
      //--------------------------------------------------------- 5 --
      Str_DtTm =FileReadString(Handle);// Date and time of the event (date)
      D1     =FileReadString(Handle);     
      
      if(FileIsEnding(Handle)==true)   // File pointer is at the end
         break;                        // Exit reading and drawing
      //--------------------------------------------------------- 6 --
      Dat_DtTm =StrToTime(Str_DtTm);   // Transformation of data type
           
      DD1 = floor(StrToDouble(D1)); 
          
      //------------------------------------------------------ Pricing    
     
      double CalcDigits = MarketInfo(Symbol(),MODE_DIGITS);
      {
      if(CalcDigits == 2 || CalcDigits == 3) CalcPoint = 100;
      else if (CalcDigits == 4 || CalcDigits == 5) CalcPoint = 10000;
      }
      
      double j =  iOpen(Symbol(), PERIOD_D1, iBarShift(Symbol(),PERIOD_D1, Dat_DtTm));
      double SK = floor((36*Scale));
      double k = floor(round((j*CalcPoint))/SK);
     
      double L1 = ((k * SK)+ floor((DD1 * Scale)))/CalcPoint;
 
     
  // Orders-----------------------------
      if(TimeCurrent() == Dat_DtTm)
      {
       double T1 = L1;
       
       
       for(int x=OrdersTotal()-1;x>=0;x--)
      {
         if(OrderSelect(x,SELECT_BY_POS) && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
          {
          if(OrderType()==OP_BUY)
             can_buy=false;
          if(OrderType()==OP_SELL)
             can_sell=false;
          }
      }
  //----------------------------------------
     if(can_buy)
     {
     
     if(Close[1]> T1 ) 
       
       {
       
       double BuyStopLoss = Ask - (StopLoss * CalcPoint1);
       double BuyTakeProfit = Ask + (TakeProfit *  CalcPoint1);
       BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green); 
       
       }
     
     
     }
 //--------------------      
    if(can_sell)
     {
    
       if(Close[1]< T1 ) 
       
       {
       double SellStopLoss = Bid + (StopLoss * CalcPoint1);
       double SellTakeProfit = Bid - (TakeProfit *  CalcPoint1);
       SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Order",MagicNumber,0,Red);     
       
       }   
    
     }
  ///------------------------          
        
      }
//-----------------end of orders
           
      }
    
    //--------------------------------------------------------------- 8 --
   FileClose( Handle );                // Close file
   PlaySound("bulk.wav");              // Sound accompaniment
   WindowRedraw();                     // Redraw object
   return(0);                             // Exit start()
  }

//------------------------------------------------------------
 
cashcube:

In the start function it need to run tick by tick cause it fetches data from a csv file.

I am here sharing the whole code..please help me to make it working...Currently it stuck after taking one trade.

 

You're fortunate that it takes 1 trade

if(TimeCurrent() == Dat_DtTm)

If there are irregular ticks this may never be true.

If it is true and a trade is opened, it will not be true again unless the value is changed in the file. So no 2nd trade opened.

 
GumRai:

You're fortunate that it takes 1 trade

if(TimeCurrent() == Dat_DtTm)

If there are irregular ticks this may never be true.

If it is true and a trade is opened, it will not be true again unless the value is changed in the file. So no 2nd trade opened.

I added that code because on my CSV file there are two column: One is Date & 2nd one is Data (with which we will be calculating level for entries).

If Current date get matches with with CSV data "Date column" then it takes the "Data" & calculate the level for trade entries & trades it.

Then how can I do this?

Thanks.. 

 

Any Highlight on this thread...?

I declared  them locally. 

bool can_buy=true;
 bool can_sell=true;

How to compare two dates on mql4. Current date with the date fetched from csv file? 

Reason: