NexusFi: Find Your Edge


Home Menu

 





EL help: ATR conversion


Discussion in EasyLanguage Programming

Updated
    1. trending_up 1,651 views
    2. thumb_up 2 thanks given
    3. group 2 followers
    1. forum 7 posts
    2. attach_file 0 attachments




 
Search this Thread

EL help: ATR conversion

  #1 (permalink)
 
mntman's Avatar
 mntman 
Houston+TX/USA
 
Experience: Intermediate
Platform: TradeStation, ThinkorSwim
Trading: ES, CL
Posts: 11 since Jan 2019
Thanks Given: 1
Thanks Received: 5

could anyone help me convert this thinkorswim ATR study into easy-language please? ive gotten sorta close (see screenshot) but i think im getting hung up on defining the init/long/short "state" and "trail" portion.




Quoting 
input length = 14;
input trailType = {default modified, unmodified};
input ATRPeriod = 5;
input ATRFactor = 3.5;
input firstTrade = {default long, short};
input averageType = AverageType.WILDERS;

Assert(ATRFactor > 0, "'atr factor' must be positive: " + ATRFactor);

def HiLo = Min(high - low, 1.5 * Average(high - low, ATRPeriod));
def HRef = if low <= high[1]
then high - close[1]
else (high - close[1]) - 0.5 * (low - high[1]);
def LRef = if high >= low[1]
then close[1] - low
else (close[1] - low) - 0.5 * (low[1] - high);

def trueRange;
switch (trailType) {
case modified:
trueRange = Max(HiLo, Max(HRef, LRef));
case unmodified:
trueRange = TrueRange(high, close, low);
}

def loss = MovingAverage(averageType, TrueRange(high, close, low), length);

def state = {default init, long, short};
def trail;
switch (state[1]) {
case init:
if (!IsNaN(loss)) {
switch (firstTrade) {
case long:
state = state.long;
trail = close - loss;
case short:
state = state.short;
trail = close + loss;
}
} else {
state = state.init;
trail = Double.NaN;
}
case long:
if (close > trail[1]) {
state = state.long;
trail = Max(trail[1], close - loss);
} else {
state = state.short;
trail = close + loss;
}
case short:
if (close < trail[1]) {
state = state.short;
trail = Min(trail[1], close + loss);
} else {
state = state.long;
trail = close - loss;
}
}

plot TrailingStop = trail;
TrailingStop.SetPaintingStrategy(PaintingStrategy.POINTS);
TrailingStop.DefineColor("Buy", GetColor(0));
TrailingStop.DefineColor("Sell", GetColor(1));
TrailingStop.AssignValueColor(if state == state.long
then TrailingStop.Color("Sell")
else TrailingStop.Color("Buy"));


Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
How to apply profiles
Traders Hideout
ZombieSqueeze
Platforms and Indicators
Better Renko Gaps
The Elite Circle
REcommedations for programming help
Sierra Chart
Exit Strategy
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Spoo-nalysis ES e-mini futures S&P 500
33 thanks
Just another trading journal: PA, Wyckoff & Trends
28 thanks
Tao te Trade: way of the WLD
24 thanks
Bigger Wins or Fewer Losses?
23 thanks
GFIs1 1 DAX trade per day journal
21 thanks
  #3 (permalink)
benracz
Miami, USA
 
Posts: 11 since May 2017
Thanks Given: 1
Thanks Received: 9


Hi mtman... an EL guy here,

I don't want to come across as a naysayer, but I can see why this one would be a little tricky unless someone is intimately familiar with the source codes of ToS as well.

If you want someone to be able to help you, I'd recommend posting the source codes of all the functions involved as well. There are many things in this code that need to be expanded on in order to reproduce your ToS results more closely.

Another concern is - if you're looking for precisely the same results, that's going to be about 6-8 hours of debugging work for a professional, in my estimation. At least that's how long it would take me to dig in and debug stuff on a trade-by-trade basis.

a.) Although people here seem to be fairly generous with their time, I'm not sure whether it's reasonable to expect for a random person to just to do that on a thread like this,

b.) Do you really need this and is it really worth it? There are many ways you can come up with similar strategies in TS/MC without having to translate something like this over very precisely.

Just my 2 cents.

-Ben

Reply With Quote
  #4 (permalink)
 
mntman's Avatar
 mntman 
Houston+TX/USA
 
Experience: Intermediate
Platform: TradeStation, ThinkorSwim
Trading: ES, CL
Posts: 11 since Jan 2019
Thanks Given: 1
Thanks Received: 5

hey Ben... thanks for your reply and feedback. im going to reply in 2 parts


Quoting 
I'd recommend posting the source codes of all the functions involved as well

unfortunately thinkorswim doesn't have source codes for their functions. but in converting other codes i have learned that Max = MaxList and Min = MinList in EL. since EL also has its own functions for AverageTrueRange and TrueRange im not 100% sure if its the same as what is written in the TOS script.

below is my current EL script converted if anyone is curious. i have the "hangup section" part marked, as well as my "work around" section marked where i tried bypassing the switch altogether. i comment out one or the other sections to see how it plots as i con't working on it:

 
Code
{ source: thinkorswim }

Inputs:
	length(14),
	trailType.1.2(1),
	atrPeriod(5),
	atrFactor(3.5),
	firstTrade.1.2(1);

Variables:
	hilo(0),
	href(0),
	lref(0),
	trueRang2(0),
	atr(0),
	loss(0),
	state(0),
	trail(0),
	trailColor(0);
	

{ indicator }

hilo = MinList(H - L, 1.5 * Average(H - L, atrPeriod));

If L <= H[1] then
	href = H - close[1] else
	href = (H - close[1]) - 0.5 * (L - H[1]);

If H >= L[1] then
	lref = close[1] - L else
	lref = (close[1] - L) - 0.5 * (L[1] - H);
	

{ 1 = modified, 2 = unmodified }
switch(trailType.1.2) begin	
case 1:
	trueRang2 = MaxList(hilo, MaxList(href, lref));
case 2:
	trueRang2 = TrueRange;
end;

atr = AvgTrueRange(atrPeriod);
loss = atr;

{
//>>>>>>>>>> hang up begins <<<<<<<<<<<<<<<

{ 0 = init, 1 = long, -1 = short }
switch(state[1]) begin
case 0:
	switch(firstTrade.1.2) begin
		case 1:
			state = 1;
			trail = close - loss;
		case 2:
			state = -1;
			trail = close + loss;
	end;

case 1:
	If (Close > trail[1]) then begin
		state = 1;
		trail = MaxList(trail[1], close - loss);
	End
	Else begin
		state = -1;
		trail = close + loss;
	end;

case -1:
	If (Close < trail[1]) then begin
		state = -1;
		trail = MinList(trail[1], close + loss);
	End
	Else begin
		state = 1;
		trail = close - loss;
	end;
end;

//>>>>>>>>>> hang up ends <<<<<<<<<<<<<<<
}

//>>>>>>>>>> work around begins <<<<<<<<<<<<<<<

state = 1;
trail = close - loss;

{long}
If (Close > trail[1]) then begin
	state = 1;
	trail = MaxList(trail[1], close - loss);
End;

{short}
If (Close < trail[1]) then begin
	state = -1;
	trail = MinList(trail[1], close + loss);
End;

//>>>>>>>>>> work around ends <<<<<<<<<<<<<<<


{ plots }
If state = 1 then trailColor = green else if state = -1 then trailColor = red else trailColor = darkgray;

Plot1(trail, "Trail Stop", trailColor);

Started this thread Reply With Quote
  #5 (permalink)
 
mntman's Avatar
 mntman 
Houston+TX/USA
 
Experience: Intermediate
Platform: TradeStation, ThinkorSwim
Trading: ES, CL
Posts: 11 since Jan 2019
Thanks Given: 1
Thanks Received: 5


Quoting 
Do you really need this and is it really worth it

to me yes i feel its worth my time because when i played with the TOS script a little more i found some interesting relations when i would have two aggregations match (both were state = long or both were state = short). even the single aggregation results i thought were interesting. so its worth it to myself to con't working on in hopes to eventually make an EL strategy out of it and backtest in tradestation to see what results i get as a simple trend following approach.

below are some screenshots in TOS of CL and ES with the bars colored when 2 aggregations matched or when using just a single aggregation which is what peaked my interest:




Started this thread Reply With Quote
  #6 (permalink)
benracz
Miami, USA
 
Posts: 11 since May 2017
Thanks Given: 1
Thanks Received: 9

Hi mntman,

I understand what you're saying and it looks interesting. But I'd still recommend hiring an outsourcer for this or something - if you want to do it properly, it's going to be a lot of work, and without ToS function sources it might actually not be possible that it's never going to be replicated accurately.

-Ben

Reply With Quote
  #7 (permalink)
 
mntman's Avatar
 mntman 
Houston+TX/USA
 
Experience: Intermediate
Platform: TradeStation, ThinkorSwim
Trading: ES, CL
Posts: 11 since Jan 2019
Thanks Given: 1
Thanks Received: 5

i was able to figure it out on my own... apparently tradestation's ATR is defaulted for the simple avg. so to find the wilders smoothed avg (to match TOS) i had to replace my EL ATR definition with this below:

 
Code
//original using simple avg
atr = AvgTrueRange(length);


//corrected using wilders avg
If currentBar <= length then
	Value1 = Summation(TrueRange, CurrentBar) / CurrentBar
Else
	Value1 = ((Value1 * (length - 1)) + TrueRange) / length;

atr = Value1;
now my tradestation matches with the tos finally:


Started this thread Reply With Quote
  #8 (permalink)
benracz
Miami, USA
 
Posts: 11 since May 2017
Thanks Given: 1
Thanks Received: 9

Ha! Good job!


mntman View Post
i was able to figure it out on my own... apparently tradestation's ATR is defaulted for the simple avg. so to find the wilders smoothed avg (to match TOS) i had to replace my EL ATR definition with this below:

 
Code
//original using simple avg
atr = AvgTrueRange(length);


//corrected using wilders avg
If currentBar <= length then
	Value1 = Summation(TrueRange, CurrentBar) / CurrentBar
Else
	Value1 = ((Value1 * (length - 1)) + TrueRange) / length;

atr = Value1;
now my tradestation matches with the tos finally:



Reply With Quote
Thanked by:




Last Updated on July 1, 2019


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts