EL help: ATR conversion - futures io
futures io futures trading



EL help: ATR conversion


Discussion in EasyLanguage Programming

Updated by benracz
    1. trending_up 573 views
    2. thumb_up 1 thanks given
    3. group 2 followers
    1. forum 7 replies
    2. attach_file 0 attachments




Welcome to futures io: the largest futures trading community on the planet, with well over 100,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Thread Tools Search this Thread
 

EL help: ATR conversion

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

EL help: ATR conversion

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"));


Reply With Quote

Can you help answer these questions
from other members on futures io?
Convert NinjaTrader NT7 indicator to Tradestation EasyLanguage and RADARSCREEN
Platforms and Indicators
Issue meeting order entry conditions
Elite Automated NinjaTrader Trading
Last 5 minutes Label
ThinkOrSwim
Moving average with Lag reduction
NinjaTrader
Linux?
ThinkOrSwim
 
 
  #3 (permalink)
Miami, USA
 
 
Posts: 11 since May 2017
Thanks: 1 given, 8 received

Mmm


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)
Houston+TX/USA
 
Experience: Intermediate
Platform: TradeStation, ThinkorSwim
Trading: ES, CL
 
mntman's Avatar
 
Posts: 8 since Jan 2019
Thanks: 1 given, 1 received

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);

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


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:




Reply With Quote
 
  #6 (permalink)
Miami, USA
 
 
Posts: 11 since May 2017
Thanks: 1 given, 8 received

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)
Houston+TX/USA
 
Experience: Intermediate
Platform: TradeStation, ThinkorSwim
Trading: ES, CL
 
mntman's Avatar
 
Posts: 8 since Jan 2019
Thanks: 1 given, 1 received

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
 
  #8 (permalink)
Miami, USA
 
 
Posts: 11 since May 2017
Thanks: 1 given, 8 received

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
The following user says Thank You to benracz for this post:



futures io Trading Community Platforms and Indicators EasyLanguage Programming > EL help: ATR conversion




Upcoming Webinars and Events
 

Free BloodHound Licenses to everyone!

June
 

Every journal equals ten meals for the hungry

June
     



Copyright © 2020 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts