NexusFi: Find Your Edge


Home Menu

 





ExitPosition


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one user10 with 4 posts (0 thanks)
    2. looks_two cutzpr with 2 posts (2 thanks)
    3. looks_3 Quick Summary with 1 posts (0 thanks)
    4. looks_4 RHT34 with 1 posts (1 thanks)
    1. trending_up 1,777 views
    2. thumb_up 3 thanks given
    3. group 2 followers
    1. forum 7 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
user10
Dresden + Germany
 
Posts: 21 since Feb 2018
Thanks Given: 11
Thanks Received: 5

Hello to the forum,

Is there a way to automatically close existing positions when completing a strategy?
Ninjatrader support claims that is not possible !?

else if (State == State.Terminated)
{
// close position ==> does not work!?
if (Account.Name == "Sim" && Position.MarketPosition == MarketPosition.Long) {ExitLong ();}

// Ending Strategy ==> Stop and Target ==> works!
Close Strategy ("Long");
}


Many Thanks


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Day 96 Missiles Hit Kuwait and Bahrain: June 15 Peace at …
Prediction Markets & Event Contracts
Kalshi Sets $4.13B All-Time Weekly Record as Polymarket …
Prediction Markets & Event Contracts
Victory Day Delivers: Russia-Ukraine Ceasefire Market Cl …
Prediction Markets & Event Contracts
The Confluence Meter: A Multi-Layered Signal Framework B …
The Elite Circle
Orban Crashes to 21pct on Record Turnout -- McIlroy Drop …
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)
 
cutzpr's Avatar
 cutzpr 
United States
 
Experience: None
Platform: TWS,Ninja Trader
Trading: Forex, Futures, Stocks
Posts: 35 since Apr 2012
Thanks Given: 10
Thanks Received: 10


What do you mean by "completing a strategy? " What event would trigger your strategy to be complete?


Reply With Quote
Thanked by:
  #4 (permalink)
user10
Dresden + Germany
 
Posts: 21 since Feb 2018
Thanks Given: 11
Thanks Received: 5

I mean, that I finish the strategy and I still have a position.
I would like to close this position automatically.


Reply With Quote
  #5 (permalink)
RHT34
Miami, Fl
 
Posts: 21 since Mar 2016
Thanks Given: 0
Thanks Received: 73

Are you asking that the strategy should automatically exit your position at a certain time if still in?


Reply With Quote
Thanked by:
  #6 (permalink)
user10
Dresden + Germany
 
Posts: 21 since Feb 2018
Thanks Given: 11
Thanks Received: 5

No, it is independent of time.
I close the strategy manually and want all orders and positions closed as well.


Reply With Quote
  #7 (permalink)
 
cutzpr's Avatar
 cutzpr 
United States
 
Experience: None
Platform: TWS,Ninja Trader
Trading: Forex, Futures, Stocks
Posts: 35 since Apr 2012
Thanks Given: 10
Thanks Received: 10

Try something like this, I havent tested if this works or not but this is what I use to flatten my strategies.

 
Code
                            
else if (State == State.Terminated)         

{
    foreach(
Order order in Orders// This should cancel all orders that were made by the strategy
    
{
    if( 
order != null && order.OrderState != OrderState.Filled)
        {
         
base.CancelOrder(order);
        }    

    }

    
// Or -  this cancels all pending orders on the account                                        

    
Collection<Cbi.InstrumentinstrumentsToCancel = new Collection<Instrument>(); 
    
instrumentsToCancel.Add(this.Instrument);
    
Account.CancelAllOrders(this.Instrument);


       
// After all pending orders are canceled, now close all open orders
        
if(IsLong()) ExitLong();    

        if(
isShort()) ExitShort();    



 
Code
                            
    public bool IsShort()

        {
         return (
Position.MarketPosition == MarketPosition.Short) ? true false;
        }

     public 
bool IsLong()
        {
        return (
Position.MarketPosition == MarketPosition.Long) ? true false;
        } 


Reply With Quote
Thanked by:
  #8 (permalink)
user10
Dresden + Germany
 
Posts: 21 since Feb 2018
Thanks Given: 11
Thanks Received: 5

Thanks for the hint!
For me, the proposed solution does not work ...
I have added the code; now also the position is closed

 
Code
                            
else if (State == State.Terminated)    

{
    
// Cancel all pending Orders
    
foreach (Order order in Orders)    
      {if (
order != null && order.OrderState != OrderState.Filled)base.CancelOrder(order);}
                
    
// Cancel Position
    
Collection<Cbi.InstrumentinstrumentsToCancel = new Collection<Instrument>();
    
instrumentsToCancel.Add(this.Instrument);
    
Account.CancelAllOrders(this.Instrument);
    
Account.Flatten(instrumentsToCancel);   // !


Reply With Quote




Last Updated on July 23, 2018


© 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