Results 1 to 2 of 2
 0 Attachment(s)    

Thread: Correlation strategy backtesting

  1. #1
    Administrator funyoo's Avatar
    Join Date
    Sep 2008
    Posts
    7,003

    Default Correlation strategy backtesting

    Here is a way to get the value of another pair in order to compare it with the current pair during the backtesting.

    Register the wanted pair and timeframe chart in tester/files with the .csv format (for example GBPUSD15.csv).

    Then in the code, after the extern parameters, add :

    Code:
    datetime time0;
    datetime daterewritten;
    string date;
    string time;
    double open;
    double high;
    double low;
    double close;
    double volume;
    In the start function, add :

    Code:
       int handle;
       string filename="GBPUSD15.csv";
       
       handle=FileOpen(filename,FILE_READ|FILE_CSV,",");
       if(handle==-1 || FileSize(handle)==0)return;
    
       if(handle<0){
          if(GetLastError()==4103){
             Alert("No file named ",filename);
          }
          else{
             Alert("Error while opening file ",filename);
             return;
          }
       }
       
       if(time0!=Time[0]){
          while(!FileIsEnding(handle)){
             if(Time[1]==daterewritten)break;
             date=FileReadString(handle,10);
             time=FileReadString(handle,5);
             open=FileReadNumber(handle);
             high=FileReadNumber(handle);
             low=FileReadNumber(handle);
             close=FileReadNumber(handle);
             volume=FileReadNumber(handle);
             daterewritten=StrToTime(date+" "+time);
          }
          time0=Time[0];
       }
       
       if(handle!=-1)FileClose(handle);
    
       Comment(
       "\ndate = "+date,
       "\ntime = "+time,
       "\nopen = "+DoubleToStr(open,5),
       "\nhigh = "+DoubleToStr(high,5),
       "\nlow = "+DoubleToStr(low,5),
       "\nclose = "+DoubleToStr(close,5),
       "\nvolume = "+DoubleToStr(volume,0),
       "\ndaterewritten = "+DoubleToStr(daterewritten,0),
       "\ncurrenttime = "+DoubleToStr(Time[1],0));

  2. #2

    Default

    Hello,
    I try to do it like explained but i get the following error 'time' - variable already defined MetaTrader - Alpari UK\experts\Correlation Strategy EA v1.03.mq4 (70, 8), where do i go wrong? Also if i remove time from initial init and make it a new init, and try to load the the file it goes through to tester and i get
    2010.03.11 11:38:43 2010.03.02 23:57 Correlation Strategy EA v1.03: invalid handle -1 in FileSize
    Could someone please clarify this for me.

    [QUOTE][CODE]//+----------------------------------------------------------------------------+
    //| _Expert Advisor.mq4 |
    //| Copyright 2009-2010, TradingSystemForex.Com |
    //| http://www.tradingsystemforex.com/ |
    //| 01.18.2010 |
    //+----------------------------------------------------------------------------+

    #property copyright "Copyright 2009-2010, TradingSystemForex.Com"
    #property link "http://www.tradingsystemforex.com/"

    //| Martingale is only for counter pairs like EURUSD and USDCHF

    string comment="EA"; // comment to display in the order
    extern bool automagic=true; // auto magic number
    extern bool printprofit=false; // print profit to check the EA functionability
    extern int magic=1234; // magic number required if you use different settings on a same pair, same timeframe

    extern string moneymanagement="Money Management";

    extern double lots=0.1; // lots size
    extern bool autofactor=false; // lot factors autotomatically calculated based on 14 hourly atr
    extern double lotfactor1=2; // manual lot factor 1
    extern double lotfactor2=3; // manual lot factor 2
    extern double minlot=0.01; // minimum lots size
    extern double maxlot=50; // maximum lots size
    extern int lotdigits=2; // lot digits, 1=0.1, 2=0.01
    extern bool martingale=false; // martingale -
    extern int martingalemode=0; // 0=buy sell sell - sell buy buy, 1=buy sell buy - sell buy sell
    extern int delayminutes=30; // enable martingale delay minutes after the last trades
    extern double multiplier=1.333; // multiplier for the martingale

    extern string ordersmanagement="Order Management";

    extern bool reversesignals=false; // reverse the signals, long if short, short if long
    extern bool addpos=false; // add position if price goes against us
    extern int pipstep=30; // negative difference between price and order open price
    extern double target=20; // target in dollars
    extern bool stoptradingaftertarget=false;// stop trading once target is reached
    bool closeoncross=false; // close on opposite cross
    bool closeifprofit=false; // close only if profit
    extern bool timeout=false; // close "exit minutes" after order open time
    extern double exitminutes=240; // exit minutes

    extern string entrylogics="Entry Logics";

    extern int entrymode=0; // 0=close-open, 1=gap, 2=direction, 3=price difference
    extern string symbol1="EURUSD"; // symbol 1
    extern string symbol2="USDCHF"; // symbol 2
    extern string direction="EURCHF"; // symbol used for direction, if entrymode=2
    extern int maperiod=10; // ma period for the direction
    extern double difference=20; // difference in point to open trades, if entrymode=3
    int shift=1; // bar to consider for the signalz

    datetime t0,t1,tstart,tend,tfriday,tmonday,lastbuyopentime, lastsellopentime,time,prevtime,lastopentime1,lasto pentime2,lasttime,
    buyhistopentime1,sellhistopentime1,buyhistopentime 2,sellhistopentime2,buyopentime1,sellopentime1,buy opentime2,sellopentime2;
    int i,bc=-1,cnt,tpb,tps,tries=100,lastorder,buyorderprofit,s ellorderprofit,lotsize;
    int nstarthour,nendhour,nfridayhour,nmondayhour,number ,ticket,number2,ticket2,gmtshift,tradetime,lasttyp e,total,delay;
    string istarthour,istartminute,iendhour,iendminute,ifrida yhour,ifridayminute,imondayhour,imondayminute,isym bol1,isymbol2,status,ltype,signal;
    double cb,sl,tp,ilots,lastbuylot,lastselllot,lastlot,last profit,mlots,win[14],sum[14],array[15],totalprofit,oldprofit;
    double lastbuyopenprice,lastsellopenprice,lastbuyprofit,l astsellprofit,tradeprofit,factor1,factor2;
    double stoploss=0,takeprofit=0,slippage=100;
    bool closebasket=false,targetreached=false,stoptrading= false;

    double pt,mt;
    int dg;

    datetime time0;
    datetime daterewritten;
    string date;
    string time;
    double open;
    double high;
    double low;
    double close;
    double volume;

    int init(){

    //+----------------------------------------------------------------------------+
    //| First bar |
    //+----------------------------------------------------------------------------+

    prevtime=Time[0];

    //+----------------------------------------------------------------------------+
    //| Auto magic |
    //+----------------------------------------------------------------------------+

    if(automagic){
    string s1cur1=StringSubstr(symbol1,0,3);
    string s1cur2=StringSubstr(symbol1,3,3);
    string s2cur1=StringSubstr(symbol2,0,3);
    string s2cur2=StringSubstr(symbol2,3,3);
    string strmagic1=0;
    string strmagic2=0;
    string strmagic3=0;
    string strmagic4=0;

    if(s1cur1=="EUR")strmagic1=1;if(s2cur1=="EUR")strm agic3=1;
    if(s1cur1=="GBP")strmagic1=2;if(s2cur1=="GBP")strm agic3=2;
    if(s1cur1=="USD")strmagic1=3;if(s2cur1=="USD")strm agic3=3;
    if(s1cur1=="AUD")strmagic1=4;if(s2cur1=="AUD")strm agic3=4;
    if(s1cur1=="CHF")strmagic1=5;if(s2cur1=="CHF")strm agic3=5;
    if(s1cur1=="CAD")strmagic1=6;if(s2cur1=="CAD")strm agic3=6;
    if(s1cur1=="JPY")strmagic1=7;if(s2cur1=="JPY")strm agic3=7;
    if(s1cur1=="NZD")strmagic1=8;if(s2cur1=="NZD")strm agic3=8;
    if(s1cur2=="EUR")strmagic2=1;if(s2cur2=="EUR")strm agic4=1;
    if(s1cur2=="GBP")strmagic2=2;if(s2cur2=="GBP")strm agic4=2;
    if(s1cur2=="USD")strmagic2=3;if(s2cur2=="USD")strm agic4=3;
    if(s1cur2=="AUD")strmagic2=4;if(s2cur2=="AUD")strm agic4=4;
    if(s1cur2=="CHF")strmagic2=5;if(s2cur2=="CHF")strm agic4=5;
    if(s1cur2=="CAD")strmagic2=6;if(s2cur2=="CAD")strm agic4=6;
    if(s1cur2=="JPY")strmagic2=7;if(s2cur2=="JPY")strm agic4=7;
    if(s1cur2=="NZD")strmagic2=8;if(s2cur2=="NZD")strm agic4=8;

    string strautomagic=StringConcatenate(strmagic1,strmagic2 ,strmagic3,strmagic4);
    magic=StrToInteger(strautomagic);
    }

    //+----------------------------------------------------------------------------+
    //| Factor |
    //+----------------------------------------------------------------------------+

    if(factor(symbol1)>factor(symbol2)){
    factor1=1;
    factor2=factor(symbol1)/factor(symbol2);
    }
    if(factor(symbol2)>factor(symbol1)){
    factor1=factor(symbol2)/factor(symbol1);
    factor2=1;
    }
    if(factor(symbol1)==factor(symbol2)){
    factor1=1;
    factor2=1;
    }
    t0=Time[0];
    t1=Time[0];
    dg=Digits;
    if(dg==3 || dg==5){pt=Point*10;mt=10;}else{pt=Point;mt=1;}
    if(minlot>=1){lotsize=100000;}
    if(minlot<1){lotsize=10000;}
    if(minlot<0.1){lotsize=1000;}
    return(0);
    }

    int start(){

    int handle;
    string filename="USDCHF.csv";

    handle=FileOpen(filename,FILE_READ|FILE_CSV,",");
    if(handle==-1 || FileSize(handle)==0)return;

    if(handle<0){
    if(GetLastError()==4103){
    Alert("No file named ",filename);
    }
    else{
    Alert("Error while opening file ",filename);
    return;
    }
    }

    if(time0!=Time[0]){
    while(!FileIsEnding(handle)){
    if(Time[1]==daterewritten)break;
    date=FileReadString(handle,10);
    time=FileReadString(handle,5);
    open=FileReadNumber(handle);
    high=FileReadNumber(handle);
    low=FileReadNumber(handle);
    close=FileReadNumber(handle);
    volume=FileReadNumber(handle);
    daterewritten=StrToTime(date+" "+time);
    }
    time0=Time[0];
    }

    if(handle!=-1)FileClose(handle);

    Comment(
    "\ndate = "+date,
    "\ntime = "+time,
    "\nopen = "+DoubleToStr(open,5),
    "\nhigh = "+DoubleToStr(high,5),
    "\nlow = "+DoubleToStr(low,5),
    "\nclose = "+DoubleToStr(close,5),
    "\nvolume = "+DoubleToStr(volume,0),
    "\ndaterewritten = "+DoubleToStr(daterewritten,0),
    "\ncurrenttime = "+DoubleToStr(Time[1],0));
    //+----------------------------------------------------------------------------+
    /

Similar Threads

  1. Correlation JPY Manual Strategy with control Panel
    By advisor in forum Commercial manual systems
    Replies: 0
    Last Post: 08-16-2014, 11:55
  2. XAUUSD BACKTESTING 99%
    By FX-BoT-FX in forum MQL programming
    Replies: 0
    Last Post: 01-05-2012, 19:45
  3. Backtesting
    By j_l in forum Ideas for expert advisors
    Replies: 2
    Last Post: 06-16-2010, 15:11
  4. Correlation Strategy EA
    By funyoo in forum Expert advisors backtesting
    Replies: 1
    Last Post: 02-17-2010, 22:51

Tags for this Thread

100, add, advisor, alpari, alpari uk, backtesting, correlation, date, dmi, eurusd, expert advisor, experts, gbpusd, high, low, manual, martingale, metatrader, mq4, nzd, profit, slippage, stop, stoploss, strategy, time

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •