Draw multiple vertical lines on MQL5 EA
Draw multiple vertical lines on MQL5 EA
Loading saved threads...
Camilo · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Camilo
Original post: https://stackoverflow.com/questions/77124842
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I would like to draw vertical lines for times when a new signal appears in an EA. For simplicity lets say a signal is simply the close price crossing over an EMA.
I managed to create a vertical line only on the latest signal. For any new signal, all prior Vlines disappear.
Looking over an old
POST
, I noted that one may have to update the positions of all vertical lines each new candle. The problem with that post, is that it re-runs all the calculations at each new candle, which appears inefficient.
So my solution was to create an array of datetime and store the time each new signal appears, and then loop over that array to place vertical lines over the time of all prior signals. However, as a new line is drawn, the prior one is erased. Any thought what could be wrong, or is there any other way to place such vlines on all crossovers in an EA?
Here is a simplified version of the code:
//General variables
bool IsNewbar; //Bollean to do calcualtion only on new bar.
MqlRates Price[]; // to staore price data used in EMA cross over
// Moving average variables
int MovAve_Handle;
double MovAve_Buffer[];
//for marking signals as vertical lines
datetime TimeLong;
int NSignalsLongs=0;
datetime Sig_L_Pos[]; //Array to store datetimes in which signals where reported
int OnInit()
{
ArraySetAsSeries(Sig_L_Pos,true);
ArraySetAsSeries(Price,true);
MovAve_Handle= iMA(_Symbol,PERIOD_M1, 100,0, MODE_SMA,PRICE_CLOSE);
ArraySetAsSeries(MovAve_Buffer,true);
return(INIT_SUCCEEDED);
}
void OnTick()
{
///---check for new bar
static datetime StartTime;
datetime ThisBarTime = iTime(_Symbol,PERIOD_M1,0);
if(StartTime != ThisBarTime) {IsNewbar=true; StartTime = ThisBarTime; } else {IsNewbar=false;}
if(IsNewbar==true){
//Collect data of signal on new candle
string SignalEntryType= GetSignal_BBCont_MA();
//Check for signal type
if (SignalEntryType=="Long") {
NSignalsLongs=ArraySize(Sig_L_Pos);
ArrayResize(Sig_L_Pos,NSignalsLongs+1);
Print(NSignalsLongs);
Sig_L_Pos[NSignalsLongs]=TimeCurrent(); //store time of signal into array
Print(SignalEntryType);
}
//Draw vLines for each of the times in which signals arrived.
for(int i = 0; i<=NSignalsLongs-1; i++){
TimeLong=Sig_L_Pos[i];
ObjectCreate(0,"Long", OBJ_VLINE,0, TimeLong ,0);
}
}
}
///GETSIGNAL fucntion
string GetSignal_BBCont_MA(){
// close price
CopyRates(_Symbol,PERIOD_M1,1,2,Price);
//Data from Moving Average
CopyBuffer(MovAve_Handle,0,1,2,MovAve_Buffer);
//GetSignal Close Crossover EMA
if (Price[0].close > MovAve_Buffer[0] && Price[1].close<=MovAve_Buffer[1]) {return("Long");}
return("None");
}
Quote
Report
Camilo · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Camilo
Original post: https://stackoverflow.com/a/77125941
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
The reason for drawing only the latest vline, is that the name was not unique.
This fixed the problem:
string name = "L" + IntegerToString(i);
// ObjectCreate(0, name, OBJ_VLINE, 0, TimeLong,0 ); //Shows bottom labels.
ObjectCreate(0,name, OBJ_TRENDBYANGLE, 0, TimeLong, 0, TimeLong, 1); // straight lines
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Camilo Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/77124842 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I would like to draw vertical lines for times when a new signal appears in an EA. For simplicity lets say a signal is simply the close price crossing over an EMA. I managed to create a vertical line only on the latest signal. For any new signal, all prior Vlines disappear. Looking over an old POST , I noted that one may have to update the positions of all vertical lines each new candle. The problem with that post, is that it re-runs all the calculations at each new candle, which appears inefficient. So my solution was to create an array of datetime and store the time each new signal appears, and then loop over that array to place vertical lines over the time of all prior signals. However, as a new line is drawn, the prior one is erased. Any thought what could be wrong, or is there any other way to place such vlines on all crossovers in an EA? Here is a simplified version of the code: //General variables bool IsNewbar; //Bollean to do calcualtion only on new bar. MqlRates Price[]; // to staore price data used in EMA cross over // Moving average variables int MovAve_Handle; double MovAve_Buffer[]; //for marking signals as vertical lines datetime TimeLong; int NSignalsLongs=0; datetime Sig_L_Pos[]; //Array to store datetimes in which signals where reported int OnInit() { ArraySetAsSeries(Sig_L_Pos,true); ArraySetAsSeries(Price,true); MovAve_Handle= iMA(_Symbol,PERIOD_M1, 100,0, MODE_SMA,PRICE_CLOSE); ArraySetAsSeries(MovAve_Buffer,true); return(INIT_SUCCEEDED); } void OnTick() { ///---check for new bar static datetime StartTime; datetime ThisBarTime = iTime(_Symbol,PERIOD_M1,0); if(StartTime != ThisBarTime) {IsNewbar=true; StartTime = ThisBarTime; } else {IsNewbar=false;} if(IsNewbar==true){ //Collect data of signal on new candle string SignalEntryType= GetSignal_BBCont_MA(); //Check for signal type if (SignalEntryType=="Long") { NSignalsLongs=ArraySize(Sig_L_Pos); ArrayResize(Sig_L_Pos,NSignalsLongs+1); Print(NSignalsLongs); Sig_L_Pos[NSignalsLongs]=TimeCurrent(); //store time of signal into array Print(SignalEntryType); } //Draw vLines for each of the times in which signals arrived. for(int i = 0; i<=NSignalsLongs-1; i++){ TimeLong=Sig_L_Pos[i]; ObjectCreate(0,"Long", OBJ_VLINE,0, TimeLong ,0); } } } ///GETSIGNAL fucntion string GetSignal_BBCont_MA(){ // close price CopyRates(_Symbol,PERIOD_M1,1,2,Price); //Data from Moving Average CopyBuffer(MovAve_Handle,0,1,2,MovAve_Buffer); //GetSignal Close Crossover EMA if (Price[0].close > MovAve_Buffer[0] && Price[1].close<=MovAve_Buffer[1]) {return("Long");} return("None"); }
Checking account access…