Stochastic Oscillator with an alert for cross.
Stochastic Oscillator with an alert for cross.
12 Feb 2016, 07:50
I'm not sure if I'm missing something, but this code sends the alert sound properly. Problem is it doesn't stop sending it and I'm not sure how to fix it. It's a simple stochastic and I want the alert sent at the cross of the two lines but currently it just spams it nonstop. Did I use the wrong method? Code in the wrong place? Frankly any indicator I code has the problem of a sound alert every tick so help would be greatly appreciated.
using System; using cAlgo.API; using cAlgo.API.Internals; using cAlgo.API.Indicators; using cAlgo.Indicators; namespace cAlgo { [Indicator(IsOverlay = false, ScalePrecision = 0, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class StochasticCrossAlert : Indicator { private StochasticOscillator stoc; [Parameter("Sound ON", DefaultValue = true)] public bool PlaySound { get; set; } [Parameter("Media File", DefaultValue = "c:\\windows\\media\\reaction.mp3")] public string MediaFile { get; set; } [Parameter("K_Periods", DefaultValue = 8, MinValue = 1)] public int K_Period { get; set; } [Parameter("Slow_K", DefaultValue = 3, MinValue = 2)] public int Slow_K { get; set; } [Parameter("D_Period", DefaultValue = 3, MinValue = 0)] public int D_Period { get; set; } [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)] public MovingAverageType MAType { get; set; } [Output("%D", Color = Colors.Blue, PlotType = PlotType.Line, LineStyle = LineStyle.Lines)] public IndicatorDataSeries Percent_D { get; set; } [Output("%K", Color = Colors.Red)] public IndicatorDataSeries Percent_K { get; set; } protected override void Initialize() { // Initialize and create nested indicators stoc = Indicators.StochasticOscillator(K_Period, Slow_K, D_Period, MAType); } public override void Calculate(int index) { // Calculate value at specified index // Result[index] = ... Percent_K[index] = stoc.PercentK.LastValue; Percent_D[index] = stoc.PercentD.LastValue; if (stoc.PercentK.HasCrossedAbove(stoc.PercentD.LastValue, 1) && PlaySound == true) { Notifications.PlaySound(MediaFile); } if (stoc.PercentD.HasCrossedAbove(stoc.PercentK.LastValue, 1) && PlaySound == true) { Notifications.PlaySound(MediaFile); } } } }
Replies
silverslasher_13
13 Feb 2016, 00:04
It took me 6 nonstop hours to figure out something you could have told me in 6 minutes because I don't understand C# lol. I don't even understand the HasCrossedAbove method so I axed it entirely in favor of something simpler. This was my solution and it works to a degree I'm okay with but I'd like to see what your solution would have been. It's extremely crude I'm sure, but it's all I could come up with after pouring through material I didn't understand. Thanks again for the help.
using System; using cAlgo.API; using cAlgo.API.Internals; using cAlgo.API.Indicators; using cAlgo.Indicators; namespace cAlgo { [Indicator(IsOverlay = false, ScalePrecision = 0, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class StochasticCrossAlert : Indicator { private StochasticOscillator stoc; bool b = true; bool a = true; [Parameter("Sound ON", DefaultValue = true)] public bool PlaySound { get; set; } [Parameter("Media File", DefaultValue = "c:\\windows\\media\\reaction.mp3")] public string MediaFile { get; set; } [Parameter("K_Periods", DefaultValue = 8, MinValue = 1)] public int K_Period { get; set; } [Parameter("Slow_K", DefaultValue = 3, MinValue = 2)] public int Slow_K { get; set; } [Parameter("D_Period", DefaultValue = 3, MinValue = 0)] public int D_Period { get; set; } [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)] public MovingAverageType MAType { get; set; } [Output("%D", Color = Colors.Blue, PlotType = PlotType.Line, LineStyle = LineStyle.Lines)] public IndicatorDataSeries Percent_D { get; set; } [Output("%K", Color = Colors.Red)] public IndicatorDataSeries Percent_K { get; set; } protected override void Initialize() { // Initialize and create nested indicators stoc = Indicators.StochasticOscillator(K_Period, Slow_K, D_Period, MAType); } public override void Calculate(int index) { // Calculate value at specified index // Result[index] = ... Percent_K[index] = stoc.PercentK.LastValue; Percent_D[index] = stoc.PercentD.LastValue; if (stoc.PercentK.LastValue > stoc.PercentD.LastValue && a == true && PlaySound == true) { Notifications.PlaySound(MediaFile); a = false; b = true; } if (stoc.PercentD.LastValue > stoc.PercentK.LastValue && b == true && PlaySound == true) { Notifications.PlaySound(MediaFile); b = false; a = true; } } } }
@silverslasher_13
ClickAlgo
12 Feb 2016, 09:42
Hi,
I will not give you the solution, but instead give you some hints so you can learn and solve it yourself.
The conditions will always be true as soon as HasCrossedAbove is true, what you need is to implement a trigger that gets set and then reset when the conditions are false. So the first time the condition is true the trigger is activated, if it is activated do not play sound, when it is reset it becomes deactivated. There are many other ways to solve this.
Look up software engineering design patterns for this kind of scenario, this pattern happens time and time again, almost all simple to complex logic has a design pattern.
https://www.redweb.com/agency/blog/2014/january/design-patterns-part-one-brief-introduction
@ClickAlgo