Initial commit
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30011.22
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmaHmaCrossoverAlert", "SmaHmaCrossoverAlert\SmaHmaCrossoverAlert.csproj", "{a1056547-1f1b-47ec-bc9f-dbee33be00dc}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{a1056547-1f1b-47ec-bc9f-dbee33be00dc}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{a1056547-1f1b-47ec-bc9f-dbee33be00dc}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{a1056547-1f1b-47ec-bc9f-dbee33be00dc}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{a1056547-1f1b-47ec-bc9f-dbee33be00dc}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -0,0 +1,118 @@
|
||||
using System;
|
||||
using cAlgo.API;
|
||||
using cAlgo.API.Internals;
|
||||
using cAlgo.API.Indicators;
|
||||
|
||||
namespace cAlgo
|
||||
{
|
||||
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
|
||||
public class SmaHmaCrossoverAlert : Indicator
|
||||
{
|
||||
#region Parameters
|
||||
|
||||
[Parameter("SMA Period", DefaultValue = 200, MinValue = 1)]
|
||||
public int SmaPeriod { get; set; }
|
||||
|
||||
[Parameter("HMA Period", DefaultValue = 250, MinValue = 1)]
|
||||
public int HmaPeriod { get; set; }
|
||||
|
||||
[Parameter("Sound File Path", DefaultValue = @"C:\Windows\Media\Alarm05.wav")]
|
||||
public string SoundFilePath { get; set; }
|
||||
|
||||
[Parameter("Source")]
|
||||
public DataSeries Source { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Outputs
|
||||
|
||||
[Output("SMA", LineColor = "#FF01FF01", PlotType = PlotType.Line, Thickness = 2)]
|
||||
public IndicatorDataSeries SmaResult { get; set; }
|
||||
|
||||
[Output("HMA", LineColor = "#FFFF6666", PlotType = PlotType.Line, Thickness = 2)]
|
||||
public IndicatorDataSeries HmaResult { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fields
|
||||
|
||||
private SimpleMovingAverage _sma;
|
||||
private HullMovingAverage _hma;
|
||||
private int _lastAlertBarIndex;
|
||||
|
||||
#endregion
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
_sma = Indicators.SimpleMovingAverage(Source, SmaPeriod);
|
||||
_hma = Indicators.HullMovingAverage(Source, HmaPeriod);
|
||||
_lastAlertBarIndex = -1;
|
||||
}
|
||||
|
||||
public override void Calculate(int index)
|
||||
{
|
||||
// Update indicator values
|
||||
SmaResult[index] = _sma.Result[index];
|
||||
HmaResult[index] = _hma.Result[index];
|
||||
|
||||
// Ensure sufficient data exists
|
||||
if ((index <= SmaPeriod) || (index <= HmaPeriod))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Processing alerts only for the last bar (real-time) to prevent playing sounds during backfill
|
||||
if (!IsLastBar)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Prevent multiple alerts on the same bar
|
||||
if (index == _lastAlertBarIndex)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
double smaCurr = _sma.Result[index];
|
||||
double smaPrev = _sma.Result[index - 1];
|
||||
double hmaCurr = _hma.Result[index];
|
||||
double hmaPrev = _hma.Result[index - 1];
|
||||
|
||||
// Condition 1: SMA Slope
|
||||
bool isSmaRising = smaCurr > smaPrev;
|
||||
bool isSmaFalling = smaCurr < smaPrev;
|
||||
|
||||
// Condition 2: Crossover
|
||||
// CrossUp: Previous HMA was below/equal SMA, Current HMA is above SMA
|
||||
bool isCrossUp = (hmaPrev <= smaPrev) && (hmaCurr > smaCurr);
|
||||
|
||||
// CrossDown: Previous HMA was above/equal SMA, Current HMA is below SMA
|
||||
bool isCrossDown = (hmaPrev >= smaPrev) && (hmaCurr < smaCurr);
|
||||
|
||||
// Signal Evaluation
|
||||
if ((isSmaRising) && (isCrossUp))
|
||||
{
|
||||
PlayAlert(index);
|
||||
}
|
||||
else if ((isSmaFalling) && (isCrossDown))
|
||||
{
|
||||
PlayAlert(index);
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayAlert(int index)
|
||||
{
|
||||
_lastAlertBarIndex = index;
|
||||
|
||||
// Try/Catch block to prevent indicator crash if file is missing
|
||||
try
|
||||
{
|
||||
Notifications.PlaySound(SoundFilePath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Print("Error playing sound: {0}", ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="cTrader.Automate" Version="*" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user