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,920 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?
April Jobs Beat Flips Fed Hike Odds Past 52% for First T …
Traders Hideout
Iran Forward Curve: June 30 at 56% vs June 15 at 28% -- …
Prediction Markets & Event Contracts
Al Arabiya: US-Iran Draft Deal Within Hours Contains Hor …
Prediction Markets & Event Contracts
Beijing Summit Closes: Xi Pledges Hormuz Help -- $1.14B …
Prediction Markets & Event Contracts
CPI Eve: Fed Hike Odds Hold at 52% for the First Time, I …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
NexusFi site changelog and issues/problem reporting
8 thanks
Darmok and Jalad at Tanagra
3 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