< All Topics

Avoiding TradeStation same entry/exit issue

The Issue

TradeStation has had a very long outstanding issue in their backtest engine that periodically produces unexpected (and, to me, undesirable) trade results.  This situation is not a “bug”, per se, but rather “unexpected behavior”.  When your exit order is executed, it leads to the placement of an additional trade that is entered and then immediately exited, resulting in an additional commission/slippage cost (which, in the scheme of things, is probably a mild concern) and possible re-entry into a position, depending on the logic of your strategy.

I have come to the opinion that avoiding this unusual trade order situation may be a matter of personal preference. For some, the extra “phantom” order is inconsequential and if the strategy backtests profitably, then they have no problem using it. For me, I would rather keep the order flow clear and simple rather than unexpected or confusing. I don’t want to introduce any unusual behavior when placing orders at the trade desk. That is my rationale for this article — to keep trade orders as simple as possible.

Also, this “same entry/exit” trade phenomenon is the exact same phenomenon found in this true TradeStation bug I reported back in 2015 and remains unresolved to this day.  That makes me leary.  This issue appears to be multi-faceted, which may be why that 2015 bug has not been resolved.

Therefore the consequences are difficult to assess, and in some cases, such as breakout strategies or profit target stop orders, this phenomenon may produce a more profitable strategy since the strategy will immediately jump back into the trade if breakout conditions are still met.  I would rather forfiet a little bit of profit to keep the orders clear and simple.  But you may be perfectly comfortable with this extra “phantom” trade situation. So, as always, do your own testing to decide if the below suggestion to avoid this situation suits your own trading style, risk levels and objectives.

The Solution
Qualify EXIT market orders

The rule-of-thumb is to always qualify your market order trade EXITS so that they match the current market position.  This only applies to exit orders (i.e., when MarketPosition is equal to 1 (long) or -1 (short).

For example, if you find code that has this (or write it this way yourself):

if  openpositionprofit<-(sloss*ncons) or barssinceentry>=bse then begin
    sell ncons contracts next bar at market;
    buytocover ncons contracts next bar at market;
end;

Change it to this:

if  openpositionprofit<-(sloss*ncons) or barssinceentry>=bse then begin
	if ( MarketPosition = 1  ) then sell ncons contracts next bar at market;
	if ( MarketPosition = -1 ) then buytocover ncons contracts next bar at market;
end;

 

Qualify ENTRY or EXIT stop orders

Stop orders are a different situation since a stop order may pertain to an entry condition (qualified as MarketPosion = 1 or -1) or an exit condition (order qualified as MarketPosition = 0).

So instead of this:

sell ("MW StopLoss-L") next bar at longprice stop; 
buytocover ("MW StopLoss-S") next bar at shortprice stop; 

You could do this:

if ( MarketPosition = 0 or MarketPosition = 1 ) then
    sell ("MW StopLoss-L") next bar at longprice stop; 

if ( MarketPosition = 0 or MarketPosition = -1 ) then
    buytocover ("MW StopLoss-S") next bar at shortprice stop; 

If you do not include an IF statement that aligns with the exit, then there may be times within the lifetime of your strategy that produces an unexpected interaction of the trade orders.  If the stop conditions would still be met, then the result is that a position will exit, a new position will be created and then immediately exit again (costing the performance hit of slippage and commission) and a new position will be re-entered based on the other active stop.  While this may produce a slightly more profitable strategy, to me it introduces an unexpected change to the logic flow of the strategy and potentially complicates orders within the strategy or at the trade desk.

Therefore, for me, even if a strategy backtests profitably with this unexpected behavior, I have chosen to take the more simple route and use “If MarketPosition…” statements to prevent this trade behavior.

But, as I mentioned above, do your own testing to decide if avoiding this situation suits your own trading style, risk levels and objectives.

Example: Dissection of a trade

I dug into one of these same entry/exit trades and compared the orders generated to the orders generated for the solution I proposed above. The results were revealing and clearly demonstrate just why this “phantom” same entry/exit trade was created.

My test strategy was a simple breakout.

Entries were:

if ( MarketPosition <> 1 ) then Buy (“BrkOut-L”) next bar at ExpectedLongEntryPrice stop;
if ( MarketPosition <> -1 ) then Sellshort (“BrkOut-S”) next bar at ExpectedShortEntryPrice stop;

Exits were custom stop loss stop-order and custom profit target limit-order.  These emulate the behavior of TradeStation’s SetStopLoss and SetProfitTarget.  However, using those keywords triggered this TradeStation bug, so I could not use them.

I chose one of the same entry/exit trades and dissected the orders.

Orders WITHOUT the “If MarketPosition…” solution:

The current position was short.

Breakout order:
Buy (“BrkOut-L”) next bar at -1.7582 (ExpectedLongEntryPrice) stop

Profit target orders:
sell (“MW ProfitTarget-L”) next bar at -1.7655 (longprice) limit
buytocover (“MW ProfitTarget-S”) next bar at -1.8369 (shortprice) limit

Stop loss orders:
sell (“MW StopLoss-L”) next bar at -1.8131 (longprice) stop
buytocover (“MW StopLoss-S”) next bar at -1.7893 (shortprice) stop

The two orders in red did not match the current short position, but were included since there was not the IF qualfication to exclude them.

The sell limit order was active on the bar during the short position. When the short position was stopped out, price hit the buy stop at -1.7582, which was above the active limit order at -1.7655, making the limit order trigger an immediate sell.

The two extra sell orders (in red) caused this unexpected condition. It also had the effect of defering the next trade signal, interrupting the expected flow of the strategy.

Allowing only orders that match the current position remove the possibility of unexpected and confusing order interactions.

Orders WITH my solution:

Breakout order:
Buy (“BrkOut-L”) next bar at -1.7582 (ExpectedLongEntryPrice) stop

Profit target orders:
buytocover (“MW ProfitTarget-S”) next bar at -1.8369 (shortprice) limit

Stop loss orders:
buytocover (“MW StopLoss-S”) next bar at -1.7893 (shortprice) stop

Only orders that matched the short position were placed on the bar. This made order flow more sensical and reduced the possibility of unexpected behaviors, like a limit order being immediately triggered. The strategy’s logic was able to continue without interruption.

Table of Contents