Trade Once Per Bar with selectable TimeFrame

 
Hi!

I have EA (Yeaaa!) which uses selectable Time Frame TradeOncePerBar function. I want to store the value in GlobalVariableSet to prevent EA trading in same Bar when MT4 or EA restarts. Original function for TradeOncePerBar is:

if (CheckAlertTime == iTime (NULL, 5, 0)) return(0);
else CheckAlertTime = iTime (NULL, 5, 0);
modified to:
if (TradedThisBar == iTime (NULL, OncePerBarTF, 0)) return(0);
else TradedThisBar = iTime (NULL, 5, 0);
Tried coding like this (present COde):
datetime CheckAlertTime, TradedThisBar; // this variation makes no trades
// datetime TradedThisBar = 0; // this works irratic - it works, but for ex. EUR USD it opened 2 trades at once !!! when MT4 restarted
...
init()
...
if (!GlobalVariableCheck ("TradedThisBar_" + Symbol())) GlobalVariableSet ("TradedThisBar_" + Symbol(), TradedThisBar);
...
start()
...
if (!SignalsOnly)
{
if (GlobalVariableGet ("TradedThisBar_" + Symbol()) == iTime (NULL, OncePerBarTF, 0)) // GlobalVariableGet is Double, it should match with iTime which is Integer
{
// GlobalVariableSet ("TradedThisBar_" + Symbol(), 1);
Print ("GV TradedThisBar BUY -> TRUE (1), iTime = " + TimeToStr (iTime (NULL, OncePerBarTF, 0)) + " at " + TimeToStr (TimeCurrent(), TIME_DATE | TIME_MINUTES));
return(0);
}
else TradedThisBar = iTime (NULL, OncePerBarTF, 0);
{
GlobalVariableSet ("TradedThisBar_" + Symbol(), TradedThisBar);
Print ("GV TradedThisBar BUY -> FALSE (0), iTime = " + TimeToStr (iTime (NULL, OncePerBarTF, 0)) + " at " + TimeToStr (TimeCurrent(), TIME_DATE | TIME_MINUTES));
}
... Lots calculation
...
// if (GlobalVariableGet ("TradedThisBar_" + Symbol()) == 0)
// {
RefreshRates();
buy (Lots, BuySL, 0, magic, " 7Chi BUY " + Symbol());
Print ("BUY order opened " + Symbol() + " because GV TradedThisBar = 0!");
// }
return(0);
}
}

Either EA will not trade at all or it trades on same Bar at MT4 restart. Tried many variations in last 3 weeks... not successfull.

Please somebody point me to the Path of the Correct Code...

Thank you,

Have fun,

Simon
PS: marked Code and clicked SRC button... subwindow opened... inserted Code and clicked Insert... doesn't seem to work. At least not now when I write this...
 

Hi,

The path to the example of correct code -> https://www.mql5.com/en/forum/124521/page7

The are some explanations in the end of page.

Have fun,

Airat

 

maybe can using new bar function for more easy...

if(New_Bar()){
if(condtion)
{
print or order function..................
}
}




bool New_Bar()
{
static datetime New_Time=0;
if(New_Time!=Time[0]){
New_Time=Time[0];
return(true);
}
return(false);
}
 
if (!SignalsOnly)
{
   // GlobalVariableGet is Double, it should match with iTime which is Integer
   if (GlobalVariableGet ("TradedThisBar_" + Symbol()) 
     == iTime (NULL, OncePerBarTF, 0)) 
   {
       // GlobalVariableSet ("TradedThisBar_" + Symbol(), 1);
       Print ("GV TradedThisBar BUY -> TRUE (1), iTime = " 
             + TimeToStr (iTime (NULL, OncePerBarTF, 0)) + " at " 
             + TimeToStr (TimeCurrent(), TIME_DATE | TIME_MINUTES));
       return(0);
   }
   else TradedThisBar = iTime (NULL, OncePerBarTF, 0);  
   // Does this ^^ line belong inside braces vvv?
{  // << These braces do nothing otherwise
   GlobalVariableSet ("TradedThisBar_" + Symbol(), TradedThisBar);
   Print ("GV TradedThisBar BUY -> FALSE (0), iTime = " 
          + TimeToStr (iTime (NULL, OncePerBarTF, 0)) + " at " 
          + TimeToStr (TimeCurrent(), TIME_DATE | TIME_MINUTES));
}
// ?? Where is the terminating brace for if(!SignalsOnly)
If the SRC popup insert button fails to insert, refresh the page.
 

how about this? should work on any timeframe

   bool newbar;
//----set newbar
   if(Volume[0]==1)
   newbar=true;


//----test code   
   if (newbar==true)
    {
     Alert("NEWBAR");
     newbar=false;
    }

or for a timeframe of a chart other than the current chart

   bool newbar;
//----set newbar for 30 minute chart
   if(iVolume(NULL,30,0)==1)
     newbar=true;

//----test code   
   if (newbar==true)
    {
     Alert("NEWBAR");
     newbar=false;
    }
EA should not trade on restart because tick volume will not be 1, well not unless mt4 restarted at the same time as the last bar ended
 
Ais:

Hi,

The path to the example of correct code -> https://www.mql5.com/en/forum/124521/page7

The are some explanations in the end of page.

Have fun,

Airat


Hello Ais!


With all respect (I admire your sense for order and organization), but with my present WannaBeCoder skills your Code is an ancient Greek to me. Bear in mind I am WannaBeCoder - used CTRL-C -> CTRL-V not far ago... now I wanna Optimize the Code for LIVE account. And I guess it is time to learn how to use 'for', 'while' and everything rest... heh...

I did checked all post. There are suggestions which could be usefull for my purpose - at initialization (MT4 start etc.) I should check if this is first time the Code is executed, then proceed...


Thank you,


Viva la Ais,

Have fun,


Simon

 
hardyyanto:

maybe can using new bar function for more easy...


Hi, Hardyyanto!

I am familiar with this type of Codes. Now, to clearify this - TradeOncePerBar is NOT the problem, problem is how to tell the EA (I tried yelling, whispering and singing with guitar, but it didn't work... LOL) NOT to trade on the same Bar when MegaTrader4 or EA restarts. My original code works just fine:

if (TradedThisBar == iTime (NULL, OncePerBarTF, 0)) return(0);

else TradedThisBar = iTime (NULL, OncePerBarTF, 0);

but at MT4 restart the value of

static datetime New_Time=0;

would be reset? Except if I am wrong, and this value gets saved somewhere? DuNo.

Just want to be on the safe side when trading LIVE. Although the more I think about it the more I see I actually do not need this function so badly. Because the problem arised when I was coding and F5 compiling the code - EA would instantly open new order, if Signal condition was met. Yea, I know, Duuh! it restarts from init() - when F5 it. Though, better safe than sorry!

Thank you for your help,

Long live the Hardyyanto,


Have fun,


Simon

 
SDC:

how about this? should work on any timeframe

if(Volume[0]==1)
Volume is unreliable, you can skip ticks. Always use Time[] or iTime():
static datetime Time0;  bool    newBar  = Time0 < Time[0];
if (!newBar) return(0);                   Time0 = Time[0];
 
WHRoeder:
If the SRC popup insert button fails to insert, refresh the page.

Herr WHRoeder, will keep this in my mind.

The Braces are not the problem, complete Code works just fine (although I wish it would be Self-Huge-Profitable - LOL), like I said, just need to be sure that EA would not open trade on same Bar at MT4 or EA restart - that means that I would have to save some value somewhere for EA to pick it up at startup - the only way I am aware of doing this is with GlobalVariableSet - or am I mistaken? DuNo. Please let me know, guys.

May the (see below) walk you from home to office every day (except if you are married, in this case send them to your best friend, or, better, tell them to wait for you at office... hehe...)


Sorry, I ran out of clishes... (well, Insert Picture button didn't fail - OLE!)


Have fun,


Simon.


BTW, must say, for all you guys to sleep better, that my Code seems to work just fine (testing on Demo), only had 1 glitch at startup with EUR USD. And Must addmit it is great to have such community to share and offer help...

No offence, guys, must have some fun after 3 weeks of THE CODE FRUSTRATION sleepless nights... I bet you know how this feels...

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

Yes, I have read strange stories about a man called "El Volume" on this forum (I checked all threads 'Please help me Once per Bar'... I DID MY HOMEWORK, MAMA!). Beside, like I said, iTime() works just fine. Restart MT4 is worrying me... Beside I am afraid of Arrays... pssst, don't tell anybody...


Have fun,


SleepyTime (CheckClock, NULL, TimeToSleep),


Simon

 

Hello Simon !

"CTRL-C" -> "CTRL-V" <- "CTRL-X" and "CTRL-INSERT" -> "SHIFT-INSERT" <- "SHIFT-DELETE", there are my mostly used key combinations.

Why not !

Regards,

Airat