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

How to evaluate logic on previous closed bar

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

    How to evaluate logic on previous closed bar

    Hi,

    I am new to Ninja scripting. Appreciate if someone Can share thoughts on below

    I am working on an indicator that evaluates a condition on the bar that just closed and sends an email alert message.

    Example: Send an alert if open and close of previous bar is below 50% of its range.


    Protected override void Initialize()
    {
    Overlay= false;
    CalculateOnBarClose = true;
    }


    protected override void OnBarUpdate()
    {

    double RangeBar = High[0] - Low[0] ;
    double range_pct = (0.50 * RangeBar) + Low[0];

    if (range_pct > Close[0] && range_pct > Open[0])
    {
    Print(Time[0] + "----" + "Called Email Method in Range_PCt section");
    DrawArrowDown();
    SendMail();
    }
    }


    I tested this in market replay connection. When above logic is implemented, it prints out alerts multiple times for the previous bar. I believe for each incoming tick, its evaluating condition to true on the previous bar . Is it correct to write above logic in onBarUpdate method ? is there a another method or a better way to implement above logic. Please advise !

    Thanks,
    Nag

    #2
    Hello ebnag,

    Thank you for your post.

    This would be the correct way to calculate this and it would be appropriate to have this logic in OnBarUpdate().

    Please let me know if you have any questions.

    Comment


      #3
      Thanks Patrick. I tested this using live connection and it generated email alerts as expected.
      However, I noticed it was printing few statements in output window although those print statements were removed from the code and compiled . This behavior was noticed on replay connection as well . Please advise!

      Can you guide me on the following ?

      1. If CalculateOnBarClose is set to true in initialize method, will it execute OnBarUpdate() only ONCE after the close ?
      2. Currently, it draws an UP arrow or a down arrow on the chart based on a condition and sends an alert email. Is there a way to control these alerts using input paramters ?
      Ex: Can I turn off plotting ARROWs and have only emails enabled ? ( I disable email alert by making the email address input parameter blank).

      Thank you,
      Nag

      Comment


        #4
        Hello ebnag,

        Thank you for writing in.

        Regarding the prints to the window, please remove the instances of the strategy/indicators from any charts then re-add.

        With Calculate on bar close set to true, the strategy or indicator will wait till the end of the bar before evaluating whether conditions are true or false.

        See COBC section of our helpguide.


        Yes, it is possible to specify whether an action will take place based on user defined input by enclosing the action within an if statement evaluating the value of a user defined input. I have attached a strategy which will allow you to set true or false to BoolEmail and BoolArrow. Setting both to true will draw an arrow and send an email when the condition is true, while false will do nothing.

        Please let us know if you need further assistance.
        Attached Files
        Alan P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Gerik, Today, 09:40 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by RookieTrader, Today, 09:37 AM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by alifarahani, Today, 09:40 AM
        0 responses
        5 views
        0 likes
        Last Post alifarahani  
        Started by KennyK, 05-29-2017, 02:02 AM
        3 responses
        1,285 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by AttiM, 02-14-2024, 05:20 PM
        11 responses
        186 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X