stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

New Bar in MTF code
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4010
Page 1 of 1
Author:  Radar [ Wed Dec 24, 2014 1:44 pm ]
Post subject:  New Bar in MTF code

Hey Gang,

I'm working on a multi-timeframe EA, and am having a problem with detecting a new bar on the higher timeframes.

The EA sits on an M1 chart, and loops through the timeframes, and uses an ATR/AverageSpread filter to determine which timeframe to enter trades on.

I have another filter in place to ensure that only 1 trade is entered per bar, but it's not quite right :( Whilst it does prevent multiple entries on an M1 bar, it still allows multiple entries on HTF bars :(

Here's the code for the M1 (the code for the other timeframes are the same, Just replace M1_ with M5_, M15_, etc)...

Code: Select all

   for (TFCount = 0; TFCount <= ArrayRange(TimeFrames, 0) - 1; TFCount++)
   {
      CTF = TimeFrames[TFCount];

      switch(CTF)
      {
         case 1:
            // Calculate & Store SL, BE, Bep, etc
            AdjustStuffByRange();
            // Do Trade Management
			   TradeManagementModule(M1_Managed, M1_Exported, None);

            // Do Trade Entries
            M1_SpreadIntoATR = iATR(Symbol(), CTF, 14, 0) / (gvg(SpreadGvName) / factor);
            if (M1_SpreadIntoATR >= 2)
            {
               TradingLowerTF = true;
               SpreadIntoATR = M1_SpreadIntoATR;
               ReadIndicatorValues();
               if ((LastTradeTime[TFCount] <= iTime(Symbol(), CTF, 0)) && (OldReadBarTime[TFCount] != iTime(Symbol(), CTF, 0)))
               {
                  OldReadBarTime[TFCount] = iTime(Symbol(), CTF, 0);
                  LookForTradingOpportunities(M1_Managed);
               }
            }
         break;
Any assistance will be greatly appreciated :)

Have fun!

Radar =8^)
Author:  gringoh [ Wed Dec 24, 2014 3:27 pm ]
Post subject:  New Bar in MTF code

Hi Radar,

In MPTM EA is trading multiple pairs I have a control to limit trades to 1 trade per bar.

If you check the code, you can find a solution which should suits your needs.

Control is line 1855 “OneTradePerBar”, it is using CountOpenTrades first to identify open trades.

Hope it can help.

Merry Christmas.

Cheers,
G
Author:  Radar [ Wed Dec 24, 2014 3:29 pm ]
Post subject:  New Bar in MTF code

Thanks, Gringoh :) I'll have a squizz ;)

edit...

Looks like we're doing the same thing, but different ;)

Code: Select all

 if ((LastTradeTime[TFCount] <= iTime(Symbol(), CTF, 0)) && (OldReadBarTime[TFCount] != iTime(Symbol(), CTF, 0)))
What gets me, though, is that I had added a second filter (OldReadBarTime[TFCount]), and the M30 loop was still sending a trade on every new M1 bar.

Note that I said was... Even though I haven't changed the code, it is now working properly... I've noticed this a few times when testing my code in the backtester... Something will go haywire for the first few tests, then, after messing with the code for hours, and eventually going back to the original code, the damn thing works properly, and doesn't mess up again. If I coded more often, I'd remember that bit ;)

'Ave a good one,

Radar =8^)
Author:  dietcoke [ Fri Dec 26, 2014 11:41 pm ]
Post subject:  New Bar in MTF code

Radar,

I have a library function which can identify whether there is a new bar on any timeframe.

just add it as an include file in your EA and run LookForNewBars(Symbol()) on every pass.

The bool variables

IsNewM1bar
IsNewM5bar
IsNewM30bar
IsNewH4bar
IsNewM15bar
IsNewD1bar
IsNewH1bar
IsNewW1bar
IsNewMN1bar

are available for you to use.

eg.

Code: Select all


start()
{
LookForNewBars(Symbol())

If(IsNewD1bar)
{
DoStuff()
}
If(IsNewM1bar)
{DoSomeOtherStuff()
}
}
The current bar time is stored as a global variable which means that the new bar state persists through chart timeframe switches and terminal restarts.
Author:  Radar [ Sat Dec 27, 2014 4:51 am ]
Post subject:  New Bar in MTF code

Hey Dietcoke,

Thanks for that :) Very handy indeed! :)

Have fun!

Radar =8^)
All times are UTC Page 1 of 1