View Full Version : Automation issue with TD
sublime
02-10-2009, 09:09 AM
Started my live trading on TD this week with a couple of glitches. Yesterday my orders were rejected because they were GTC instead of Day. I corrected this and started anew this morning. The order fired through and executed fine. However it did not create the Profit Target or Stop Loss orders. I quickly logged into my TD account and confirmed that no other orders were created. Is there a simple reason for the non-creation of these orders? When it does work, I will be able to see these Limit orders in Ninja, correct?
I will note that I've done extensive backtesting on this strategy. I also ran it in simulation for a month and everything worked fine. Hope this is an easy one.
NinjaTrader_Josh
02-10-2009, 09:15 AM
sublime,
You will want to run your strategy with TraceOrders = true and also isolate your profit target logic with Print statements. If an order was actually placed you will get an output and a reason as to why it was rejected. Otherwise you know your code never went into that code block.
sublime
02-10-2009, 09:36 AM
I'll make sure to do that. However this worked 100% in simulation, so I'm thinking its more of a syntax or setting issue, than a logic issue.
I create the Target and Stop Loss orders in the Initialize()
SetProfitTarget("MT3QLDOpen", CalculationMode.Percent, profit);
SetStopLoss("MT3QLDOpen", CalculationMode.Percent, StopLoss, true);
and then my OnBarUpdate() calls the EnterLong as
{
EnterLong(DefaultQuantity, "MT3QLDOpen");
}
and we know its hitting the EnterLong as the Buy is executed successfully.
NinjaTrader_Josh
02-10-2009, 09:42 AM
You have to TraceOrders it to see why your order was rejected or not received or anything along those lines. If your order was actually submitted you will receive some print lines to inform you of this.
sublime
02-11-2009, 02:02 PM
Day 2 update. So I put TraceOrders = True in my code and reviewed the Output. The output actually showed no errors, but the log told me that my account had *express trading* disabled. I fixed that at TD, but to no avail. The Buy and Sell orders are being sent to TD fine. The limit orders are not. In the Orders tab, the strategy actually created the stop order and profit target order, but did not send them to TD.
The Stop Limit order has a status of *Initialize* and the Profit target order has a status of *PendingSubmit*
I'm wondering...I have *Orders per direction* set at 1. Is that preventing them from being sent? I thought that parameter applied only to opening positions.
Any further insight?
NinjaTrader_Josh
02-11-2009, 02:10 PM
Please post the actual TraceOrders log for the relevant point in time.
sublime
02-11-2009, 05:33 PM
Ok... attached is a small portion of the Output log. It's a little crazy as this is a multi-timeframe, multi-instrument strategy. From a pure debugging standpoint I'm going to add logic to not even hit the trade block if a position is already open. It would cut this log down by 80%. And it looks as if the strategy thinks the position is closed, therefore it kills the stop order. But your expertise is needed.
I will also say that the initialize block has a profit and stop order per instrument. Not sure if this is a problem.
SetProfitTarget("MT3QLDOpen", CalculationMode.Percent, profit);
SetProfitTarget("MT3QIDOpen", CalculationMode.Percent, profit);
SetStopLoss("MT3QLDOpen", CalculationMode.Percent, StopLoss, true);
SetStopLoss("MT3QIDOpen", CalculationMode.Percent, StopLoss, true);
Thanks for the help guys.
NinjaTrader_Bertrand
02-12-2009, 06:38 AM
Yes, the strategy ignores the the stop / target order as no position to exit from exists. Did you recheck your various signal names to match up?
Multiple SetStopLoss and SetProfitTarget calls are ok, as long as you use unique signal names per position and instrument.
sublime
02-12-2009, 07:15 AM
Yes, they are unique signal names per position and instrument, however I've combined some of the redundant code which might be confusing TD. Such as:
if (BarsInProgress ==0 || BarsInProgress==2)
{
if ((MACD(BarsArray[0],12, 26, 9).Diff[0] > .02) && (MACD(BarsArray[2],12, 26, 9).Diff[0] > 0))
{
EnterLong(DefaultQuantity, "MT3QLDOpen");
}
}
The 0 and 2 BarsArray are the same instrument, just at different time periods.
I'll do two things. Simplify the code to try and avert any confusion. I'll also do a live test of a very simple strategy I have with profit and stop targets to ensure its the code and not something at TD.
I'll add that there is definitely a position to set a profit and stop price and it has not been closed. Additionally, when the close signal is triggered, the strategy successfully closes the position.
NinjaTrader_Josh
02-12-2009, 07:27 AM
sublime,
You need to step through your code and ensure the positions with the correct signal names exists at the time you place the Set() methods. From your logs it is evident you are sending Set() methods when there are no positions. Furthermore you start running into these: Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties' which suggests your signal names to be wrong earlier.
sublime
02-12-2009, 07:36 AM
Here is how I'm interpreting the Output log.
Understand that the two instruments are inverse to each other (QLD, QID) So when one instrument is approaching a buy signal, the other instrument is firing exit signals.
1. The program hits the Entry signal for QLD and buys QLD.
2. In the same bar, the program hits the ExitLong block of code for QID, however there is no position.
3. For some reason, it confuses the fact that there is no position in QID, and therefore kills the profit and target orders for the QLD position.
Even with unique names, it looks like that's what's happening.
NinjaTrader_Josh
02-12-2009, 07:43 AM
Not sure I follow. Don't submit ExitLong()s if you don't have the position.
sublime
02-12-2009, 07:43 AM
Well the reason it has all the exceeds positions messages is that I don't have logic that checks to see if the strategy is already in a position. I merely kill it at the strategy window by setting "Entries per Direction" to 1. So it will continue to try and create a position as long as the entry criteria is true. It makes the log super big, so I will add that logic to not even hit the Set() if already in a position. Maybe that will clear all of it up.
sublime
02-12-2009, 09:12 AM
Yes, I'll do this. I knew the program would ignore the Exit call if no position existed, so I didn't worry about building MarketPosition qualifying logic. I just didn't think it would mess up other parts of the program.
NinjaTrader_Josh
02-12-2009, 09:30 AM
sublime,
It doesn't mess it up, but when your logs are flooded with traces of those ignores it becomes extremely difficult to track down what is causing the issue.
sublime
02-12-2009, 02:07 PM
Success! I added marketposition logic to the instruments and set the *simulated* parameter to false on the setstoploss(). I think the latter was causing more issues than anything, and it was not my intention to *hold* this order in the first place. When running live, it barely created any output (which is good) and when the buy hit, the order was created and then it created the limit and stop orders. I watched them go right into TD. Then when the strategy hit its .5% profit, the limit order fired in TD, cancelled the stop and updated Ninja immediatelly. Great and Fast!
Looks like I'm in business!
NinjaTrader_Josh
02-12-2009, 02:16 PM
Glad you got it resolved.
sublime
02-12-2009, 02:29 PM
A follow up. I have so many strategies I find that I sometimes miskey a param value in the strategy popup. Is there a way to compile all these into the strategy so I don't have to retype them?
NinjaTrader_Josh
02-12-2009, 02:46 PM
What parameters are you talking about here. You can set user defined parameters' defaults in the Variables section of the code. The strategy parameters you can set are these: http://www.ninjatrader-support.com/vb/showpost.php?p=18506&postcount=5