Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Enable/disable sound conditions in a indicator

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

    Enable/disable sound conditions in a indicator

    I have written a simple indicator that plot a marker and play a sound. How can I improve it with the possibility of enable/disable that sounds whan I put it in a chart?

    Thanks a lot

    #2
    Hello Impeesa,

    Thank you for your note.

    You'd want to use a bool user input that can be checked true or false. Here's an example of how you might set that up in a script:

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class SampleAlertToggle : Strategy
        {
            private Brush Brush1;
            private Brush Brush2;
            private SMA SMA1;
            private SMA SMA2;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "SampleAlertToggle";
                    Calculate                                    = Calculate.OnBarClose;
                    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;
                    Fast                    = 10;
                    Slow                    = 25;
                    AlertsOn                 = true;
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {                
                    SMA1                = SMA(Close, Convert.ToInt32(Fast));
                    SMA2                = SMA(Close, Convert.ToInt32(Slow));
                    SMA1.Plots[0].Brush = Brushes.Goldenrod;
                    SMA2.Plots[0].Brush = Brushes.Goldenrod;
                    AddChartIndicator(SMA1);
                    AddChartIndicator(SMA2);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (BarsInProgress != 0) 
                    return;
    
                if (CurrentBars[0] < 1)
                    return;
    
                 // Set 1
                if (CrossAbove(SMA1, SMA2, 1))
                {
                    if(AlertsOn)
                    {
                        Alert(@"SampleAlertToggle_1", Priority.Medium, @"SMA Fast cross above", NinjaTrader.Core.Globals.InstallDir+@"\sounds\Alert1.wav", 0, Brushes.Transparent, Brush1);
                    }
                    Draw.ArrowUp(this, @"SampleAlertToggle Arrow up_1  " + Convert.ToString(CurrentBars[0]), false, 0, (High[0] + (2 * TickSize)) , Brushes.Lime);
                }
    
                 // Set 2
                if (CrossBelow(SMA1, SMA2, 1))
                {
                    if(AlertsOn)
                    {
                        Alert(@"SampleAlertToggle_1", Priority.Medium, @"SMA Fast cross below", NinjaTrader.Core.Globals.InstallDir+@"\sounds\Alert1.wav", 0, Brushes.Transparent, Brush2);
                    }
                    Draw.ArrowDown(this, @"SampleAlertToggle Arrow down_1  " + Convert.ToString(CurrentBars[0]), false, 0, (High[0] + (2 * TickSize)) , Brushes.Red);
                }
            }
    
            #region Properties
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="Fast", Description="Fast SMA Period", Order=1, GroupName="Parameters")]
            public int Fast
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="Slow", Description="Slow SMA Period", Order=2, GroupName="Parameters")]
            public int Slow
            { get; set; }
    
            [NinjaScriptProperty]
            [Display(Name="AlertsOn", Order=1, GroupName="Parameters")]
            public bool AlertsOn
            { get; set; }
    
            #endregion
    
        }
    }
    You'd just have to wrap the alert in a check for whether the bool is true or false as seen above.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi Kate. I have a similar question on this one relating to some simple code below. So the idea is to be able turn various parameters on and off at will while running various strategies. So I may wish to turn the PriceEMA parameter on and leave the lastbar and twobar parameters off. So I would just check the PriceEMA box. My understanding is that if I kept the lastbar and twobars parameters as false and just the PriceEMA as true then it would ignore those the two criteria below in on bar update (Lastbar&&Close[1]>Open[2]) because both of these flags are set as false so should not bother with the Close[1]>Open[1] etc. It would then just process the PriceEMA piece. The problem is it is requiring all of these to be true otherwise no trades are triggered. So how do I make it so whichever criteria I select as true only those are required to be actioned not all of them. I have tried or || but that doesn't work either as there may be three criteria selected but if only one is true instead of all selected as true then the trade will trigger just on one. Any ideas on this one? Thanks


      if (State == State.SetDefaults)
      {
      PriceEma =false;
      Lastbar =false;
      Twobars =false;
      }

      protected override void OnBarUpdate()
      {
      if((Ematrend&&Emashort[0]>Emamedium[0]&&Emamedium[0]>Emalong[0])
      &&(Lastbar&&Close[1]>Open[1])
      &&(Twobars&&Close[2]>Open[2])



      {
      EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(), @"Long1");
      EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(), @"Long2");
      EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(), @"Long3");
      }

      }


      #region Properties

      [NinjaScriptProperty]
      [Display(Name = "EMA Trend Up or Down", Order = 1, GroupName = "EMA", Description = "EMA")]
      public bool Ematrend
      { get; set; }

      [NinjaScriptProperty]
      [Display(Name = "Last Bar", Order = 1, GroupName = "Last Bars", Description = "Last Bars")]
      public bool Lastbar
      { get; set; }

      [NinjaScriptProperty]
      [Display(Name = "Two Bars", Order = 2, GroupName = "Last Bars", Description = "Last Bars")]
      public bool Twobars
      { get; set; }


      Comment


        #4
        Just close the sound display in your computer.

        and it works.

        Comment


          #5
          Hello djkiwi,

          Thank you for your reply.

          Unfortunately that's not really how conditions work - flipping the bool doesn't mean that condition no longer is considered. For something like this, you'd basically have to have a set of conditions for each scenario.

          So for example, if you've got three true/false conditions like in your example, you'd have to do something like this:

          Code:
          if (State == State.SetDefaults)
          {
          PriceEma =false;
          Lastbar =false;
          Twobars =false;
          }
          
          protected override void OnBarUpdate()
          {
          
          if (Ematrend && Lastbar && Twobars)
          {
          //Do Something if all are true
          }
          else if (!Ematrend && Lastbar && Twobars)
          {
          // Do something else if Ematrend is false and the other two are true
          }
          else if (Ematrend && !Lastbar && Twobars)
          {
          // Do something else if Lastbar is false and the other two are true
          }
          else if (Ematrend && Lastbar && !Twobars)
          {
          // Do something else if Twobars is false and the other two are true
          }
          else if (Ematrend && !Lastbar && !Twobars)
          {
          // Do something else if only Ematrend is true
          }
          else if (!Ematrend && Lastbar && !Twobars)
          {
          // Do something else if only LastBar is true
          }
          else if (!Ematrend && !Lastbar && Twobars)
          {
          // Do something else if only Twobars is true
          }
          else if (!Ematrend && !Lastbar && !Twobars)
          {
          // Do something else if all bools are false
          }
          
          }
          Please let us know if we may be of further assistance to you.
          Kate W.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by CortexZenUSA, Today, 12:53 AM
          0 responses
          1 view
          0 likes
          Last Post CortexZenUSA  
          Started by CortexZenUSA, Today, 12:46 AM
          0 responses
          1 view
          0 likes
          Last Post CortexZenUSA  
          Started by usazencortex, Today, 12:43 AM
          0 responses
          5 views
          0 likes
          Last Post usazencortex  
          Started by sidlercom80, 10-28-2023, 08:49 AM
          168 responses
          2,265 views
          0 likes
          Last Post sidlercom80  
          Started by Barry Milan, Yesterday, 10:35 PM
          3 responses
          12 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Working...
          X