NexusFi: Find Your Edge


Home Menu

 





How do I close positions 5 bars after they are opned


Discussion in NinjaTrader

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




 
 

How do I close positions 5 bars after they are opned

 
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!


Can you help answer these questions
from other members on NexusFi?
ZombieSqueeze
Platforms and Indicators
How to apply profiles
Traders Hideout
Better Renko Gaps
The Elite Circle
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Exit Strategy
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
29 thanks
Just another trading journal: PA, Wyckoff & Trends
25 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
23 thanks
GFIs1 1 DAX trade per day journal
18 thanks
 
 
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,672



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
 
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

 
 
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,672

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
 
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!


 



Last Updated on January 3, 2019


© 2024 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 - Privacy Policy - Downloads - Top
no new posts