| / | Articles |
Articles
Tester
Break Through The Strategy Tester Limit On Testing Hedge EA
To post a new article, please log in or register
|
Break Through The Strategy Tester Limit On Testing Hedge EA [ ru ]IntroductionAn idea of testing hedge EA in the strategy tester will be given in this article. As you know, the strategy tester has its own limit unabling to open any order for another symbol. Every user that would like to test his/her own hedge Expert Advisors needs to test it/them live only. But does this limit our capabilities? I am sure that every hedge trader needs to test his own EA before trading live. So, I am giving the idea of generating a virtual strategy testing behavior (tester-like) to you all with hope that it can help us break through the mt4 strategy tester limit and hope that it will useful for further usage. The Concept Of Virtual TesterThe idea of a virtual tester appeared in my head while working with "Files" function in mq4. An idea of taking some important data from a file to set a virtual trading scheme has come out of my brain, "Can this be an answer of testing my hedge EA ?" Let's try it out. My virtual tester does not need any other external program or software. Everything can be done by means of mq4 parameters. The concept of this virtual tester is to let the indicated parameters of opening or closing hedge orders tell us to collect the necessary data. Such as opening price, opening time, closing price, closing time and all other important data. After the necessary data is set, they will be used for comparing with the latest tick value of any compatible type, such as open price and last Bid, or open price and last Ask. Such values can lead us to the profit calculation method that will guide us to collect a new group of data after they meet the closing hedge condition. These groups of data will be exported to a file for the further usage. After the testing is done and all data types are collected to a file(s), we will be able to see "how the hedge EA is performed ". By getting the data from these files to plot them as an indicator of the performance's curve, I think we can finish the virtual testing of our hedge EA. By this concept, I assume that we can get the testing result that is similar to the real strategy tester result. By the way, this is only an idea of creating a tester for hedge Expert Advisors. I will not guarantee that it will be exactly the same as a real tester. But hope that it will be a good reference for future usage. Now let's begin. The Easy Meaning Of Hedge TradingBefore we start , let's talk a little about "HEDGE" (this is from my own blog name pipsmaker located here ). The easy way to say about hedging is to open two opposite trades of 2 currency pairs at the same time. This is to lower the trading risk, if one moves up, one will surely move down, but there is nothing to worry about, because we’ve got both buy and sell orders at the same time, then even if we lose one, we get one, that is why they call it ”low risk”. There are many kinds of opposite trade styles in the Forex world.
In hedge trading there are some facts that can not be doubted.
By the way, have you ever wondered, how a hedger gains from that trading style. Don’t worry, there will always be an overlap between the 2 currency pairs, the correlation is not a constant scheme in nature, there will frequently be a delay by one pair, one starts moving, then one will follow after it, again: like rabbit and turtle - a rabbit will take a rest, wait for the turtle to beat and win. That’s why hedgers gain a nice profit from them. And nowadays many people use hedging style to make money in Forex, nothing to worry about. Hedge, wait, close when a positive profit is shown. That’s it. The Hedge ConceptBefore starting to code the virtual tester, let's try to understand the hedging concept in an experiment. Without the hedging concept we will never know which type of data should be exported, recorded and calculated. These data can show us which type of order should be virtually generated. In this experiment I will set the hedging rules like this.
According to these rules the virtual orders need daily open prices (of both pairs) be used as an order-open-price. To calculate the intra-day profit, the price on every hour, as tick price, of every day should be recorded as the data for the order-close-price (ask for sell and bid for buy) and should be recorded together with the tick's time( to make sure that the tick price is from the same time value). And due to the concept of opening the hedge daily, I will separate all data needed into 2 file-type, that are daily-open and tick-value of both 2 pairs. Both 2 data types will be exported as a string file(s) with separate name, like GBPJPYD1.csv and GBPJPYTick.csv. And because of tick data that we wanted the virtual tester to be as much similar to the real one as possible, these 2 steps have to be proceeded.
And both 2 steps should be done for EURJPY too. But I think we can combine them into one expert advisor, this EA should export both 2 types of data into 2 separate files. Then after this EA finished the data recording process, a new one EA for generating the virtual trading will get all data of both GBPJPY and EURJPY from all exported files to perform the virtual testing show. The 3 Steps To Break The Testing Limit
By the idea above, I have concluded that our dream to break through this limit could be done by the following 3 steps.
So let's begin the first step. Step 1 : Export The Price DataBelow is the Expert Advisor to export the daily-open-price of attached symbol into a file, named "GBPJPYD1.csv" for GBPJPY and "EURJPYD1.csv" for EURJPY, in the mean time it will export the tick price into a file too, named like this "symbolT. csv" (the same as D1 file). Please read comments to learn how the EA works. Note : All the files created by this EA will be exported to "MetaTrader 4/tester/files" directory. //+------------------------------------------------------------------+ //| symbol-D1.mq4 | //| A Sexy Trader | //| http://pipsmaker.wordpress.com/ | //+------------------------------------------------------------------+ #property copyright "A Sexy Trader" #property link "http://pipsmaker.wordpress.com/" #include <stdlib.mqh> extern string StartDate = "2007.03.17" //set the starting date to get the same time data ,StopDate = "2007.06.28"; //set the testing time limit to protect the over //data recording process extern bool For_OP_SELL = true;/*This is to guide for the right type of data to be collected ->if For_OP_SELL = true the daily Open will be collected as an order open price ->if For_OP_SELL = false , means for OP_BUY, the daily Open+SPREAD will be collected instate. */ string name,tname; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //-------------------------------------------------------------------+ //| Some Essential Parameters In This EA | //-------------------------------------------------------------------+ int day // a variable to mark that today Open has been collected ,ho // handle of the file recording the Open price ,ht // handle of the file recording the tick price ,x=1 /* the order of D1 file increasing every time D1 data is equal to 4086 characters and generate the new recording file*/ ,xt=1 // same as x but for tick data ,bartime // a variable to mark that current bar's Open has been collected ; double ot // latest Open Time ,op // latest Open price ,lt // latest tick time ,ltk // latest tick price ; string OStr // the string to collect the daily open ,TStr // the string to collect the tick value ; //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { /*---------------------Only collect the data in the specific time period.----------------------*/ if(TimeToStr(TimeCurrent(),TIME_DATE)>=StartDate && TimeToStr(TimeCurrent(),TIME_DATE)<=StopDate) { name=Symbol()+x+"D1.csv"; // setup the name of daily open price file tname=Symbol()+xt+"T.csv" // the name of tick price file ; //---- if(day!=TimeDay(Time[0])) // the new day has come { ot=Time[0]; // get the new bar time if(For_OP_SELL)op=Open[0]; // get the new order open price for SELL Symbol else op=Open[0]+MarketInfo(Symbol(),MODE_SPREAD)*Point; // else if collecting for the BUY Symbol OStr=OStr+TimeToStr(Time[0],TIME_DATE)+",";//collect the new time data separate each data with "," OStr=OStr+DoubleToStr(op,Digits)+","; //now the new open price //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~now it's time to export as a file ho=FileOpen(name ,FILE_CSV|FILE_WRITE); // open a file to record the daily open if(ho>0) // if the file Symbol()+x+"D1.csv" exist { FileWrite(ho,OStr); // write the collected data FileClose(ho); // close the file every time your file process done if(StringLen(OStr)==4086){x++;OStr="";} /* if the data contains 4086 characters set new x to*/ } /* create the new file and prepare the new string for the new file */ Print(TimeToStr(Time[0],TIME_DATE)); // print the collected day time int thex=FileOpen(Symbol()+"x.csv",FILE_CSV|FILE_WRITE); // create a file to record "how many x?" for D1 file for further usage if(thex>0) // if the file exist { string xs=DoubleToStr(x,0); // transform x into string FileWrite(thex,xs); // write the x value FileClose(thex); // close file (every time you finish) } day=TimeDay(Time[0]); // now mark today as collected } //--------------------------------FOR TICK VALUE /*Because of the decision of collecting the tick data hourly*/ if(bartime!=Time[0]) // the new hourly bar has come // and to make it more flexible when you decided // to collect data in another time frame { lt=TimeCurrent(); // get the tick time if(!For_OP_SELL) ltk=Bid; // the tick price for sell order else ltk=Ask; // in case for buy order TStr=TStr+TimeToStr(lt,TIME_DATE|TIME_MINUTES)+","; // insert the data into the collected string TStr=TStr+DoubleToStr(ltk,Digits)+","; // //~~~~~~~~ // now export the data ht=FileOpen(tname,FILE_CSV|FILE_WRITE); // open a file to record the tick value if(ht>0) // if the file Symbol+xt+"T.csv" exist { FileWrite(ht,TStr); // write the collected tick with time FileClose(ht); // finish. if(StringLen(TStr)==4080){xt++;TStr="";}// prepare for new file if this file reached 4080 character } int thext=FileOpen(Symbol()+"xt.csv",FILE_CSV|FILE_WRITE); // record the xt value .. same as D1 file if(thext>0) // if the file exist { string xts=DoubleToStr(xt,0); // transform into string FileWrite(thext,xts); // write xt FileClose(thext); // done } bartime=Time[0]; // mark as current hourly bar Open price has been collected } } else if(TimeToStr(TimeCurrent(),TIME_DATE)>StopDate) // if out of specified period Print("Done."); // let us know it all done. //---- return(0); } //+------------------------------------------------------------------+ Step 2 : Generate The Virtual TradingThis step is the most excited step. The step of making the hedge EA to be testable by the strategy tester. See the script below to know what it looks like. And don't forget to read comments to understand how it works. And like in the first EA, the result file will be exported to "MetaTrader 4/tester/files" directory. //| VirtualHedge.mq4 | |