Excellink problem with handle on 2nd pass

 

Trying to access data from Excel file using Excellink. All OK at first Tick when indicator dropped onto chart ie reads and writes to file but when I later try to access Excellink reads only 0 data value.

Problem appears to be Handle which on 1st pass = 0 but on subsequent accesses = -1. Error code is 4101 in both cases ie "Wrong File Name". Appears I need to delete original handler in some way.

I have included some of the code the selection variable being highlighted (XLLnk = 0;)

Would be very interested if someone had this problem before and has an answer.

Thanks in anticipation

John W

#property copyright "Copyright 2012, No Emotion Trading"
#property link ""

#property indicator_chart_window
#import "excellink.dll"
int ExcelStart(string,int);
int ExcelPutValue(int,int,int,double);
int ExcelPutString(int,int,int,string);
int ExcelPutCalc(int,int,int,string); 
double ExcelGetValue(int,int,int);
string ExcelGetString(int,int,int);
string ExcelGetCalc(int,int,int,string);
int ExcelSaveFile(string);
int ExcelEnd();
int ExcelClose();
int ExcelOpen();
#import

string GetSymbolName;
string PivDir;
double PivVal;

#property indicator_buffers 8
#property indicator_color2 Red
#property indicator_color4 CornflowerBlue
#property indicator_color3 Magenta 
#property indicator_color5 Yellow 
extern string MA_Mode_Types="0=SMA; 1=EMA; 2=SMMA; 3=LWMA;";
extern int MA1MFF=5; // MA1FF Yellow
extern int MA1MFF_Mode=3; 
extern int MA1MF=21; // MA1MF RED 
extern int MA1MF_Mode=3;
extern int MA1MS=144; //MA1MS CornflowerBlue 233
extern int MA1MS_Mode=3;
extern int MA1MSS=233; // MA1MSS Magenta 377
extern int MA1MSS_Mode=3; 
extern int iTimeFrame = PERIOD_M1; //Moving Average Timeframe
int Handle; 
int Scan_bars_Dn=0,Scan_bars_Up=0;
int iBarsPerPeriod;
int i, counter;
int SegPiv;
int SegTestPiv;
double MA1MF_now, MA1MS_now, MA1MF_prv, MA1MS_prv, MA1MF_aft, MA1MS_aft, MA1MSS_now, MA1MFF_now;
double SegPivH,SegPivL, Seg45H, Seg45L, Seg90H, Seg90L, Seg135H, Seg135L, Seg180H, Seg180L, Seg225H, Seg225L, Seg315H, Seg315L, Seg360H, Seg360L;
double Buf_0[],Buf_1[],Buf_2[],Buf_3[],Buf_4[],Buf_5[]; // Declaring indicator arrays
string TRADE;
bool Seg180HActive, Seg180LActive;

int Ticks = 1;
int CurTickNosgv;
string TicksName = "GV_TicksName"; ///string Quantity = "GV_Quantity";
double PivLevL;
double SegPivgv;
double Seg135gv;
double Seg180gv;
double Seg225gv;
double Seg315gv;
double Seg360gv; 

// |=========================================|
// | Indicator initialization function |
// |=========================================|
int init() 
{ 
Ticks = GlobalVariableGet(TicksName); 
Ticks = Ticks;
GlobalVariableSet(TicksName,Ticks); 

GlobalVariableSet(SegPivgv, 1);
GlobalVariableSet(Seg135gv, 1);
GlobalVariableSet(Seg180gv, 1);
GlobalVariableSet(Seg225gv, 1);
GlobalVariableSet(Seg315gv, 1);
GlobalVariableSet(Seg360gv, 1);


return(0);
} 
// |========================================|
// | Indicator deinitialization function |
// |========================================|
int deinit() 
{ 
if (CurTickNosgv >1)
GlobalVariableDel(TicksName); 
else
GlobalVariableSet(TicksName,Ticks); 

ExcelEnd();
ObjectsDeleteAll();
return(0);
} 
// |=================================|
// | Indicator start function |
// |=================================|
int start()
{
//===================================// 
// --- Excellink Transfer Select --- // 

{
int ExcelCom, ExcelCom1; 
int XLLnk = 0; /// Set XLLnk to = 1 for individual Access

CurTickNosgv = GlobalVariableGet( TicksName); 

//For Cold Start and Random access to Excel Link (XLLnk = 1). 
{
if ((CurTickNosgv < 1) || (XLLnk == 1 && CurTickNosgv < 1)) ExcelCom(); 
Ticks = (Ticks +1);
GlobalVariableSet(TicksName,Ticks); 
Alert("After Cold Start and XLLnk = 1 Ticks = ", CurTickNosgv);
}

return(0);
} 
} 

// =================================================================================================================================================== //
// =================================================================================================================================================== // 

//===================================================================================// 
//--- Excellink Calc Segment Levels Call Write Com and Draw Segment level lines' ---// 

void ExcelCom() 

{ 
string PivDir = "L";
double PivVal = 1.0149;
string filename = "C:\\Users\\Owner\\Documents\\Seg Table MQL4.xls";
int iErrorCode;

///int Handle = 0;
Handle = ExcelStart(filename,1); 
Alert("Handle ",Handle); ///When Handle = -1 means "Wrong File Name" !!!!
iErrorCode = GetLastError(); 
Alert("iErrorCode ",iErrorCode); 


ExcelPutString(1,5,3,PivDir); //Write to Excel
ExcelPutValue(1,6,4,PivVal); //Write to Excel
{ 
string OneShot =ExcelGetCalc(1,7,3,"Symbol:" +GetSymbolName);

//---Get Gann Segment Values' from Segment Table---//
string OneShotVal = OneShot;

Alert("OneShotVal Before if ExcelLink = ",OneShotVal); 

if (OneShot == OneShotVal)

Alert("OneShot After if ExcelLink = ",OneShot); 

//---Read string from Excel and convert to int and display
string SegPivS = ExcelGetCalc(1,9,4,"Symbol:" +GetSymbolName); 
double SegPiv = StrToDouble(SegPivS); 
SegPivgv = SegPiv;
Alert("SegPivgv ExcelLink = ",SegPivgv);

string Seg135S =ExcelGetCalc(1,9,5,"Symbol:" +GetSymbolName); 
double Seg135=StrToDouble(Seg135S); 
Seg135gv = Seg135; 
Alert("Seg135gv ExcelLink = ",Seg135gv);

string Seg180S = ExcelGetCalc(1,9,6,"Symbol:" +GetSymbolName);
double Seg180 = StrToDouble(Seg180S);
Seg180gv = Seg180;
Alert("Seg180gv ExcelLink = ",Seg180gv);

string Seg225S =ExcelGetCalc(1,9,7,"Symbol:" +GetSymbolName);
double Seg225 = StrToDouble(Seg225S); 
Seg225gv = Seg225; 
Alert("Seg225gv ExcelLink = ",Seg225gv);

string Seg315S =ExcelGetCalc(1,9,8,"Symbol:" +GetSymbolName);
double Seg315 = StrToDouble(Seg315S); 
Seg315gv = Seg315; 
Alert("Seg315gv ExcelLink = ",Seg315gv);

string Seg360S =ExcelGetCalc(1,9,9,"Symbol:" +GetSymbolName);
double Seg360 = StrToDouble(Seg360S); 
Seg360gv = Seg360; 
Alert("Seg360gv ExcelLink = ",Seg360gv);


FileWriteCom(); ///Call FileWriteCom

return(0);
}
}
//+----------------------+// 

void FileWriteCom()

{
string sString;
string sFileName ="NewsMine.csv";
int iErrorCode;
int iInteger;
double dDouble;
int iDecimalPlaces;

int iHandle = FileOpen(sFileName,FILE_CSV|FILE_WRITE);
if(iHandle < 1)
{
iErrorCode = GetLastError();
return(false);
}

sString = (SegPivgv+","+Seg135gv+","+Seg180gv+","+Seg225gv+","+Seg315gv+","+Seg360gv);
Alert("Write sString = ",sString);
FileWrite(iHandle,iInteger,sString);

// === Integration of Draw Lines

PivLevL = SegPivgv/ 10000;
Seg135L = Seg135gv/ 10000;
Seg180L = Seg180gv/ 10000;
Seg225L = Seg225gv/ 10000;
Seg315L = Seg315gv/ 10000;
Seg360L = Seg360gv/ 10000;

ObjectCreate("PivLevLLine",OBJ_HLINE,0,0,PivLevL);
ObjectSet("PivLevLLine",OBJPROP_COLOR,Red);
ObjectSet("PivLevLLine",OBJPROP_WIDTH,2);
ObjectSet("PivLevLLine",OBJPROP_STYLE,STYLE_DOT); 
ObjectCreate("Pivot Level",OBJ_LABEL,0,0,0,0,0);
ObjectSet("Pivot Level",OBJPROP_XDISTANCE,1480);
ObjectSet("Pivot Level",OBJPROP_YDISTANCE,20);
ObjectSetText("Pivot Level","Pivot_Level",10,"Tahoma",Red); 

ObjectCreate("Seg135Line",OBJ_HLINE,0,0,Seg135L);
ObjectSet("Seg135Line",OBJPROP_COLOR,White);
ObjectSet("Seg135Line",OBJPROP_WIDTH,1);
ObjectSet("Seg135Line",OBJPROP_STYLE,STYLE_DOT);
ObjectCreate("Seg135",OBJ_LABEL,0,0,0,0,0);
ObjectSet("Seg135",OBJPROP_XDISTANCE,1480);
ObjectSet("Seg135",OBJPROP_YDISTANCE,40);
ObjectSetText("Seg135","Seg_135",8,"Tahoma",White); 

ObjectCreate("Seg180Line",OBJ_HLINE,0,0,Seg180L);
ObjectSet("Seg180Line",OBJPROP_COLOR,LightBlue);
ObjectSet("Seg180Line",OBJPROP_WIDTH,1);
ObjectSet("Seg180Line",OBJPROP_STYLE,STYLE_DOT);
ObjectCreate("Seg180",OBJ_LABEL,0,0,0,0,0);
ObjectSet("Seg180",OBJPROP_XDISTANCE,1480);
ObjectSet("Seg180",OBJPROP_YDISTANCE,60);
ObjectSetText("Seg180","Seg_180",8,"Tahoma",LightBlue);

ObjectCreate("Seg225Line",OBJ_HLINE,0,0,Seg225L);
ObjectSet("Seg225Line",OBJPROP_COLOR,CornflowerBlue);
ObjectSet("Seg225Line",OBJPROP_WIDTH,1);
ObjectSet("Seg225Line",OBJPROP_STYLE,STYLE_DOT);
ObjectCreate("Seg225",OBJ_LABEL,0,0,0,0,0);
ObjectSet("Seg225",OBJPROP_XDISTANCE,1480);
ObjectSet("Seg225",OBJPROP_YDISTANCE,80);
ObjectSetText("Seg225","Seg_225",8,"Tahoma",CornflowerBlue);

ObjectCreate("Seg315Line",OBJ_HLINE,0,0,Seg315L);
ObjectSet("Seg315Line",OBJPROP_COLOR,Gold);
ObjectSet("Seg315Line",OBJPROP_WIDTH,1);
ObjectSet("Seg315Line",OBJPROP_STYLE,STYLE_DOT);
ObjectCreate("Seg315",OBJ_LABEL,0,0,0,0,0);
ObjectSet("Seg315",OBJPROP_XDISTANCE,1480);
ObjectSet("Seg315",OBJPROP_YDISTANCE,100);
ObjectSetText("Seg315","Seg_315",8,"Tahoma",Gold);

ObjectCreate("Seg360Line",OBJ_HLINE,0,0,Seg360L);
ObjectSet("Seg360Line",OBJPROP_COLOR,Yellow);
ObjectSet("Seg360Line",OBJPROP_WIDTH,2);
ObjectSet("Seg360Line",OBJPROP_STYLE,STYLE_DOT);
ObjectCreate("Seg360",OBJ_LABEL,0,0,0,0,0);
ObjectSet("Seg360",OBJPROP_XDISTANCE,1480);
ObjectSet("Seg360",OBJPROP_YDISTANCE,120);
ObjectSetText("Seg360","Seg_360",8,"Tahoma",Yellow);

ExcelClose(); 
ExcelEnd();


}
//+------------------------------------------------------------------+


 

I edit your code using SRC button, next time use SRC button to post the codes.

 
I know this is almost a year old, but it is so hard to find anything about ExcelLink. Maybe I can help you or someone else. I found that if ExcelStart was accessed more than once that I would get -1. I put ExcelStart in my int init() section and my ExcelEnd, ExcelSave and ExcelClose in my int deinit() section. I know it seems like you don't want to open the Excel file until the last moment, or you do want save it right away, or mainly you think it is needed at the start of each section where ExcelLink is used, but it just doesn't seem to work well that way. So, my Excel opens as soon as the EA starts. I will try using ExcelSave more often as long as there are no problems. Right now it seems to work flawlessly. Before I had to close MT4 and Excel after each trade/Excel session.