Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple alerts on entering position

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

    Multiple alerts on entering position

    Im trying to write a simple strategy. This is a part for entering:

    Code:
    protected override void OnBarUpdate()
            {
                var boling = Bollinger(2, 20);
                var currentPrice = Bars.GetClose(Bars.CurrentBar);
    
                if (Position.MarketPosition == MarketPosition.Flat)
                {
                    // Short from upper border
                    if (currentPrice - boling.Upper[0] > TickSize * MinTicks)
                    {
                        if (Position.Quantity != 0)
                            return;
    
                        EnterShort();
                        _exitPrice = currentPrice - TickSize * ProfitTicks;
                        _stopPrice = currentPrice + TickSize * LossTicks;
                        _enterPrice = currentPrice;
                        MessageLog.WarningMessage(this, "EnteredShort", playSound: PlaySound);
                        return;
                    }
    /* .............. */
    Function Warning message:

    Code:
    public static void WarningMessage(NinjaScriptBase ind, string message, bool playSound = true, int cooldown = 0)
            {
                var soundPath = (playSound) ? @"\sounds\Alert4.wav" : "";
                ind.Alert("WARNING",
                    Priority.Medium,
                    message,
                    Core.Globals.InstallDir + soundPath,
                    cooldown,
                    Brushes.Chocolate,
                    Brushes.BlanchedAlmond);
            }
    This robot works well but when it enters or exits it starts to spam alert messages. For example:




    I always enter (and exit) just with 1 contract (One pip. One quantity. Whatever it called). So it must send just one alert message. Why it doesn't?

    And the robot sets wrong markers too. For example:



    These markers shows the deal was a loss (with commission). But I can see in my account window that it gave me profit. Why does this happen? How to fix the strategy?
    Last edited by Defake; 11-30-2016, 10:11 AM.

    #2
    Hello Defake,

    Thanks for your post and welcome to the NinjaTrader forums!

    If you are getting multiple alerts it is because the logic is issuing the alert multiple times. This can happen when you use calulationmode of Calculate.OnEachTick or Calculate.OnPriceChange. In your code example you are testing for MarketPosition to be flat which is a good check, however depending on the timing it may be some period of time before MarketPosition gets updated (waits for order confirmation from broker/exchange which happens asynchronously) and many ticks may have passed causing your logic to repeatedly send the alert message assuming the entry condition remains true. Depending on your entries per direction setting, if it was set to greater than 1 you would likely see multiple entry orders as well.

    You may want to add a bool that will change state on the first pass through the entry logic and use that bool as part of the entry conditions. You would also need to be able to reset the bool at an appropriate time.

    Can you provide a screenshot of your account performance that identifies that specific trade as a profit (along with entry/exit times and also a screenshot that shows the trade on the chart relative to the time base to ensure we are looking at the correct specific trade?

    Comment


      #3
      Originally posted by NinjaTrader_Paul View Post
      Hello Defake,

      Thanks for your post and welcome to the NinjaTrader forums!

      If you are getting multiple alerts it is because the logic is issuing the alert multiple times. This can happen when you use calulationmode of Calculate.OnEachTick or Calculate.OnPriceChange. In your code example you are testing for MarketPosition to be flat which is a good check, however depending on the timing it may be some period of time before MarketPosition gets updated (waits for order confirmation from broker/exchange which happens asynchronously) and many ticks may have passed causing your logic to repeatedly send the alert message assuming the entry condition remains true. Depending on your entries per direction setting, if it was set to greater than 1 you would likely see multiple entry orders as well.

      You may want to add a bool that will change state on the first pass through the entry logic and use that bool as part of the entry conditions. You would also need to be able to reset the bool at an appropriate time.

      Can you provide a screenshot of your account performance that identifies that specific trade as a profit (along with entry/exit times and also a screenshot that shows the trade on the chart relative to the time base to ensure we are looking at the correct specific trade?

      Hello, thanks =)

      Ohh, I have deleted all bool variables and thought that it will be better to use Ninjatrader API =D Okay, I'll get them back, thank you!

      Ohh I saw this fake-profit trade yesterday and this strategy trades too much, so I can't find it now. Anyway this robot trades when it shouldn't (for example it should enters outside the bollinger, but sometimes I see that it enters inside the bollinger). Maybe it's connected with this multiple enters issue. So I'll fix it first.

      By the way, can it be that the strategy calls the EnterLong() at one price and then for some reason (e.g. too quick price changing) actually enters the trade with different price? Maybe it's better to set limit orders?

      Comment


        #4
        Hello Defake,

        Thanks for your reply.

        Yes, market orders are subject to filling price at the markets convenience, whereas limit orders will get you your price but may not be filled.

        Comment


          #5
          Originally posted by NinjaTrader_Paul View Post
          Hello Defake,

          Thanks for your reply.

          Yes, market orders are subject to filling price at the markets convenience, whereas limit orders will get you your price but may not be filled.
          Thanks, I will change all to limits.

          May I ask another question in this thread? How can I know (and change) bar-time in code? I mean this:



          Maybe I see one chart, but the strategy sees another


          By the way. I fixed multiple messages and now Im really sure that EnterLong() and EnterShort() are bad functions. Because my robot thinks that it was short trade with profit:




          But actually it is:

          Comment


            #6
            Hello Defake,

            Thanks for your post.

            I will answer your last question first.

            Your code was assuming your entry price was _enterPrice = currentPrice; You can find the actual entry price with Position.AveragePrice: http://ninjatrader.com/support/helpG...erageprice.htm

            If you want to change the bar and time selections in the drop down window, select "configure" in the bottom right corner of the dropdown window.

            When you apply the strategy to a chart, the strategy will use the bars in the chart as its base series. If you apply the strategy to the strategy analyzer or run the strategy from the strategy tab you will need to specify the instrument and bar type and value (time or ticks as needed for the bar type).

            Comment


              #7
              Originally posted by NinjaTrader_Paul View Post
              Hello Defake,

              Thanks for your post.

              I will answer your last question first.

              Your code was assuming your entry price was _enterPrice = currentPrice; You can find the actual entry price with Position.AveragePrice: http://ninjatrader.com/support/helpG...erageprice.htm

              If you want to change the bar and time selections in the drop down window, select "configure" in the bottom right corner of the dropdown window.

              When you apply the strategy to a chart, the strategy will use the bars in the chart as its base series. If you apply the strategy to the strategy analyzer or run the strategy from the strategy tab you will need to specify the instrument and bar type and value (time or ticks as needed for the bar type).
              Okay, I see. Thanks for your help!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Yesterday, 05:17 AM
              0 responses
              71 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              143 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              76 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              47 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              51 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X