Static variable problem :|

Romeo1998

Well-Known Member
#1
Dear friends and respected seniors,
My afl generates many signals before completion of the candle(repainting), so i used static variables , but its not working :( I know i am making some mistake, but I am not able to find it :(
this is the code that i m using
Code:
_SECTION_BEGIN("Price100");
SetChartOptions(0,chartShowArrows|chartShowDates);
_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( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

Buy=Cross(MACD(), Signal());
PlotShapes(Buy*shapeUpArrow,colorWhite,0,L,-12);

DT = LastValue(DateNum());
TT = LastValue(TimeNum());
BuyVar = StaticVarGetText(Name() +"-Buy");
BuyVar1 = Name()+"-Buy-"+NumToStr(DT,1)+"-"+NumToStr(TT,1)+"-"+NumToStr(GetChartID(),1);
BuyL = LastValue(Buy);

if ( BuyL AND BuyVar != BuyVar1)
{
Filepath = "C:/buy_signal.txt";
fh = fopen( Filepath, "a");
if (fh)
fputs(BuyVar + "\n" + BuyVar1 + "\n",fh);
fclose( fh );
}
_SECTION_END();
This code appends the generated signals to a .txt file, but for a 15 min candle i m getting 15 signals :(
the value of static variable BuyVar is always null, I tried to set its value with StaticVarSet and StaticVarSetText, but still it remains null ( dont know y ) :(
these r the contents of that .txt file
Code:
NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045
Can anyone please kindly guide me and point out my mistake
Awaiting your replies,
Thank you.
 
Last edited:

LOVEENAJYOTHI

Well-Known Member
#2
Dear friends and respected seniors,
My afl generates many signals before completion of the candle(repainting), so i used static variables , but its not working :( I know i am making some mistake, but I am not able to find it :(
this is the code that i m using
Code:
_SECTION_BEGIN("Price100");
SetChartOptions(0,chartShowArrows|chartShowDates);
_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( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

Buy=Cross(MACD(), Signal());
PlotShapes(Buy*shapeUpArrow,colorWhite,0,L,-12);

DT = LastValue(DateNum());
TT = LastValue(TimeNum());
BuyVar = StaticVarGetText(Name() +"-Buy");
BuyVar1 = Name()+"-Buy-"+NumToStr(DT,1)+"-"+NumToStr(TT,1)+"-"+NumToStr(GetChartID(),1);
BuyL = LastValue(Buy);

if ( BuyL AND BuyVar != BuyVar1)
{
Filepath = "C:/buy_signal.txt";
fh = fopen( Filepath, "a");
if (fh)
fputs(BuyVar + "\n" + BuyVar1 + "\n",fh);
fclose( fh );
}
_SECTION_END();
This code appends the generated signals to a .txt file, but for a 15 min candle i m getting 15 signals :(
the value of static variable BuyVar is always null, I tried to set its value with StaticVarSet and StaticVarSetText, but still it remains null ( dont know y ) :(
these r the contents of that .txt file
Code:
NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045

NIFTY-Buy-1,171,103-103,000-1,045
Can anyone please kindly guide me and point out my mistake
Awaiting your replies,
Thank you.
Try "_" stead of "-" in StaticVar Names.
Hope it may eliminate null errors.
 

Romeo1998

Well-Known Member
#3
Try "_" stead of "-" in StaticVar Names.
Hope it may eliminate null errors.
Dear friend Loveenajyothi,
thank u for helping again :happy:
i was setting the staticvar value incorrectly :D ( a silly mistake , as always :D )
just adding 1 line of code before the if statement solved this problem
Code:
_SECTION_BEGIN("Price100");
SetChartOptions(0,chartShowArrows|chartShowDates);
_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( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

Buy=Cross(MACD(), Signal());
PlotShapes(Buy*shapeUpArrow,colorWhite,0,L,-12);

DT = LastValue(DateNum());
TT = LastValue(TimeNum());
BuyVar = StaticVarGetText(Name() +"-Buy");
BuyVar1 = Name()+"-Buy-"+NumToStr(DT,1)+"-"+NumToStr(TT,1)+"-"+NumToStr(GetChartID(),1);
BuyL = LastValue(Buy);

// i was setting the value of static variable incorrectly earlier
// this line solved my problem :)
StaticVarSetText(Name()+"-Buy",BuyVar1);

if ( BuyL AND BuyVar != BuyVar1)
{
Filepath = "C:/buy_signal.txt";
fh = fopen( Filepath, "a");
if (fh)
fputs(BuyVar + "\n" + BuyVar1 + "\n",fh);
fclose( fh );
}
_SECTION_END();
Thanks :happy:
 
#6
Dear friend Loveenajyothi,
thank u for helping again :happy:
i was setting the staticvar value incorrectly :D ( a silly mistake , as always :D )
just adding 1 line of code before the if statement solved this problem
Code:
_SECTION_BEGIN("Price100");
SetChartOptions(0,chartShowArrows|chartShowDates);
_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( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

Buy=Cross(MACD(), Signal());
PlotShapes(Buy*shapeUpArrow,colorWhite,0,L,-12);

DT = LastValue(DateNum());
TT = LastValue(TimeNum());
BuyVar = StaticVarGetText(Name() +"-Buy");
BuyVar1 = Name()+"-Buy-"+NumToStr(DT,1)+"-"+NumToStr(TT,1)+"-"+NumToStr(GetChartID(),1);
BuyL = LastValue(Buy);

// i was setting the value of static variable incorrectly earlier
// this line solved my problem :)
StaticVarSetText(Name()+"-Buy",BuyVar1);

if ( BuyL AND BuyVar != BuyVar1)
{
Filepath = "C:/buy_signal.txt";
fh = fopen( Filepath, "a");
if (fh)
fputs(BuyVar + "\n" + BuyVar1 + "\n",fh);
fclose( fh );
}
_SECTION_END();
Thanks :happy:

Dear Sir,

I am trying to use static variable for below mentioned condition. But couldn't succeed.

My moto is to select the sector first through Amibroker Exploratiion. Where, i am trying to set the Buy static variable But not understand whether correctly declared or not.

Say in My Watch list there are symbols like " NIFTYAUTO, NIFTYIT, NIFTYFMCG."

///////BELOW is the code for exploration ////////

MA1 = m20 = MA( Close, 20 ); // shorter SMA
MA3 = m200 = MA( Close, 200 ); // longer SMA

Buy = Cross ( m20 , m200 ) ;
Sell = TimeNum ()> 151500;

Buy = ExRem ( Buy , Sell ) ;
Sell =ExRem ( Sell , Buy ) ;

StaticVarSet("signals" + Name() , Buy);

//////////////////////////////

Now i wish to refer the selected Buy stock to another AFL. to generate Buy signal in equity stock. In amibroker symbol information define FullName for AUTO secttor stock as AUTO EQ.

Said Code in below

sig = StaticVarGet("signals" + Name ());

MA1 = m20 = MA( Close, 20 ); // shorter SMA
MA3 = m200 = MA( Close, 200 ); // longer SMAvv


Buy = Cross ( m20 , m200 ) AND FullName() == "AUTO EQ" and sig ==1 ;
Sell = TimeNum ()> 151500;

Buy = ExRem ( Buy , Sell ) ;
Sell =ExRem ( Sell , Buy ) ;

Filter = Buy OR Sell ;

AddColumn ( Buy , " Buy " , 1.2 ) ;

AddColumn ( Sell , " sell " , 1.2 ) ;

StaticVarRemove("signals" + Name () );


with this no BUY signal is generated. Please help in this.

Thanks and Regards.