NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > Application Technical Support > Automated Trading

Automated Trading Support for automated trading systems using NinjaScript. Support for our ATI (Automated Trading Interface) used to link an external application such as TradeStation and eSignal to NinjaTrader.

Reply
 
Thread Tools Display Modes
Old 08-05-2012, 02:10 AM   #1
woodies
Junior Member
 
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
Default How to close active ATM strategy and pending orders?

I want to test a strategy which calls ATM strategy and reverses it on opposite rules. However, the ATM strategy and pending orders once opened does not close on opposite signal.
I have a code as follows.
Code:
 
protectedoverridevoid OnBarUpdate()
{
// Make sure this strategy does not execute against historical data
if (Historical)
return;
 
if (orderId.Length == 0
&& atmStrategyId.Length == 0
&& FirstTickOfBar
&& CrossAbove(MAMA cross)
)
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId); 
}
 
elseif (orderId.Length == 0
&& atmStrategyId.Length == 0
&& FirstTickOfBar
&& CrossBelow(MAMA cross) )
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);
}
 
// Check for a pending entry order
if (orderId.Length > 0)
{
 
string[] status = GetAtmStrategyEntryOrderStatus(orderId);
 
if (status.GetLength(0) > 0) AtmStrategyCancelEntryOrder(atmStrategyId);
 
if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
{
orderId = string.Empty;
}
}
// If the strategy has terminated reset the strategy id
elseif
(atmStrategyId.Length > 0 &&
GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat
)
{atmStrategyId = string.Empty;}
}
// Close ATM strategy and pending orders

if ( atmStrategyId.Length > 0 &&
GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Long &&
CrossBelow(MAMA cross)
{
AtmStrategyClose(atmStrategyId);
atmStrategyId = string.Empty;
}
if ( atmStrategyId.Length > 0 &&
GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Short&&
CrossAbove(MAMA cross)
{
AtmStrategyClose(atmStrategyId);
atmStrategyId = string.Empty;
}
}
woodies is offline  
Reply With Quote
Old 08-05-2012, 02:38 PM   #2
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

woodies,

You would need to close the ATM strategy before entering into the opposite direction here. It looks like you are closing the strategies below, however I would suggest doing it before you enter into a reverse order.
NinjaTrader_AdamP is offline  
Reply With Quote
The following user says thank you to NinjaTrader_AdamP for this post:
Old 08-05-2012, 10:59 PM   #3
woodies
Junior Member
 
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
Default

Thanks for suggestion.

Do you mean something like this?

if( Market Position==MarketPosition.Long && CrossBelow(MAcross)&& atmStrategyId.Length >0)
{
AtmStrategyClose(atmStrategyId);
atmStrategyId= string.Empty;
orderId=string.Empty
if (orderId.Length == 0
&& atmStrategyId.Length == 0
)
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);
}


}

Followed by cancelling pending entry order if it is filles, cancelled or rejected and resetting Id?
I will try this and see if it works.
woodies is offline  
Reply With Quote
Old 08-06-2012, 02:45 AM   #4
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,414
Thanks: 252
Thanked 978 times in 961 posts
Default

Yes, this should be what you seek here to reverse with the ATM's.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 08-11-2012, 07:29 AM   #5
woodies
Junior Member
 
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
Default

The code does not cancel out the pending stop and target orders on reversal and the ATM opens a new order in opposite direction. Can you suggest an alternate way?
Thanks.
woodies is offline  
Reply With Quote
Old 08-13-2012, 04:15 AM   #6
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello woodies,
You should close the existing position and the enter a new position at the same time. A sample code will be

Code:
if( Market Position==MarketPosition.Long && CrossBelow(MAcross)&& atmStrategyId.Length >0)
{

//exit current position
AtmStrategyClose(atmStrategyId);

//sell at market
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId); 
}
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 08-13-2012, 10:52 PM   #7
woodies
Junior Member
 
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
Default

Thanks a lot for the support.

However, the strategy still does not take a trade.
Can you please check the error again?
Attached Files
File Type: cs MAcrossATM.cs (3.6 KB, 8 views)
woodies is offline  
Reply With Quote
Old 08-14-2012, 05:30 AM   #8
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello woodies,
Thanks for the code.

In you code you never made the initial entry (i.e. when market position is flat).
Please append the following code and see if you can get the correct entries.


Code:
if (atmStrategyId.Length == 0 && CrossAbove(SMA(Close,20),SMA(Close,100),1))
{
	atmStrategyId = GetAtmStrategyUniqueId();
	 orderId = GetAtmStrategyUniqueId(); 
	 AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);	
}
else if (atmStrategyId.Length == 0 && CrossBelow(SMA(Close, 20), SMA(Close, 100), 1))
{
	atmStrategyId = GetAtmStrategyUniqueId();
	 orderId = GetAtmStrategyUniqueId();
     AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);
}
Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Joydeep for this post:
Old 08-14-2012, 10:41 PM   #9
woodies
Junior Member
 
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
Default

Hello Joydeep,

I have modified the code as per your advise and it takes the trades on both sides.
However, when it reverses, it does not close the existing stop and profit target orders.
So on reversal, there are two orders in opposite direction.
Can you please check it? Do we need to use AtmStrategy ChangeStopTarget() method to close the existing orders?
Thanks for help.
Attached Files
File Type: cs MAcrossATM.cs (4.4 KB, 3 views)
woodies is offline  
Reply With Quote
Old 08-15-2012, 04:51 AM   #10
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello woodies,
Can you please try this code.

In this context let me say that the positions of the Atm straegy are updated atleast a tick after the order gets filled.

Quote:
NOTE: Changes to positions will not be reflected till at least the next OnBarUpdate() event after an order fill.
http://www.ninjatrader.com/support/h...etposition.htm

This is conflicting with your code
Code:
else  if 
(atmStrategyId.Length > 0 &&
GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat
)
{
	atmStrategyId = string.Empty;
	orderId= string.Empty;
}
If I remove it I can get the strategy working.
Attached Files
File Type: cs MAcrossATM.cs (3.5 KB, 8 views)
NinjaTrader_Joydeep is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Joydeep for this post:
Old 08-15-2012, 06:07 AM   #11
woodies
Junior Member
 
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
Default

Great!!! That finally works as expected.
This could be the only example on the forum of ATM strategy use in a main strategy.

I have seen problems with overfill when dealing with managed approach and unmanaged approach requires a lot of order handling. And ATM strategy would offer a simpler solution to his.

Thanks a lot for all the help and support to everybody.
woodies is offline  
Reply With Quote
The following user says thank you to woodies for this post:
Old 08-15-2012, 06:12 AM   #12
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello woodies,
Thanks for your note.

Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 10-15-2012, 10:08 AM   #13
woodies
Junior Member
 
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
Default Problem with realized PL

Hello,

I am trying to get relalized PL from ATM strat example shown here but it does not update properly.
Can you check the code and show me the error?

Code:
 // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
                      if (status.GetLength(0) > 0)
                            {
                                 
                                  // If the order state is terminal, reset the order id value
                                  if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
                                  {
                                    orderId = string.Empty;
                                  }
                
                            }
                    }
                 
                    else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId)==MarketPosition.Flat)
                        {
                            if(GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
                               { realizedProfitLoss += Instrument.MasterInstrument.Round2TickSize(GetAtmStrategyRealizedProfitLoss(atmStrategyId));
                                  atmStrategyId = string.Empty;
                                  unrealizedProfitLoss = 0;
                                }
                        }
                        if(atmStrategyId.Length > 0)
                        { 
                            unrealizedProfitLoss=Instrument.MasterInstrument.Round2TickSize(GetAtmStrategyUnrealizedProfitLoss(atmStrategyId));
                        }
I am attaching the .cs file here for reference.
Thanks.
Attached Files
File Type: cs MAcrossATM.cs (6.9 KB, 4 views)
woodies is offline  
Reply With Quote
Old 10-15-2012, 11:22 AM   #14
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello woodies,
You are always in a position as the first 2 block of codes will never let you in a flat position. Thus the last block of code (where you calculate the PnL) never gets updated.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 10-15-2012, 12:05 PM   #15
woodies
Junior Member
 
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
Default

Thanks for reply.
Is there any workaround this problem so that we can get the ATM PL while in a long/shot psotion?
Can we use PL methods we use for managed approach like Performance.AllTrades.TradesPerformance.Currency.C umProfit
for ATM based strategy?
woodies is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pending ATM Orders daviddelish@hotmail.com ATM Strategies (Discretionary Trading) 5 10-13-2011 06:41 AM
Modifying Active ATM strategy ssc351 ATM Strategies (Discretionary Trading) 3 09-05-2011 10:57 AM
Plotting active orders on non strategy chart farang Charting 2 08-09-2011 05:17 AM
Active ATM Strategy / Chart Trader richp6 Version 7 Beta General Questions & Bug Reports 7 05-04-2010 06:47 AM
Active ATM Strategy - text too long in chart trader pulldown foxthorn Version 7 Beta General Questions & Bug Reports 6 01-16-2010 10:52 PM


All times are GMT -6. The time now is 09:08 PM.