Buy when symbol is X% below its closing price Y days ago
Author: innertrader
Creation Date: 12/17/2014 4:28 PM
profile picture

innertrader

#1
I would like to build a trading strategy that provides a real-time (using Fidelity streaming) BUY signal when a symbol is X% below its closing price Y days ago. A sell signal would then be generated N trading days after the BUY signal was issued.

X, Y, and N are the three programmatic variables. I would use Daily close for the price Y days ago, but I want the BUY signal in real-time (Minute bar?). The SELL signal can be at the open or close of previous day of the trade day. Can you point me to one or more examples of scripts that I could piece together or modify to accomplish this?
profile picture

Eugene

#2
In general, for daily bars you could accomplish this in Rule-based strategies by adding a "Price is lower by X% than it was a number of bars ago" from Conditions > Price (or Volume) Action. On how to expose a parameter, please see the Wealth-Lab User Guide > Strategy Window > Strategy Builder > Parameter Sliders for the Strategy Builder.

For intraday bars we probably need this addressed before jumping into it:

QUOTE:
BUY signal when a symbol is X% below its closing price Y days ago.

What is the order type: AtLimit (which limit price?) or AtMarket?

QUOTE:
The SELL signal can be at the open or close of previous day of the trade day.

How could one exit at the open of the trade day (let alone previous day)? Both events should happen in the past. Please clarify.
profile picture

innertrader

#3
QUOTE:
What is the order type: AtLimit (which limit price?) or AtMarket?

AtMarket. To clarify, I won't be using WLP to actually place the trade, only to alert me. I am less interested in back simulation at this point as I am in automating an already-developed trading strategy going forward.
QUOTE:
How could one exit at the open of the trade day (let alone previous day)? Both events should happen in the past.

I'm not sure I understand your question, therefore my description probably wasn't clear. The exit event is purely based on number of trading days elapsed since purchase. I would like WLP to alert the Sell either the night before on on the Open in the morning. That way I won't have to calendar to track outside the program.

Thanks for moving my post to the proper folder.
profile picture

Eugene

#4
If you're into automating it, you'll need an AtStop order - neither AtLimit nor AtMarket. Likewise, no need in intraday bars - Daily data is enough. Consequently, it boils down to something like this:

profile picture

innertrader

#5
I see now that the Sell at Market is a built-in rule in Conditions:Position-Based. Where did you find the built-in rule "Price is higher by X% than it was by a number of days ago"? I am using Fidelity V 6.8 according to the Help drop-down.I don't see it as an option for my Position-Based conditions.
profile picture

Eugene

#6
I "found" it in Community Rules.
profile picture

Eugene

#7
Actually, you indeed need an AtLimit order for a "lower than" condition (I misread it for "higher"). Here's the code for use with Daily data:

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

innertrader

#8
Thank you so much Eugene for the outtanding support and rapid responses. I have been working with what you sent me and the code generated by rules (after downloading the Community Rules you pointed me to). I had to make code changes to allow for multiple positions to be open at the same time and to individually track the expiration of each. I have set up a test case with 1-day lookback and 0% below yesterday's price (lower than yesterday). It is buying and selling correctly with backtesting but I can't seem to get it to generate a real-time BuyAtLimit signal during the day, with streaming enabled with Fidelity. Here's the code. Please let me know if you see something I'm missing.

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

Eugene

#9
Robert,
Do not change anything than AtClose from bar+1 to bar, this is wrong. That makes the code peek into the future badly. As a side effect, this is the reason for not getting alerts:

FAQ | Strategies and WealthScript > Alerts are not triggered

Corrected code:

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

innertrader

#10
I tried this code with index symbol .NDX and it did not show Buy signals or trades for yesterday or today, which it should, although it appears to be back-testing correctly starting 2 days back.
profile picture

Eugene

#11
I'm sure you will find the reason after inspecting your position sizing. A buy alert is correctly generated for today. Yesterday there could not be a signal by definition.
profile picture

innertrader

#12
Here's what I'm using:

Starting Equity: 100000
% of Equity: 2

I am now using symbol QQQ and it correctly trades yesterday, but ONLY when streaming.
QUOTE:
A buy alert is correctly generated for today.

What are you seeing that lets you know that is the case? A Buy symbol on the chart? Today's date in the Trade tab?
QUOTE:
Yesterday there could not be a signal by definition.

I don't understand why you say that. Why not?

profile picture

Eugene

#13
Wait until today's bar closes and you'll see that trade on the chart.
profile picture

innertrader

#14
Market is now closed but trade is still not showing on the chart or on trade tab. Besides, a signal at the end of the day would not be helpful, as I am looking for intraday trading.
profile picture

Eugene

#15
QUOTE:
Market is now closed but trade is still not showing on the chart or on trade tab.


Order was not filled. It's an AtLimit trade. Price did not reach the level.

QUOTE:
Besides, a signal at the end of the day would not be helpful, as I am looking for intraday trading.


A signal before the OPEN, not at the end of day.

FAQ: Is it necessary to have access to intra-bar tick data to daytrade with Wealth-Lab?
profile picture

innertrader

#16
QUOTE:
Order was not filled. It's an AtLimit trade. Price did not reach the level.

Limit price is yesterday's close. It went below that intraday so Buy should have been triggered. It also closed below that.
QUOTE:
A signal before the OPEN, not at the end of day.

Do you mean before the open tomorrow? How is that intraday trading? I'm still looking for some kind of alert during the trading day so i can place a trade if the price opens at, or dips below, the limit price.

Trades for yesterday and today showed up when market closed, but only when I clicked on symbol again to reload it. At any rate, after-hours signals are not what I am looking for; I am looking for intraday signals and I still don't understand if I am getting them.
profile picture

Eugene

#17
QUOTE:
Limit price is yesterday's close. It went below that intraday so Buy should have been triggered. It also closed below that.

It DID trigger the signal on ^NDX yesterday. In QQQ it has to wait to liquidate the already established position (on Monday) before buying again.

QUOTE:
Do you mean before the open tomorrow? How is that intraday trading?

Yes, before the open tomorrow because the level (yesterday's or whatever closing price) is known and fixed. From your description of trading rules, it has never been evident why you insist on trading it intraday. From my point of view, there is no need for intraday bars.

If the code is not working as you envision it, your description of what you intended to do was not sufficient. For me it functions in accordance to the initial plan. I do not have anything else to suggest in this context without a clear explanation of your intraday idea
profile picture

innertrader

#18
If I understand what you are saying, then a BuyAtLimit will generate a Buy signal end-of-day / overnight if the limit price was *touched* the previous day, even if the close price ended higher than the limit.

I've been thinking about your comment about why I want to trade intraday but don't have a good answer for you. It sounds like your question is not a technical one, but rather has something to do with philosophy with which to approach trading. I'm presuming you are indicating that there is no average improvement in buying intraday or the next day because the price could be higher or lower the next day in either case, and on average about the same. Strategies that involve next-day trading rather than intraday are certainly much easier to manage. I'm interested to know whether this is what you are intending to convey. Would you care to elaborate?

Eugene, have a very happy holiday. Thanks for your help.

-Robert
profile picture

Eugene

#19
QUOTE:
If I understand what you are saying, then a BuyAtLimit will generate a Buy signal end-of-day / overnight if the limit price was *touched* the previous day, even if the close price ended higher than the limit.

Not quite so. A BuyAtLimit signal will be generated for tomorrow after today's close. The price level (X% below its closing price Y days ago) is known ahead. The limit order may get filled intraday tomorrow but the daily close price tomorrow may indeed end up higher (there's no contradicting requirement).

QUOTE:
Would you care to elaborate?

Robert, actually it wasn't my purpose to discourage you from intraday trading. I only supposed that your description of rules from the first post might not reflect your trading system's idea, and was looking for possible clarification.
profile picture

innertrader

#20
I thought my initial description indicated that (emphasis added).
QUOTE:
I would like to build a trading strategy that provides a real-time (using Fidelity streaming) BUY signal when a symbol is X% below its closing price Y days ago.

How might I have worded it differently?
profile picture

Eugene

#21
If you're looking to automate it as you stressed (and not produce signals to be executed elsewhere), why complicate things by throwing intraday data into the equation? The Quotes tool even has "Convert to Market" option to turn those daily limit signals into market orders. I don't get it.
profile picture

innertrader

#22
Now I see the disconnect.... I'm not sure where the automate word came in or maybe it has a specific meaning in your context, but I will be placing the trades myself.
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).