different back-test-results of TimeCurrent() and iTime() in EA and indicator??? - page 2

 
gooly:

May the pips be with you :)

Gooly

Thank you,  but I prefer £s,  I can't spend pips  ;-)
 

I solved my long-standing problem helping out with this. It was how to use file to communicate between indicator and expert advisor {which is in testing mode}. Because of the file access limitation testing vs real, I kept thinking that the only way to solve the problem was dll. or post message. However the simple solution is just FileOpenHistory().

I created the f_function for these purposes. 1) Create as many [Global_Scope_Variables] without taking up memory. 2) Having my [Global_Scope_Variables] Restart && Shutdown resistant. Any comments on if it's achieving this would be appreciated. Also, if you like the idea feel free to use it :-) 

Expert Code:

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//FileSave Defines
#define Cur_Time 1
#define Siz_File 2
#define Byt_Size 8
#define Dou_Size 8
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){
    if(!IsTesting()){
        static bool Alert1; if(Alert1){return;}
        Alert("This Expert Is Compatable During Testing Only");
        Alert1=true; return;
    }datetime TimeCurrenti=TimeCurrent();
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //Example Of Passing Time Through GV When TimeAccess=1[Global]
    GlobalVariableSet("TimeCurrenti",TimeCurrenti);
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //Example Of Passing Time to iCustom When TimeAccess=2[Extern]
    int Buffer0=0; int BarShift=0;
    double Buffer0_Result=iCustom(Symbol(),Period(),
        "Gooly_Indicator",TimeCurrenti,Buffer0,BarShift);
    Print("TimeCurrenti="+TimeToStr(Buffer0_Result));
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //Example Of Passing Time With File When TimeAccess=3[File]
    f('w',Cur_Time,TimeCurrenti);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
double f(int Switch, int Cursor, double Info){
#property copyright "Copyright © Ubzen"
#property link      "https://www.mql5.com/en/users/ubzen"
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    static int F; static bool Opened;
    string N=WindowExpertName()+".txt";
    if(IsTesting()){N=WindowExpertName()+"_Test.txt";}
    if(Opened==false){
        if(IsTesting()){
            F=FileOpenHistory(N,FILE_BIN|FILE_WRITE);
            if(F>-1){FileClose(F); FileDelete(N);}
        }
        F=FileOpenHistory(N,FILE_BIN|FILE_READ|FILE_WRITE);
        if(F>-1){Opened=true; int Position;
            if(FileSize(F)==0){
                while(Position<Siz_File*Byt_Size){
                    FileWriteDouble(F,0,Byt_Size);
                    Position+=Byt_Size; FileFlush(F);
    }   }   }   }
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    FileSeek(F,Cursor*Byt_Size,SEEK_SET);
    if(Switch=='r'){
        return(FileReadDouble(F,Dou_Size));}
    if(Switch=='w'){
        FileWriteDouble(F,Info,Dou_Size);
        FileFlush(F);}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Indicator Code:

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#property   indicator_chart_window
#property   indicator_buffers 1
            double  Buffer0[];
extern      double  TimeCurrenti;
extern      string  TA_Choices="Below 1=Global, 2=Extern, 3=File";
extern      int     TimeAccess=3;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//FileSave Defines
#define Cur_Time 1
#define Siz_File 2
#define Byt_Size 8
#define Dou_Size 8
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void init(){SetIndexBuffer(0,Buffer0);}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void start(){
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if(TimeAccess==1){
        TimeCurrenti=GlobalVariableGet("TimeCurrenti");
        if(TimeCurrenti==0){return;}
        ObjectDelete("time");
        ObjectCreate("time",OBJ_TEXT,0,TimeCurrenti,High[0]);
        ObjectSetText("time",TimeToStr(TimeCurrenti),
                            10,"Arial Black",White);
    }
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if(TimeAccess==2){
        if(TimeCurrenti==0){return;}
        ObjectDelete("time");
        ObjectCreate("time",OBJ_TEXT,0,TimeCurrenti,High[0]);
        ObjectSetText("time",TimeToStr(TimeCurrenti),
                            10,"Arial Black",White);
    }
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    if(TimeAccess==3){
        TimeCurrenti=f('r',Cur_Time,NULL);
        Print("TimeCurrenti="+TimeCurrenti);
        if(TimeCurrenti==0){return;}
        ObjectDelete("time");
        ObjectCreate("time",OBJ_TEXT,0,TimeCurrenti,High[0]);
        ObjectSetText("time",TimeToStr(TimeCurrenti),
                            10,"Arial Black",White);
    }
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Buffer0[0]=TimeCurrenti;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
double f(int Switch, int Cursor, double Info){
    static int F; static bool Opened;
    //string N=WindowExpertName()+".txt";
    //if(IsTesting()){N=WindowExpertName()+"_Test.txt";}
    string N="Gooly_Expert_Test.txt";
    //Print("Opened="+Opened);
    if(Opened==false){
        /*if(IsTesting()){
            F=FileOpen(N,FILE_BIN|FILE_WRITE);
            if(F>-1){FileClose(F); FileDelete(N);}
        }*/
        F=FileOpenHistory(N,FILE_BIN|FILE_READ|FILE_WRITE);
        if(F>-1){Opened=true; int Position;
            if(FileSize(F)==0){
                while(Position<Siz_File*Byt_Size){
                    FileWriteDouble(F,0,Byt_Size);
                    Position+=Byt_Size; FileFlush(F);
    }   }   }   }
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    FileSeek(F,Cursor*Byt_Size,SEEK_SET);
    if(Switch=='r'){
        return(FileReadDouble(F,Dou_Size));}
    if(Switch=='w'){
        FileWriteDouble(F,Info,Dou_Size);
        FileFlush(F);}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Files:
 
ubzen:

I solved my long-standing problem helping out with this. It was how to use file to communicate between indicator and expert advisor {which is in testing mode}. Because of the file access limitation testing vs real, I kept thinking that the only way to solve the problem was dll. or post message. However the simple solution is just FileOpenHistory().

I created the f_function for these purposes. 1) Create as many [Global_Scope_Variables] without taking up memory. 2) Having my [Global_Scope_Variables] Restart && Shutdown resistant. Any comments on if it's achieving this would be appreciated. Also, if you like the idea feel free to use it :-) 

Expert Code:

Indicator Code:

 

Hi ubzen,

You really amazing! I like your diversified thoughts, seems you have found out all solutions to solve this issue!

I tried all 3 methods, 1 and 2 are simplest and easy to understand, 3 is some kinda comlicated,  because FILE function is a new thing for me =P

All I can suggest is maybe we can delete " Print("TimeCurrenti="+TimeCurrenti);" as below because it's duplicate and result is wrong.

    if(TimeAccess==3){
        TimeCurrenti=f('r',Cur_Time,NULL);
        //Print("TimeCurrenti="+TimeCurrenti);
        if(TimeCurrenti==0){return;}
        ObjectDelete("time");
        ObjectCreate("time",OBJ_TEXT,0,TimeCurrenti,High[0]);
        ObjectSetText("time",TimeToStr(TimeCurrenti),
                            10,"Arial Black",White);
    }

 

But I still have another question please, if we don't have original code of EA, how can we realize it?

For example, I have a visual simulator to manually place orders in tester, and I also have an indy to show how much time left in current bar, which needs information of Timecurrent(), yes of course it's useless in tester, but do you have any good suggestion to solve this problem? I appreciate your kind help very much!

 

to Gooly, thanks for openning this thread, I really learnt a lot!

 

Jane 

 
1105: But I still have another question please, if we don't have original code of EA, how can we realize it? 

For example, I have a visual simulator to manually place orders in tester, and I also have an indy to show how much time left in current bar, which needs information of Timecurrent(), yes of course it's useless in tester, but do you have any good suggestion to solve this problem? I appreciate your kind help very much!

Unfortunately, you wouldn't be able to use the methods I provided without the Expert codes. If you have the Indicator's code then the best I can think of is using Time[0]. You wouldn't be able to get time-of-every-tick. One-minute open times would be the closest resolution you can get by default [ unless you're using tick data ... then I dunno how it'll play out ].

Thanks for the kind words. Yeah that Print should have been Print("TimeCurrenti="+TimeToStr( TimeCurrenti )); [ to make it more reader friendly ]. It works if you look under the Terminal Journal > Expert Tab [ Does not work in Tester's Log -for Obvious reasons ]. That print was for my debugging and forgot to remove/format. The codes above were not optimized [lots of duplicates].

 
ubzen:

Unfortunately, you wouldn't be able to use the methods I provided without the Expert codes. If you have the Indicator's code then the best I can think of is using Time[0]. You wouldn't be able to get time-of-every-tick. One-minute open times would be the closest resolution you can get by default [ unless you're using tick data ... then I dunno how it'll play out ].

Thanks for the kind words. Yeah that Print should have been Print("TimeCurrenti="+TimeToStr( TimeCurrenti )); [ to make it more reader friendly ]. It works if you look under the Terminal Journal > Expert Tab [ Does not work in Tester's Log -for Obvious reasons ]. That print was for my debugging and forgot to remove/format. The codes above were not optimized [lots of duplicates].


There is a possibility, but it's a bit complicated.


Run the EA, save a detailed report, copy all trades to Excel of LibreOffice and create text-strings ready to copy like:

        
        DrawArrow("11in sell ",  D'2012.10.08 10:55', 1.29672, SYMBOL_LEFTPRICE,  Aqua);
        DrawArrow("11out sell ", D'2012.10.08 11:11', 1.29572, SYMBOL_RIGHTPRICE, Aqua);
        DrawArrow(...);
...

string DrawArrow(string n, datetime xTme, double xPrc, int typeArrow, color clr=DimGray, string dscr="") {
   if(ObjectFind(n) >= 0) ObjectDelete(n);
   ObjectCreate(n, OBJ_ARROW, 0, xTme, xPrc);
   ObjectSet(n, OBJPROP_ARROWCODE, typeArrow);
   ObjectSet(n, OBJPROP_COLOR, clr);
   ObjectSetText(n,dscr,10);
   return(n);
} 

Copy all these 'string-trades' into a new empty EA with all your time-Objects and run that on the same backtest-period.

You can as well create orders (in and out) instead of Arrows.

Hope it helps, Gooly

 

Gooly,

Sorry I don't quite catch the points, you said "Run the EA" , which EA? the EA of ubzen or the EA of my visual simulator?

 

Jane 

 

well yes you can but you don't need.

Create in the editor a new EA  and put in the start()-function all the lines with the arrows - may be like this so they are not drawn again with each new tick:

int start()   {
        
   static bool isSet=false;
        
//----

   if ( isSet == false ) { // drawn only once..
      isSet = true;
      DrawArrow("11in sell ",  D'2012.10.08 10:55', 1.29672, SYMBOL_LEFTPRICE,  Aqua);
      DrawArrow("11out sell ", D'2012.10.08 11:11', 1.29572, SYMBOL_RIGHTPRICE, Aqua);
      DrawArrow(...);
   }
   // here you can place you objects or just show the time by Comment:
   Comment(TimeToStr(TimeCurrent(),TIME_SECONDS));
   return(0);
}

// finally the draw-function:
string DrawArrow(string n, datetime xTme, double xPrc, int typeArrow, color clr=DimGray, string dscr="") {
   if(ObjectFind(n) >= 0) ObjectDelete(n);
   ObjectCreate(n, OBJ_ARROW, 0, xTme, xPrc);
   ObjectSet(n, OBJPROP_ARROWCODE, typeArrow);
   ObjectSet(n, OBJPROP_COLOR, clr);
   ObjectSetText(n,dscr,10);
   return(n);
} 

Now 'back-test' this EA. I'll draw the labels when the original EA has trades and it show the time. That's what you wanted - no?

The problem is the trades of the original EA are written to the report without seconds!

So need to slow down the speed and watch when Bid or Ask hits the price-lables.

Gooly

 

Ah, I know what you mean, that's really a creative idea.

But still have problem, because I want to manually trade with visual simulator and that means  I need to know how many seconds left FIRST , THEN place order.

But that's okay, I'm trying to find another simulator which can provide mq4 file.

 

Thanks all the same for all help you provided, very appreciate that!

 

Jane