Trade Once Per Bar with selectable TimeFrame - page 2

 
WHRoeder:
Volume is unreliable, you can skip ticks. Always use Time[] or iTime():


Oh I see, I didnt take that into account ... oh well it seemed like a good idea at the time lol, Im glad you pointed that out I was going to use it in my EA.

I did notice something when I tested the volume Alert, new bars dont always start at the exact time of the new bar, the first tick to starts the new bar is often several seconds after the beginning of the time period. Would this not mean a second order could still be sent on the last price quote of the old bar even though a new time period on time[] has begun ?

 

Chistabo:

"... I should check if this is first time the Code is executed, then proceed ..."

"... I should check if this is first time the Code is succesfully executed, then proceed ..." ?

 
Ais:

"... I should check if this is first time the Code is succesfully executed, then proceed ..." ?


Hello, guys!


Yes, Ais, we agree.

If anybody interested, here is present Code which works just fine:

datetime          TradedThisBar = 0; // this works
...
init()
...
   if (!GlobalVariableCheck ("TradedThisBar_" + Symbol())) GlobalVariableSet ("TradedThisBar_" + Symbol(), TradedThisBar);  // TDB
...
start()
...
      if (!SignalsOnly)
         {
         if (GlobalVariableGet ("TradedThisBar_" + Symbol()) == iTime (NULL, OncePerBarTF, 0)) return(0);
         else TradedThisBar = iTime (NULL, OncePerBarTF, 0);
            {
            GlobalVariableSet ("TradedThisBar_" + Symbol(), TradedThisBar);
            Print ("GV TradedThisBar BUY -> FALSE, iTime = " + TimeToStr (iTime (NULL, OncePerBarTF, 0)) + " at " + TimeToStr (TimeCurrent(), TIME_DATE | TIME_MINUTES));
            }
         if (MoneyManagement) Lots = NormalizeDouble ((AccountFreeMargin() * Risk / 100 / ((Ask - BuySL) / Point / TickValue)), 2);
         if (Lots < MarketInfo (Symbol(), MODE_MINLOT)) Lots = MarketInfo (Symbol(), MODE_MINLOT);
         else Lots = Lots;
//----- CHECK FREE MARGIN --------------------------------------------------------------------------------------------
         if (AccountFreeMargin() < (1000 * Lots))
            {
            Print ("We have no money. Free Margin = ", AccountFreeMargin());
            return(0);
            }
         RefreshRates();
         buy (Lots, BuySL, 0, magic, name + " BUY " + Symbol());
         Print ("BUY order opened " + Symbol() + "!");
         return(0);
         }
...

Thank you all for help,


HAVE FUN,


Simon

PS: will upload complete Code, if I find where to...

 
SDC:


Oh I see, I didnt take that into account ... oh well it seemed like a good idea at the time lol, Im glad you pointed that out I was going to use it in my EA.

I did notice something when I tested the volume Alert, new bars dont always start at the exact time of the new bar, the first tick to starts the new bar is often several seconds after the beginning of the time period. Would this not mean a second order could still be sent on the last price quote of the old bar even though a new time period on time[] has begun ?

static datetime Time0;  bool    newBar  = Time0 < Time[0];
if (!newBar) return(0);                   Time0 = Time[0];
A new bar starts when a tick comes in. Start is called when the tick comes in. You can't open a second order because start hasn't been called.
 

Two solutions to store values between reloads:

1. using Global Variables -> https://docs.mql4.com/globals;

2. using files -> https://docs.mql4.com/files.

I like 1.

"Global variables are kept in the client terminal within 4 weeks since the last access <added by me> <even if terminal restarts or shuts off> </added by me>, then they will be deleted automatically. An access to a global variable is not only setting of a new value, but reading of the global variable value, as well." <- https://docs.mql4.com/globals.

Reason: