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

Setting stop loss based on time

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

    Setting stop loss based on time

    Hi, I'm trying to test a simple strategy - after setting my profit target and calling EnterLong(), if my long position is still open after 30 thirty days (i.e. profit target still not achieved) then I would like to exit that position. Btw I have only 1 open long position at any given time (i.e. I set EntriesPerDirection = 1) and I'm using 5-min bars.

    To achieve this, the code I have in mind is:

    Code:
    protected override void OnBarUpdate()
    {
     	if (CrossAbove(SMA(10), SMA(20), 1))
    		EnterLong(100, "myEntryOrder");
    	
    	/* Check if position is still open 30 days (i.e. 8640 5-min bars) later */
    	if ((Position.MarketPosition != MarketPosition.Flat) && (BarsSinceEntryExecution() > 8640)) 
    		ExitLong();
    }
    Would this work? I'm asking because I looked through the documentation for NinjaTrader 8 and saw that there might be other means available (e.g. using Order.Time?) so I'm just wondering if there might be a better way to do this...

    Greatly appreciate any advice, thanks

    #2
    Hello Vypre,

    Thanks for your post.

    Yes, BarsSinceEntryExecution() will work and in the case of a named entry "myEntryOrder" you would want to specify that same name in the within the method, ie: BarsSinceEntryExecution("myEntryOrder")

    Using 5 minute bars, 8640 bars would equate to 720 trading hours. If you instead wanted to exit after 30 calendar days then you could so something like:

    if (your conditions to enter)
    {
    EnterLong("myEntryOrder");
    myEntrydatetime = Time[0]; // assign the entry bar time to a previously created variable datetime called myEntrydatetime
    }

    if (Position.MarketPosition != MarketPosition.Flat && Time[0] >= myEntrydatetime.AddDays(30)) // add 30 calendar days to the entry date
    {
    ExitLong("myEntryOrder");
    }


    Please note that you can test any situation using the Sim101 account.
    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by GussJ, 03-04-2020, 03:11 PM
    11 responses
    3,221 views
    0 likes
    Last Post xiinteractive  
    Started by andrewtrades, Today, 04:57 PM
    1 response
    10 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by chbruno, Today, 04:10 PM
    0 responses
    6 views
    0 likes
    Last Post chbruno
    by chbruno
     
    Started by josh18955, 03-25-2023, 11:16 AM
    6 responses
    436 views
    0 likes
    Last Post Delerium  
    Started by FAQtrader, Today, 03:35 PM
    0 responses
    9 views
    0 likes
    Last Post FAQtrader  
    Working...
    X