Custom Moving Average Cross Indicator with alerts.

Created at 12 May 2023, 12:36
RA

rafiki.matu

Joined 12.05.2023

Custom Moving Average Cross Indicator with alerts.
12 May 2023, 12:36


Hey guys, 

Trying to code an indicator that gives and arrow notification when the fast MA crosses the slow MA either from above or below. 
How do i add a filter to the code to include the 200 EMA and only give a notification when the cross occurs above the 200 EMA for buy signals and below the 200 EMA for sell signals. 
Anything i try gives errors.

Below is the piece of code where am stuck. 

The code does work whenever there is a MA cross but i want to add a filter to only give alerts after incorporating the 200EMA. 

if(Functions.HasCrossedAbove(fastMA.Result, slowMA.Result, 0)){
                Chart.DrawIcon("HasCrossedAbove",ChartIconType.UpTriangle,Bars[index].OpenTime,Bars[index].Low-arrowOffset,Color.LightBlue);
                
                if (Playsound && Cup<2 ){
                    Notifications.PlaySound(SoundPath);
                    Cup++;
                    Cdown=0;
                    }
                
                }

            if (Functions.HasCrossedBelow(fastMA.Result, slowMA.Result, 0)){
                
                Chart.DrawIcon("HasCrossedBelow",ChartIconType.DownTriangle,Bars[index].OpenTime,Bars[index].High+arrowOffset,Color.Red);
                
                if (Playsound && Cdown<2 ){
                    Notifications.PlaySound(SoundPath);
                    Cdown++;
                    Cup=0;
                   
                    }
                
                }
        }

        

    }
}

 

Any help will be highly appreciated. 

Thanks. 


 


@rafiki.matu
Replies

firemyst
01 Jun 2023, 05:05

RE:

rafiki.matu said:

Hey guys, 

Trying to code an indicator that gives and arrow notification when the fast MA crosses the slow MA either from above or below. 
How do i add a filter to the code to include the 200 EMA and only give a notification when the cross occurs above the 200 EMA for buy signals and below the 200 EMA for sell signals. 
Anything i try gives errors.

Below is the piece of code where am stuck. 

The code does work whenever there is a MA cross but i want to add a filter to only give alerts after incorporating the 200EMA. 

if(Functions.HasCrossedAbove(fastMA.Result, slowMA.Result, 0)){
                Chart.DrawIcon("HasCrossedAbove",ChartIconType.UpTriangle,Bars[index].OpenTime,Bars[index].Low-arrowOffset,Color.LightBlue);
                
                if (Playsound && Cup<2 ){
                    Notifications.PlaySound(SoundPath);
                    Cup++;
                    Cdown=0;
                    }
                
                }

            if (Functions.HasCrossedBelow(fastMA.Result, slowMA.Result, 0)){
                
                Chart.DrawIcon("HasCrossedBelow",ChartIconType.DownTriangle,Bars[index].OpenTime,Bars[index].High+arrowOffset,Color.Red);
                
                if (Playsound && Cdown<2 ){
                    Notifications.PlaySound(SoundPath);
                    Cdown++;
                    Cup=0;
                   
                    }
                
                }
        }

        

    }
}

 

Any help will be highly appreciated. 

Thanks. 


 

 

Just test to see if the current closing price is above/below the 200MA.

//This is a mixture of sample/pseudo code but hopefully is enough to get you started
if (Bars.ClosePrices.LastValue > MA200.Result.LastValue)
{
	if (Functions.HasCrossedAbove(fastMA.Result, slowMA.Result, 0))
	{
		Chart.DrawIcon("HasCrossedAbove",ChartIconType.UpTriangle,Bars[index].OpenTime,Bars[index].Low-arrowOffset,Color.LightBlue);
                
                if (Playsound && Cup < 2 )
		{
                    Notifications.PlaySound(SoundPath);
                    Cup++;
                    Cdown=0;
		}
	}
}

else if (Bars.ClosePrices.LastValue < MA200.Result.LastValue)
{
	if (Functions.HasCrossedBelow(fastMA.Result, slowMA.Result, 0))
	{
		Chart.DrawIcon("HasCrossedBelow",ChartIconType.DownTriangle,Bars[index].OpenTime,Bars[index].High+arrowOffset,Color.Red);
                
                if (Playsound && Cdown < 2 )
		{
                    Notifications.PlaySound(SoundPath);
                    Cdown++;
                    Cup=0;
		}
	}
}

 


@firemyst