Triggering First Buy signal using Static Variable

Hello All,

I am trying to generate only first BUY signal based on below conditions using Static Variable and base timeframe is 1 minute.

  • Daily volume is 3 times more than 200 MA of daily volume
  • If price is more than high value of first 15 mins of the day opening (Day starts at 9:15 AM)

Once BUY signal is generated, calculating Stop Loss and Target based on current price and accordingly SELL signal is generated.

My code is not generating BUY signal. Below is the code. Request members to give comments to help me.

_SECTION_BEGIN("DSK-Trade");


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() ); 

SetBarsRequired( sbrAll );
Filter = 1;
i=62;
SLPct = 2;//SL Percentage based on entry price
TargetPct =2;//Target Percentage based on entry price

Buy[BarCount-1]=0;
Sell[BarCount-1]=0;
btn = TimeNum();

// define start/end hours in TimeNum format
StartTime = 91500;
Endtime = 92900;

// these conditions are true when TimeNum of the bar equals startime/endtime
StartBar = btn == StartTime;
EndBar = btn == Endtime;

// on the end bar we read the value of highest high or lowest low since the start bar
myH = ValueWhen( EndBar, HighestSince( StartBar, High ) );
myL = ValueWhen( EndBar, LowestSince( StartBar, Low ) );

TimeFrameSet(inDaily);
ChkVol = IIf(V>3*MA(V,200),1,0);
TimeFrameRestore();

ChkVol = TimeFrameExpand(ChkVol,inDaily,expandFirst);

//Check Entry condition and other calculations
if(Nz( StaticVarGet(Name()+"c2"+i) )  == 0 AND ChkVol[BarCount-1] == 1 AND Close[BarCount-1]>myH[BarCount-1] AND btn[BarCount-1]>Endtime)
{
Buy[BarCount-1]=1;
StaticVarSet(Name()+"c2"+i,1);
StaticVarSet(Name()+"c3"+i,0);

StaticVarSet( Name()+"StopLossPrice"+i,Close[BarCount-1] - ( SLPct*Close[BarCount-1]/100) );
StaticVarSet( Name()+"TargetPrice"+i,Close[BarCount-1] + ( TargetPct*Close[BarCount-1]/100 ) );
}

//SL
if ( Close[BarCount-1]<StaticVarGet(Name()+"StopLossPrice"+i) && StaticVarGet(Name()+"c3"+i)  == 0 )
{
StaticVarSet(Name()+"c3"+i,1);
Sell[BarCount-1]=1;
}

//Initial Target
if( Close[BarCount-1]>=StaticVarGet(Name()+"TargetPrice"+i) && StaticVarGet(Name()+"c3"+i)  == 0 )
{
StaticVarSet(Name()+"c3"+i,1);
Sell[BarCount-1]=1;
}

AddColumn(StartBar,"StartBar");
AddColumn(EndBar,"EndBar");
AddColumn(Close,"Close");
AddColumn(myH,"My High");
AddColumn(myL,"My Low");
AddColumn(ChkVol,"Chk Vol");
AddColumn(Buy,"Buy");
AddColumn(Sell,"Sell");

_SECTION_END();


Thanks
Shiva

Hi All,

I was trying to figure out what is wrong with my code and realized that I am using BARCOUNT-1 which is not correct because it is taking number based on number of bars but I am expecting it should take as Zero for the first bar of the day.
But again, when I am trying to use the code in Exploration based on a particular date then it should take first bar as Zero but it is not taking.

Not sure how to proceed from here, any inputs are highly appreciated.

I am looking to scan set of stocks based on below condition and enter into trade once only in a day and ignore rest of the signals.

  • Vol>3*MA(Vol,200) on daily time
  • BUY If price crosses above high of first 15 minutes
  • SELL if price crosses below low of first 15 minutes

Thanks
Shiva

See important links below.

Understanding How AFL Works.

Thanks. I will once again go through the suggestions given.

Shiva

For starters, you should never assume that a particular bar number corresponds to a particular date and time. Once you learn to think in terms of arrays (time series) that hold events, prices, indicator values, etc., everything will become much easier.

Thanks for your suggestion. I am trying again based on suggestions given by all.

Thanks
Shiva