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

making an alert sound only once on bar close

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

    making an alert sound only once on bar close

    i have an alert that i use but i cant seem to figure out how to make it only sound once when it is set to alerm on the bar close. Right now it will sound on each tick of the bar. I want the alert to sound once as soon as the the two lines it is looking at crosses on the current bar but not to sound on each tick jump. In other words i want it to be a live indicator i guess is what you would call it.

    if(sMAD[1] > sMA[1] && sMAD[0] < sMA[0] && sTime[0] == 1)
    {
    if(!Historical && sTime[0] == 1)
    {
    vPlayCount =
    3;
    PlaySound(iAlert);
    }
    }

    This is what i have for now.

    #2
    Hello cbart_1,

    You could set CalculateOnBarClose = true and it will only check on bar close.

    If you want to run your script with COBC = false, but only want this evaluated at bar close, could work with FirstTickOfBar for this, by adding FirstTickOfBar to your condition and referencing 1 bar back like in this reference sample.

    FirstTickOfBar is also a good mechanism for resetting a bool flag that could control sound playback.

    if (myBool)
    {
    PlaySound(iAlert);
    myBool = false;
    }

    if (FirstTickOfBar)
    myBool = true;
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      waht i said was wrong

      I meant i have the indicator calculate on bar close set to false. This way if the bar ticks going up or down make the lines cross i only want the alarm to sound once, because the bar ticks make the lines cross multiple times on 1 bar. I just want it to sound once. This way it wont sound the alarm on every tick in a 1, 5, 10, 15, or 30 minute bar.

      Comment


        #4
        Thanks for the reply. Using a bool flag and resetting at FirstTickOfBar is a good option for this then.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          i think i got it maybe

          I used the FirstTickOfBar and made a variable true and made it false in the crossing code. So i think this way it should set it back to being true on the beginning of each bar.

          if (FirstTickOfBar)
          {
          FBTick =
          true;
          }
          if(sMAD[1] > sMA[1] && sMAD[0] < sMA[0] && sTime[0] == 1 && FBTick == true)
          {
          if(!Historical && sTime[0] == 1)
          {
          vPlayCount =
          3;
          PlaySound(iAlert);
          FBTick =
          false;
          }
          }

          Comment


            #6
            Yes, that's close, but will also want to include a check to the bool flag before playing sound.

            if (FBTick)
            {
            PlaySound(iAlert);
            FBTick = false;
            }
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              now i get no alarm

              Ok this is how i put it in my code but now i dont get an alarm.

              if
              (FirstTickOfBar)
              {
              FBTick =
              true;
              }

              if(sMAD[1] > sMA[1] && sMAD[0] < sMA[0] && sTime[0] == 1)
              {
              if(!Historical && sTime[0] == 1)
              {
              if (FBTick)
              {
              vPlayCount =
              3;
              PlaySound(iAlert);
              FBTick =
              false;
              }
              }
              }

              Comment


                #8
                Sorry, I'm not sure why that wouldn't work. Using bool flags is a common technique for executing something only once. It does look like you took some other parts of your script and included it after the bool check. Maybe that part of your script needs to be executed more than once per bar? I would try using my previous two examples used, where only PlaySound and bool assignment executes after your FirstTick returns true.

                It's also good to learn techniques for debugging and simplifying your code, so you can follow code flow and work through these types of issues. Please see here for help debugging:
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  ok thank you it is someting wrong with my sound code. i will go through it and find out what is wrong.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by gemify, 11-11-2022, 11:52 AM
                  6 responses
                  803 views
                  2 likes
                  Last Post ultls
                  by ultls
                   
                  Started by ScottWalsh, Today, 04:52 PM
                  0 responses
                  3 views
                  0 likes
                  Last Post ScottWalsh  
                  Started by ScottWalsh, Today, 04:29 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post ScottWalsh  
                  Started by rtwave, 04-12-2024, 09:30 AM
                  2 responses
                  22 views
                  0 likes
                  Last Post rtwave
                  by rtwave
                   
                  Started by tsantospinto, 04-12-2024, 07:04 PM
                  5 responses
                  70 views
                  0 likes
                  Last Post tsantospinto  
                  Working...
                  X