NexusFi: Find Your Edge


Home Menu

 





How do I close positions 5 bars after they are opned


Discussion in NinjaTrader

Updated
    1. trending_up 3,183 views
    2. thumb_up 0 thanks given
    3. group 1 followers
    1. forum 5 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
xNICK
chattanooga
 
Posts: 10 since Dec 2018
Thanks Given: 0
Thanks Received: 0

Hey, I'm currently switching over from thinkorswim to ninjatrader 8 and I need to know the code I need to add to ninjascript or how I add it to the strategy builder. But I would like to close all short positions 5 bars after the position was opened. (or 5 mins or would be even better If I could close the position on the next 5 min increment, so if short position was opened at 10:22 it will close at 10:25)
Here is the current code
// Set 1
if (CrossAbove(ParabolicSAR1, Close, 1))
{
EnterShort(Convert.ToInt32(DefaultQuantity), "");
}

// Set 2
if (CrossAbove(ParabolicSAR1, Close, 1))
{
ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
}

Also what does the 1 mean after the para crossabove the close?

Thank you so much for your time and help!


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Ripple Prime Joins Nodal Clear as Clearing Member -- $3 …
Cryptocurrency
$500M Riding on World Cup 2026: France/Spain Co-Favored …
Prediction Markets & Event Contracts
April CPI Preview: +3.7% YoY Expected at 8:30 AM ET -- C …
Traders Hideout
Post-Summit Scorecard: $36M in May 15 Bets Settle Near-Z …
Prediction Markets & Event Contracts
Expiration Day: Wall Street Rallies on Peace Hopes While …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
24 thanks
2026 Jlab journal
10 thanks
Lady Vols Primer: Trading Volatility Journal
7 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Trying to learn Volume and price action correlation
5 thanks
  #3 (permalink)
 
TradingOgre's Avatar
 TradingOgre 
Evans GA/USA
Legendary Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: NinjaTrader Brokerage - Philip Capital
Trading: NQ,ES,6E,CL
Posts: 556 since Jul 2018
Thanks Given: 908
Thanks Received: 1,673



xNICK View Post
Hey, I'm currently switching over from thinkorswim to ninjatrader 8 and I need to know the code I need to add to ninjascript or how I add it to the strategy builder. But I would like to close all short positions 5 bars after the position was opened. (or 5 mins or would be even better If I could close the position on the next 5 min increment, so if short position was opened at 10:22 it will close at 10:25)
Here is the current code
// Set 1
if (CrossAbove(ParabolicSAR1, Close, 1))
{
EnterShort(Convert.ToInt32(DefaultQuantity), "");
}

// Set 2
if (CrossAbove(ParabolicSAR1, Close, 1))
{
ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
}

Also what does the 1 mean after the para crossabove the close?

Thank you so much for your time and help!

The 1 refers to when you want the cross to occur in number of previous bars.

There is a command in NT8 (not sure about 7) called BarsSinceEntryExecution().


Quoting 
BarsSinceEntryExecution()

Previous page Return to chapter overview Next page
Definition
Returns the number of bars that have elapsed since the last specified entry.


Method Return Value
An int value that represents a number of bars. A value of -1 will be returned if a previous entry does not exist.



Syntax
BarsSinceEntryExecution()
BarsSinceEntryExecution(string signalName)



The following method signature should be used when working with multi-time frame and instrument strategies:



BarsSinceEntryExecution(int barsInProgressIndex, string signalName, int entryExecutionsAgo)



Note: When working with a multi-series strategy the BarsSinceEntryExecution() will return you the elapsed bars as determined by the first Bars object for the instrument specified by the barsInProgressIndex.





Parameters
signalName

The signal name of an entry order specified in an order entry method.

barsInProgressIndex

The index of the Bars object the entry order was submitted against.



Note: See the BarsInProgress property.

entryExecutionsAgo

Number of entry executions ago. Pass in 0 for the number of bars since the last entry execution.





Examples
ns

protected override void OnBarUpdate()
{
if (CurrentBar < BarsRequiredToTrade)
return;

// Only enter if at least 10 bars has passed since our last entry
if ((BarsSinceEntryExecution() > 10 || BarsSinceEntryExecution() == -1) && CrossAbove(SMA(10), SMA(20), 1))
EnterLong();

}


Visit my NexusFi Trade Journal Reply With Quote
  #4 (permalink)
xNICK
chattanooga
 
Posts: 10 since Dec 2018
Thanks Given: 0
Thanks Received: 0

Thanks for that quote and telling me what that 1 meant! This is code for me that closes my last position 5 bars later.
// Set 1
if (CrossAbove(ParabolicSAR1, Close, 1))
{
EnterShort(Convert.ToInt32(DefaultQuantity), "");
}

// Set 2
if (BarsSinceEntryExecution()==5)
{
ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
}

which will work, still trying to learn how to close it on 5 minute increments though


Reply With Quote
  #5 (permalink)
 
TradingOgre's Avatar
 TradingOgre 
Evans GA/USA
Legendary Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: NinjaTrader Brokerage - Philip Capital
Trading: NQ,ES,6E,CL
Posts: 556 since Jul 2018
Thanks Given: 908
Thanks Received: 1,673

You could store the time when you send the entry order, then compare it to the current time. If it is > 5 minutes then exit.


Visit my NexusFi Trade Journal Reply With Quote
  #6 (permalink)
xNICK
chattanooga
 
Posts: 10 since Dec 2018
Thanks Given: 0
Thanks Received: 0

Thank you for that idea Ill go see what I can do with that!
Thank you for your time and help!


Reply With Quote




Last Updated on January 3, 2019


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts