Regular visitors will have noticed ever increasing amounts of spam on the Trading Gurus Community Forum. On November 24th 2012 we implemented some changes:

1. New users will be moderated 2. Registered users who have a post count of zero will not be able to view attachments or download code. 3. Registered users who have a post count of zero will not be able to create a new thread

As an additional countermeasure we anticipate introducing a modest paywall in the near future.

If this is your first visit here our apologies for the inconvenience.

Existing users may wish to contribute their ideas to this thread.

Results 1 to 9 of 9

Thread: Back testing or coding problem

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Location
    Belgium
    Posts
    10

    Back testing or coding problem

    I start to write an EA fo MT4 and want to first analyze the strategy.
    From start i got a problem to get properly in time the last good computed bar value! So my question is: What i am doing wrong. Here below a fraction of the code showing the logic to start the computation at new bar. The problem is that the first bar(0) seem the actual wild value and the bar(1) is the preeceding bar value, as i start on a 15 min chart a time modulo 15+ some seconds i get the value of the bar 15 minutes ago in bar(1) so in fact it's bar(2)!
    The other problem is that the iCustom value was off about 40 pips! but it's maybe an indicator problem.

    int start() {
    //----+ Declaration of local variables
    int bar,Vol[4];
    double StepF[4], StepS[4], Atr[4], Cprice[4], dMov12, dMov23;
    //----+ Declaration of static variables
    static int LastBars_Up, LastBars_Dn;
    static bool BUY_Sign, BUY_Stop, SELL_Sign, SELL_Stop;

    //----++ CODE FOR LONG POSITIONS
    if (Test_Up) {
    int IBARS_Up = iBars(NULL, Timeframe_Up);
    if (IBARS_Up >= MinBar_Up) {
    if (LastBars_Up != IBARS_Up) { // We get a new bar!
    //---- refresh price data
    RefreshRates();
    //----+ Initialization of variables
    BUY_Sign = false;
    BUY_Stop = false;
    LastBars_Up = IBARS_Up;

    //----+ CALCULATING INDICATOR VALUES AND UPLOADING THEM TO BUFFERS
    for(bar = 1; bar <= 4; bar++) {
    //StepF[bar-1] = iCustom(Symbol(),Timeframe_Up,"StepMa_v3",FPeriod_ Up,Kwatr_Up,Advance_Up,HighLow_Up,bar-1);
    StepS[bar-1] = iCustom(Symbol(),Timeframe_Up,"StepMa_v3",SPeriod_ Up,Kwatr_Up,Advance_Up,HighLow_Up,bar-1);
    //Vol[bar-1] = iVolume(Symbol(),Timeframe_Up, bar-1);
    //Atr[bar-1] = iATR(Symbol(),Timeframe_Up,14, bar-1);
    //Cprice[bar-1] = iClose(Symbol(),Timeframe_Up, bar-1);

    }
    //----+ DEFINING SIGNALS FOR TRADES
    if (Close[0] > StepS[0]) // Above StepS
    //if (StepF[1] > StepF[2]) // StepF moving up
    //if (Atr[1] > Atrmin // < dline[2]) // previous Main under signal
    //if (Vol[1] > Volmin) // Enough Volume?
    BUY_Sign = true;
    if (BUY_Sign == true) SELL_Stop = true; // force sell close before buy to avoid Hedging
    if (BUY_Sign && Debug) {
    Print ("Longopen S0 ",DoubleToStr(StepS[0],Digits)," S1 ",DoubleToStr(StepS[1],Digits),
    " S2 ",DoubleToStr(StepS[2],Digits), "S3 ",DoubleToStr(StepS[3],Digits));
    Print (" CL ",DoubleToStr(Close[0],Digits)," CL1 ",DoubleToStr(Close[1],Digits)," CL2 ",DoubleToStr(Close[2],Digits),
    " CL3 ",DoubleToStr(Close[3],Digits));
    }
    if (Close[0] < StepS[0]) // Under StepS
    //if (StepF[1] < StepF[2]) // StepF moving down
    //if (Atr[1] > Atrmin // < dline[2]) // previous Main under signal
    //if (Vol[1] > Volmin) // Enough Volume?
    BUY_Stop = true;
    if (BUY_Stop && Debug) {
    Print ("Longclose S0 ",DoubleToStr(StepS[0],Digits)," S1 ",DoubleToStr(StepS[1],Digits),
    " S2 ",DoubleToStr(StepS[2],Digits), "S3 ",DoubleToStr(StepS[3],Digits));
    Print (" CL1 ",DoubleToStr(Cprice[1],Digits)," F0 ",DoubleToStr(StepF[1],Digits)," F1 ",DoubleToStr(StepF[2],Digits),
    " F2 ",DoubleToStr(StepF[3],Digits));
    }
    } //----End (NewBar_Up)


    Unable to load a .doc file??

    Thanks
    Marcel
    Last edited by fantasio; 01-15-2011 at 08:50 PM.

  2. #2
    Administrator Jim's Avatar
    Join Date
    Apr 2010
    Location
    South West England
    Posts
    492
    Hi Marcel,

    Quote Originally Posted by fantasio View Post
    From start i got a problem to get properly in time the last good computed bar value! So my question is: What i am doing wrong. Here below a fraction of the code showing the logic to start the computation at new bar. The problem is that the first bar(0) seem the actual wild value and the bar(1) is the preeceding bar value, as i start on a 15 min chart a time modulo 15+ some seconds i get the value of the bar 15 minutes ago in bar(1) so in fact it's bar(2)!
    The other problem is that the iCustom value was off about 40 pips! but it's maybe an indicator problem.
    The MT4 start() function is called on every tick. In those circumstances bar(0) contains the bar currently being formed, and bar(1) contains the most recent complete 15 minute bar. If your expert has some logic that only needs to execute every bar you can save lots of CPU cycles by putting all that code in some sort of OnBar() handler (see below for a simple example)

    Unable to load a .doc file??
    The usual practice is to "Go Advanced" in the editor and wrap small snippets of code in "Code" tags, like this:

    Code:
      static int last_bars = 0;
    
    //+------------------------------------------------------------------+
    //| Return true if a new bar appears for the current symbol/period   |
    //+------------------------------------------------------------------+
    bool isNewBar()
    {
       if (last_bars == 0) {
          last_bars = Bars;
       }
       else if (last_bars != Bars) {
          last_bars = Bars;
          return(true);
       }     
       return(false);
    }
      
    int start()
    {
       if (isNewBar()) {
          OnBar();
       }
       // OnTick() stuff goes here
    }
    Anything longer just package it up inside a ZIP and upload that. Hope that's some help.

    Cheers,

    Jim
    Reality is merely an illusion, albeit a very persistent one - Albert Einstein

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Location
    Belgium
    Posts
    10
    Hi Jim,
    I change my code with your code but same result and it's normal both codes are correct!
    So i have to find what is wrong and the result is!
    It's only my fault! I started on the fact that the closed value of the bar that you see at 1:15 is obtained at 1:15 it's by evidence incorrect, the bar starts drawing at 1:15 and ends at 1:30 and not from 1:00 to 1:15.
    Sorry for this newbie issue, it's by making errors that we learns! Anyway i will use the idea to make a function with a period parameter to ease programing of multi level period decision.
    By instance i use a stepma indicator but the value listed during backtest doesnt match the value on the backtest graph, do you have a good stepma indicator for me?
    Attached a screen capture where you see in the data window stepma =1.36426 at 1:30 but using icustom during backtest i got 1.36469, that explain why an order close at 12:15! I can not go further if the numbers are incorrect.
    Thanks in advance.
    Marcel
    Attached Images Attached Images

  4. #4
    Administrator Jim's Avatar
    Join Date
    Apr 2010
    Location
    South West England
    Posts
    492
    Hi Marcel,

    Quote Originally Posted by fantasio View Post
    I change my code with your code but same result and it's normal both codes are correct!
    So i have to find what is wrong and the result is!
    It's only my fault! I started on the fact that the closed value of the bar that you see at 1:15 is obtained at 1:15 it's by evidence incorrect, the bar starts drawing at 1:15 and ends at 1:30 and not from 1:00 to 1:15.
    Sounds like you might be coming to MetaTrader from TradeStation? TS labels its bars with the CLOSE time, wheres MT4 labels them with the OPEN time.

    Sorry for this newbie issue, it's by making errors that we learns!
    No problem. You have to do your own research. Especially if you're considering forex trading! It's best to make those errors (and learn from those mistakes) with pretend money to start with however. There's another batch of stuff to learn when you start risking the real folding stuff too.

    Anyway i will use the idea to make a function with a period parameter to ease programing of multi level period decision.
    By instance i use a stepma indicator but the value listed during backtest doesnt match the value on the backtest graph, do you have a good stepma indicator for me?
    I don't I'm afraid. I don't much care for the vast majority of indicators, and I've never experimented with a stepMA.

    Attached a screen capture where you see in the data window stepma =1.36426 at 1:30 but using icustom during backtest i got 1.36469, that explain why an order close at 12:15! I can not go further if the numbers are incorrect.
    As you say, it's hard to make progress if you can't even get 2 numbers that should be equal to line up. At this distance it's also hard for me to suggest what the problem is. Maybe the spread, although 4.3 seems a bit excessive for EUR/USD. You do see that sort of thing when backtesting at weekends though. If you're not using a fixed spread broker, MT4 backtests are best performed when the markets are open!

    Cheers,

    Jim
    Reality is merely an illusion, albeit a very persistent one - Albert Einstein

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Location
    Belgium
    Posts
    10
    Quote Originally Posted by Jim View Post
    Hi Marcel,



    Sounds like you might be coming to MetaTrader from TradeStation? TS labels its bars with the CLOSE time, wheres MT4 labels them with the OPEN time.
    No i never work on any trader station, i started 2 months ago with MT4

    As you say, it's hard to make progress if you can't even get 2 numbers that should be equal to line up. At this distance it's also hard for me to suggest what the problem is. Maybe the spread, although 4.3 seems a bit excessive for EUR/USD. You do see that sort of thing when backtesting at weekends though. If you're not using a fixed spread broker, MT4 backtests are best performed when the markets are open!
    Well i read the article but don't undestand the relation backtest/spread. I make the test on MBT wich use a spread = 0 (in equity propriety). When i load the indy on a living chart i see just the last indy value moving, thats normal as it's the 0 bar, but all the preeceding bars didnt move up or down!
    So it's not a repainting indy!
    When i look the historic chart open with graphic in tester, the value are different from the value computed during the backtest and during those week-end tests all the opened charts was pefectly stable/fixed! This mornig i redo a test during live market session and i saw that the indy value in last october 2010 is moving (not always) with a new tick. Normally the MBT spread for EURUSD during normal activity time is about 0.9 but could increase when market is closed or low liquidity!
    I didn't see why the spread acts on the backtesting chart!
    Fortunately the numbers collected during backtesting sessions are always the same, so the result is stable but is not corresponding with the television!
    I modify the indy by adding protection like not enough bars, erroneaous indicator counted values, to zeroize the value under the necessary bars needed to trust the result, but this not change anything except shorting the start of the indy line.
    Any additional infos/tips about backtest issues?

    Marcel

  6. #6
    Junior Member
    Join Date
    Jan 2011
    Location
    Belgium
    Posts
    10
    Hi Jim,
    Here the results of last tests. By evidence backtesting during market open or market closed is not a valid option. During market close fixed spread as well as dynamic spread brokers increase the spread greatly and yes there is a direct action on the backtesting result. During open market hours spreads vary continuously for dynamic spread brokers and could increase during high volatility for (fixed) spreads brokers. You see that directly with this manipulation if you use an indy using prices. Disconnect from server-set spreads to null-run test-open graph-connect to server. You will see the graph sligthy adjusted.
    So the good practice to test is -disconnect from server - set spread to 0 -run backtest on a know good backtest file (could be preserved by changing symbol name), so you could redo a test in similar condition after a strategy modification.
    To disconnect simply connect to account 000 then go testing.
    To see/change spreads use the uploaded utility.
    Don't worry about .sel file it is reinitialized at connect time and updated continuously online!
    But youre right it's not the end of the story

    Cheers
    Marcel
    Last edited by fantasio; 01-17-2011 at 05:17 PM.

  7. #7
    Administrator Jim's Avatar
    Join Date
    Apr 2010
    Location
    South West England
    Posts
    492
    Hi Marcel,

    Quote Originally Posted by fantasio View Post
    To disconnect simply connect to account 000 then go testing.
    To see/change spreads use the uploaded utility.
    Don't worry about .sel file it is reinitialized at connect time and updated continuously online!
    Sounds like you've been experimenting with one of the MT4 "spread changer" programs. Might I ask which one?

    Not sure that I'd be happy fixing the spread at zero though. Surely it would be better to at least try using a "typical" value for your broker?

    Cheers,

    Jim
    Reality is merely an illusion, albeit a very persistent one - Albert Einstein

  8. #8
    Junior Member
    Join Date
    Jan 2011
    Location
    Belgium
    Posts
    10
    Hi Jim,

    Quote Originally Posted by Jim View Post

    Sounds like you've been experimenting with one of the MT4 "spread changer" programs. Might I ask which one?

    Not sure that I'd be happy fixing the spread at zero though. Surely it would be better to at least try using a "typical" value for your broker?

    Cheers,

    Jim
    Sorry i am certainly too tired i forgot to upload the program, here it is.

    Yes true , but the first objective is to test in a repeatable environment, i don't care of the profit amount (lol). For instance i try to understand the meaning of indicators applied to an EA. Building such an EA show directly the weaknesses of indicators or the difference between what you expect from and what is it in reality! I will see in the future if i could build a profitable EA or at least a good signal helper.

    By the way the difference between the indicator values collected during backtest and the values on the resulting chart was greatly reduced but always different (1 to 2 pips) so there is still at least one other problem left. I discovered the visual mode during backtest but strange enough the indicator was not drawed during visualization i have to stop the test to see the drawing? Any info on this one?

    Cheers

    Marcel
    Attached Files Attached Files
    Last edited by fantasio; 01-18-2011 at 08:36 AM.

  9. #9
    Administrator Jim's Avatar
    Join Date
    Apr 2010
    Location
    South West England
    Posts
    492
    Hi Marcel,

    Quote Originally Posted by fantasio View Post
    Sorry i am certainly too tired i forgot to upload the program, here it is.
    Thanks very much for the upload.

    The first objective is to test in a repeatable environment, i don't care of the profit amount (lol). For instance i try to understand the meaning of indicators applied to an EA. Building such an EA show directly the weaknesses of indicators or the difference between what you expect from and what is it in reality! I will see in the future if i could build a profitable EA or at least a good signal helper.
    Absolutely. If you can't repeat your test results, then they're as good as worthless. Hopefully the profits will come once you have a solid foundation on which to build!

    I discovered the visual mode during backtest but strange enough the indicator was not drawed during visualization i have to stop the test to see the drawing? Any info on this one?
    If an EA does the drawing itself it should certainly appear on a visual backtest. If it merely calls an indicator I think you'll need to manually add that indicator to the chart once the backtest has started.

    Cheers,

    Jim
    Reality is merely an illusion, albeit a very persistent one - Albert Einstein

Posting Permissions

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