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

Default Settings for Total Control of Flat and Exiting Session End?

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

    Default Settings for Total Control of Flat and Exiting Session End?

    Would the following code for a simple trailing stop strategy conflict with the Flat settings. Before I added the revised the code below, I only entered if the buyStop and shortStop were triggered without the time booleans that appear.

    As soon as I revised the code in the format below, the strategy analyzer stops at iteration 26 and kind of hangs there. Somehow I think that trying to fully control my own time exits is conflicting with something in the default. Please lookover the code, as I only want to trade after 0830 and before 1455, and exit any open trade after 1455. The rest is simply use of booleans to know when a last position is active, and whether it is long or short.

    It is very straightforward, but I think a default setting related to Flat, or exiting at session end is conflicting. Wouldn't the code below essentially need to tell NT8 that it will be in total control of all Flat information, and doing anything related to close of session, etc.?

    Code:
                    after0835= ToTime(Time[0]) >= 083500;
                    before1455 = ToTime(Time[0]) < 145500;
                    after1455 = ToTime(Time[0]) > 145500;
                               
                    buyStop=  CrossAbove( emahigh, trailhigh,1) && after0835 && before1455 ;
                    shortStop= CrossBelow( emalow, traillow,1) && after0835 && before1455 ;
                
                    if ( Position.MarketPosition != MarketPosition.Flat)
                    {
                        IsLastPositionActive=true;
                        if ( Position.MarketPosition == MarketPosition.Long)
                            IsPositionLong=true;
                        if ( Position.MarketPosition == MarketPosition.Short)
                            IsPositionShort=true;
                    }
                    if ( Position.MarketPosition == MarketPosition.Flat)
                        IsLastPositionActive=false;
                         
                            
                    if( IsLastPositionActive )
                     {
                        //Exit any open position 5 minutes before closing bell
                         if ( after1455 && IsPositionLong )
                            EnterShort(800,"Time");
                        if ( after1455 && IsPositionShort )
                             EnterLong(800,"Time");
    
                         //Exit any open position if necessary
                        if( IsPositionLong ) 
                        {
                            if ( shortStop ) 
                              EnterShort(800,"Limit"); 
                        }
                        else         
                        {                    
                            if ( buyStop ) 
                              EnterLong(800,"Limit"); 
                        }
                    }
                    else
                    {
                        //Open new position if nothing is open
                        if ( shortStop ) 
                         {
                            EnterShort(800,"Short");
                            IsLastPositionActive=true;
                        }
                        if ( buyStop && IsLastPositionActive == false ) 
                        {
                            EnterLong(800,"Long");
                        } 
                    }                     
                }
    Last edited by pel11; 06-22-2018, 02:46 PM.

    #2
    Hello pel11,

    Thanks for your post.

    Having exits at a certain time would not affect the behavior of the ExitOnSessionClose setting. If the strategy closes positions before the session close and this option is enabled, those positions would have already been closed. If the strategy has open positions at the time of session close (including the ExitOnSessionCloseSeconds property) and ExitOnSessionClose is enabled, the positions will be exited.

    My recommendation would be to either use the ExitOnSessionClose property, or if you want to manage your own separate exit times, to disable ExitOnSessionClose and use time checks to control your exits.

    Time checks - https://ninjatrader.com/support/help...to_limit_t.htm

    With your code, I see that you are entering new positions but there are not any exit methods.

    To provide some further direction for using the Managed Approach for Entering and Exiting, I will provide a documentation link below. This document explains the different Entry and Exit methods and the internal rules that govern them.

    Managed Approach - https://ninjatrader.com/support/help...d_approach.htm

    I also recommend using debug prints and the TraceOrders property to monitor the strategies actions in the NinjaScript Output window. The Playback Connection is also useful for repeating scenarios for testing.

    Debugging - https://ninjatrader.com/support/help...script_cod.htm

    TraceOrders - https://ninjatrader.com/support/help...aceorders2.htm

    Playback Connection - https://ninjatrader.com/support/help...connection.htm

    Please let us know if we can be of further assistance.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by maybeimnotrader, Yesterday, 05:46 PM
    1 response
    18 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by Perr0Grande, Yesterday, 08:16 PM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by f.saeidi, Yesterday, 08:12 AM
    3 responses
    24 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by algospoke, Yesterday, 06:40 PM
    1 response
    14 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by quantismo, Yesterday, 05:13 PM
    1 response
    14 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Working...
    X