NexusFi: Find Your Edge


Home Menu

 





EasyLanguage SuperTrend indicator


Discussion in EasyLanguage Programming

Updated
      Top Posters
    1. looks_one Big Mike with 11 posts (59 thanks)
    2. looks_two ABCTG with 9 posts (6 thanks)
    3. looks_3 chtangwin with 6 posts (26 thanks)
    4. looks_4 Sam7768 with 5 posts (2 thanks)
      Best Posters
    1. looks_one Big Mike with 5.4 thanks per post
    2. looks_two chtangwin with 4.3 thanks per post
    3. looks_3 cedar with 1.3 thanks per post
    4. looks_4 ABCTG with 0.7 thanks per post
    1. trending_up 97,309 views
    2. thumb_up 103 thanks given
    3. group 42 followers
    1. forum 86 posts
    2. attach_file 9 attachments




 
Search this Thread

EasyLanguage SuperTrend indicator

  #21 (permalink)
 Sam7768 
Dallas, Texas, USA
 
Experience: Beginner
Platform: NT 6.5 & Tradestation
Trading: Equities & ES
Posts: 47 since Sep 2010
Thanks Given: 21
Thanks Received: 9

Sure Mike. This is what I was doing...

***************
inputs:ATRLength(9), ATRMult(1), Strength(9);
Var: p(0),strend(0),st(0);

st = SuperTrend(ATRLength, ATRMult, Strength, strend);

If st cross below close then buy ("buy") next bar at market;

if st cross above close then Sell ("sell") next bar at market;

***********************

This works, except for the fact that, at times, even though close crosses supertrend (from above or below), the Supertrend does not change the side (which is a great thing). But,

because the way I coded (above), the strategy keeps buying & selling in this situations (as I posted in above pic). How do I control this / code in such a way that buy or sell happens only when the supertrend changes side and not just close crossing Supertrend.

Any help will be greatly appreciated.

Rgds,

Reply With Quote
The following user says Thank You to Sam7768 for this post:

Can you help answer these questions
from other members on NexusFi?
ZombieSqueeze
Platforms and Indicators
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Request for MACD with option to use different MAs for fa …
NinjaTrader
NexusFi Journal Challenge - April 2024
Feedback and Announcements
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Retail Trading As An Industry
58 thanks
Battlestations: Show us your trading desks!
51 thanks
NexusFi site changelog and issues/problem reporting
48 thanks
What percentage per day is possible? [Poll]
31 thanks
GFIs1 1 DAX trade per day journal
31 thanks

  #22 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,322 since Jun 2009
Thanks Given: 33,143
Thanks Received: 101,477

Gotcha.

The strend var is the key if i remember, monitor it for a change = if I remember it is 1 for long, -1 for short, so you could do something like:

if strend = 1 and strend[1] <> 1 then buy 1 contract next bar at market;

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Started this thread Reply With Quote
The following user says Thank You to Big Mike for this post:
  #23 (permalink)
 Sam7768 
Dallas, Texas, USA
 
Experience: Beginner
Platform: NT 6.5 & Tradestation
Trading: Equities & ES
Posts: 47 since Sep 2010
Thanks Given: 21
Thanks Received: 9


Thanks much Mike. Will try this.

Reply With Quote
  #24 (permalink)
insideday
vienna
 
Posts: 22 since Aug 2009
Thanks Given: 0
Thanks Received: 38

Does somebody know which codes lines need to be changed to plot the stop as stairstep on every bar like
in this picture https://www.janarps.com/catalog/images/images_big/Arps_Triple_Trender.jpg
instead of a solid line ?

Reply With Quote
  #25 (permalink)
 shzhning 
Madison, NJ
 
Experience: Intermediate
Platform: CQG/TOS
Broker: Optimus/CQG
Trading: ZN/TN/ES/NQ
Posts: 134 since Jun 2010
Thanks Given: 65
Thanks Received: 112

Hi Mike, I'm looking at the SuperTrend function you posted last year.

This is the part that I need some help:

if
flag = 1 then up = avg + ATR;
if
flagh = 1 then dn = avg - ATR;


1)what are flag and flagh supposed to do
?
2)you already defined "up = avg + ATR" and "dn = avg - ATR" earlier in the indicator. Why do we have to repeat it here?

Thank you.

Reply With Quote
  #26 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,322 since Jun 2009
Thanks Given: 33,143
Thanks Received: 101,477


shzhning View Post
Hi Mike, I'm looking at the SuperTrend function you posted last year.

This is the part that I need some help:

if
flag = 1 then up = avg + ATR;
if
flagh = 1 then dn = avg - ATR;


1)what are flag and flagh supposed to do
?
2)you already defined "up = avg + ATR" and "dn = avg - ATR" earlier in the indicator. Why do we have to repeat it here?

Thank you.

Yes I think we discussed this above in the thread, it was a mistake.

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #27 (permalink)
 shzhning 
Madison, NJ
 
Experience: Intermediate
Platform: CQG/TOS
Broker: Optimus/CQG
Trading: ZN/TN/ES/NQ
Posts: 134 since Jun 2010
Thanks Given: 65
Thanks Received: 112

Mike, thanks for the quick reply.

Regardless of the bug, I'm still confused about:

1) the purpose of flag and flagh, and
2) why the following is repeated in the function code
up = avg + (ATR*ATRMult);
dn = avg - (ATR*ATRMult);


Thank you.

Reply With Quote
  #28 (permalink)
 chtangwin 
NJ
 
Experience: Beginner
Platform: NinjaTrader
Posts: 7 since May 2010
Thanks Given: 2
Thanks Received: 26

Thanks to Mike and others. This is a great thread. SuperTrend is one of my favorite indicator. I converted the anaSuperTrendM1 indicator (also from futures.io (formerly BMT)) to Easy Language. Hope it is ok.

Please let me know if any issues.

Indicator: anaSuperTrendM1

 
Code
inputs:
	periodMedian(3),
	periodATR(3),
	multiplier(2);
	
variables:
	median(0),
	upTrend(true),
	priorUpTrend(true),
	currentUpTrend(true),
	stopDot(0),
	priorStop(0),
	newStop(0),
	offset(0);

median = 0.5 * (high + low);
if currentbar = 1 then begin
	upTrend = true;
	stopDot = close;
end
else begin
	// FirstTickOfBar
	priorUpTrend = upTrend[1];
	priorStop = stopDot[1];
	offset = averagetruerange(periodATR)[1];

	if close > priorStop then begin
		upTrend = true;
		currentUpTrend = true;
		newStop = _anaMovingMedian(median, periodMedian) - multiplier * offset;
		if priorUpTrend = false then
			stopDot = newStop
		else
			stopDot = maxlist(newStop, priorStop);
	end
	else 
	if close < priorStop then begin
		upTrend = false;
		currentUpTrend = false;
		newStop = _anaMovingMedian(median, periodMedian) + multiplier * offset;
		if priorUpTrend = true then
			stopDot = newStop
		else
			stopDot = minlist(newStop, priorStop);
	end	
	else begin
		upTrend = priorUpTrend;
		currentUpTrend = priorUpTrend;
		stopDot = priorStop;
	end;
end;

if currentUpTrend = true  then plot1(stopDot, "UpStopDot");
if currentUpTrend = false then plot2(stopDot, "DnStopDot");
Function: _anaMovingMedian

 
Code
Inputs:
	PriceValue( numericseries ), 
	Period(NumericSimple);

variables:
	medianIndex(0),
	priorIndex(0),
	even(false),
	sPeriod(0),
	idx(0),
	val(0),
	initialized(false);
	
array:
	mArray[](0);

{ OnStartUp }	
if initialized = false then begin
	initialized = true;
	array_setmaxindex(mArray, Period);
	fill_array(mArray, 0.0);
	if mod(Period, 2) = 0 then begin
		even = true;
		medianIndex = floor(Period/2);
		priorIndex = medianIndex - 1;
	end
	else begin
		even = false;
		medianIndex = floor((Period - 1)/2);
	end;
end;

{ OnUpdate }
if currentbar < Period then begin
	sPeriod = currentbar;
	for idx = 1 to sPeriod begin
		mArray[idx] = PriceValue[idx-1];
	end;
	array_sort(mArray, 1, sPeriod, true);
	if mod(sPeriod, 2) = 0 then begin
		idx = floor(sPeriod/2);
		val = 0.5 * (mArray[Period - idx] + mArray[Period - idx + 1]);
	end
	else begin
		idx = floor((1 + sPeriod)/2);
		val = mArray[Period - idx + 1];
	end;
end
else begin
	for idx = 1 to Period begin
		mArray[idx] = PriceValue[idx-1];
	end;
	array_sort(mArray, 1, Period, true);
	if even then
		val = 0.5 * (mArray[medianIndex] + mArray[priorIndex])
	else
		val = mArray[medianIndex];
end;

_anaMovingMedian = val;

Reply With Quote
The following 10 users say Thank You to chtangwin for this post:
  #29 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,322 since Jun 2009
Thanks Given: 33,143
Thanks Received: 101,477

Nice work, we can always use some good EL programmers

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #30 (permalink)
 chtangwin 
NJ
 
Experience: Beginner
Platform: NinjaTrader
Posts: 7 since May 2010
Thanks Given: 2
Thanks Received: 26


Thanks Mike. futures.io (formerly BMT) is the greatest forum I ever know. Certainly glad I can contribute. I am new to EL and trading. Having programming background, writing code is easy, but learning trading is hard.

Feel free to let me know if there is any programming need, could be a good exercise for me.

Reply With Quote
The following user says Thank You to chtangwin for this post:





Last Updated on October 31, 2021


© 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