Red and White

 

I am testing a MACD system, and just want arrows to appear on the chart as per specified criteria. The following code doesn't show arrows more than once on a chart.

What modification is needed, so that a white arrow appears for every long signal, and a red one appears at every short signal.

int start()
{
//----
bool GoLong=false,GoShort=false;
if(DailySignal()==1)
{
GoLong=true;GoShort=false;
ObjectCreate("up", OBJ_ARROW, 0, Time[0], Open[0]+20*Point); //draw an up arrow
ObjectSet("up", OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("up", OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
ObjectSet("up", OBJPROP_COLOR, White);

}
if(DailySignal()==-1)
{
GoLong=false;GoShort=true;
ObjectCreate("down", OBJ_ARROW, 0, Time[0], Open[0]+20*Point); //draw an up arrow
ObjectSet("down", OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("down", OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
ObjectSet("down", OBJPROP_COLOR, Red);

}


//----
return(0);
}
//+------------------------------------------------------------------+

int DailySignal()
{
double MD1,MD2,MD3,MS1,MS2,MS3;
MD1=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MD2=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
MD3=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,2);
MS1=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
MS2=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
MS3=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2);
//Check Longs
if(MD1<0&&MD2<MS2&&MD1>MS1){return(1);}
if(MD2<0&&MD2<MD3&&MD1>MD2){return(1);}
if(MD2>0&&MD2<MD3&&MD1>MD2){return(1);}
//Check Shorts
if(MD1>0&&MD2>MS2&&MD1<MS1){return(-1);}
if(MD2>0&&MD2>MD3&&MD1<MD2){return(-1);}
if(MD2<0&&MD2>MD3&&MD1<MD2){return(-1);}
}

 
Each object (arrow) needs a unique name.
 
but I am testing the strategy for the last 2-3 years, How can I assign a unique name to thousands of arrows that will appear on the backtest period.
 

Here is one way.

string name;
int unique;

int init(){
   ObjectsDeleteAll();
   return(0);
}
int start(){
.
.
   name = "Up"+unique;
   ObjectCreate(name, OBJ_ARROW, 0, Time[0], Open[0]+20*Point); //draw an up arrow
.
.
   name = "Dn"+unique;
   ObjectCreate(name,OBJ_ARROW, 0, Time[0], Open[0]+20*Point); //draw a dn arrow
.
.
   unique++;
   return(0;
}
 

Tried..............but it shows arrows only once in the begining of the test period.

 

Well, show what you have coded now.

 

add code like this:

for(int i=bar;i--;i>0)

{

}

and

modify you function int DailySignal() to int DailySignal(int pos)

now it working nornal

 

Hi Gurus, Based on the recommendations by Phy and tomfool. I still have problem of not showing red&white arrows, as well as the signal returning the following conditions.

//Check Longs
if(MD2>0&&MD2<=MD3&&MD1>MD2){return(1);}//V Signal above zero
//Check Shorts
if(MD2<0&&MD2>MD3&&MD1<MD2){return(-1);}//V' Signal below zero

//+------------------------------------------------------------------+
//| DailySignalTesting.mq4 |
//| Khurram Moied |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Khurram Moied"
#property link "https://www.metaquotes.net/"

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
bool GoLong=false,GoShort=false;string UpName,DownName;int bar=500;

for(int i=bar;i>0;i--)
{
if(DailySignal(i)==1)
{
Print("Long Triggered at Month : ",Month(), " and Day : ",Day());
UpName="UP"+i;
GoLong=true;GoShort=false;
ObjectCreate(UpName, OBJ_ARROW, 0, Time[0], Open[0]+20*Point); //draw an up arrow
ObjectSet(UpName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(UpName, OBJPROP_ARROWCODE, SYMBOL_ARROWUP);
ObjectSet(UpName, OBJPROP_COLOR, White);
}
if(DailySignal(i)==-1)
{
Print("Short Triggered at Month : ",Month(), " and Day : ",Day());
DownName="Down"+i;
GoLong=false;GoShort=true;
ObjectCreate(DownName, OBJ_ARROW, 0, Time[0], Open[0]+20*Point); //draw an up arrow
ObjectSet(DownName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(DownName, OBJPROP_ARROWCODE, SYMBOL_ARROWDOWN);
ObjectSet(DownName, OBJPROP_COLOR, Red);
}
return(0);
}

//----
return(0);
}
//+------------------------------------------------------------------+

int DailySignal(int pos)
{
double MD1,MD2,MD3,MS1,MS2,MS3;
MD1=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
MD2=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,2);
MD3=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_MAIN,3);
MS1=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
MS2=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,2);
MS3=iMACD(NULL,PERIOD_D1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,3);
//Check Longs
if(MD1<0&&MD2<MS2&&MD1>MS1){return(1);}//Signal Crossing Below Zero
if(MD2<0&&MD2<=MD3&&MD1>MD2){return(1);}//V Signal below zero
if(MD2>0&&(MD2<=MD3&&MD1>MD2)){return(1);}//V Signal above zero
//Check Shorts
if(MD1>0&&MD2>MS2&&MD1<MS1){return(-1);}
if(MD2>0&&MD2>MD3&&MD1<MD2){return(-1);}//v' Signal above zero
if(MD2<0&&(MD2>=MD3&&MD1<MD2)){return(-1);}
return(0);
}

 

You did not implement unique name for each object on the chart.

 

I have implemented the unique names as UP+i, and Down+i, where i is decremented as recommended by tomfool. Moreover the following signals are still not returning anything.

//Check Longs
if(MD2>0&&MD2<=MD3&&MD1>MD2){return(1);}//V Signal above zero
//Check Shorts
if(MD2<0&&MD2>MD3&&MD1<MD2){return(-1);}//V' Signal below zero

 

"i" is not unique during indicator run time. It repeats.

Reason: