How to initialize a variable -error 29

My intention was to make AFL where both 15 min & 1 hr timeframe %k is greater than %D, there should be a buy signal & similarly sell signal when opposite happens.

I could successfully plot multiple time frame after getting guidance from awilson but when I bring BUY & SELL, there are error 29 and it says buy1 & sell1 are variables used without being initialized. Seeing this, I tried using initializing buy1 & sell1 as 0 on top of the code and there was no error but the AFL does not show any Buy or Sell signals. Can anyone guide me how to solve this

GraphXSpace = 10; 
ChartType = ParamList("Chart Type", "Price Chart|15min|hourly|daily"); 

/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
15min
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/

if (ChartType == "15min") 
{
TimeFrameSet( in15Minute ); 
	periods 	= 5;							
	KSmoothing 	= 2;							
	DSmoothing 	= 2;	
			div = HHV(H, periods)-LLV(L, periods);// replace zero values with 0.0001
			div = IIf( div == 0, 0.0001, div ); 

			FSK = 100*(C-LLV(L, periods))/(div);// FASTSTOCHK
			FLSK = EMA( FSK, KSmoothing );// FULLSTOCHK		
												
				
			TimeFrameRestore(); 
			
	tempk = TimeFrameExpand(FSK, in15Minute, mode=expandFirst);
	tempd = TimeFrameExpand(FLSK, in15Minute, mode=expandFirst);
	
	buy1 = IIf(tempk>tempd,1,0);
	Sell1 =IIf(tempd>tempk,1,0); 	
	
Plot( tempk, "FSK", ParamColor( "ColorK", colorRed ),styleLine);
Plot( tempd, "FLSK", ParamColor( "ColorD", colorBlue ), StyleLine);}


/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1 HOUR
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/								

if (ChartType == "hourly") 
{
TimeFrameSet( inHourly ); 
	periods 	= 14;						
	KSmoothing 	= 2;						
	DSmoothing 	= 2;	
			div = HHV(H, periods)-LLV(L, periods);// replace zero values with 0.0001
			div = IIf( div == 0, 0.0001, div ); 
			FSK = 100*(C-LLV(L, periods))/(div); 												
			FLSK = EMA( FSK, KSmoothing ); 
																	
		TimeFrameRestore(); 									// restore time frame to original for plotting
	
	
	tempk = TimeFrameExpand(FSK, inHourly, mode=expandFirst);
	tempd = TimeFrameExpand(FLSK, inHourly, mode=expandFirst);
	buy2 = IIf(tempk>tempd,1,0);
	Sell2 =IIf(tempd>tempk,1,0); 

Plot( tempk, "FSK", ParamColor( "ColorK", colorRed ),styleLine);
Plot( tempd, "FLSK", ParamColor( "ColorD", colorBlue ), StyleLine);}

/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1 Daily
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/

if (ChartType == "daily") 
{
TimeFrameSet( inDaily ); 
periods 		= 22;							
	KSmoothing 	= 2;							
	DSmoothing 	= 2;	
			
			div = HHV(H, periods)-LLV(L, periods);// replace zero values with 0.0001
			div = IIf( div == 0, 0.0001, div ); 

			FSK = 100*(C-LLV(L, periods))/(div); 										
			FLSK = EMA( FSK, KSmoothing ); 														
			
		
		TimeFrameRestore(); 									
	
	
	tempk = TimeFrameExpand(FSK, indaily, mode=expandFirst);
	tempd = TimeFrameExpand(FLSK, indaily, mode=expandFirst);
	
	
Plot( tempk, "FSK", ParamColor( "ColorK", colorRed ),styleLine);
Plot( tempd, "FLSK", ParamColor( "ColorD", colorBlue ), StyleLine);}


/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
STRATEGY
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/

Buy= buy1 AND buy2;
Sell= Sell1 AND Sell2;
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
//Cover= ExRem(Cover,Short);
//Short=ExRem(Short,Cover);

if (ChartType == "Price Chart") 
{
	Plot(C,"C",colorBlack,styleCandle);
PlotShapes( shapeUpArrow*Buy, colorGreen, 0, L, -20 );
PlotShapes( shapeDownArrow*Sell, colorRed, 0, H, -20 );
	
}

You wrote that you only buy when buy1 is true and buy2 is true. If you initialize buy1 with zero and do not change it, guess what ? Buy will never be true

Sir, if you tell me how to change buy1 value from initialized 0 values. I was trying to change buy1 using iif function and giving it a value 1 when conditions are true else 0. what wrong in my code/concept, if you guide or just give a hint and i shall try by myself

When you set ChartType to "15min", your AFL will set Buy1, but not Buy2. Therefore Buy1 AND Buy2 == 0.

When you set ChartType to "hourly", your AFL will set Buy2, but not Buy1. Therefore Buy1 AND Buy2 == 0.

When you set ChartType to "daily", your AFL will not set either Buy1 or Buy2. Therefore Buy1 AND Buy2 == 0.

Perhaps you mean to use OR rather than AND?

@mradtke, Sir , I want to use AND and not OR. I want to have a buy signal when in both timeframe buy1 & buy2 is true. Daily chart i have added in the AFL just to visualize in 3 different time frames and not for any buy sell signal. If hourly chart signals buy and same is for 15 min then only i shall have a buy signal in price chart. This was my intention. So i can not use OR

GraphXSpace = 10;
ChartType = ParamList( "Chart Type", "Price Chart|15min|hourly|daily" );

/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
15min
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/

TimeFrameSet( in15Minute );
periods 	= 5;
KSmoothing 	= 2;
DSmoothing 	= 2;
div = HHV( H, periods ) - LLV( L, periods ); // replace zero values with 0.0001
div = IIf( div == 0, 0.0001, div );

FSK = 100 * ( C - LLV( L, periods ) ) / ( div ); // FASTSTOCHK
FLSK = EMA( FSK, KSmoothing );// FULLSTOCHK


TimeFrameRestore();

tempk = TimeFrameExpand( FSK, in15Minute );
tempd = TimeFrameExpand( FLSK, in15Minute );

buy1 = IIf( tempk > tempd, 1, 0 );
Sell1 = IIf( tempd > tempk, 1, 0 );

if( ChartType == "15min" )
{
    Plot( tempk, "FSK", ParamColor( "ColorK", colorRed ), styleLine );
    Plot( tempd, "FLSK", ParamColor( "ColorD", colorBlue ), StyleLine );
}


/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1 HOUR
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/

TimeFrameSet( inHourly );
periods 	= 14;
KSmoothing 	= 2;
DSmoothing 	= 2;
div = HHV( H, periods ) - LLV( L, periods ); // replace zero values with 0.0001
div = IIf( div == 0, 0.0001, div );
FSK = 100 * ( C - LLV( L, periods ) ) / ( div );
FLSK = EMA( FSK, KSmoothing );
TimeFrameRestore(); 									// restore time frame to original for plotting

tempk = TimeFrameExpand( FSK, inHourly);
tempd = TimeFrameExpand( FLSK, inHourly);
buy2 = IIf( tempk > tempd, 1, 0 );
Sell2 = IIf( tempd > tempk, 1, 0 );

if( ChartType == "hourly" )
{
    Plot( tempk, "FSK", ParamColor( "ColorK", colorRed ), styleLine );
    Plot( tempd, "FLSK", ParamColor( "ColorD", colorBlue ), StyleLine );
}

/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1 Daily
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/

TimeFrameSet( inDaily );
periods 		= 22;
KSmoothing 	= 2;
DSmoothing 	= 2;

div = HHV( H, periods ) - LLV( L, periods ); // replace zero values with 0.0001
div = IIf( div == 0, 0.0001, div );

FSK = 100 * ( C - LLV( L, periods ) ) / ( div );
FLSK = EMA( FSK, KSmoothing );
TimeFrameRestore();

tempk = TimeFrameExpand( FSK, inDaily );
tempd = TimeFrameExpand( FLSK, inDaily );

if( ChartType == "daily" )
{
    Plot( tempk, "FSK", ParamColor( "ColorK", colorRed ), styleLine );
    Plot( tempd, "FLSK", ParamColor( "ColorD", colorBlue ), StyleLine );
}


/*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
STRATEGY
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX*/

Buy = buy1 AND buy2;
Sell = Sell1 AND Sell2;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
//Cover= ExRem(Cover,Short);
//Short=ExRem(Short,Cover);

if( ChartType == "Price Chart" )
{
    Plot( C, "C", colorBlack, styleCandle );
    PlotShapes( shapeUpArrow * Buy, colorGreen, 0, L, -20 );
    PlotShapes( shapeDownArrow * Sell, colorRed, 0, H, -20 );

}
1 Like

Then as @awilson has kindly shown you, you must make sure to assign both Buy1 and Buy2 outside of the ChartType conditionals.

Sir many thanks for showing me the way and its your kindness to give full AFL. Thank you sir.

Thnk you sir for your comments and look at my problem