Simple Coding Help - No Promise.

I have 1 error in this code i tried to convert from MQ4 to AFL. Could anyone please tell me what I did wrong here? I'm new to looping. Just figured out how to create a function in C++ and so I have managed to modify this a bit.

It seems to be a syntax error somewhere but I can't find it.

Code:
//+------------------------------------------------------------------+
//| TD D-Wave.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
/*#property copyright "clam61"
#property link ""
#property indicator_chart_window
*/
//---- input parameters
/*
datetime time;
int i;
*//*
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init() {
  return(0);
}*/

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+

  //for(i = 0; i < BarCount; i++)
	// ObjectDelete(""+i);

//  return(0);
//}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
 //start() {
 
 
 SetBarsRequired(sbrAll);

    
i=0;
TimeNum()==0;
counted_bars = BarCount;
daysback=0;
dist= 1.5*ATR(10);
   
   function isMaxClose(daysBack,startDay) {

  for ( i = 1; i < daysBack + 1; i++) {
    //if today's close less than a close in the past x days
    if (Close[startDay] <= Close[startDay + i])
      false;
  }
  
  return (True);
}   
 function isMinClose(daysBack,startDay) {

  for (i = 1; i < daysBack + 1; i++) {
    //if today's close greater than a close in the past x days
    if (Close[startDay] >= Close[startDay +i ])
      False;
  }
  
  return (True);
}    




   for(i = 0; i < BarCount; i++) {
	 // for (i=0; i < BarCount; i++) {
	 // if(TimeNum()> 0);
	 //ObjectDelete(""+i);time=Time[0];}

   counted_bars = BarCount;
   

  // check for possible errors
  if(counted_bars < 0) 
    -1;
    

  // last counted bar will be recounted
  if(counted_bars>0) 
    counted_bars--;
    
   limit = 500;//Bars - counted_bars;
  
   waveNumber = 0;
   
   
   

  // For all bars starting 21 days from the earliest
  for(i = limit - 22; i >= 0; i--) {
    //If wave has not started
    if (waveNumber == 0) { //Alert("0");
      //find a 21 day low
      if (isMinClose(21, i)) {
       PlotText( "1" , i , H[i]+dist[i]*0.15, colorBlack  );
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], Low[i] - 10 * Point );
       // ObjectSetText(""+i, "A", 15, "Arial", Lime);
        waveNumber = 1;
      }
    }
    else if (waveNumber == 1) {
      //find a 13 day high
      if (isMaxClose(13, i)) {
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], High[i] + 10 * Point );
        //ObjectSetText(""+i, "1", 15, "Arial", Lime);
        
        waveNumber = 2;
      }
    }
    else if (waveNumber == 2) {
      //find a 8 day low
      if (isMinClose(8, i)) {
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], Low[i] - 10 * Point );
        //ObjectSetText(""+i, "2", 15, "Arial", Lime);
        
        waveNumber = 3;
      }
    }
    else if (waveNumber == 3) {
      //find a 21 day high
      if (isMaxClose(21, i)) {
       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],High[i] + 10 * Point );
       // ObjectSetText(""+i, "3", 15, "Arial", Lime);
        
        waveNumber = 4;
      }
    }
    else if (waveNumber == 4) {
      //find a 13 day low
      if (isMinClose(13, i)) {
       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],Low[i] - 10 * Point );
       // ObjectSetText(""+i, "4", 15, "Arial", Lime);
        
        waveNumber = 5;
      }
    }
    else if (waveNumber == 5) {
      //find a 34 day high
      if (isMaxClose(34, i)) {
       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],High[i] + 10 * Point );
       // ObjectSetText(""+i, "5", 15, "Arial", Lime);
        
        waveNumber = 0;
}

   
 }
 }
 

TracerBullet

Well-Known Member
I have 1 error in this code i tried to convert from MQ4 to AFL. Could anyone please tell me what I did wrong here? I'm new to looping. Just figured out how to create a function in C++ and so I have managed to modify this a bit.

It seems to be a syntax error somewhere but I can't find it.

Code:
//+------------------------------------------------------------------+
//| TD D-Wave.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
/*#property copyright "clam61"
#property link ""
#property indicator_chart_window
*/
//---- input parameters
/*
datetime time;
int i;
*//*
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init() {
  return(0);
}*/

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+

  //for(i = 0; i < BarCount; i++)
	// ObjectDelete(""+i);

//  return(0);
//}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
 //start() {
 
 
 SetBarsRequired(sbrAll);

    
i=0;
TimeNum()==0;
counted_bars = BarCount;
daysback=0;
dist= 1.5*ATR(10);
   
   function isMaxClose(daysBack,startDay) {

  for ( i = 1; i < daysBack + 1; i++) {
    //if today's close less than a close in the past x days
    if (Close[startDay] <= Close[startDay + i])
      false;
  }
  
  return (True);
}   
 function isMinClose(daysBack,startDay) {

  for (i = 1; i < daysBack + 1; i++) {
    //if today's close greater than a close in the past x days
    if (Close[startDay] >= Close[startDay +i ])
      False;
  }
  
  return (True);
}    




   for(i = 0; i < BarCount; i++) {
	 // for (i=0; i < BarCount; i++) {
	 // if(TimeNum()> 0);
	 //ObjectDelete(""+i);time=Time[0];}

   counted_bars = BarCount;
   

  // check for possible errors
  if(counted_bars < 0) 
    -1;
    

  // last counted bar will be recounted
  if(counted_bars>0) 
    counted_bars--;
    
   limit = 500;//Bars - counted_bars;
  
   waveNumber = 0;
   
   
   

  // For all bars starting 21 days from the earliest
  for(i = limit - 22; i >= 0; i--) {
    //If wave has not started
    if (waveNumber == 0) { //Alert("0");
      //find a 21 day low
      if (isMinClose(21, i)) {
       PlotText( "1" , i , H[i]+dist[i]*0.15, colorBlack  );
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], Low[i] - 10 * Point );
       // ObjectSetText(""+i, "A", 15, "Arial", Lime);
        waveNumber = 1;
      }
    }
    else if (waveNumber == 1) {
      //find a 13 day high
      if (isMaxClose(13, i)) {
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], High[i] + 10 * Point );
        //ObjectSetText(""+i, "1", 15, "Arial", Lime);
        
        waveNumber = 2;
      }
    }
    else if (waveNumber == 2) {
      //find a 8 day low
      if (isMinClose(8, i)) {
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], Low[i] - 10 * Point );
        //ObjectSetText(""+i, "2", 15, "Arial", Lime);
        
        waveNumber = 3;
      }
    }
    else if (waveNumber == 3) {
      //find a 21 day high
      if (isMaxClose(21, i)) {
       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],High[i] + 10 * Point );
       // ObjectSetText(""+i, "3", 15, "Arial", Lime);
        
        waveNumber = 4;
      }
    }
    else if (waveNumber == 4) {
      //find a 13 day low
      if (isMinClose(13, i)) {
       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],Low[i] - 10 * Point );
       // ObjectSetText(""+i, "4", 15, "Arial", Lime);
        
        waveNumber = 5;
      }
    }
    else if (waveNumber == 5) {
      //find a 34 day high
      if (isMaxClose(34, i)) {
       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],High[i] + 10 * Point );
       // ObjectSetText(""+i, "5", 15, "Arial", Lime);
        
        waveNumber = 0;
}

   
 }
 }
For syntax errors - Try to comment out code to isolate the problem code. AB many times does not give correct location of error, so simple way is to comment out code exposing only some at a time ( say in binary search like pattern)

Edit - There are 2 very basic errors in the code one of which is a syntax error. But the best way to learn is to do it yourself, So 1st try to isolate the syntax error as above and then re-read code and see why you get other runtime error ..
 
Last edited:

john302928

Well-Known Member
Jayakumar bro
Can i contact you via mail?
_SECTION_BEGIN("Chart Display");
GraphXSpace = 10;
SetBarsRequired( 1,1);
Param_systemTitle = ParamStr("System Title?","Golden and Dead Cross");
if(ParamToggle("Show System Title?","No|Yes",1))
{
GfxSelectFont("Arial",10,600);
GfxSetTextColor(colorBlue);
GfxSetBkMode(1);
GfxTextOut(Param_systemTitle,15,25);
}
SetChartOptions(0,chartShowArrows|chartShowDates);
SetChartBkColor(colorLightGrey);
SetBarFillColor( IIf( C>O, colorWhite, colorBlack ) );
Plot( C, "Close", ParamColor("Candle Color", colorBlack), styleCandle|styleNoTitle);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
//plot tool tip
if( ParamToggle("Tooltip shows", "All Values|Only Prices" ) )
{
ToolTip=StrFormat("Open: %g\nHigh: %g\nLow: %g\nClose: %g (%.1f%%)\nVolume: "+NumToStr( V, 1 ), O, H, L, C, SelectedValue( ROC( C, 1 )));
}


_SECTION_END();
_SECTION_BEGIN("Volume");
Plot( Volume, _DEFAULT_NAME(), ParamColor("Color", colorGrey50), ParamStyle( "Style", styleHistogram | styleOwnScale, maskHistogram), 2 );
_SECTION_END();


_SECTION_BEGIN("Trading System");

///Defining Trend with Moving Average///



x=Param("Short EMA",50,1,200,1);
y=Param("Long EMA", 200,1,200,1);
EMA1= EMA(C,x);
EMA2=EMA(C,y);


goldencross= Cross(EMA1, EMA2); //moving average bullish
deadcross= Cross(EMA2, EMA1); //moving average bearish

_SECTION_END();


//////Defining Buy or Sell Conditions/////////
//BuyCondition = ValueWhen(PL,C)<=ValueWhen(PL,L,2) AND C>ValueWhen(PL,L,2) AND BarsSince(PL)<BarsSince(PH) AND BarsSince(PL)<=Param_buyDelay AND ValueWhen(PL,C)>=LLV(C,Param_lookbackBuy) AND IIf(MH_Buy,MH>ValueWhen(PL,MH,2),True) AND IIf(SD_Buy,SD<20,True) AND IIf(BB_Buy,BB_BuyValue,True) AND IIf(SMA_Buy,SMA_BuyValue,True) AND IIf(EMA_Buy,EMA_BuyValue,True) AND IIf(Price_Buy,C<=Price_UpperBuyValue AND C>=Price_LowerBuyValue,True) AND IIf(Volume_Buy,V>=Volume_BuyValue,True) AND IIf(RS_Buy,RS_BuyValue,True);
BuyCondition = goldencross ;
//SellCondition = ValueWhen(PH,C)>=ValueWhen(PH,H,2) AND C<=ValueWhen(PH,H,2) AND BarsSince(PH)<BarsSince(PL) AND BarsSince(PH)<=Param_sellDelay AND ValueWhen(PH,C)<=HHV(C,Param_lookbackSell) AND IIf(MH_Sell,MH<ValueWhen(PH,MH,2),True) AND IIf(SD_Sell,SD>80,True) AND IIf(BB_Sell,BB_SellValue,True) AND IIf(SMA_Sell,SMA_SellValue,True) AND IIf(EMA_Buy,EMA_SellValue,True) AND IIf(Price_Sell,C<=Price_UpperSellValue AND C>=Price_LowerSellValue,True) AND IIf(Volume_Sell,V>=Volume_SellValue,True) AND IIf(RS_Sell,RS_SellValue,True);
SellCondition = deadcross ;


Buy = BuyCondition;
Short = SellCondition;

_SECTION_END();

_SECTION_BEGIN("Exploration");
PlotShapes(IIf(Buy,shapeSmallUpTriangle,shapeNone),colorBlue,0,L,-25);
PlotShapes(IIf(Short,shapeSmallDownTriangle,shapeNone),colorOrange,0,H,-25);

SetOption("NoDefaultColumns",True);
bkColour = IIf(BuyCondition ,colorGreen,colorRed);
//bkColour = IIf(BuyCondition OR Cover,colorGreen,colorRed);
txtColour = colorWhite;
AddTextColumn(Name() ," Ticker ",1.2,txtColour,bkColour,75);
AddTextColumn(FullName() ," Full Name ",1.2,txtColour,bkColour,100);
AddColumn(DateTime()," Date / Time ",formatDateTime,txtColour,bkColour,125);
AddColumn(C," Close ",1.2,txtColour,bkColour,75);
AddColumn(ROC( C,1)," % Change ",1.2,txtColour,bkColour,50);
AddColumn(V," Volume ",1.0,txtColour,bkColour,100);

Filter = Buy OR Short;
AddColumn( IIf( BuyCondition, 66, 83 ), "Buy/Sell?", formatChar,txtColour,bkColour,40);

Var = WriteIf(goldencross,"GoldenCross",WriteIf(deadcross,"DeadCross",""));
AddTextColumn( Var , "Remark", 1.2 , colorBlack, IIf( goldencross, colorLime,IIf(deadcross,colorOrange,colorWhite)),60);

_SECTION_END();
 
For syntax errors - Try to comment out code to isolate the problem code. AB many times does not give correct location of error, so simple way is to comment out code exposing only some at a time ( say in binary search like pattern)

Edit - There are 2 very basic errors in the code one of which is a syntax error. But the best way to learn is to do it yourself, So 1st try to isolate the syntax error as above and then re-read code and see why you get other runtime error ..

I'll have a look at it right now. I have a doubt I have missed a curly brace somewhere. Now at to the other error, I'll know once I get the syntax error fixed right? coz ami will show syntax errors first
 

TracerBullet

Well-Known Member
I'll have a look at it right now. I have a doubt I have missed a curly brace somewhere.
Yes, i dont know if the code formatting got messed up in tj. If not, always align your code properly so that the structure/levels are visibly clear. Having multiple unaligned brackets makes it hard to read.

Now at to the other error, I'll know once I get the syntax error fixed right? coz ami will show syntax errors first
yes, the error will change. its a very silly one. When you see the error line, just read entire code from start again. If still stuck let me know.
 
For syntax errors - Try to comment out code to isolate the problem code. AB many times does not give correct location of error, so simple way is to comment out code exposing only some at a time ( say in binary search like pattern)

Edit - There are 2 very basic errors in the code one of which is a syntax error. But the best way to learn is to do it yourself, So 1st try to isolate the syntax error as above and then re-read code and see why you get other runtime error ..

Ok, so I made the necessary corrections. The issue was with "Limit". Setting it to Setbarsrequired () worked. Now, there's the issue that if I add this afl to a chart, nothing gets displayed. Any ideas on why that's happening.

I barely have any looping skills but if you can point me in the right directions, I'll eventually figure it out. So do let me know what the issue is here. I'll read up and try to fix it asap.

Code:
daysback=0;
dist= 1.5*ATR(10);
   
   function isMaxClose(daysBack,startDay) {

  for ( i = 1; i < daysBack + 1; i++) {
    //if today's close less than a close in the past x days
    if (Close[startDay] <= Close[startDay + i])
      false;
  }
  
  return (True);
}   
 function isMinClose(daysBack,startDay) {

  for (i = 1; i < daysBack + 1; i++) {
    //if today's close greater than a close in the past x days
    if (Close[startDay] >= Close[startDay +i])
      False;
  }
  
  return (True);
}    


/*datetime time;
int i;

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init() {
  return(0);
}

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit() {
  for(i = 0; i < Bars; i++)
	 ObjectDelete(""+i);

  return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start() {

   if(time!=Time[0]){for(i = 0; i < Bars; i++)
	 ObjectDelete(""+i);time=Time[0];}

  int counted_bars = IndicatorCounted();*/

   counted_bars = 0;
   

  // check for possible errors
  if(counted_bars < 0) 
    -1;
    

  // last counted bar will be recounted
  if(counted_bars>0) 
    counted_bars--;
    
   limit = SetBarsRequired(500);//Bars - counted_bars;
  
   waveNumber = 0;
   
   
   

  // For all bars starting 21 days from the earliest
  if (BarCount >= limit) {
  for(i = limit - 20; i >= 0; i--) {
    //If wave has not started
    if (waveNumber == 0) { //Alert("0");
      //find a 21 day low
      if (isMinClose(21, i)) {
       PlotText( "0" , i , L[i]-dist[i]*0.15, colorBlack  );
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], Low[i] - 10 * Point );
       // ObjectSetText(""+i, "A", 15, "Arial", Lime);
        waveNumber = 1;
      }
    }
    else if (waveNumber == 1) {
      //find a 13 day high
      if (isMaxClose(13, i)) {
      PlotText( "1" , i , H[i]+dist[i]*0.15, colorBlack  );
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], High[i] + 10 * Point );
        //ObjectSetText(""+i, "1", 15, "Arial", Lime);
        
        waveNumber = 2;
      }
    }
    else if (waveNumber == 2) {
      //find a 8 day low
      if (isMinClose(8, i)) {
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], Low[i] - 10 * Point );
        //ObjectSetText(""+i, "2", 15, "Arial", Lime);
        
        waveNumber = 3;
      }
    }
    else if (waveNumber == 3) {
      //find a 21 day high
      if (isMaxClose(21, i)) {
       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],High[i] + 10 * Point );
       // ObjectSetText(""+i, "3", 15, "Arial", Lime);
        
        waveNumber = 4;
      }
    }
    else if (waveNumber == 4) {
      //find a 13 day low
      if (isMinClose(13, i)) {
       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],Low[i] - 10 * Point );
       // ObjectSetText(""+i, "4", 15, "Arial", Lime);
        
        waveNumber = 5;
      }
    }
    else if (waveNumber == 5) {
      //find a 34 day high
      if (isMaxClose(34, i)) {
       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],High[i] + 10 * Point );
       // ObjectSetText(""+i, "5", 15, "Arial", Lime);
        
        waveNumber = 0;
}

   
 }
}}


Edit: Ok, I just had a look at the loops trying to figure out why that syntax error was being thrown. It all comes down the functions used. if we use "limit" by assigning any value to it be it 0, 100 or setbarsrequired as I have done, all I end up with is a working afl with no errors but it won't plot anything. If however you remove the "value" aspect from "limit" the function loops will throw an "array out of subscript error". I have no idea why this does this.
 
Last edited:
Yes, i dont know if the code formatting got messed up in tj. If not, always align your code properly so that the structure/levels are visibly clear. Having multiple unaligned brackets makes it hard to read.



yes, the error will change. its a very silly one. When you see the error line, just read entire code from start again. If still stuck let me know.
Found the error. It really was silly. Referencing the wrong cell inside a loop I quite forgot that in AFL the syntax for inside the loop is pretty much the same as c++. I kept referencing startday when I should have been referencing the bar . made a few changes. Here is the code. It now plots the waves. However as you will see it plots too many points. I need to now figure out a loop to count through the bars and select the highest/lowest point in a wave to place the specific number.

Any ideas on how to do that. For example, if in wave "1" it records a number "1" and subsequently a new "1" is recorded higher than that then it should wipe the previous one and keep the new one.

further I'll also have to figure out a way to make another loop to prevent lower waves like "1" from showing up in wave "3" and "5".

If you have any ideas do let me know. I'll be happy to hunt for a solution once pointed in the right direction.

Code:
dist= 1.5*ATR(10);
   
   function isMaxClose(daysBack,startDay) {

  for ( i = 1; i < daysBack + 1; i++) {
    //if today's close less than a close in the past x days
    if (daysback[i] <= startday [i+ i])
      false;
  }
  
  return (True);
}   
 function isMinClose(daysBack,startDay) {

  for (i = 1; i < daysBack + 1; i++) {
    //if today's close greater than a close in the past x days
    if (daysback[i] >= startday [i+1])
      False;
  }
  
  return (True);
}    


/*datetime time;
int i;

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init() {
  return(0);
}

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit() {
  for(i = 0; i < Bars; i++)
	 ObjectDelete(""+i);

  return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start() {

   if(time!=Time[0]){for(i = 0; i < Bars; i++)
	 ObjectDelete(""+i);time=Time[0];}

  int counted_bars = IndicatorCounted();*/

  // counted_bars = 0;
   

  // check for possible errors
 // if(counted_bars < 0) 
  //  -1;
    

  // last counted bar will be recounted
  //if(counted_bars>0) 
   // counted_bars--;
    
   //limit = barrssince(SetBarsRequired(500));//Bars - counted_bars;
  
   waveNumber = 0;
   
   
   

  // For all bars starting 21 days from the earliest
 // if (BarCount >= limit) {
  for(i = BarCount - 20; i >= 0; i--) {
    //If wave has not started
    if (waveNumber == 0) { //Alert("0");
      //find a 21 day low
      if (isMinClose(21, i)) {
       PlotText( "0" , i , L[i]-dist[i]*0.15, colorBlack  ) ;
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], Low[i] - 10 * Point );
       // ObjectSetText(""+i, "A", 15, "Arial", Lime);
        waveNumber = 1;
        
      }
    }
    else if (waveNumber == 1) {
      //find a 13 day high
      if (isMaxClose(13, i)) {
      PlotText( "1" , i , H[i]+dist[i]*0.15, colorBlack  );
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], High[i] + 10 * Point );
        //ObjectSetText(""+i, "1", 15, "Arial", Lime);
        
        waveNumber = 2;
      }
    }
    else if (waveNumber == 2) {
      //find a 8 day low
      if (isMinClose(8, i)) {
      PlotText( "2" , i , L[i]-dist[i]*0.15, colorBlack  );
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], Low[i] - 10 * Point );
        //ObjectSetText(""+i, "2", 15, "Arial", Lime);
        
        waveNumber = 3;
      }
    }
    else if (waveNumber == 3) {
      //find a 21 day high
      if (isMaxClose(21, i)) {
      PlotText( "3" , i , H[i]+dist[i]*0.35, colorBlack  );
       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],High[i] + 10 * Point );
       // ObjectSetText(""+i, "3", 15, "Arial", Lime);
        
        waveNumber = 4;
      }
    }
    else if (waveNumber == 4) {
      //find a 13 day low
      if (isMinClose(13, i)) {
      
      PlotText( "4" , i , L[i]-dist[i]*0.35, colorBlack  ) ;
       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],Low[i] - 10 * Point );
       // ObjectSetText(""+i, "4", 15, "Arial", Lime);
        
        waveNumber = 5;
      }
    }
    else if (waveNumber == 5) {
      //find a 34 day high
      if (isMaxClose(34, i)) {
      PlotText( "5" , i , H[i]+dist[i]*0.55, colorBlack  );

       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],High[i] + 10 * Point );
       // ObjectSetText(""+i, "5", 15, "Arial", Lime);
        
        waveNumber = 0;
}

   
 }
}
 

TracerBullet

Well-Known Member
Found the error. It really was silly. Referencing the wrong cell inside a loop I quite forgot that in AFL the syntax for inside the loop is pretty much the same as c++. I kept referencing startday when I should have been referencing the bar . made a few changes. Here is the code. It now plots the waves. However as you will see it plots too many points. I need to now figure out a loop to count through the bars and select the highest/lowest point in a wave to place the specific number.

Any ideas on how to do that. For example, if in wave "1" it records a number "1" and subsequently a new "1" is recorded higher than that then it should wipe the previous one and keep the new one.

further I'll also have to figure out a way to make another loop to prevent lower waves like "1" from showing up in wave "3" and "5".

If you have any ideas do let me know. I'll be happy to hunt for a solution once pointed in the right direction.

Code:
dist= 1.5*ATR(10);
   
   function isMaxClose(daysBack,startDay) {

  for ( i = 1; i < daysBack + 1; i++) {
    //if today's close less than a close in the past x days
    if (daysback[i] <= startday [i+ i])
      false;
  }
  
  return (True);
}   
 function isMinClose(daysBack,startDay) {

  for (i = 1; i < daysBack + 1; i++) {
    //if today's close greater than a close in the past x days
    if (daysback[i] >= startday [i+1])
      False;
  }
  
  return (True);
}    


/*datetime time;
int i;

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init() {
  return(0);
}

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit() {
  for(i = 0; i < Bars; i++)
	 ObjectDelete(""+i);

  return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start() {

   if(time!=Time[0]){for(i = 0; i < Bars; i++)
	 ObjectDelete(""+i);time=Time[0];}

  int counted_bars = IndicatorCounted();*/

  // counted_bars = 0;
   

  // check for possible errors
 // if(counted_bars < 0) 
  //  -1;
    

  // last counted bar will be recounted
  //if(counted_bars>0) 
   // counted_bars--;
    
   //limit = barrssince(SetBarsRequired(500));//Bars - counted_bars;
  
   waveNumber = 0;
   
   
   

  // For all bars starting 21 days from the earliest
 // if (BarCount >= limit) {
  for(i = BarCount - 20; i >= 0; i--) {
    //If wave has not started
    if (waveNumber == 0) { //Alert("0");
      //find a 21 day low
      if (isMinClose(21, i)) {
       PlotText( "0" , i , L[i]-dist[i]*0.15, colorBlack  ) ;
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], Low[i] - 10 * Point );
       // ObjectSetText(""+i, "A", 15, "Arial", Lime);
        waveNumber = 1;
        
      }
    }
    else if (waveNumber == 1) {
      //find a 13 day high
      if (isMaxClose(13, i)) {
      PlotText( "1" , i , H[i]+dist[i]*0.15, colorBlack  );
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], High[i] + 10 * Point );
        //ObjectSetText(""+i, "1", 15, "Arial", Lime);
        
        waveNumber = 2;
      }
    }
    else if (waveNumber == 2) {
      //find a 8 day low
      if (isMinClose(8, i)) {
      PlotText( "2" , i , L[i]-dist[i]*0.15, colorBlack  );
        //ObjectCreate(""+i, OBJ_TEXT, 0, Time[i], Low[i] - 10 * Point );
        //ObjectSetText(""+i, "2", 15, "Arial", Lime);
        
        waveNumber = 3;
      }
    }
    else if (waveNumber == 3) {
      //find a 21 day high
      if (isMaxClose(21, i)) {
      PlotText( "3" , i , H[i]+dist[i]*0.35, colorBlack  );
       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],High[i] + 10 * Point );
       // ObjectSetText(""+i, "3", 15, "Arial", Lime);
        
        waveNumber = 4;
      }
    }
    else if (waveNumber == 4) {
      //find a 13 day low
      if (isMinClose(13, i)) {
      
      PlotText( "4" , i , L[i]-dist[i]*0.35, colorBlack  ) ;
       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],Low[i] - 10 * Point );
       // ObjectSetText(""+i, "4", 15, "Arial", Lime);
        
        waveNumber = 5;
      }
    }
    else if (waveNumber == 5) {
      //find a 34 day high
      if (isMaxClose(34, i)) {
      PlotText( "5" , i , H[i]+dist[i]*0.55, colorBlack  );

       // ObjectCreate(""+i, OBJ_TEXT, 0, Time[i],High[i] + 10 * Point );
       // ObjectSetText(""+i, "5", 15, "Arial", Lime);
        
        waveNumber = 0;
}

   
 }
}


1) In the original code, apart from the missing bracket, you had two nested loops both using i. This caused infinite loop, 2 loops seems to have been removed in new code, so that is gone

2) Note that in AB it is faster to work on arrays using array operations instead of loops. From what i understood, this may be because loops are interpreted whereas array operations are one single compiled commands. So try to write code using arrays, see AB reference, they have various functions for finding min/max.
This is just probably best practise, if there is no slowdowns in your code, then it doesnt really matter - i use lots of loops for things that i cant figure out with array ops.

3) I didnt read your code fully, and didnt run on my pc. i dont want to understand it :) . But still from what i half understood, you are going from last to first bar and printing 0-5. After 5 you reset it back to 0, this is why you get another series of numbers. If you dont want older numbers than beak from loop once you reach 5. If this is not what you want than pls explain in more detail

4) Try to write small batches of code at a time, test them and then add more. This way is much easier to debug issues and you can write next code batch with confidence that what you have till now is working. Its also easier to learn this way.

Anyway, i didnt get your min-max functions, you use daysBack and startDay as arrays but they are integers in input. You can use Close array for prices and inputs as indices or better look for array functions ..

Code:
 for ( i = 1; i < [B]daysBack + 1[/B]; i++) {
    //if today's close less than a close in the past x days
    if ([B]daysback[i][/B] <= startday [i+ i])
5) Read this , this and then maybe look for tutorials or look at existing afls and see how they work. It takes a little while to think in arrays.

Also finally, take all of my advice/opinion with a grain of salt, i just make do by searching for with what i need next and have not studied AB/afl fully.
 
Last edited:

Similar threads