How to code for this

Questions directed to our Support Team. Responses can be expected within 24 hours.

How to code for this

Postby Kevin_in_GA » Sat May 17, 2014 1:51 pm

Pete and gang:

I want to use a stop loss that is 2*ATR(20) below the entry price and see how it backtests. This would be a static stop loss based on the ATR(20) value the day before the trade is entered. Example, I want to backtest buying stock at a 3% pullback from the prior day close and use a stop loss that would look like this

GTCStop($entryprice - 2*ATR(20))

I know that I would need to use a function like ValueWhen() to make sure that the ATR(20) value is not dynamic, but trying this is not working. Any thoughts?
Kevin_in_GA
 
Posts: 171
Joined: Wed Aug 04, 2010 10:14 am

Re: How to code for this

Postby Overload » Sun May 18, 2014 8:41 am

You will need to test this to verify it is working as you want, but I think you can do this by using a Stop Order Type and using $daysheld in the ref() formula. So your example would be:

Exit Order Type: Stop
Exit Order Price: $entryprice - ref(2*atr(20), -$daysheld)

Again, you should test this to make sure it is working properly. But the use of $daysheld in the ref() formula is the key.

Pete
Overload
 
Posts: 2248
Joined: Wed Nov 30, 2005 12:14 pm

Re: How to code for this

Postby Kevin_in_GA » Sun May 18, 2014 11:47 am

Well, I want to use a limit exit order to lock in a profit so this approach would not let that happen. I tried to use the following:

GTCstop($entryprice - ref(2*atr(20), -$daysheld))

as the exit string but it is not allowed. This is deemed non-static even though it technically holds to a constant value.
Kevin_in_GA
 
Posts: 171
Joined: Wed Aug 04, 2010 10:14 am

Re: How to code for this

Postby Overload » Mon May 19, 2014 8:09 am

I'm a bit confused by that. You mention that you want a limit order to lock in profit but yet you're attempting to use GTCStop() instead of GTCLimit() and your exit trigger price is actually below the entry price. Are you making short trades? If so, I guess that would explain how triggering your exits with a price below your entry price would allow you to lock in a profit.

In any case, you cannot use GTCStop or GTCLimit for this. The parameter in these formulas must be static through the course of the entire evaluation period, not just the course of the trade. In other words, you can use 5 or 10 or 15 as the parameter, but you cannot use a value like atr(20) that changes every day.

The only way you can do this is to go to the Order Types tab of your setup and change the Exit Order Type to "Stop" or "Limit" depending on what you want to do. You can then enter an Exit Order Price like this:

$entryprice - ref(2*atr(20), -$daysheld)

Pete
Overload
 
Posts: 2248
Joined: Wed Nov 30, 2005 12:14 pm

Re: How to code for this

Postby Kevin_in_GA » Mon May 19, 2014 8:39 pm

Well, what I am doing in real life is to set a stop loss at $entryprice - 2*ATR(20) on the day of entry which is GTC, and then also set a limit exit at reversersi(2,80) - the price at which the rsi(2) will equal 80 intraday. I'd like to backtest this as described - so I need to have BOTH a limit exit at reversersi(2,80) and a stop loss at $entryprice - 2*ATR(20). If this can't be done then no problem, but if it can be done I figured you would know how.
Kevin_in_GA
 
Posts: 171
Joined: Wed Aug 04, 2010 10:14 am

Re: How to code for this

Postby Overload » Tue May 20, 2014 8:53 am

People often forget this, but you can't use both an intraday stop and an intraday limit at the same time with end-of-day prices. If you think about a single day's price bar, you can visualize the high and low for the day going beyond the open and close. But there is no way to tell which came first: the day's high price or the day's low price. So if you have both an intraday stop trigger and an intraday limit trigger, and both would have been hit in the day's range of prices, there is no way to know if the stop or the limit would have been hit first.

This isn't a StrataSearch problem, but a limitation of end-of-day prices. If you want to use both an intraday stop and and intraday limit, you would need to use intraday prices. Sorry, but there's no getting around that.

Pete
Overload
 
Posts: 2248
Joined: Wed Nov 30, 2005 12:14 pm


Return to Technical Support

cron