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 cmtjoancolmenero, Yesterday, 03:58 PM
    11 responses
    39 views
    0 likes
    Last Post cmtjoancolmenero  
    Started by FrazMann, Today, 11:21 AM
    0 responses
    5 views
    0 likes
    Last Post FrazMann  
    Started by geddyisodin, Yesterday, 05:20 AM
    8 responses
    52 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by DayTradingDEMON, Today, 09:28 AM
    4 responses
    26 views
    0 likes
    Last Post DayTradingDEMON  
    Started by George21, Today, 10:07 AM
    1 response
    22 views
    0 likes
    Last Post NinjaTrader_ChristopherJ  
    Working...
    X