Static variable problem :|

Romeo1998

Active 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

Active 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:
 

Similar threads

Broker Special Offers

Intraday Higher Leverage

Save up to 90% in brokerage and get higher leverage for intraday trades.

Name:Phone:
Email:City:
State:
Are you a day trader?