TASCIndicators / ATR (Vervoort) Stops
Author: stevecle
Creation Date: 8/7/2010 12:34 PM
profile picture

stevecle

#1
Greetings: I'm wanting to generate charts with an ATR overlay over the price. Specifically, it is the Vervoort Modified ATR ***

I'm trying to download the TASCIndicators which contains this indicator. According to this reference:
http://www2.wealth-lab.com/WL5Wiki/TASCIndicatorsMain.ashx
it says that "You can find its zipped source code by clicking on "Attachments" on top of the page"

Questions:
1. There is no Attachments link on the webpage referenced above. Do you have an alternate link where I can easily download the source code?

2. Could you provide a link which has a beginners' tutorial or information on integrating and referencing the TASCIndicators in WLab5 ???

THANK YOU! Clemer

======================
*** the Vervoort Modified ATR is described here:
http://www2.wealth-lab.com/WL5Wiki/TASCJune2009.ashx
profile picture

stevecle

#2
OK, I answered my own question. Didn't know that the "Extensions" tab contained exactly what I was looking for. (oops!)

However, I'm having difficulty installing the extension: TASCIndicators.MS123_2010.6.0.0.wle

In Extension Manager, I'm dragging the file into the GUI. So far so good. A restart is necessary. When I restart a window message indicates that the TASCIndicators.dll installed succesfully (Result=Succeeded). Still so far so good. However, when I now return to Extension Manager, the TASCIndicator is no longer there. (And of course the Vervoort Modified strategy doesn't generate the ATR).

Any assistance is appreciated ! Thanks !
profile picture

Cone

#3
The extension updated (because the Extension Manager said that), but the new version is not "Fidelity-Supported". See User Guide: Extension Manager > Fidelity Supported. You can use the indicator; just check for it in the Indicators dialog, TASCIndicators folder.

Also: Why aren't TASC Indicator Updates Fidelity-Supported?
profile picture

Eugene

#4
QUOTE:
1. There is no Attachments link on the webpage referenced above

Right. Just in case the sentence right next to what you quoted above got lost along the way, I'm copying it here, with the link:

"Wealth-Lab Developer/Pro customers who can't see "Attachments" - please enter a new support ticket at wealth-lab.com."

If you need them, please do as suggested.
profile picture

stevecle

#5
Thank you Gentlemen for the explanation. I got the sample code working.

Follow-up Question: Do you happen to know if anybody has customized the Modified ATR line so that the line always appears above or below price (not just when a "buy" is triggered. I didn't find anything and am not familiar with the Wlab code. Thanks again !
profile picture

Cone

#6
The line drawn by the code is a trailing stop. It only make sense after you initialize at some point, like when you create a Position. On the other hand, you could create a stop and reverse Strategy, like this quick edit.

Notes: 1. A long position is created on the first bar to start things off. Depending on where this starts, it could change one or all the trades that follow.
2. The initial stop is now based on a percentage of price instead of a set value.
3. The ATRTrail class is unchanged.

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

stevecle

#7
Cone: Thanks so much. This is very helpful. Charles Kirk of the Kirkrport.com uses this often. I know a lot of people do this with StockFinder so it's great to generate with Wealth-Lab. Thank you very much.
profile picture

stevecle

#8
Sorry, but I'm back with one more favor. Would love to export ATR stops to large dataset to csv file (1 row for each symbol with 2 columns: symbol and atr).

Based on samples I saw, I tried using "Streamwriter csv" with "csv.WriteLine( output )" but this is over my head. Once again assistance much appreciated.
profile picture

Eugene

#9
QUOTE:
Based on samples I saw, I tried using "Streamwriter csv" with "csv.WriteLine( output )" but this is over my head.

What is not clear about these samples? What code were you able to come up with, and how exactly do you plan to export ATR stops?
profile picture

stevecle

#10
The initial assistance from Cone works like a charm and I'm most appreciative. Mission creep crept in and I'm trying to export THE CURRENT ATR stop from a large dataset to a csv file (1 row for each symbol with 2 columns: symbol and atr).

To do so, I'm joining the ModifiedATR code (posted by Cone above) with code modified by Eugene (a while back) which exports data to a csv file.

My initial attempt is posted below. Assuming, I didn't mess other stuff up when I "married" the 2 codes together, my specific question is contained in the section of the code below which says //ATR HELP PLEASE !!!

THANK YOU !!!

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

Eugene

#11
Mind the CODE tags, please.

Two Execute() methods and a nested class (Export2ASCII) won't do. You need to merge that into one. I'm not familiar with Vervoort's ATR stops so double-check my code.

(For brevity, I removed the ATRTrail class code -- you'll need to add it b/c my code won't compile.)
CODE:
Please log in to see this code.
profile picture

stevecle

#12
Hmmm, after adding back the ATRTrail class code, the code (Attachement 3 below) extracts data, but not the Modified ATR based on buy and sell signals. My goal is for the extracted data to reflect the data which is drawn on the charts with ws.drawline (first succesully implemented by Cone in the post above). As you can tell, I'm quite lame with Wealth-Lab code, but can tell that there are 2 parts of Cone's code (Attachement #1 and #2 below) that need to be integrated. I spent a few hours trying to get this to work but to now avail.

================== Attachment #1: NEED TO INTEGRATE TO GET CORRECT CALCS ==================================

// Call this method to update and return the stop price on each bar after entry
public double Price(int bar)
{
double prevPrice = stopPrice;
double newPrice;
if (longPosition)
{
newPrice = bars.Close[bar] - atrMod[bar];
stopPrice = newPrice > stopPrice ? newPrice : stopPrice;
}
else
{
newPrice = bars.Close[bar] + atrMod[bar];
stopPrice = newPrice < stopPrice ? newPrice : stopPrice;
}
ws.DrawLine(ws.PricePane, bar-1, prevPrice, bar, stopPrice, Color.DarkViolet, LineStyle.Dashed, 1);
return stopPrice;
}
}

=============== Attachment #2: AND THIS CODE ==============
// After creating a position, initialize a stop object
BuyAtMarket(1);
Position p = LastPosition;
ATRTrail atrStop = new ATRTrail(this, Bars, true, Close[1] * (1 - stop), _period.ValueInt, _atrMult.Value);
}

=============== Attachment #3: CURRENT REVISED CODE WITH ATRTrail Class restored ==========
=============== GOAL: The extracted data to be the same as what is drawn on the charts with ws.drawline (above) ====

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;
using System.IO;

namespace WealthLab.Strategies
{
//clem inserted back ATRTRail class code per Eugene's instructions
/* Class that encapsulates the ATR Trailing Stop, closing basis */
public class ATRTrail
{
private WealthScript ws;
private Bars bars;
private double stopPrice;
private bool longPosition;
private DataSeries atrMod;

public ATRTrail(WealthScript wL, Bars b, bool positionLong, double initialStop, int period, double factor )
{
ws = wL;
bars = b;
longPosition = positionLong;
stopPrice = initialStop;
atrMod = factor * TASCIndicators.ATRModified.Series(bars, period);
}
//atr code2
// Call this method to update and return the stop price on each bar after entry
public double Price(int bar)
{
double prevPrice = stopPrice;
double newPrice;
if (longPosition)
{
newPrice = bars.Close[bar] - atrMod[bar];
stopPrice = newPrice > stopPrice ? newPrice : stopPrice;
}
else
{
newPrice = bars.Close[bar] + atrMod[bar];
stopPrice = newPrice < stopPrice ? newPrice : stopPrice;
}
ws.DrawLine(ws.PricePane, bar-1, prevPrice, bar, stopPrice, Color.DarkViolet, LineStyle.Dashed, 1);
return stopPrice;
}
}
//end Clem's insert
public class ATRTrailingStopsSAR : WealthScript
{
private StrategyParameter _initStop = null;
private StrategyParameter _period = null;
private StrategyParameter _atrMult = null;

public ATRTrailingStopsSAR()
{
_initStop = CreateParameter("Initial Stop %", 10, 0.5, 50.0, 0.5);
_period = CreateParameter("ATR Period", 5, 2, 100, 1);
_atrMult = CreateParameter("ATR Multiplier", 3.5, 1.0, 5.0, 0.1);
}

/* Execute a strategy - trade on a specified date */
protected override void Execute()
{
double stop = _initStop.Value / 100d;
//end atr code

// public class Export2ASCII : WealthScript
// {
// protected override void Execute()
// {
const string sp = ",";
//end putting buy / sell code back

string path = @"C:\temp\ATR\";
string file = Path.Combine( path, "VervoortMod5.csv" );
StreamWriter csv = new StreamWriter( file );

for(int s = 0; s < DataSetSymbols.Count; s++)
{
SetContext( DataSetSymbols[s], false );

int bar = Bars.Count - 1; // Last bar

ATRTrail atrStop = new ATRTrail(this, Bars, true, Close[1] * (1 - stop), _period.ValueInt, _atrMult.Value);

//THIS WORKS BUT WRONG VALUES !!!
string output = Bars.Date[bar].ToShortDateString() + sp + DataSetSymbols[s] + sp + atrStop.Price( bar ).ToString();

csv.WriteLine( output );

RestoreContext();
}

csv.Close();
csv = null;
}
}
}
profile picture

Eugene

#13
Steve, I edited your posts three times including this thread, and already suggested how to use CODE tags in the past. If you want technicians to look at your code, please learn how to hit the CODE button to make it properly formatted. Thanks.

I'm not familiar with the ATRTrail class, my task was just to give you a working file export code.
profile picture

stevecle

#14
OK, sorry to be a pain, but I really am struggling with the coding (and this is the 101 thread : ).

I spent hours but can't figure out how to extract the darn ATR Stop for each stock. So I have another method in mind which is to copy the price alert from Strategy Monitor.

My only problem here is I cannot figure out how to set ExitAtStop, ShortAtStop, and BuyAtStop orders (which will then reflect the stop price in Strategy Montior). It's probably farily simple, but after a few hours of trying to convert doubles and strings, I cry "uncle".

Again, assistance would be most appreciated. (Specific question imbedded in code below)

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

Cone

#15
Steve, please use the CODE tags!

There are 7 buttons above each edit box. Next time (and every time after) just before you post code, click the one that says CODE and then paste your code between the tags.

Tips:
1. NEVER use bar + 0 in a trading signal.
2. ALWAYS use bar + 1 in a trading signal.
(There's an exception for AtClose orders, but it's not a beginner topic.)

CODE:
Please log in to see this code.
The thing you probably had the most trouble here in converting the code from AtMarket to AtStop orders was the conditional logic required for the AtStop orders. You see, AtMarket orders are practically guaranteed to execute, but AtStop orders will not unless there trigger price is reached. Consequently, if you don't use if(ExitAt...) the logic runs right into the atrStop = new ATRTrail... logic, which throws everything out of synch, causing orders to exit improperly on the following bar (and then repeating that error over and over).
profile picture

stevecle

#16
Cone: Thanks a million. From Strategy Monitor, I can now copy and paste the Vermoort ATR levels. This is a big help. Yee Hah. (Will use code tags in the future !)
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).