Skip to content

Instantly share code, notes, and snippets.

@marketcalls
Last active October 2, 2020 17:33
Show Gist options
  • Save marketcalls/a795f6d73dc883c5d5c23f0559f4d148 to your computer and use it in GitHub Desktop.
Save marketcalls/a795f6d73dc883c5d5c23f0559f4d148 to your computer and use it in GitHub Desktop.
Alert IF Example
_SECTION_BEGIN("My First Indicator"); //Iam going to build my first indicator
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
SetChartOptions(0,chartShowArrows|chartShowDates); //Enable X-Axis (Date/Time Axis)
Plot(Close,"Candles",colorDefault,styleCandle); //Plot Candles
par1 = Param("Short EMA",20,2,100,1);
par2 = Param("Long EMA",50,2,100,1);
bullcolor = ParamColor("Bullcolor",colorGreen);
bearcolor = ParamColor("Bearcolor",colorRed);
Plot(EMA(Close,par1),"EMA-20",bullcolor , styleLine | styleThick); //Plot Historical Short EMA lines
Plot(EMA(Close,par2),"EMA-50",bearcolor , styleLine | styleThick); //Plot Historical Long EMA Lines
Plot( LastValue(EMA(Close,par1)),"Last EMA 20 Value",colorYellow, styleDashed); //Plot Recent Short EMA value in dots
//Strategy requires candle to complete so that signal stays
//Trading Logic - When to enter / When to Exit the positions
Buy = Cross(EMA(Close,par1),EMA(Close,par2)); //Positive EMA Crossover (True/False)
Sell = Cross(EMA(Close,par2),EMA(Close,par1)); //Negative EMA Crossover (True/False)
//What price to Buy ->Backtesting Purpose
BuyPrice = ValueWhen(Buy,Close); //Extract the candle close value when Buy signal occurs
SellPrice = ValueWhen(Sell,Close); //Extract the candle close value when sell signal occurs
//Signal will come true only on close of the candle
//Always signal will happen at next bar open
//Signal will not disappear here in this case
Buy = Ref(Buy,-1);
Sell = Ref(Sell,-1);
Short = Sell;
Cover = Buy;
SetTradeDelays(1,1,1,1);
BuyPrice = Open;
SellPrice = Open;
_SECTION_END();
_SECTION_BEGIN("Alerts");
//alerts disabled by default
alerts = ParamToggle("Alert","ON|OFF",0);
voice = ParamToggle("Voice","ON|OFF",0);
email = ParamToggle("Email","ON|OFF",0);
//default alerts are disabled
if(alerts) //if(alerts) -> if(alerts == TRUE) -> if(alerts == 1)
{
//AlertIF( Buy, "EMAIL", "Buy alert Triggered on "+Name() + " BuyPrice ="+BuyPrice+ " RSI= "+ValueWhen(Buy,RSI()), 1 );
//AlertIF( Sell, "EMAIL", "Buy alert Triggered on "+Name() + " SellPrice ="+SellPrice+ " RSI= "+ValueWhen(Sell,RSI()), 1 );
AlertIF( Buy, "SOUND C:\\Windows\\Media\\Ding.wav", "Audio alert", 2 );
AlertIF( Sell, "SOUND C:\\Windows\\Media\\Ding.wav", "Audio alert", 2 );
}
if(voice)
{
if(LastValue(Buy))
{
Say("Buy Alert");
}
if(LastValue(Sell))
{
Say("sell Alert");
}
}
if(email)
{
if(LastValue(Buy))
{
//we are not using timenum because -> timenum refers to bar time or candle open time or close timing depends upon your settings in preferences
//instead we are using now -> which refers to system clock
SendEmail("Buy: " + Name() + "Time =" + Now(), "Buy alert Triggered on "+Name() + " BuyPrice ="+BuyPrice+ " RSI= "+ValueWhen(Buy,RSI()));
}
if(LastValue(Sell))
{
SendEmail("Sell: " + Name() + "Time =" + Now(), "Sell alert Triggered on "+Name() + " SellPrice ="+BuyPrice+ " RSI= "+ValueWhen(Sell,RSI()));
}
}
_SECTION_END();
_SECTION_BEGIN("Trading Signals");
arrows = ParamToggle("Signals","ON|OFF",0);
if(arrows)
{
/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
}
_SECTION_END();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment