View Full Version : ATM Strategy question
eDanny
01-27-2009, 07:20 AM
The very first time I tested my strategy after inserting a routine to use an ATM Strategy and on the very first trade, the target and stop showed on the DOM and I could move them around as in regular manual trading. After that it has never happened again. Why can't I see and manipulate my stops and why did it happen once and never again?
NinjaTrader_Josh
01-27-2009, 07:28 AM
eDanny,
Have you managed the ATM strategy properly in your code by emptying out your atmStrategyIds and other Ids? Please see the SampleAtmStrategy for more information.
eDanny
01-27-2009, 07:56 AM
I assume everything was right since I lifted the code right out of the sample.
NinjaTrader_Josh
01-27-2009, 08:00 AM
You will need to check through debugging. Use print functions to confirm.
eDanny
01-27-2009, 09:24 AM
As in the sample and order will not be taken of the atmStrategyId.Length or orderId.Length are > 0. Printing those to the output window confirms lengths of zero while waiting for an order signal. I still never see the target and stop in the DOM and have to resort to another chart with chart trader enabled to be able to click and drag changes, which is clumsy at best.
NinjaTrader_Josh
01-27-2009, 09:44 AM
eDanny,
Are you saying the orders show up on Chart Trader but not on DOM? If so, please try just using SampleAtmStrategy and see if you get the same behavior.
eDanny
01-27-2009, 09:55 AM
Tried it and it took 3 trades in quick succession but the DOM showed no stops or targets, only the chart trader did.
NinjaTrader_Josh
01-27-2009, 10:05 AM
Please provide reproducible steps. It works on my end with SampleAtmStrategy on both the Dynamic SuperDOM as well as Chart Trader.
eDanny
01-27-2009, 10:20 AM
No steps taken beyond changing the AtmStrategyTemplate to the name of one of mine. Apply to chart and get the results I see.
NinjaTrader_Josh
01-27-2009, 10:30 AM
eDanny,
Please use SampleAtmStrategy with no changes and an AtmStrategyTemplate that just has a 4 tick stop and 8 tick profit.
eDanny
01-27-2009, 11:05 AM
Same results.
NinjaTrader_Josh
01-27-2009, 11:07 AM
Unfortunately all I can suggest is for you to reinstall your NinjaTrader. We are unable to reproduce on our end.
eDanny
01-27-2009, 11:38 AM
Ok will try that later.
eDanny
01-27-2009, 02:15 PM
OK haven't tried that yet but tried the sample strategy again with no other strategies running on any charts and this time was going to let it run it's course with a 8 tick target and 4 tick stop without just closing positions as in the last test. The DOM ATM was called AtmStrategyTemplate to be compatible with the code and was set to PT of 8 and S of 4. The strategy filled and still no target or stop bars in the DOM. I noticed that price was below my stop so I looked at a chart with chart trader and saw the target was 8 ticks but the stop was 10 ticks. I'm just not understanding what is going on here but will reinstall NT now. :mad:
NinjaTrader_Josh
01-27-2009, 02:18 PM
I am not sure what is happening on your end eDanny. Backup your files before you uninstall.
1. Backup NT
2. Uninstall NT
3. Delete My Documents\NinjaTrader 6.5 (you will lose everything)
4. Clear internet browser cache
5. Reinstall NT
eDanny
01-27-2009, 02:54 PM
OK, just did a repair first. Loaded NT and closed my workspace. Opened a DOM and bare chart and added the sample strategy and started replay. I discovered a few very weird things. Once a position pending or entered I had to manually select something like 'AvtiveStrategy' in the DOM to see the target and stop bars. This is not even there to select if there is not a pending trade or an open position. Once the position closes, the ATM Strategy reverts back to the last. WTH is that all about? The next weird thing was that the 4 tick stop would move down to 5 ticks. As price traded lower, the stop crept down until at one point it was at -9 ticks. Not good at all. Soooo, I hope these things are not normal and will be fixed with a complete reinstall? Waiting to hear any explanations before doing so.
NinjaTrader_Josh
01-27-2009, 03:03 PM
These behaviors are likely controlled by the settings you choose for your SuperDOM.
Right click->Properties->ATM Strategy selection mode.
Just reset it to default.
The SampleAtmStrategy sends price modifications. This is what you are seeing.
eDanny
01-27-2009, 03:23 PM
Looking at the default strategy I can see it is moving the stop, something I took out when I lifted the code for my use. I don't know what the default setting for the DOM is since my settings are the default. I thought I read it should be DisplaySelectedAtmStrategyOnly, which I use for the default. I see this SelectActiveAtmStrategyOnOrderSubmission works and automatically switches the DOM.
Another question is:
In my strategy I have two separate long and short entry tests so that depending on which entry is taken, the appropriate ATM Strategy will work. The problem is that only the first long test and first short test ever are executed. Using Print() shows the if() is hit but nothing ever happens.
By the way, thanks for the help, turns out I'm not going crazy!
NinjaTrader_Josh
01-27-2009, 03:27 PM
SelectActive is the default. Please just stick with SampleAtmStrategy. No changes or modifications. I cannot help you diagnose the scenario unless you stick to the base case scenario.
To make subsequent entries you need to have reset the strategyId. Check Control Center logs for errors.
eDanny
01-27-2009, 03:49 PM
Everything is good now with the sample, now I'm trying to debug the problem I just described. Two separate tests for longs and two for shorts. Essentially:
if(xxx)
"go long with ATM Strategy A", etc.
if(yyy)
"go long with ATM Strategy B", etc.
if(aaa)
"go short with ATM Strategy A", etc.
if(bbb)
"go short with ATM Strategy B", etc.
The problem is that (yyy) and (bbb) are never acted upon and no parenthesis are out of place.
NinjaTrader_Josh
01-27-2009, 03:50 PM
Please post actual code eDanny. Thank you.
eDanny
01-27-2009, 04:14 PM
Ok here is the section, probably with redundant code and not optimized, but needs to be working first.
if(useATMstrategy) //Begin Section
{
if (Historical)return;
if(wwwww || xxxxx || yyyyyy || !zzzzzz)
{if(useOutputWindow){Print("Long Signals OK ");Print("");}
if(FirstTickOfBar)
{if (orderId.Length == 0 && atmStrategyId.Length == 0 && BuySignal[1] == 1)
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Action.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "Strategy1", atmStrategyId);
}
}}
if(nnnn && oooo && BuySignal[1] ==1 || ppppp && BuySignal[1] ==1) //<---No matter what is here, it never gets executed
{if(useOutputWindow){Print("Long Signals+ OK ");Print("");}
if(FirstTickOfBar)
{if (orderId.Length == 0 && atmStrategyId.Length == 0)
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Action.Buy, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "Strategy2", atmStrategyId);
}
}}
if(!wwwww || !xxxxx || !yyyyyy || zzzzzz)
{if(useOutputWindow){Print("Short Signals OK ");Print("");}
if(FirstTickOfBar)
{if (orderId.Length == 0 && atmStrategyId.Length == 0 && SellSignal[1] == 1)
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Action.Sell, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "Strategy1", atmStrategyId);
}
}}
Print("Using ATM Strategy S+ ");
if(!nnnn && !oooo && SellSignal[1] ==1] || !ppppp && SellSignal[1] ==1) //<---No matter what is here, it never gets executed
{if(useOutputWindow){Print("Short Signals+ OK ");Print("");}
if(FirstTickOfBar)
{if (orderId.Length == 0 && atmStrategyId.Length == 0)
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Action.Sell, OrderType.Market, 0, 0, TimeInForce.Day, orderId, "Strategy2", atmStrategyId);
}
}}
if (orderId.Length > 0)
{
string[] status = GetAtmStrategyEntryOrderStatus(orderId);
// 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)
{
// Print out some information about the order to the output window
Print("The entry order average fill price is: " + status[0]);
Print("The entry order filled amount is: " + status[1]);
Print("The entry order order state is: " + status[2]);
// If the order state is terminal, reset the order id value
if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
orderId = string.Empty;
}
} // If the strategy has terminated reset the strategy id
else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
atmStrategyId = string.Empty;
if (atmStrategyId.Length > 0)
{
// You can change the stop price
if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)
//AtmStrategyChangeStopTarget(0, Low[0] - 3 * TickSize, "STOP1", atmStrategyId);
// Print some information about the strategy to the output window
Print("The current ATM Strategy market position is: " + GetAtmStrategyMarketPosition(atmStrategyId));
Print("The current ATM Strategy position quantity is: " + GetAtmStrategyPositionQuantity(atmStrategyId));
Print("The current ATM Strategy average price is: " + GetAtmStrategyPositionAveragePrice(atmStrategyId)) ;
Print("The current ATM Strategy Unrealized PnL is: " + GetAtmStrategyUnrealizedProfitLoss(atmStrategyId)) ;
}
}//Closes the "if(useATMstrategy)" section
NinjaTrader_Josh
01-28-2009, 08:02 AM
You have completely separate conditions here. You should not be using the same variable to store IDs. You need separate variables for these.
eDanny
01-28-2009, 08:48 AM
Why would that matter? When any of the four conditions are met, orderId.Length is checked and atmStrategyId.Length is checked to see if an order is in progress. If I make separate ID's for the two different ATM Strategy trades then I could have two orders open at once, correct? That is not my goal at this time. I must be not seeing something.
NinjaTrader_Josh
01-28-2009, 09:00 AM
eDanny,
From your code I have no way to discern if you actually check orderId or atmStrategyIds. From what I can tell you could be overwriting Id values that are still in use by an ATM somewhere and that can lead to some concerns as orders lose tracking.
eDanny
01-28-2009, 09:08 AM
{if (orderId.Length == 0 && atmStrategyId.Length == 0)
Before the order is executed.
else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
atmStrategyId = string.Empty;
After an order is made.
Are these not the checks you are looking for?
That code posted is the whole body of trade execution section, the rest is all testing for the trade conditions. If there is something more I need that I missed, what is it?
eDanny
01-28-2009, 10:07 AM
OK, changed orderId to orderId0 and added orderId1. Did the same for atmStrategyId. Now for every trade I should check if orderId0.Length == 0 && atmStrategyId0.Length == 0 && orderId1.Length == 0 && atmStrategyId1.Length == 0 and if all true an entry is made, correct?
NinjaTrader_Josh
01-28-2009, 10:14 AM
Right. So those may be the necessary checks and those checks are likely the ones that are preventing your additional entries because they are not reset. You will need to slowly step through your code and track the state of them to see when they reset (if ever).
eDanny
01-28-2009, 10:40 AM
Print("atmStrategyId1 " + atmStrategyId0.Length);Print("orderId1 " + orderId0.Length);
shows:
atmStrategyID1 32
orderId1 0
in the output window while in a trade and:
atmStrategyID1 0
orderId1 0
when flat. I fact I have never seen the orderId show anything other than zero, ever.
NinjaTrader_Josh
01-28-2009, 10:46 AM
eDanny,
Unfortunately I cannot assist you in debugging. You need to slowly step through your code at every single step and find what all your variables are before each if-statement and find which one is stopping you from calling additional AtmStrategyCreate()s.
Always check the Control Center for errors as well. You should be able to get unique orderIds. Print it out immediately after you set it.
eDanny
01-28-2009, 11:04 AM
I have another question about orderID and atmStrategyId. If they are checked, additional orders should not be executed? The sample strategy just keeps entering orders.
NinjaTrader_Josh
01-28-2009, 11:10 AM
eDanny,
That does not happen on my end. On the sample strategy it will enter once and then in the Output Window as long as that strategy is active it will keep printing things like this:
The current ATM Strategy market position is: Flat
The current ATM Strategy position quantity is: 0
The current ATM Strategy average price is: 0
The current ATM Strategy Unrealized PnL is: 0
The entry order average fill price is: 0
The entry order filled amount is: 0
The entry order order state is: Working
The current ATM Strategy market position is: Flat
The current ATM Strategy position quantity is: 0
The current ATM Strategy average price is: 0
The current ATM Strategy Unrealized PnL is: 0
If it prints that it will never enter continuously.
eDanny
01-28-2009, 11:18 AM
Grrr, every time I tried it, multiple entries have occured, starting with those tests yesterday.
Ok, quit my other strategy so output window isn't cluttered and trying again.
eDanny
01-28-2009, 11:25 AM
I see the constant output whereas my strategy only prints that type of status when in a position.
NinjaTrader_Josh
01-28-2009, 11:30 AM
The constant output should occur as you are in a position. As you are in a position the sample should never enter a new entry.
eDanny
01-28-2009, 02:00 PM
If my strategy is running on two charts with the same instruments, how can I check to see if there is an open position on one chart to prevent another entry? Got a feeling there is no way to tell.
NinjaTrader_Josh
01-28-2009, 02:03 PM
No way to tell.
cirion
02-03-2009, 08:02 PM
If my strategy is running on two charts with the same instruments, how can I check to see if there is an open position on one chart to prevent another entry? Got a feeling there is no way to tell.
I would suggest you break your code into states for example so in your code it deals with multi signals happening at once and you are not falling back on anything other than your code :)
//method 1
if(XXX || YY && openorder == false)
{
method1 = (true);
}
Then do something like this
if (method1 == true && openorder == false)
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Action.Sell, OrderType.Limit, Close[0], 0, TimeInForce.Day, orderId, "ATMTemplate", atmStrategyId);
openorder = (true);
}Remember to set your variables back to a reset state once your account is flat again.
Side note this is an example ONLY.
Hope that helps.
CiRioN
eDanny
02-04-2009, 06:35 AM
Don't believe that would do anything since the strategy on another chart would not know openorder=true.