Buying and selling a position in the same bar
Author: htg
Creation Date: 8/26/2010 10:58 AM
profile picture

htg

#1
Is it possible to do so in WLP 6.0?
profile picture

Eugene

#2
Yes.
profile picture

htg

#3
Excellent. Here's my specific situation. I compute an entry point that is likely to be higher than the current price. I place a BuyAtStop for bar+1. I want to exit the same bar I entered if a particular profit target has been reached. Otherwise I want to exit the following bar at the open.

I think the following should accomplish this, but would appreciate your feedback.

CODE:
Please log in to see this code.


Thanks.
profile picture

Cone

#4
You shouldn't try to do it that way because:
1. you're likely to get yourself into a peeking problem, and,
2. the SellatLimit won't generate an Alert. Always use bar + 1!

But look, you cannot backtest with stop and limit orders on the same bar. Why?

Because you only know that the open occurred before all the other prices and that the close occurred the last. The order in between is an unknown for a single bar. Consequently, you can generate that limit order Alert on the same bar, but you should not backtest with it. Your results will always be inflated.

Nonetheless, here's how to code that mistake for backtesting:

CODE:
Please log in to see this code.

In live trading, Wealth-Lab's Orders tool can generate that same-bar order, however. See User Guide: Preferences > Trading > Allow Same Bar Exits
profile picture

htg

#5
I see. Thank you, that is quite helpful.
profile picture

htg

#6
Cone:

Supposing I wanted to execute that code in a real-time situation; that is, having Wealthlab issue the orders.

As I understand it, if a BuyAtStop (as well as BuyAtLimit) is not filled in the current bar then it is cancelled.

Can you please explain how that is done in the Fidelity environment? As far as I know there are only two kinds of orders: Day and GTC. So, an inter-day BuyAtStop that expires at the end of the day will work. But what about an intra-day order?

I spoke with a couple of people at Fidelity and they are unable to answer the question.

Thank you.
profile picture

Cone

#7
QUOTE:
As I understand it, if a BuyAtStop (as well as BuyAtLimit) is not filled in the current bar then it is cancelled.
Not really.

First, Trading Alerts by Strategies using weekly, monthly, or larger scales are placed as Good Til Canceled (GTC). (That means if you're trading Weekly bars, for example, and generate a new set of Alerts on Friday night, you should probably cancel all your active orders for the previous week and then enter the new set of orders.)

Any other scale is a "Day order"...

If you're not Auto-Trading, an order will remain Active until it's filled, you cancel it, or at the end of day.

If you are Auto-Trading, an order will remain Active until it's filled, you cancel it, at the end of day, or if the Strategy doesn't keep them open on the next bar. In other words, the way to cancel an order is to not send one (create an Alert)! You don't have to do anything special for that, it's exactly how [just about] any Strategy's logic already works.

profile picture

htg

#8
I'm still a little confused.

The context for this is auto-trading.

Now consider this code snippet (from above):

CODE:
Please log in to see this code.


To me this says that the BuyAtStop is effective only during one bar. It either hits or it goes away.

This is reinforced by your statement that

QUOTE:
If you are Auto-Trading, an order will remain Active until it's filled, you cancel it, at the end of day, or if the Strategy doesn't keep them open on the next bar.


So, how does one code a strategy that doesn't keep orders open on the next bar?

Also, is there some documentation on this you can point me to?

Thanks.
profile picture

Cone

#9
QUOTE:
To me this says that the BuyAtStop is effective only during one bar. It either hits or it goes away.
To me it says, BuyAtStop is effective during one bar, and, if the same BuyAtStop (with the same stop price) occurs on the next bar, then no cancel occurs and the previous order continues.

You don't have to guess with this stuff. Just create a Strategy and assign it to a Paper Account. Then, turn on Auto-Trading and click Auto-Place in the Alerts view of a Streaming window and just watch.

QUOTE:
So, how does one code a strategy that doesn't keep orders open on the next bar?
Well, this is just programming. You can do it a thousand different ways. For example, if CrossOver(bar, series1, series2) BuyAtLimit(bar + 1... Since a series can't crossover another series on 2 consecutive bars, that order (Alert) will occur only once and then be canceled on the next bar.

Consider this: What would you expect to happen if you started a streaming 1-min chart with 100 bars with a stock trading at 330.5 and moves between 330 and 331 for 5 minutes while 5 more 1 minute bars enter the chart and the following set of orders are processed?

CODE:
Please log in to see this code.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).