![]() |
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
|
|||||||
| 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. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Junior Member
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
|
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;
}
}
|
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
|
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.
Adam P.
NinjaTrader Customer Service |
|
|
|
|
The following user says thank you to NinjaTrader_AdamP for this post: |
|
|
|
#3 |
|
Junior Member
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
|
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. |
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,414
Thanks: 252
Thanked 978 times in 961 posts
|
Yes, this should be what you seek here to reverse with the ATM's.
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Junior Member
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
|
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. |
|
|
|
|
|
#6 |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
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);
}
Joydeep M.
NinjaTrader Customer Service |
|
|
|
|
|
#7 |
|
Junior Member
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
|
Thanks a lot for the support.
However, the strategy still does not take a trade. Can you please check the error again? |
|
|
|
|
|
#8 |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
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);
}
Joydeep M.
NinjaTrader Customer Service |
|
|
|
|
The following user says thank you to NinjaTrader_Joydeep for this post: |
|
|
|
#9 |
|
Junior Member
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
|
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. |
|
|
|
|
|
#10 | |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
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:
This is conflicting with your code Code:
else if
(atmStrategyId.Length > 0 &&
GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat
)
{
atmStrategyId = string.Empty;
orderId= string.Empty;
}
Joydeep M.
NinjaTrader Customer Service |
|
|
|
|
|
The following user says thank you to NinjaTrader_Joydeep for this post: |
|
|
|
#11 |
|
Junior Member
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
|
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. |
|
|
|
|
The following user says thank you to woodies for this post: |
|
|
|
#12 |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
Hello woodies,
Thanks for your note. Please let me know if I can assist you any further.
Joydeep M.
NinjaTrader Customer Service |
|
|
|
|
|
#13 |
|
Junior Member
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
|
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));
}
Thanks. |
|
|
|
|
|
#14 |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
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.
Joydeep M.
NinjaTrader Customer Service |
|
|
|
|
|
#15 |
|
Junior Member
Join Date: Sep 2010
Posts: 22
Thanks: 10
Thanked 1 time in 1 post
|
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? |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |