ATR based exit strategy
Author: hsidhu88
Creation Date: 3/4/2011 9:38 AM
profile picture

hsidhu88

#1
Could someone help me write the following ATR based Exit strategy?

Exit Strategy

If EntryPrice + ATR*3 then sell with a trailing stop of 0.5%
For example, Entry Price = 50; ATR = $.25 then input a sell order at $50.75 and sell at $50.49 or higher (depending on how the price moves up)

If EntryPrice - ATR*3 then sell
For example, Entry Price = 50; ATR = $.25 then input a sell order at $40.25 and sell at $40.75

profile picture

Cone

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

hsidhu88

#3
Thank you for a prompt response.

Could you please explain # 5 in the following:
DataSeries atr = ATR.Series(Bars, 5);

PlotStops();

Could you please explain # 20 in the following:
for(int bar = 20; bar < Bars.Count; bar++
profile picture

hsidhu88

#4
Also,

How can we adjust scale in the code?
For example, when backtesting, I can select a scale of daily, weekly, 60 minutes, 30 minutes etc. When backtesting, say at 60 min scale, I would like to calculate ATR at a daily scale, how can I do that.
Moreover, I would like to exit my positions while checking for exit conditions on a "tick" scale.

Please advise.
profile picture

Eugene

#5
QUOTE:
Could you please explain # 5 in the following:

Welcome to the Wealth-Lab Wiki > Standard Indicators > ATR. There you'll see each indicator with their parameters documented.
QUOTE:
Could you please explain # 20 in the following:

* WealthScript Guide > Programming Trading Strategies > Trading Loop
* Wiki Knowledge Base > Bars, Loops, and Bar + 1

QUOTE:
How can we adjust scale in the code?

* WealthScript Guide > Multi-Time Frame Analysis
* QuickRef (hit F11) > Time Frames.
QUOTE:
Moreover, I would like to exit my positions while checking for exit conditions on a "tick" scale.

Neither Fidelity provides tick-based static data, nor it makes great sense to exit on a tick/second scale:

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

hsidhu88

#6
I will run the strategy at a 60 min scale. But I would like to exit by checking "forming bars"... not check prices at the end of 60 min but check it as frequently as I can. You earlier suggested, I can't do it at every tick (is that right?). If not every tick, could I check it every 1 min.
Please help with the code.
profile picture

Eugene

#7
Use 1-minute data as the basis scale, make the necessary calculations after SetScaleCompressed( 60 ), then trade on the basis time scale after RestoreScale(). See more code examples in the WealthScript Programming Guide > Multi-Time Frame Analysis.
profile picture

hsidhu88

#8
I have the following code. I cut and pasted from different strategies..so I am sure there are some inconsistencies. Please help.
Strategy is:
*************
Entry:
If MACD and Bollinger and Stoch conditions are true then
Buy
Exit:
If EntryPrice + ATR*3 then sell with a trailing stop of 0.5%
else if EntryPrice - ATR*3 then sell
else if the price goes up by 3% then sell with a trailing stop of 0.25%
else if the price goes down by 2.75% then sell if price goes down another 0.25%
*************

I think I have most of the code in place but few things are missing. Could you also explain the items that are underlined? I also want to check my exit conditions every 1 min but entry is based on 60 min. You have given me some information previously..but I am not sure how to integrate that.
Please review the code below... Thanks a million.


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

Eugene

#9
QUOTE:
You have given me some information previously..but I am not sure how to integrate that.

Yes I did but have you experimented with the code examples there? I couldn't see a single attempt to integrate them in your code. If you "also want to check my exit conditions every 1 min but entry is based on 60 min", then I also couldn't find it in your exit rules. As you understand, clear and unambiguous rules are required before coding anything. Please review your rules and expand them; for example, does it mean that the ATR is from 60-minute scale but the indicators are calculated on 1-min scale?
QUOTE:
GetTradingLoopStartBar

You could find the answer in the QuickRef, the function is documented there.
QUOTE:
Group2

This is a FAQ: After generating strategy code with "View Strategy Code", there are strings like "Group1" and "Group1|". What do they mean?
QUOTE:
>>>>>>>>>>>>>>>>>>>>>

Hint: Just use the CODE button to wrap your code blocks in between a pair of CODE tags (next to Bold,Italic... CODE,IMG).
profile picture

hsidhu88

#10
I have made another attempt to write my logic.. Please review.
I would like to
- check entry conditions BB, Stoch, MACD at 60 min bar;
- check ATR at daily interval and
- exit at 1 min bar or every tick.

I ran the back-test at 1 min bar.


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

Eugene

#11
Good attempt. A few notes:

1. The compressed DataSeries have to be synchronized before e.g. plotting.
2. Move the Daily ATR code block out of the trading loop.
3. Same for PlotStops.
4. Placement of AtMarket orders above AtStop orders is suggested.
5. No need to use AtClose orders with 1-min intraday data.

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

hsidhu88

#12
Thanks.

When I ran this strategy, I see only "ATR Stop" show up under Exit Name under Trades tab. There are other trades with Exit Name blank, although I have put in a description for every exit.

Moreover, when I changed aATR and bATR values to 100, a scenario a position is EXTREMELY unlikely to hit, I still see a lot of "ATR Stop" trades.
profile picture

Eugene

#13
Just a couple of hints to aid in your debugging:

1. the compressed daily ATR is OK (not to blame)
2. I see other exits e.g. "Sell Trailing Stop" (try other DataSets)
profile picture

hsidhu88

#14
How do I debug? Insert PrintDebug statements??? I tried that... I ended up having no trades

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

Eugene

#15
PrintDebug statements can't affect trade generation per se (unless your code flow got broken).

If you prefer, there's more ways to skin a cat: How can I debug my trading strategies in Wealth-Lab? and Debugging a Strategy with Visual Studio Express or SharpDevelop.
profile picture

hsidhu88

#16
I have been getting these errors when I run the strategy.
Could you please explain?


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

Eugene

#17
See: KB > Errors | Strategy > Index was out of range in the Wealth-Lab Wiki
profile picture

hsidhu88

#18
I read through the Wiki... what am I doing wrong in the code?

I am currently using the strategy to back-test. But once I finalize a strategy I would like to turn on Auto-Trading. I am already set up for that. Would I need to make any changes at that time? Ex. would the following for loop still valid?
CODE:
Please log in to see this code.



Is Index was out of range error caused by this for loop statement?
profile picture

Eugene

#19
QUOTE:
Ex. would the following for loop still valid?

Of course.
QUOTE:
I read through the Wiki... what am I doing wrong in the code?

Firstly, I do not know what you are doing in the code.

Regarding my code (see above # 3/10/2011 4:57 AM), then suggestion #2 may apply:
QUOTE:
* accessing the data that does not exist. Example: your DataSet contains the price data for a symbol that stopped trading, and you select the data range that goes beyond that date, and do not account for this fact in your Strategy.

I think this is your next step at debugging.
profile picture

hsidhu88

#20
I am running back-test on 1 min bar.

CODE:
Please log in to see this code.


In the if statement above, in atr[bar], I think the barInterval is 1 min..but I want to check the condition against daily ATR.

Please advise, how I can do that?


-------

Also I would write a function that I can call from any strategy? Could I create my own class MyClass and incorporate that into WealthLab code?
profile picture

Eugene

#21
QUOTE:
In the if statement above, in atr[bar], I think the barInterval is 1 min..

No, this is Daily ATR. See my comment above from 3/10/2011 11:25 AM.
QUOTE:
Also I would write a function that I can call from any strategy? Could I create my own class MyClass and incorporate that into WealthLab code?

This is a FAQ: Is there a way to reuse a function in Strategies? Is it possible to create a library of reusable functions?
profile picture

hsidhu88

#22
I want to use earnings date in my strategy.
Please refer to: http://www2.wealth-lab.com/WIKI/EarningsDate.ashx

The write up suggests the following:
...With a "using" reference to Community.Components, access EarningsDate methods (below) by direct calls as showing in the Example...

But when I include the reference ..
Using Community.Components
the program doesn't recognize Community and doesn't compile.
profile picture

Eugene

#23
Install the Community Components library (Extensions section of this site) and restart WL6.
profile picture

hsidhu88

#24
How can I check Ask and Bid before taking a position?
profile picture

Eugene

#25
profile picture

hsidhu88

#26
How do you cancel an order?
Is there any way you can change (cancel and place) a live (cover) order?
profile picture

hsidhu88

#27
More specifically, is it possible to change setTrailingStop parameters (like pullback etc) after a cover order is placed?
profile picture

Cone

#28
QUOTE:
How do you cancel an order?

Don't execute it on the next bar.

QUOTE:
Is there any way you can change (cancel and place) a live (cover) order?
If the strategy changes the stop or limit price on a subsequent bar, then the previous order is canceled and replaced with the new order.

Manually, you can select the order and click "Cancel/Replace Seleted" in the toolbar.
profile picture

hsidhu88

#29
just confirming...talking about auto-trading only...(not manual)...
If a setTrailingStop is placed with a pullback of 5%
then price moves up or down and now we want to change the pullback to 6%.
All we have to do is place the order again...Right?
profile picture

hsidhu88

#30
Cone, Please refer to your response dated 3/17/2011 6:30 PM
How do you "Don't execute it on the next bar"?
profile picture

Cone

#31
CODE:
Please log in to see this code.

Just see almost any strategy ever published. You execute a signal (BuyAt, ExitAt, etc.) based on conditional logic. If the condition is false, the signal is not executed.

Look, just like you stated, "If a setTrailingStop is placed with... then price moves up or down and now we want...". That's exactly how code operates - if (check something) [then] do something [else] do something else.
profile picture

hsidhu88

#32
After I run a back test, in the Trades report... can I add/delete more columns...specifically some "comments" or "description" columns
profile picture

Eugene

#33
No.
profile picture

hsidhu88

#34
how do you capture unrealized gains/loss in an active position?
profile picture

Cone

#35
See QuickRef (F11) > Position Object
> MAEAsOfBarPercent
> MFEAsOfBarPercent
> NetProfitAsOfBarPercent

Don't use "non" Percent measures in the trading script since dollar amounts will be based on a 1-share Position there. Those dollar-based properties are there to be used by Visualizers, PosSizers, etc.

That said, an exception is using the "WealthScript Override (SetShareSize)" option. By performing the sizing operation in the script itself, the dollar-based MAE/MFE/NetProfit values should be accurate in the trading script - though I don't think it includes commission costs.
profile picture

hsidhu88

#36
How do I determine number of shares in my position? I think position.shares also always returns 1.
profile picture

hsidhu88

#37
how do you use WealthScript Override (SetShareSize) in the code?
profile picture

Eugene

#38
The thread has got too far away from "Trading Strategies" (i.e. discussing trading techniques) with these various questions on WL6 features, daytrading and WealthScript. Please consider posting them in a new thread (or if you come up with non-related questions, in new threads) that we would then move to a proper subforum. Thanks.
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).