Missing email with Alertif

I tried Alertif for the first time today, so much to learn...see my attempt below. Complete code should run okay... The alertif I have used sends emails reliably. However, the emails received are always 1 stock less than the list generated in the scan. That is, one buy and one sell signal are always missing. I have set the scan to date range and the start and ends dates to the same date. scan1 email
have tired many dates...always one sell and one buy missing. TIA Help appreciated.

maxpos = 5; // maximum number of open positions
SetOption("InitialEquity", 100000 ); // set initial equity = 100K
SetOption( "MaxOpenPositions", maxpos );
SetPositionSize( 100 / maxpos, spsPercentOfEquity );
SetOption("AllowSameBarExit", false);
SetOption("AllowPositionShrinking", true);
SetOption("InterestRate", 0);
//a=Optimize("maup",1.5, 1.5,1.51,.01);
//SetBacktestMode( backtestRegular );

minprice 	= C > 3;
turnover 	= C * V > 150000;

enter 		= BBandBot(c,55,1.25);
exit 		= BBandBot(c,5,0);
bollidown	= RSI(2)<25;
bolliup		= RSI(2)>70;

buyif 		= bollidown 
			AND minprice
			AND turnover
sellif 		= bolliup;

PositionScore = 1/C;

Buy 		= BUYIF;
Sell		= sellif;


amount = 40; // 10% loss
ApplyStop( stopTypeLoss, stopModePercent, amount, True );

Plot(enter,"Below 55",color=colorred,styleLine);
Plot(exit,"Above 1",color=colorgreen,styleLine);

AlertIf(LastValue(ValueWhen(Status("LastBarInRange"), Buy)), "EMAIL", "Buy " + Name(),1, lookback = 0);
AlertIf(LastValue(ValueWhen(Status("LastBarInRange"), sell)), "EMAIL", "sell " + Name(),2, lookback = 0);

If you remove both , lookback = 0 does that make a difference?

No I have run the test several times without lookback = 0 and the result is still missing emails.

Okay, I think I realise what I did wrong...setting the look back to 1, gives correct number of emails. I misunderstood what the parameter was doing.

So, my question now is how do I get emails for the set scan date? All emails are dated the day before the scan date. Image is email received that says buy yesterday???
image Note scan was run on 11/7/

TIA again.

Never used alertif() but perhaps it’s to do with the tradedelays(). Having them set at 1 will send a buy signal the next day?

Could be wrong and tradelays() only pertains to backtest and not scan.

Thanks for the reply, trade delays does not seem to impact the outcome, at least as far as I can tell. Just what the "rules" are for repeat emails from alertif remains a mystery until I find docs that describe it fully. Eg, how long does the non repetition last? Under what circumstances... It is good that it does not spam you with repeat signals were you to run a scan multiple times, and you can set it to email regardless. However, I have struggled to get a consistent result. I.e. a list of emails that match the scan window result. Will probably look at exporting a csv and post processing it into an email.
I should add, I am a complete novice at this function and my coding skills are limited...I am sure it can be made to work just fine, but not by me at present.


I don't use AlertIf either sorry. But try using the below.

AlertIF( Buy, "EMAIL", "A 'Buy' sample alert on "+FullName(), 1 );
AlertIF( Sell, "EMAIL", "A 'Sell' sample alert on "+FullName(), 2 );

Also see AlertIf.

Got up this am and ran some scans....all working perfectly with:
AlertIF( Buy, "EMAIL", "A sample alert on "+FullName(), 1 , 1+2, lookback = 0);
AlertIF( Sell, "EMAIL", "A sample alert on "+FullName(), 2, 1+2, lookback = 0);

Go figure??? Can't explain why so many problems yesterday...no reboot, pc asleep overnight.

Cheers all for responses.

The adjustments you made to your code is why it now works. :+1: