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

Flatten everything from my strategy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • pstrusi
    replied
    Originally posted by soyjesus View Post
    Hello,

    How can I make a "flatten everything" with a strategy? For example, I have severals opened positions with another strategies and I need to close all the positions from a different stratety.

    Thanks in advance.
    Perhaps this code extract, Flatten position if a "daily goal has been reached", might give you some ideas. In regards to Flatten according to Time, it seems better to use the built-in NT feature.

    This code extract works under unmanaged approach. It's pretty basic, one account and one instrument.

    Code:
    protected override void OnAccountItemUpdate(Account account, AccountItem accountItem, double value)
    		{ 
    			daypnl=Account.Get(AccountItem.RealizedProfitLoss, Currency.UsDollar)+Account.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar);
    			
    			if ( daypnl >= Maxpro || daypnl <= -Maxlos ) 
    			{				
    				if (Position.MarketPosition == MarketPosition.Short)					
    				{	
    					if ( stoptrading==0 )
    					{
    						if ( setshort != null )					
    						{	
    							CancelOrder(setshort);				
    						}	
    						if ( setshort == null )					
    						{	
    							if ( setlong == null )				
    							{	
    								setlong = SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.Limit, Position.Quantity, (Close[0]+stop*TickSize), 0, "", "EXITSHORT");
    							}
    							else 
    							{
    								ChangeOrder( setlong, Position.Quantity, (Close[0]+stop*TickSize), 0);
    							}
    							stoptrading=1;
    							SendMail (********);			
    						}		
    					}	
    				}	
    				else if (Position.MarketPosition == MarketPosition.Long )
    				{	
    					if ( stoptrading==0 )
    					{
    						if (setlong != null )					
    						{	
    							CancelOrder(setlong);				
    						}
    						if (setlong == null )					
    						{	
    							if (setshort == null )					
    							{	
    								setshort = SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Limit, Position.Quantity, (Close[0]-stop*TickSize), 0, "", "EXITLONG");
    							}
    							else
    							{
    								ChangeOrder( setshort, Position.Quantity, (Close[0]-stop*TickSize), 0);
    							}
    							stoptrading=1;
    							SendMail (*****);				
    						}	
    					}
    				}
    				else
    				{
    					stoptrading=1;
    				}
    			}
    		}
    If you find some other ways to Flatten all, this forum would appreciate highly.
    Last edited by pstrusi; 09-29-2017, 05:02 AM.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello,

    Thank you for the post.

    You can use the addon framework commands to flatten instruments using the Account object. Please see the following page:



    The flatten command takes a collection of instruments to flatten.

    You can also use more advanced techniques to monitor or do other actions with the account using the addon commands: https://ninjatrader.com/support/help...ount_class.htm

    Please also see the PositionAccount object if you are working with multiple strategies at one time: https://ninjatrader.com/support/help...ionaccount.htm

    I look forward to being of further assistance.

    Leave a comment:


  • soyjesus
    started a topic Flatten everything from my strategy

    Flatten everything from my strategy

    Hello,

    How can I make a "flatten everything" with a strategy? For example, I have severals opened positions with another strategies and I need to close all the positions from a different stratety.

    Thanks in advance.

Latest Posts

Collapse

Topics Statistics Last Post
Started by f.saeidi, Today, 12:14 PM
7 responses
16 views
0 likes
Last Post f.saeidi  
Started by Russ Moreland, Today, 12:54 PM
1 response
6 views
0 likes
Last Post NinjaTrader_Erick  
Started by philmg, Today, 12:55 PM
1 response
7 views
0 likes
Last Post NinjaTrader_ChristopherJ  
Started by TradeForge, 04-19-2024, 02:09 AM
2 responses
32 views
0 likes
Last Post TradeForge  
Started by aprilfool, 12-03-2022, 03:01 PM
3 responses
329 views
0 likes
Last Post NinjaTrader_Adrian  
Working...
X