New Bar in MTF code

Post Reply
Radar
Trader
Posts: 437
Joined: Fri Mar 23, 2012 5:39 pm
Location: Round the bend ;)

New Bar in MTF code

Post by Radar »

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^)
Check out my new, (well, old now), manual trade & automatic scale-in manager,
StackManV2
gringoh

New Bar in MTF code

Post by gringoh »

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
Radar
Trader
Posts: 437
Joined: Fri Mar 23, 2012 5:39 pm
Location: Round the bend ;)

New Bar in MTF code

Post by Radar »

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^)
Check out my new, (well, old now), manual trade & automatic scale-in manager,
StackManV2
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

New Bar in MTF code

Post by dietcoke »

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.
You do not have the required permissions to view the files attached to this post.
Image
Radar
Trader
Posts: 437
Joined: Fri Mar 23, 2012 5:39 pm
Location: Round the bend ;)

New Bar in MTF code

Post by Radar »

Hey Dietcoke,

Thanks for that :) Very handy indeed! :)

Have fun!

Radar =8^)
Check out my new, (well, old now), manual trade & automatic scale-in manager,
StackManV2
Post Reply

Return to “Coders Hangout”