How do I detect new bars multiple times?
How do I detect new bars multiple times?
Loading saved threads...
Ady Legziel · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Ady Legziel
Original post: https://stackoverflow.com/questions/66541640
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
This EA is supposed to detect new bars and Alert some info, but it only detects the first new bar (not the next ones). Any idea why? Also, The MaxPercent function doesn't run nicely and the 'Alert' inside it doesn't show. Please helpppppppppppppppppppppppppppp.
My code:
static double barOpen1 = Open[0];
static double barOpen2 = Open[5];
int IsNewCandle()
{
if(barOpen1 != Open[0] || barOpen2 != Open[5]) //If any of the statements pass, a new candle must have opened changing the index number.
{
barOpen1 = Open[0]; // Update variables with current prices
barOpen2 = Open[5];
Alert("New bar. The time is: ",TimeToStr(TimeCurrent(),TIME_SECONDS));
return(1);
}
else
{
return(0);
}
}
//--------MaxPercent-------
double MaxPercent(string Pair, int First, int Last)
{
double Highest = 0;
for (int p = First; p <= Last; p++)
{
if(iHigh("Pair",PERIOD_H1,p)> Highest)
{
Highest = iHigh("Pair",PERIOD_H4,p);
Alert("Inside MaxPercent(), Highest= ",DoubleToStr(Highest));
}
};
return((Highest/(iOpen("Pair",PERIOD_H1,Last)))-1);
}
//--------MinPercent-------
double MinPercent(string Pair, int First, int Last)
{
double Lowest = 0;
for (int p = First; p <= Last; p++)
{
if(iLow("Pair",PERIOD_H1,p) < Lowest )
{
Lowest = iLow("Pair",PERIOD_H1,p);
}
};
return((Lowest/(iOpen("Pair",PERIOD_H1,Last)))-1);
}
//-----ClosePercent--------------
double ClosePercent(string Pair, int First, int Last)
{
return(((iClose("Pair",PERIOD_H1,First))/(iClose("Pair",PERIOD_H1,Last)))-1);
}
void PrintDataEURUSD()
{
double MaxPercentD1EURUSD = MaxPercent("EURUSD", 1, 3);
Alert("MaxPercentD1EURUSD is: ", MaxPercentD1EURUSD );
}
void OnTick()
{
if(IsNewCandle()==1)
{
PrintDataEURUSD();
}
}
Thanks
Quote
Report
PaulB · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: PaulB
Original post: https://stackoverflow.com/a/66553583
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Your IsNewCandle() function should operate fine, there are however better ways of achieving new bar detection, such as:
void OnTick()
{
if(TimeBar==Time[0])
{
//Carry out any operations on each tick here
return;
}
if(TimeBar==0)
{
// Operations carried out on First Run only here
TimeBar=Time[0];
return;
}
// Operations at the start of a new bar here
PrintDataEURUSD();
TimeBar=Time[0];
return;
}
Your MaxPercent() function does not work because you are not passing your string properly (you are using the string
"pair"
instead the the variable
Pair
.
For example, your code
Highest = iHigh("Pair",PERIOD_H4,p);
should be
Highest = iHigh(Pair,PERIOD_H4,p);
Other lines of code should similarly be altered.
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: PaulB Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/66553583 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Your IsNewCandle() function should operate fine, there are however better ways of achieving new bar detection, such as: void OnTick() { if(TimeBar==Time[0]) { //Carry out any operations on each tick here return; } if(TimeBar==0) { // Operations carried out on First Run only here TimeBar=Time[0]; return; } // Operations at the start of a new bar here PrintDataEURUSD(); TimeBar=Time[0]; return; } Your MaxPercent() function does not work because you are not passing your string properly (you are using the string "pair" instead the the variable Pair . For example, your code Highest = iHigh("Pair",PERIOD_H4,p); should be Highest = iHigh(Pair,PERIOD_H4,p); Other lines of code should similarly be altered.
Checking account access…