Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Simple Audio Alert

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Simple Audio Alert

    Hi,

    I want to use the Strategy builder to play Audio Alerts, because:

    1) then I'll be able to copy alerts between templates/workspaces easily and more importantly
    2) one of my most important indicators doesn't support Audio Alerts, but it does output into Ninja strategy.

    This is a simple strat I tried to make in the strategy builder,
    if any kind person could direct me as to what I did wrong,
    I'd appreciate that as the alert is not working at all.

    Thank you.

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class EmaAudioAlert : Strategy
    {
    private EMA EMA1;
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Ema Audio Alert";
    Name = "EmaAudioAlert";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    EMA1 = EMA(Close, 2);
    EMA1.Plots[0].Brush = Brushes.Goldenrod;
    AddChartIndicator(EMA1);
    }
    }
    
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 1)
    return;
    
    // Set 1
    if (CrossAbove(EMA1, EMA1, 1))
    {
    PlaySound(@"C:\Program Files\NinjaTrader 8\sounds\Alert2.wav");
    }
    
    }
    }
    }

    #2
    Hello davydhnz,

    Thanks for your post.

    'Play sound' (Misc > Play sound) could be used in the Actions section of the Conditions and Actions screen of the Strategy Builder to play a sound when a certain condition becomes true.

    I see in your script that you are comparing EMA indicators that have the same exact period in your condition. You would instead need to compare two EMA indicators with different periods.

    I have attached a simple example script created with the Strategy Builder to demonstrate this concept that you could view.

    Also, here is a demonstration video showing the attached example script being run. Note that a sound plays when the EMA(10) crosses above EMA(25): https://brandonh-ninjatrader.tinytak...Ml8yMDcxMDM4OQ

    See this help guide page for more information about creating Strategy Builder Actions: https://ninjatrader.com/support/help...eActionsWindow

    Let me know if I may assist further.
    Attached Files
    Last edited by NinjaTrader_BrandonH; 12-20-2022, 08:42 AM.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Thanks so much for that Brandon.

      I have a question though. Are you saying that you can't compare different bars of a single MA?
      i.e. (0) current bar crosses above (1) previous bar?

      Comment


        #4
        Hello davydhnz,

        Thanks for your note.

        It is possible to compare different BarsAgo values of two EMA indicators that use the same period in a Strategy Builder condition.

        However, you would need to use a Greater or Less than comparison to compare these values instead of CrossAbove or CrossBelow.

        This is because when the same indicator period is used but you compare a BarsAgo 0 value to a BarsAgo 1 value, the comparison is only comparing the current price and previous price of the same Series (EMA1) and not two Series.

        CrossAbove conditions compare two Series to see if the first series crosses above the second Series based on the lookback period provided. When you use the same period then you are comparing the same Series.

        CrossAbove(ISeries<double> series1, ISeries<double> series2, int lookBackPeriod)

        ​For example, in the code you shared we see the same Series (EMA1) is being compared. Instead, you would need to use a different period as seen in the example script shared in post # 2. When we view the code of the example script, we see two Series are being compared, EMA1 and EMA2.

        Please let me know if I may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          i would suggest adding state check:
          if (State == State.Realtime)
          {
          PlaySound(@"D:\Documents\NinjaTrader 8\sounds\tada.wav");
          }​

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Today, 05:17 AM
          0 responses
          50 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          126 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          69 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          42 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          46 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X