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?

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.

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.

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.

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.

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.

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

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?

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.

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.

We sent an Verification
Code to your email address. If you don't receive this code within the next 5 minutes.
Please click here to
request a new code.
New Verification Code sent. Please check your Inbox.Please enter your Email to receive a new Verification Code