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

"Exit on time" error on non time based charts

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

    "Exit on time" error on non time based charts

    I´ve set a condition where trades are exited at a given time.

    It is working fine on 1m, 2m, 4min charts, exits the trades and draws the magenta line at 10am.

    It does not work on non time based charts (range etc) or on 3 minute charts, 5 minutes charts etc. The script is ignored then the trades exit on close of session.

    Why is this script not working on non time based charts (and uneven # minute charts)?

    Code:
    // Condition set 2
                if (ToTime(Time[0]) == ToTime(10, 0, 0))
                {
                    ExitLong("", "");
                    ExitShort("", "");
                    DrawVerticalLine("My vertical line" + CurrentBar, 0, Color.Magenta);
                }
    Last edited by ScottieDog; 04-13-2016, 05:25 AM.

    #2
    Hello ScottieDog,


    Your script does not trigger on non time based charts and uneven # minute charts because your condition only allows for your script to trigger at exactly 10AM. Meaning, the current bar would have to end at 10AM to trigger your script.


    If you change your condition to reflect when the time is greater than or equal to, then the script would trigger when the current bar closes on or after 10AM.


    Code:
    [LEFT][FONT=Courier New][SIZE=2][COLOR=#0000FF]if[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2](ToTime(Time[[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]]) >= ToTime([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]10[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]))[/SIZE][/FONT]
    [FONT=Courier New][SIZE=2]        {[/SIZE][/FONT]
    [FONT=Courier New][SIZE=2]                ExitLong([/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
    [FONT=Courier New][SIZE=2]                ExitShort([/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
    [FONT=Courier New][SIZE=2]                DrawVerticalLine([/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800000]"My vertical line"[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]+ CurrentBar,[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2], Color.Magenta);[/SIZE][/FONT]
    [FONT=Courier New][SIZE=2]        }[/SIZE][/FONT][/LEFT]
    Shawn B.NinjaTrader Customer Service

    Comment


      #3
      Thanks. Only issue is that now it plots the magenta line on every bar after the time hits. How to stop that?

      Comment


        #4
        Hello ScottieDog,


        You could use a range of time in your condition. This will make it so that the script only draws your line within the specified range of time instead of every bar after 10AM.
        Code:
        [LEFT][FONT=Courier New][SIZE=2][COLOR=#0000FF]if[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]((ToTime(Time[[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]]) >= ToTime([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]10[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2])) && (ToTime(Time[[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]]) <= ToTime([/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]10[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]02[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2],[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]) ))[/SIZE][/FONT]
        [FONT=Courier New][SIZE=2]{[/SIZE][/FONT]
        [FONT=Courier New][SIZE=2]    ExitLong([/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
        [FONT=Courier New][SIZE=2]    ExitShort([/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2],[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]);             [/SIZE][/FONT]
        [FONT=Courier New][SIZE=2]    DrawVerticalLine([/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800000]"My vertical line"[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2]+ CurrentBar,[/SIZE][/FONT] [FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT] [FONT=Courier New][SIZE=2], Color.Magenta);[/SIZE][/FONT]
        [FONT=Courier New][SIZE=2]} [/SIZE][/FONT]
        For example.
        [FONT=Courier New][SIZE=2][/SIZE][/FONT]

        [/LEFT]
        Shawn B.NinjaTrader Customer Service

        Comment


          #5
          Thanks. Understood.

          Out of curiosity, is using a time filter like this the only way to stop a line repeating its print? Or would there be other ways too? Just trying to get a few basic functions etc into my newbie brain :-)

          Comment


            #6
            Hello,

            You could use a bool to act as a switch and stop your script from drawing more lines.

            You would have to reset the bool afterwards. I have included an example showing this.



            Code:
            [COLOR=#000000][FONT=Tahoma][LEFT][LEFT]         protected override void OnBarUpdate()
                    {
                              
                               if ((ToTime(Time[ 0 ]) >= ToTime( 10, 0, 0)) )
                        {               
                                                ExitLong( "", "" );                                     ExitShort( "", "" ); [LEFT]                         
                              if(myBool == true)
                                    {
                                    DrawVerticalLine( "My vertical line" + CurrentBar, 0 , Color.Magenta);
                                    myBool = false;
                                    }
                        }
                              
                               if (ToTime(Time[ 0]) < ToTime( 10, 0, 0))
                              {
                              myBool = true;
                              }
                    }[/LEFT]
            [/LEFT][LEFT][/LEFT][LEFT][/LEFT]
            [/LEFT][/FONT][/COLOR]

            Here is the Basic Syntax Help Guide to assist you further.
            Shawn B.NinjaTrader Customer Service

            Comment


              #7
              Understood. Thank-you.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by pvincent, 06-23-2022, 12:53 PM
              14 responses
              238 views
              0 likes
              Last Post Nyman
              by Nyman
               
              Started by TraderG23, 12-08-2023, 07:56 AM
              9 responses
              383 views
              1 like
              Last Post Gavini
              by Gavini
               
              Started by oviejo, Today, 12:28 AM
              0 responses
              1 view
              0 likes
              Last Post oviejo
              by oviejo
               
              Started by pechtri, 06-22-2023, 02:31 AM
              10 responses
              125 views
              0 likes
              Last Post Leeroy_Jenkins  
              Started by judysamnt7, 03-13-2023, 09:11 AM
              4 responses
              59 views
              0 likes
              Last Post DynamicTest  
              Working...
              X