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 AttiM, 02-14-2024, 05:20 PM
    12 responses
    212 views
    0 likes
    Last Post DrakeiJosh  
    Started by cre8able, 02-11-2023, 05:43 PM
    3 responses
    236 views
    0 likes
    Last Post rhubear
    by rhubear
     
    Started by frslvr, 04-11-2024, 07:26 AM
    8 responses
    115 views
    1 like
    Last Post NinjaTrader_BrandonH  
    Started by stafe, 04-15-2024, 08:34 PM
    10 responses
    47 views
    0 likes
    Last Post stafe
    by stafe
     
    Started by rocketman7, Today, 09:41 AM
    3 responses
    11 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X