Help with code for bar change

To add comments, please log in or register
Inception12
40
Inception12  

Hello traders, kindly assist me with the following code.

Am trying to open a trade based on a decision read from a csv. I want to do this on every bar change but the code doesnt seem to be changing with bar changes very well. Kindly help.



//--- parameters for data reading

input string InpFileName="DECISION.csv"; // file name

input string InpDirectoryName="Data"; // directory name

int BarsCount = 0;

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

 {


  if (Bars > BarsCount)

  {


//--- open the file

   ResetLastError();

   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;

      string str;

      //--- 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

         str=FileReadString(file_handle,str_size);

         //--- print the string

       Alert(str);

        }

      //--- close the file

      FileClose(file_handle);

      Alert("Data is read, %s file is closed",InpFileName);

     }

  

  

 BarsCount = Bars;

  }

 

  return(0);

}


Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is developing along with you. Indicators: Volume Average percent...
Eleni Anna Branou
Moderator
6565
Eleni Anna Branou  

Please use the </> button to insert your code.


Snelle Moda
660
Snelle Moda  

It's simple but effective.


// Rates structure array voor prijs data van de laatste 2 minuten.  
   MqlRates Realtime_Data[2];  
   
// Rates structure voor de intraday data.               
   CopyRates(Symbol(), PERIOD_M1, 0, 2, Realtime_Data);

   static double   dBar_Open;     
   static double   dBar_High;
   static double   dBar_Low;
   static double   dBar_Close;
   static long     lBar_Volume;
   static datetime nBar_Time;

// Boolean for new BAR check.
   bool bStart_NewBar = false;

// Check if the new price is different compared to the old price.   
   if(Realtime_Data[0].open != dBar_Open || Realtime_Data[0].high != dBar_High || Realtime_Data[0].low != dBar_Low || Realtime_Data[0].close != dBar_Close || Realtime_Data[0].tick_volume != lBar_Volume || Realtime_Data[0].time != nBar_Time || prev_calculated == 0)
         {
         bStart_NewBar = true; // Update new BAR check.        
     
         dBar_Open   = Realtime_Data[0].open;      
         dBar_High   = Realtime_Data[0].high;
         dBar_Low    = Realtime_Data[0].low;
         dBar_Close  = Realtime_Data[0].close;                 
         lBar_Volume = Realtime_Data[0].tick_volume;
         nBar_Time   = Realtime_Data[0].time;
         }
         
   if(bStart_NewBar == true)
         {
        
        
// Your code:         
         
         
         }         
         
Inception12
40
Inception12  
//+------------------------------------------------------------------+
//|                                                        bars4.mq4 |
//|                                                             Mine |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Mine"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//--- parameters for data reading
input string InpFileName="DECISION.csv"; // file name
input string InpDirectoryName="Data"; // directory name
int BarsCount = 0;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
 {

  if (Bars > BarsCount)
  {

//--- open the file
   ResetLastError();
   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;
      string str;
      //--- 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
         str=FileReadString(file_handle,str_size);
         //--- print the string
       Alert(str);
       int Ticket;
       double Lots=0.05;
       double SL = 20;
       double TP = 50;
       //--- sell conditions
       if (str=="SELL")
      {
       Ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,2,SL,TP);
       return(0);
       }
       //--- buy conditions
   if(str=="BUY")
     {
      Ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,SL,TP);
      return(0);
         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);
     }
  
  
 BarsCount = Bars;
  }
 
  return(0);
}

Haah,got it right this time. Sorry about that misposting. It wont happen again
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is developing along with you. There certainly is a Holy Grail! It is...
Inception12
40
Inception12  
I've tried as directed but it didnt work out as i expected. Thanks for pointing that out. Kindly help me find out what's wrong with my code. It returns the decsion, Whether sell or buy but it's not executing the decision.
whroeder1
17943
whroeder1  
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2.  double SL = 20;
    double TP = 50;
           Ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,2,SL,TP);
    Check your return codes for errors, report them and you would know why. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

Eleni Anna Branou
Moderator
6565
Eleni Anna Branou  

So, you don't learn !!!

Please use the </> button to insert your code in every post!


Inception12
40
Inception12  
whroeder1:
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Check your return codes for errors, report them and you would know why. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

The compiler is not showing any errors nor warnings. I've tweaked the code multiple times and still nothing
whroeder1
17943
whroeder1  
Inception12: The compiler is not showing any errors nor warnings. I've tweaked the code multiple times and still nothing
  1. Means nothing. That means the code runs. A car with four flat tires runs.
  2. You don't know what's wrong, to tweaking means nothing. What part of #7.2 was unclear?
Inception12
40
Inception12  
whroeder1:
  1. Means nothing. That means the code runs. A car with four flat tires runs.
  2. You don't know what's wrong, to tweaking means nothing. What part of #7.2 was unclear?
I understood 7.2 but it's not solving my problem. there are no erros and the log shows that the ea was completed succefully
whroeder1
17943
whroeder1  
Inception12: I understood 7.2 but it's not solving my problem.

It can't "solve your problem," because you didn't do it. You don't know where the problem is, so you can't even begin to know what the problem is.

To add comments, please log in or register