Databases In MQL4

 

Hi there... I need to make an EA that can retrieve data from several currency spesific charts(That I know how to do) and on another metatrader enter the trades based on the results on the metatrader hosting the EAs....

This might sound abit complicated but I was thinking an EA writes to a database file, and as soon as the 2nd metatrader gets the signal (which is also reading of that database file),and it enters the trades ...

So basicaly I want to know how can I get an EA to write to an external sort of database file as soon as the parameters i specify are fullfilled... and how another EA from another metatrader can read this and make a desicion from there to enter a trade...



The reason why I need two metatraders running at the same time is I run many many charts and this may tend to make metatrader lag abit and sometimes crash. So all analysis is done on one metatrader and orders done on another.

This whole process I am keen to automate now and I know how to program a robot to do this... its just getting the data into a database file and getting it read by another EA...

Hope you understand ...thanks!

I have not seen any code in MQL4 that works with any database coding...

 
 

You can use SQLite via DLL (you have to write a DLL): http://www.sqlite.org/

 
Just in case we have you going off and over-complicating things for yourself, let me ask one question:

Do you really need a relational database, or would a flat file be sufficient?

CB
 

Thanks guys great deal of help....


Awh I guess I could just use a text file...



but If your gona do something you might aswell do it properly and build it to last...
Knowing its going to come with a few headaches ha ha...!

In the end it's all worth it.

Thanks guys..!

 
If a flat file will do the job, then use a flat file.
There are native MQL functions to implement reading and writing flat files.
My advice is to use those, keep the complexity down and the result will be a more reliable, efficient and easy to implement solution.

I wrote some file read/write functions for someone a while back, hope they will help you:

https://www.mql5.com/en/forum/123129


Note that your EA will operate on files which reside in the experts/files folder (your EA residing in the experts folder).

CB
 

This is an Indicator that looks at the trend direction, now I want it to post the results to a .csv file. It manages to create a .csv file but does not post the data, can anyone see the problem here. The Indicator is working perfectly its probably something to do with the write function, I have marked all this codeing that has to do with writin the code to the file in Yellow.

Thanks in advance !!


#property copyright "Xlr8er"
#property link ""
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Yellow
#property indicator_color2 Red
//---- input parameters
extern int CountBars=400;
extern int SSP=7;
extern double Kmin=1.6;
extern double Kmax=50.6;
extern bool gAlert=True; // Switch to allow alerts
extern string ONE = "";


//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
bool gSellAlertGiven=false; // Used to stop constant alerts
bool gBuyAlertGiven=false; // Used to stop constant alerts
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(2);
SetIndexStyle(0,DRAW_LINE,0,1,DarkO range);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_LINE,0,1,Red);
SetIndexBuffer(1,ExtMapBuffer2);

return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);

}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{


int Handle, // File descriptor
Qnt_Symb; // Number of recorded symbols
string File_Name="EURUSD.csv"; // File name
string Erray[1,1];


Handle=FileOpen(File_Name,FILE_CSV| FILE_WRITE,";");//File opening
if(Handle==-1) // File opening fails
{
Alert("An error while opening the file. ",// Error message
"May be the file is busy by the other applictiom");
PlaySound("Bzrrr.wav"); // Sound accompaniment
return; // Exir start()
}
//--------------------------------------------------------------- 6 --
for(int c=0; c<=4; c++) // Cycle throughout the array
{
if(StringLen(Erray[c,0])== 0 || // If the value of the first or..
StringLen(Erray[c,1])== 0) // ..second variable is not entered
break; // .. then exit the cycle
Qnt_Symb=FileWrite(Handle,Erray[c,0],Erray[c,1]);//Writing to the file
if(Qnt_Symb < 0) // If failed
{
Alert("Error writing to the file",GetLastError());// Message
PlaySound("Bzrrr.wav"); // Sound accompaniment
FileClose( Handle ); // File closing
return; // Exit start()
}
}
//--------------------------------------------------------------- 7 --
//---------------------------------------------------------------


int i,
i2,
loopbegin,
counted_bars=IndicatorCounted();

double SsMax,
SsMin,
K,
val1,
val2,
smin,
smax,
price;

if (CountBars>=Bars){
CountBars=Bars;
}

SetIndexDrawBegin(0,Bars-CountBars+SSP);
SetIndexDrawBegin(1,Bars-CountBars+SSP);

if(Bars<=SSP+1){
return(0);
}

if(Bars<=SSP+1){
return(0);
}

//---- initial zero

if(counted_bars<SSP+1){
for(i=1;i<=SSP;i++) ExtMapBuffer1[CountBars-i]=0.0;
for(i=1;i<=SSP;i++) ExtMapBuffer2[CountBars-i]=0.0;
}

//+++++++-SSP
for(i=CountBars-SSP;i>=0;i--) {
SsMax = High[Highest(NULL,0,MODE_HIGH,SSP,i-SSP+1)];
SsMin = Low[Lowest(NULL,0,MODE_LOW,SSP,i-SSP+1)];
smin = SsMin-(SsMax-SsMin)*Kmin/100;
smax = SsMax-(SsMax-SsMin)*Kmax/100;
ExtMapBuffer1[i-SSP+6]=smax;
ExtMapBuffer2[i-SSP-1]=smax;
val1 = ExtMapBuffer1[0];
val2 = ExtMapBuffer2[0];

if (val1 > val2){

ONE = "buy";
Print (ONE);
Comment("buy",val1);
if( gAlert==true && gBuyAlertGiven==false){
PlaySound("alert.wav");
Alert("Buy signal at " + DoubleToStr(val1, 4) + " on " + Period() + " minute chart");
gBuyAlertGiven=true;
gSellAlertGiven=false;

Erray[0,0] = ONE ;
Qnt_Symb=FileWrite(Handle,Erray[0,0]);
}
}
if (val1 < val2){


ONE = "sell";
Print (ONE);


Comment("sell",val2);
if(gAlert==true && gSellAlertGiven==false){
PlaySound("alert.wav");
Alert("Sell signal at " + DoubleToStr(val2, 4) + " on " + Period()+ " minute chart");
gBuyAlertGiven=false;
gSellAlertGiven=true;

Erray[0,0] = ONE ;
Qnt_Symb=FileWrite(Handle,Erray[0,0]);
}
}
}



FileClose( Handle ); // File closing
Alert("The ",File_Name," file created.");// Message
PlaySound("Bulk.wav"); // Sound accompaniment



return(0);
}




Thanks again.

 

Yes I do realise I made sum dumb*ss mistakes but I wont lie I know nothing of programming to a exturnal file ...