Stop loss and Trailing stop using Rules
Author: jholmberg083
Creation Date: 12/10/2019 6:50 PM
profile picture

jholmberg083

#1
Good morning!

I have an idea I’d like to backtest. I believe I have figured out a way to do it using the Rules Wizard (see below), but was hoping someone could take a look and make sure they agree that the rules will achieve what I’ve intended. Or does anyone have any better ideas how to achieve my goal with a Rule Based strategy? I’d like to save wealthscript code as a last resort because I’ve found I’m terrible at trying to make any tweaks to it. Thank you,

Jeff

----------------------

EXAMPLE OF WHAT I’M TRYING TO ACHIEVE

I want to place a tight bracket around the entry price, where:
--If the price falls 2% or more below my entry price, I exit the position (hereinafter, “stop loss #1”).
--If the price climbs 2% or more above my entry price, I want to:
(a) exit the position if the price tries to slip back below that that 2% gain price point (hereinafter, “stop loss #2”).
(b) use a trailing stop to allow the price to climb even higher, with the stop kicking in when the price falls by 1% from the recent high (hereinafter, the “trailing stop”)

HOW I’VE TRIED TO DO IT

ENTRY: I set up a rule to enter a long position at market price whenever the time is after 9:50am ET but before 3:30PM ET (Eugene: thanks again for your help in the 8/4/19 forum thread with creating these time-based rules).

As for the exit, I actually set up two different exits, trusting that the software will know to exit the position as soon as either of the two exits kick in:

EXIT #1: I used the “Long Exit at Profit Target with fixed stop + Trailing Stop.” I believe this will satisfy “stop loss #1” and the “trailing stop” from my Example above. For the inputs, I used the following:
--”Profit Pct”: I’m assuming that the software will close the position entirely when this target is met, so I made it fairly high, like 30%. I’m assuming the tight trailing stop will close the position well before this is triggered though.
--“Stop Pct”: I used 2%.
--"Trail Stop Trigger:”I used 2%
--"Trail Stop Pct”: I used 1%

EXIT #2: In an effort to create the exit for “stop loss #2” in my example, I decided to try simply using a “Sell at Stop Loss” rule. But where the input % would normally be positive (example: an input of 2% will trigger an exit when a position drops from $100 entry down to $98), I input a negative 2% (hoping that, for example, if a $100 position grows to $102, only then will the software be concerned about the price falling below $102). That said, I want to make sure the software isn’t triggering this exit before the stock even has a chance to make that 2% gain. Can anyone think of a better way to trigger my “stop loss #2”?
profile picture

Eugene

#2
Hi Jeff,

QUOTE:
But where the input % would normally be positive (example: an input of 2% will trigger an exit when a position drops from $100 entry down to $98), I input a negative 2% (hoping that, for example, if a $100 position grows to $102, only then will the software be concerned about the price falling below $102).

Sorry to rain on your parade but a negative input % won't work for a Stop Loss, if you mean it.
profile picture

jholmberg083

#3
Thanks Eugene, any ideas for other Rules I could drag and drop that would achieve the “Stop Loss #2” I’m looking for?
profile picture

Eugene

#4
Please correct me if I'm wrong but to me these two rules look somewhat mutually exclusive:
QUOTE:
(a) exit the position if the price tries to slip back below that that 2% gain price point (hereinafter, “stop loss #2”).
(b) use a trailing stop to allow the price to climb even higher, with the stop kicking in when the price falls by 1% from the recent high (hereinafter, the “trailing stop”)
profile picture

jholmberg083

#5
I agree that they're technically mutually exclusive, but they can still be part of the same plan as long as we just wait to see which one gets triggered first. A fair analogy would be that I'm walking into a farmers market, determined to buy either an apple or an orange (but not both), and the decision will be made based on which booth I encounter first. The apple and the orange are mutually exclusive, but both are a part of my shopping plan.

Bringing that back to my trading plan, I would want the exit would happen at the sooner of exit (a) or (b) in your Post #4 above. Here are two scenarios of how my plan would react to various upward price movements (downward price movements are addressed by my Stop Loss #1 in Post #1 above):

SCENARIO 1: Price climbs 2.2%, but then starts to slip backward. If I only had exit (b) in place (the 1% trailing stop), I would exit the position with a gain of 1.2%. But I would want exit (a) to kick in first, and lock in the 2% gain.

SCENARIO 2: Price climbs to anything higher than 3%. Now, only exit (b) (the 1% trailing stop) really matters, because it will get triggered before exit (a) will have a chance to.
profile picture

Eugene

#6
QUOTE:
A fair analogy would be that I'm walking into a farmers market, determined to buy either an apple or an orange (but not both), and the decision will be made based on which booth I encounter first.

Good point. You can logically OR conditions in the Rule Wizard. To separate two different exits, drop the OR Divider (at the top of the Conditions list) on top of the second exit in an OR-ed pair.
profile picture

jholmberg083

#7
Thanks, I'll do that. But what kind of Rule can I use to "exit the position if the price tries to slip back below that that 2% gain price point"? (See “stop loss #2” in Post #1 above). I'm open to whatever you propose, but here's a possible solution I've hypothesized (I think I'm just missing a tool for it, see below):

Suppose I used the "Sell at Stop Loss" rule with the stop loss set at .05%, but then attached a condition to it called "Price is higher by X% than it was entry", with that % set at 2.00%. Only problem is, there is no such condition in my "Price (or Volume) Action" folder in the Conditions window. I found a very similar condition called "Price higher by X% than it was a number of bars ago", which helped me come up with the idea at least. Can you think of a tools in the Community Library that would achieve this condition?
profile picture

Eugene

#8
The proposed condition hasn't existed but it won't hurt to add it to Community.Rules:

* "Price is higher (lower) by X% than it was at Current Position's Entry Bar"

I got it done. Grab the updated library from the Download page.
profile picture

jholmberg083

#9
Thanks Eugene! I can't wait to try it out. I'm hitting a snag though. I believe I have followed all the directions you've provided in the past to download and install the latest community rules, but I can't seem to find the new indicator in Conditions list (I looked through the "Price (or Volume Action" folder). Here's a recap of what I did:

1.) Went to: http://www2.wealth-lab.com/WL5WIKI/CommunityRules.ashx
2.) Clicked the link labeled "log in to the Wiki"
3.) Tried refreshing and/or logging in again until the Green "Attachments" link appeared in the upper right
4.) Clicked on that green attachments link, then clicked on the link "C.Rules.zip" to download it
5.) Unzipped the file to C:\Program Files\Fidelity Investments\Wealth-Lab Pro 6\Data\Rules
6.) Unzipped the file to C:\Program Files (x86)\Fidelity Investments\Wealth-Lab Pro 6\Data\Rules for good measure
7.) Opened Wealth Lab and looked for the new indicator but couldn't find it.

Can you think of anything I forgot to do?
profile picture

Eugene

#10
You'll find it under the "Position-Based" group (because it's specific to the Position, not Price).
profile picture

jholmberg083

#11
Found it, thanks again for coding it for me!

I'm onto another snag though: I've been trying out this new Condition but the results don't seem to make any sense. I'm not sure if I'm applying it wrong, or maybe something is wrong with the code? (I'm sure that my own user error is more likely). Anyway, here's the process that leads me to believe something isn't right:

HYPOTHESIS

>>IF I use a very tight stop loss (i.e., 0.05%) with a condition that this stop not be triggered until the price has climbed at least 2% from my entry price (this was my idea in post #7 above that you wrote the code for),

>>THEN, here's how I would expect this strategy to compare to one that simply exits a position when a 2% profit target is reached: (1) the number of gain transactions should be identical or very close, and (2) instead of each gain ending up around 2%, each gain will should end up around 1.95%.

This prediction seems reasonable to me since, with either exit, all we're doing is waiting for a stock to rise 2% in value before selling it. The only difference is that one strategy allows a 0.05% slippage before that sale takes place.

EXPERIMENT

First, I created a baseline that we can compare the experiment to: I made set of rules that would buy the stock "IQ" at market price (on a 1 minute basis for 1/1/19 to 6/30/19), then sell at either a 2% stop loss a 2% profit target. The strategy yielded 251 trades, of which 137 were winners. The average winner was just over 2%.

Next, I ran my experiment: I replaced the "sell at profit target" rule with a "sell at stop loss" rule. The "stop loss %" field was set to 0.05. Grouped underneath this "sell at stop loss" rule is the new condition you wrote, "Price is higher by X% than it was at Current Position's Entry Bar", with the "Percentage" field set at 2.00.

FINDINGS

The experiment yielded only one trade, which entered into on 1/2/19 and never closed. I've repeated similar experiments with other %'s and other stocks, and have found that the only trades that close are the ones that hit the lower stop (a drop in price to 2% below entry price).

Any ideas for next steps? Thank you as always for your help.
profile picture

Eugene

#12
You probably lost me. If price is higher (for a long trade) than it was at the bar of entry then the whole notion of selling at stop loss is mutually exclusive. The two events have a chance to work only if the stop triggers on a future bar i.e. by then the trade becomes losing.

Your scenario ("price climbs [on one bar] but then starts to slip backward [on another bar]") requires something that isn't readily possible to accomplish using Rules. However, it's quite easy to do in code as this Tutorial shows: Setups, Triggers, Delays, and Timeouts.

Good luck!
profile picture

jholmberg083

#13
Sorry I lost you, I think I'm just using different semantics than you since I'm not as experienced. I'll try to clarify one last time, just in case it changes your answer:

I understand that many day traders "move their stop" once the trade is in profitable territory, following the old adage "never let a winning trade turn into a losing trade". That's what I'm trying to accomplish here: once the trade moves past a certain gain percentage, I "move my stop" to make sure I lock in a small amount of gain. In your post #12 above, I think you're saying that I shouldn't call it a "stop loss" because I'd actually be exiting the position profitably. I still think of it as a stop loss because of the price "slipping backward" and losing ground from a higher point. If this doesn't change anything, and the Rules can't factor in "moving the stop", then I'll try your recommended next steps using WealthScript.

However, I'll need to pay someone to do the coding for me, as I've given up on trying to write it myself. Can you recommend anyone besides Yuri Zolotarev who would be interested in writing the script for hire?

FYI: The link Yuri provided to his website in this forum post goes to a Chinese Medicine website https://trade-algo.com, and I can't find him on Upwork either.
profile picture

Eugene

#14
Hi Jeff,

If you're willing to drop the confusing statement in strikethrough then "Sell at percent based Trailing Stop" condition from the Trailing Stops group should get the job done using Rules:

QUOTE:
--If the price climbs 2% or more above my entry price, I want to:
(a) exit the position if the price tries to slip back below that that 2% gain price point (hereinafter, “stop loss #2”).
(b) use a trailing stop to allow the price to climb even higher, with the stop kicking in when the price falls by 1% from the recent high (hereinafter, the “trailing stop”)


P.S. Thanks for the heads-up re: Yuri's website redirect.
profile picture

jholmberg083

#15
Hi Eugene,

I can see where the strikethrough sentence above is confusing. For the benefit of future reference, I should probably rephrase (a) above to "move my fixed stop to a price that locks in the 2% gain"

As for the idea to change (a) into "Sell at percent based trailing stop", that's actually what (b) is already doing, so what you're effectively proposing is that we drop step (a) altogether. I can technically create a profitable strategy by doing this, but barely. The problem is that in order to preserve the 2% gains (FYI, the reason I want to do so is because these gains pretty much offset all the 2% losses, which forms a zero-sum game I can improve upon), I would need a razor thin trailing stop (say a trail of 0.05%). This razor thin trail nearly always stops out the trade before it can grow to anything substantial.

I believe I can get a much better edge by moving the fixed stop from its <2%> starting point up to the 2% gain point (when the price actually moves that high of course), then giving the trade some breathing room to grow the gain even more (say to 3%) before implementing a trailing stop that's not as restrictive (say 1%).

All that said, I recognize the limitations of the Rule based strategy you have pointed out. I think I'll start a separate post asking if anyone is interested in coding for hire.

Thanks again for all the help. Best regards,

Jeff
profile picture

Eugene

#16
Jeff,

I think you can do it in Rules yet it would take a minor tweak to the output code. What you describe fits into "Sell at Breakeven Stop" where you set the Breakeven level to 2%. However, by definition it sells at breakeven whereas you'd want it to lock in a small profit of 2%. To accomplish it you could set everything up in a Rule-based Strategy, click "Open Code in new Strategy Window", and then make a simple edit to the relevant section. Conversion to code should be the final step as there's no turning back.

Here instead of selling at stop at the entry price the code is adjusted to sell at the entry price + 2%:
CODE:
Please log in to see this code.


Does this make sense to you?
profile picture

jholmberg083

#17
Yes, that makes sense, and I was even able to tweak the code to your specifications without breaking it! (A rare occurence for me). I'll spend some time playing around with it, but based on a preliminary test I just did, I think we got it! Thanks again!
profile picture

Eugene

#18
Cool! Glad you got it working.
profile picture

jholmberg083

#19
Just to wrap this thread up, I wanted to note that backtesting revealed that my idea wasn't very profitable after all. But thank god (and Eugene) for the ability to discover this through back-testing and not through dozens of live trades.
profile picture

Eugene

#20
Jeff, thanks for sharing your feedback.
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).