Same Bar Exit
Author: ichechet
Creation Date: 1/30/2012 11:08 AM
profile picture

ichechet

#1
Gretings!

I have a question. I see an exaple in the Programming Guide:

CODE:
Please log in to see this code.


As I see it's always using bar + 1 construction for exit. But how about the entry and exit on the same bar? Sounds like it not checked in this code.
profile picture

Eugene

#2
Quoting the WealthScript Programming Guide (Programming Trading Strategies > Single-Position Strategies):
QUOTE:
SP Strategy Code Template

The code template installed with Wealth-Lab is a template for SP Strategies. It consists of the main loop and logic that makes entries and exits mutually-exclusive on the same bar. In other words, by following the template you'll automatically write code that only opens Positions or closes Positions on a given bar.
profile picture

Cone

#3
In backtesting the only proper same bar exit is an AtStop or AtLimit and/or AtClose order that follows an AtMarket entry. If you don't start with an AtMarket entry, no other combinations will produce realistic results. Just execute the same-bar exit logic following the AtMarket entry, on the same bar of course.
profile picture

Cone

#4
It was brought to my attention that I may have answered the wrong question.

First, when we say "Same Bar Exit" (as in the title of this post), we immediately think of entering and exiting the same Position on the same bar. In other words, to take a loss or profit on the same bar on which you entered a Position. That's the question I anwered.

However, it may be that you were really asking how to reverse a Position on the same bar, i.e., Cover and go long, or sell and go short. The answer is straightforward: just execute those signals on the same bar. This may be the example that you were looking for:

CODE:
Please log in to see this code.
profile picture

JDardon

#5
Thank you guys. Actually both questions answered something I was looking for.

Just a follow up question: For backtesting, placing the following code effectively simulates the same bar exits for back test purposes:
CODE:
Please log in to see this code.


However, my understanding from the online documentation is that in Preferences enabling the "Allow Same Bar Eits for Auto Trading" would effectively place those orders for the lifetime of the first bar through to the broker, without the explicit SellAtStop and SellAtLimit right after the BuyAtMarket. Did I get it right or are we still required to place the code above for real time auto trading?
profile picture

Cone

#6
It's a good question, and perhaps we should be more explicit in the guide. Try to follow this..

First, when the BuyAtMarket creates an Alert (i.e., on a bar that does not exist), there's no way that the signal can return a Position object.. there is no price to fill the position yet! Consequently, the code creates only one "Alert" for the entry because the SellAtStop/AtLimit orders will never be processed on the last (most recent) bar because "if ( BuyAtMarket(bar + 1) )" is always false on the last bar.

That's where the "Allow Same Bar Exits" preference comes in. When an entry Alert occurs and that option is enabled, Wealth-Lab checks if the code has assigned a non-zero RiskStopLevel and/or an AutoProfitLevel. If it has, then after the entry order is filled, the Orders tool automatically places the Stop and/or Limit orders based on the assigned levels.

Finally, assume that a same-bar exit occurred in live trading. In order to keep the code in synch with real life, you need your code as shown above to process the position on that now completed bar. Of course the StopPrice and ProfitTgt values should be assigned to RiskStopLevel and AutoProfitLevel, respectively -

CODE:
Please log in to see this code.


Make sense?
profile picture

JDardon

#7
So let me see if I got it right.

a) If I had enabled the "Allow Same Bar Exits" preference option but didn't place the SellAtStop and SellAtLimit statements in the code right after the BuyAtMarket, in real live trading the position would have been exited from my broker's account (if the price moved south past my stoploss level because WL indeed sent the same bar exit orders to the broker for that 1st bar; for the rest of the bars in the positions of course it is my responsibility to send them) but the strategy would have never noticed that the position was really closed and thus would continue to behave as it the position was still open?

However,

b) If I had not enabled the "Allow Same Bar Exits" and I had placed the SellAtStop and SellAtLimit statements in the code, they would have not been executed anyway until the next bar (the 1st bar in the position) had closed, effectively leaving your position unprotected because the broker never received any stop loss or limit order.

BTW I should have put the whole portion of the code as I did get from the documentation the need to set the RiskStopLevel and AutopProfit Level according to your code.
CODE:
Please log in to see this code.
profile picture

Cone

#8
a) right, if the 'real life' same-bar exit occurred and you didn't have the code there, it would cause the script to be out of sync with your live account. The script would start to process exit logic on the following bar. The result would be orders that "errrored out" since Positions do not exist for those exits.

b) right, 'unprotected' for the entry bar only.

The code is correct!
profile picture

kcbars

#9
Hello. I'm trying to understand the logic for back testing same bar exits. I realize that a market order is required to get somewhat realistic results, but I having a hard time grasping why, exactly. For instance, assume in the example above that instead of the buy at market line, it read:

CODE:
Please log in to see this code.


I've noticed when I run something like this, every same bar exit that occurs is the bar opening price (whereas with a buy at market order, the buy price is the bar opening price, which makes sense). Is the reason because the strategy has no way to calculate the basis for the sell logic as anything but the opening price? Thanks.
profile picture

Eugene

#10
The futility of backtesting same-bar AtLimit/AtStop orders is explained in the FAQ, with pointers to solutions:

FAQ | Strategies and WealthScript > I want to test a strategy that buys and sells with stop/limit orders on the same bar.