Best way to send email for signals

1 Like

Thanks . So to send email as and when signals are generated if i use a loop like:

for( i = 0; i < BarCount; i++ ) 
if( Buy[i] ) 

Above code would send email for the whole set of data. But instead i want everyday when i run systems, the signals which comes only on that day to be send , instead of all the past signals as well. I was thinking of using some sort of date check.
How would you go about solving this problem?

I was thinking of doing the below code for the above problem:

if(Buy[i] AND barcomplete[i])

Kindly suggest better methods.

Why do you use loops? They are not needed at all.

if( LastValue( Buy ) ) SendEmail(...);
1 Like

plus some checks to send mail just one time and not at every tick received in real time

Or use AlertIf() that automatically handles that “check” and does not send duplicate,

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

I have given SendEmail example instead of AlertIF because typically users complain that “they don’t get email sent”. Repetition preventing logic is eluding to many of people and they only start to think once they receive 60 mails per minute.

1 Like

Yes agree. But i have coded it like following:

if (LastValue(Ref(Buy,-1)) AND StaticVarGet(Name()+GetChartID()+"buyAlert")==0)
StaticVarSet(Name()+ GetChartID() + "buyAlertBar", LastValue(TimeNum()));
if (LastValue(TimeNum()) == StaticVarGet(Name()+GetChartID()+"buyAlertBar"))
StaticVarSet(Name()+GetChartID()+"buyAlert",1); //alert was triggered, no more alerts on this bar
StaticVarSet(Name()+GetChartID()+"buyAlert",0); // new bar formed, and alerts can be trigered.

nice to know, I didn’t notice that AlertIf does the repetition check internally

I was used to write some code like the one posted by akshay to that check

At this point I ask you if AB has some other function to do the anti repetition check in other cases, I think to order that has to be sent just one time for a signal, because a solution written by you it is for sure better written that by me and also more simple because it’s not need to re-invent the wheel every time

AlertIf is the only AFL function that does that. Of course in other areas there is some logic too (like preventing multiple entries when Buy array has repeated signals (state form instead of signal form)). From experience and feedback I received about AlertIf I found out that the more “inner logic” given function has the more likely is not what users want. So simpler, single feature per function, no-hidden logic solutions are preferred in long term.

1 Like

I understand, maybe you could consider a function like OneTimeForCandleIf specialized to just do the anti-repetition check avoiding all the code with static variables done by akshay.gupta55

just an idea for your next release

That is unlikely. With declared static variables it is way easier to code these things.

Besides “one time for candle” is not what AlertIf is doing. AlertIf sends given order once not per candle, but ONCE for symbol unless other signal (such as sell/cover) happens that resets the state.

Proper coding of such things involves State machine mechanisms that are easily implemented with static vars.

Version( 6.26 );
// code for illustration purposes only - not for copy pasting
#pragma enable_static_decl(myprefix)

function SendEmailOnStateChange( newstate )
  static _state;

  if( _state != newstate ) 
    text = StrFormat("State changed from %g to %g", _state, newstate );
    SendEmail( text, "" );
    _state = newstate;

if( LastValue( Buy ) ) SendEmailOnStateChange( 1 );
if( LastValue( Sell ) ) SendEmailOnStateChange( 2 );

That is closer to what AlertIf is doing (AlertIf is doing a bit more).

@akshay.gupta55 code is illustration how NOT to code. It violates one of basic principles of coding: Never write the same code twice. His code contains lots of repetitions. At absolute minimum he should put the var name into variable

sname = Name()+GetChartID()+"buyAlert";
StaticVarGet( sname ); // use 'sname'  variable instead of copy-pasting over and over

and use it later in the code instead of doing copy-paste.

MS telemetry data show that clipboard Paste is most abused feature ever. Kids may use copy-paste to copy wikipedia to their homework but when it comes to coding you should avoid copy-pasting. It is evil. The rule is never ever repeat the same code twice. If you have any repetition use variable and/or function.


Much appreciate your comments. It helps me improve my coding and understanding amibroker .

I have been trying to make a function similar to what you suggested in amibroker 6.2.
I get following error:

you need to upgrade to latest beta

1 Like

Hi im a newbie here, learning to code AFL
My trading system is a long and short system where i intend to exit and enter again on the same candle.
I am using alertif function to notify me each time a buy/sell/short/cover signal comes in.
I am not using the same conditions for buy/cover OR Short/Sell, however the criteria defining cover is a subset of the criteria defining Buy. Similarly sell is a subset of Short.
Coming to my problem, when i run a scan say for 5 sec for a particular symbol, the first signal i get is a
Buy or Short signal which is fine. Now when the exit criteria(sell/cover) matches, the sell/cover signals are picked first by alertif and there are instances when sell is followed by Buy again in the same scan as both the criteria matches. In this case, since alertif stores the last state for every new scan(5sec) either cover/buy or sell/short is getting repeated in a continuous loop.
Kindly let me know how to overcome this.

You should really post your code here.
Your post just outlines a common issue among others (user-caused) that you can also search the forum for the same.

Please find it below. ```
SetChartOptions( 0,chartShowArrows|chartShowDates );
Plot( C, "Close", ParamColor( "Color", colorBlack ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

SetBarsRequired( 100000, 0 );

SetPositionSize( 1, spsShares );

x = Param( "fast",10,10,21,1);
y = Param( "slow",21,21,42,1);
z = Param("smooth",6,6,12,1);

macdline = MACD(x,y);
Signalline = Signal(x,y,z);

Buy = (Cross(macdline,signalline) AND rsi()> 60) OR Cross(Ref(macdline,-1),Ref(signalline,-1));
Sell = Cross(Ref(signalline,-2),Ref(macdline,-2)) OR (Cross(signalline,macdline) AND RSI()<60);
Short = Cross(Ref(signalline,-1),Ref(macdline,-1)) OR (Cross(signalline,macdline) AND rsi()<40);
Cover = (Cross(macdline,signalline) AND RSI()>40) OR Cross(Ref(macdline,-2),Ref(signalline,-2));


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

PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorGreen, 0, L, Offset = -10 );
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorLime, 0, L, Offset = -15 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorWhite, 0, L, Offset = -12 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, Offset = -35 );
PlotShapes( IIf( Short, shapeSquare, shapeNone ), colorRed, 0, H, Offset = 10 );
PlotShapes( IIf( Short, shapeSquare, shapeNone ), colorOrange, 0, H, Offset = 15 );
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorWhite, 0, H, Offset = -12 );
PlotShapes( IIf( Cover, shapeUpArrow, shapeNone ), colorBlue, 0, L, Offset = -35 );

AlertIf( Buy, "EXEC https://", "textbuy",1);
AlertIf( Sell, "EXEC https://", "textsell",2);
AlertIf( Short, "EXEC https://", "textshort",3);
AlertIf( Cover, "EXEC https://", "textcover",4);

Hello again,

Can any senior members help? I searched for the same. Any idea on how to mitigate the continuous loop will aid me.
Thanks in advance.

Are your signals repainting? I think you first need to "fix" or handle that.

You will have to work harder on the logic, it cant be just that simple ( using Cross() ) for the trades especially when you are trying to exit on the same bar.

In theory, say a manual scalper, for a large bar on 5m TF, you can buy then sell again short the top and cover near low for an inverted bearish hammer pattern in a single bar. The question, "is your coded logic fit enough" to do that ?

Thanks. Point taken sir. I am exactly trying to do the same. Repainting happens. I will look in to my logic again and improve it. Currently, i have changed my condition to previous candle close and removed rsi. Thinking of using momentum indicators.