help with code. stop loss varying on each new trade

To add comments, please log in or register
Inception12
45
Inception12  
Hello, Some help needed on the code below. The EA gives me a different stop loss on each trade yet i want to be fixed at 10pips for the 1hr timeframe. I dont know if it's the sleep function causing it or it's something else.
input double MaximumRisk   =0.012;
//--- parameters for data reading
input string InpFileName="decision1hr.csv"; // file name
input string InpDirectoryName="Data"; // directory name
int BarsCount = 0;
double Lots=1.05;
datetime old_bar = 0;
int Ticket;



int open()
  {
//--- open the file
 
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ);
   {
   
      Alert("%s file is available for reading",InpFileName);
      Alert("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      //--- additional variables
      int    str_size;
      int decision;
      //--- read data from the file
      while(!FileIsEnding(file_handle))
        {
         //--- find out how many symbols are used for writing the time
         str_size=FileReadInteger(file_handle,INT_VALUE);
         //--- read the string
         decision=FileReadString(file_handle,str_size);
         //--- print the decision
       Alert(decision);
      
       
       double SL = 100;
       int TP=30000;
       //--- trade conditions
       RefreshRates();
       if(decision==1) 
       {Alert("BUY"); Ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,2,Ask-SL*Point,Ask+TP*Point);}
        else 
        if (decision==2)
        {Alert("SELL"); Ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,2,Bid+SL*Point,Bid-TP*Point);}
       }
       //--- trade conditions
         if(Ticket<0)
       {
      Alert("OrderSend failed with error #",GetLastError());
     }
   else
      Alert("OrderSend placed successfully");
     }
       
      //--- close the file
      FileClose(file_handle);
      Alert("Data is read, %s file is closed ",InpFileName);
     }
 

int close()  
{
    for (int i = OrdersTotal() - 1; i >= 0; i--) 
      {
        if(OrderSelect(i,SELECT_BY_POS) == true) 
        {
          int ticket=OrderTicket();
          if(OrderType()==OP_BUY)          
          OrderClose(ticket,Lots,Bid,3);
          else
          OrderClose(ticket,Lots,Ask,3);

        }
      }
}
 
  
int start()
{
  if (Bars > BarsCount)
{   
close();
Sleep(90000);    // interval
open();
}

  BarsCount = Bars;
}
Carl Schreiber
Moderator
8846
Carl Schreiber  

You buy at Ask and Sell at Bid and you exit a buy at Bid and exit a sell at Ask.

So may be this solves the problem?

       {Alert("BUY"); Ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,2,Bid-SL*Point,Bid+TP*Point);}
        else 
        if (decision==2)
        {Alert("SELL"); Ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,2,Ask+SL*Point,Ask-TP*Point);}
BTW mql4/mt4 questions belong the the thread at the bottom just for mt4/mql4!
Inception12
45
Inception12  
Carl Schreiber:

You buy at Ask and Sell at Bid and you exit a buy at Bid and exit a sell at Ask.

So may be this solves the problem?

BTW mql4/mt4 questions belong the the thread at the bottom just for mt4/mql4!

Alright, thanks for this correction. Another issue am having is whenever i restart mT4, the EA restarts and open a position, yet i want it to only opne positions at the hour change. How can i sort that one

William Roeder
20214
William Roeder  
EAs must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV+flush or files) of ticket numbers required.
To add comments, please log in or register