Converting Tarzan indicator to TOS

T

tradebyday

Active member
Hello community,

I do not know if any of you will even want to try and accomplish this, but I myself will definitely be giving it a go (though I am very lack luster in coding). I have provided the script from MT4 below (which I believe is based in python?) as the guy who transferred it to tradingview's pinescript has hidden his source code. But this indicator is called "Tarzan" and is quite a useful trend indicator on the smaller time frames for futures and has shown promise in FX as well (where it was originally discovered by a colleague of mine). I am creating this thread to document my attempts at transferring this indicator over to TOS, but anyone else that wants to take a stab at it, please be my guess. It is a rather simple indicator based on a few MA's and the RSI. It essentially allows you to notice when price is trending or consolidating when combined with a decent trend system, helping you avoid some false signals. The less losses we take, the more gains we keep. Here is a link of what it is supposed to look like visually, and ultimately is the goal https://www.tradingview.com/script/bcVnD20w-PpSignal-Trazan-MT4-System/

Code:
//+------------------------------------------------------------------+
//|                                                 TARZAN+ALERT.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"

#property indicator_separate_window

#property indicator_buffers  6
#property  indicator_color1  DodgerBlue
#property  indicator_color3  Lime 
#property  indicator_color4  Red
#property  indicator_color5  Lime
#property  indicator_color6  Red
#property  indicator_width1  2
#property indicator_level1 15.0
#property indicator_level2 85.0
//----+
extern bool Alert_ = false;
extern int TimeSleep_Alert = 120;
//----+
extern int TimeFrame = 15;
extern int RSI=23;
extern int applied_RSI=0;
extern int MA=62;
extern int method_MA=0;
extern int koridor = 4;
//---- buffers
double BU0[];
double BU1[];
double BU2[];
double BU3[];
double BU4[];
double BU5[];
string name = "";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   name = "TARZAN+NOALERT "+GetNameTF(TimeFrame)+" "+Symbol()+" RSI="+RSI+" MA="+MA;
   IndicatorShortName(name);
   SetIndexBuffer(0, BU0);
   SetIndexStyle(0, DRAW_LINE); 
   SetIndexBuffer(1, BU1);
   SetIndexStyle(1,DRAW_NONE);
   //----
   SetIndexBuffer(2, BU2);
   SetIndexStyle(2, DRAW_LINE); 
   SetIndexBuffer(3, BU3);
   SetIndexStyle(3,DRAW_LINE);
   //----
   SetIndexBuffer(4, BU4);
   SetIndexStyle(4, DRAW_ARROW);
   SetIndexArrow(4,110);
  
   SetIndexBuffer(5, BU5);
   SetIndexStyle(5,DRAW_ARROW);
   SetIndexArrow(5,110);     
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//----
   int limit,b_sh; 
   int counted_bars=IndicatorCounted(); 
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//----
   for(int i=0; i<limit; i++)
   {
    b_sh=iBarShift(Symbol(),TimeFrame,Time[i]);           
    BU0[i] = iRSI(Symbol(),TimeFrame,RSI,applied_RSI,b_sh);       
   }
//----
   int koef = 1;
   if(TimeFrame>0)koef = TimeFrame/Period();
//----
   for(i=0; i<limit; i++)
    {         
     BU1[i] = iMAOnArray(BU0,0,MA*koef,0,method_MA,i);
     BU2[i] = iMAOnArray(BU0,0,MA*koef,0,method_MA,i)+koridor;
     BU3[i] = iMAOnArray(BU0,0,MA*koef,0,method_MA,i)-koridor;       
    }       
//----
   for(i=0; i<limit; i++){
    if(BU0[i]>BU1[i]) BU4[i]= iMAOnArray(BU0,0,MA*koef,0,method_MA,i);
    else  BU5[i]= iMAOnArray(BU0,0,MA*koef,0,method_MA,i);       
   }
//---- 
//----
   return(0);
  }
//+------------------------------------------------------------------+
string GetNameTF(int TimeFrame=0) {
  if (TimeFrame==0) TimeFrame=Period();
  switch (TimeFrame) {
    case PERIOD_M1:  return("M1");
    case PERIOD_M5:  return("M5");
    case PERIOD_M15: return("M15");
    case PERIOD_M30: return("M30");
    case PERIOD_H1:  return("H1");
    case PERIOD_H4:  return("H4");
    case PERIOD_D1:  return("Daily");
    case PERIOD_W1:  return("Weekly");
    case PERIOD_MN1: return("Monthly");
    default:         return("UnknownPeriod");
  }
}
//----+
bool NewTime() 
 {
  static int time0 = -1;
  int time1 = TimeCurrent(); 
  if(time1-time0>TimeSleep_Alert)
   {
    time0 = TimeCurrent();   
    return(true);
   }
  else return(false);
}
//----
 
D

diazlaz

Well-known member
2019 Donor
VIP
interesting. please share your results and findings after you test.

Took a first pass at it:



Ruby:
#Tarzan indicator to TOS V1.0
#
#CREDITS
# unknown
#
#CHANGELOG
# 2020.04.01 1.0 @diazlaz - Initial Port
#
#LINKS
# https://usethinkscript.com/threads/converting-tarzan-indicator-to-tos.2031/
# https://www.tradingview.com/script/bcVnD20w-PpSignal-Trazan-MT4-System/
#

declare lower;

input length = 5; 
input ma_length = 50;
input koridor  = 5;

input price = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
plot RSI = 50 * (ChgRatio + 1);

def ma = average(RSI, 50);
plot upper = ma + koridor;
upper.SetDefaultColor(GetColor(8));
plot lower = ma - koridor;
lower.SetDefaultColor(GetColor(8));

plot k = ma;
k.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
k.AssignValueColor(if rsi > upper then COLOR.GREEN else if rsi < lower then COLOR.RED else COLOR.GRAY);

#End of Tarzan indicator to TOS V1.0
 
zeek

zeek

Active member
2019 Donor
Very interesting indeed! Did a quick test on some different stocks and timeframes and the results is looking really good.
Thanks @tradebyday for bringing it here and thanks @diazlaz for the port.

Will share more results as i have more data later.
 
horserider

horserider

Well-known member
VIP
Nothing new here just a pretty colored display. Just the RSI crosses of a RSI moving average. Several studies on here previously doing the same thing.
 
T

tradebyday

Active member
Dang you're fast! I went golfing today and was going to try and finish up my attempt after dinner but I guess I will put mine to the side for a few and test yours out. @horserider may be right in terms of its usefulness. I simply wanted to bring this out for testing on TOS since it is the platform I use on a daily basis. Great job @diazlaz !!!
 
D

diazlaz

Well-known member
2019 Donor
VIP
@tradebyday it's interesting, there are a number of ways to interpret and improve upon it, let me know if you find any other ways to use it or any optimizations. i usually gravitate to simpler less complex indicators over the more complicated ones.
 
T

tradebyday

Active member
Definitely, this is meant to be used alongside others, so I do not know how it will be stand alone. But I do want to tweak it and play with the moving averages and such so I will share anything that makes it better or just simply more interesting
 
horserider

horserider

Well-known member
VIP


RSI bands stripped down version. Maybe someone likes the colors and 50 and OB/OS lines. Just changed the lengthbb to 50
 
T

tradebyday

Active member
@horserider The colors definitely make it easier to read (if using it to trade the crossovers). I want to take this and apply it to other lower indicators as well in a "plug-n-play" type format. I'll share if anything promising shows up but am going into this not expecting much. May try some sort of smoothing to the RSI or an RSI based indicator and see what happens
 
horserider

horserider

Well-known member
VIP
Can play with the inputs here and see what can get. Same idea just derivative RSI. Maybe bblength 50 and 8,8,8,13 ????

Code:
#  Derivative RSI Oscillator
#  Can be used alone as a smoother RSI or as part of the Carting Wealth strategy when combined    with the MACD and MAs Charting Wealth study.
#  By Horserider 12/25/2019
#

declare lower;

input length = 14;
input length1 = 5;
input length2 = 3;
input length3 = 9;
input price = close;
input overbought = 4;
input oversold = -4;

def NetChgAvg = WildersAverage(price - price[1], length);
def TotChgAvg = WildersAverage(AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

def RSI = (50 * (ChgRatio + 1) - 50);

#################################
plot  ZeroLine = 0;
ZeroLine.SetDefaultColor(Color.YELLOW);
ZeroLine.SetLineWeight(1);

plot ob = overbought;
ob.SetDefaultColor(Color.GRAY);
ob.SetLineWeight(1);

plot os = oversold;
os.SetDefaultColor(Color.GRAY);
os.SetLineWeight(1);

input averageType = AverageType.EXPONENTIAL;
input averageType2 = averageType.SIMPLE;
def x = MovingAverage(averageType,RSI, length1);
def x2 = MovingAverage(averageType, x, length2);
def xs = MovingAverage(averageType2, x2 , length3);
def rsi__linediff = x2 - xs;

plot RSI_Line = rsi__linediff;
RSI_Line.SetDefaultColor(GetColor(1));

RSI_Line.SetDefaultColor(GetColor(5));
RSI_Line.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
RSI_Line.SetLineWeight(3);
RSI_Line.DefineColor("Positive and Up", Color.GREEN);
RSI_Line.DefineColor("Positive and Down", Color.DARK_GREEN);
RSI_Line.DefineColor("Negative and Down", Color.RED);
RSI_Line.DefineColor("Negative and Up", Color.DARK_RED);
RSI_Line.AssignValueColor(if RSI_Line >= 0 then if RSI_Line > RSI_Line[1] then RSI_Line.color("Positive and Up") else RSI_Line.color("Positive and Down") else if RSI_Line < RSI_Line[1] then RSI_Line.color("Negative and Down") else RSI_Line.color("Negative and Up"));

input AverageTypeBB = {default SMA, EMA, HMA};
input displaceBB = 0;
input lengthBB = 5;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;


plot midline ;


switch (AverageTypeBB) {
case SMA:

    midline   = reference BollingerBands( RSI_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).Midline;
case EMA:


    midline   = reference BollingerBands( RSI_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).Midline;
case HMA:


    midline   = reference BollingerBands( RSI_Line, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).Midline;
}

midline.SetDefaultColor(GetColor(3));
 
T

tradebyday

Active member
Just off preliminary testing with the original Tarzan indicator, my favorite settings are as follows: 20,21,5,close,hull. These settings are very smooth and if you combine it with some heiken ashi candles and a moving average of choice, or simply use this indicator (https://tos.mx/CvBHnWg) with settings close,OHLC4,(moving avg value of choice),0 then you can find some decent trends to capitalize on in various tickers and time frame preferences. You can edit that indicator to the preferred type of moving average if you wish by editing the source. Going to try and combine Tarzan and what @horserider shared and see what becomes of it over the next week
 
horserider

horserider

Well-known member
VIP
Recommend the ultimate RSI. Search for it. Stripped down is same as this but with OB/OS/50 lines. Full version has more info.
 
horserider

horserider

Well-known member
VIP
@tradebyday I did the derivative rsi variation. Smoother curve as expected but signals slightly slower. Somehow I guess I erased it while playing around with different studies so not able to post the code. Not sure I will redo it either. No advantage to my trading so just sticking with ultimate RSI with a longer length.
 
T

tradebyday

Active member
@horserider that I understand. You want to grab entries as close to the tops and bottoms as possible. I will be playing with the ultimate RSI tarzan combo next couple days. We'll see if it improves on the original at all. Whether or not that makes it worth using is a different story haha
 
horserider

horserider

Well-known member
VIP
It is the same thing, tarzan line and midline of URSI
 
T

tradebyday

Active member
But the added bonus of color changes for the lines allowing delayed entries if it is a false signal, and the bands could allow early exits, so it does "look" better. I just want to test it. The Korridor in tarzan helps a bit with eliminating false signals, just look at the marketforce indicator mod I posted earlier in the other forum (granted the marketforce tarzan would probably do poorly in real trading, I thought the way it displayed the data was rather cool).
 
T

tradebyday

Active member
From a visual standpoint, I thought the indicator would look better. Maybe I just need this baby on a 50" TV... but anyways I do like how both the tarzan korridor around the moving avg AND the ultimate RSI color changes filter out range trades. Settings DEFINITELY need to be played with on a per asset traded basis and per time frame basis to find the sweet spot, but it does its job well when you find it. However, because of the visual strain for me I can not test this beyond /ES haha. Here is the code for people that have larger displays than mine...
Code:
#Tarzan + Ultimate RSI
#
#CREDITS
# unknown
#
#
#LINKS
#

declare lower;

input ma_length = 21;
input koridor  = 5;

# RSI Bands and Double RSI SMA Cross Long Term Trades
#Created by Horserider 7/20/2019

#RSI
input length = 20;
input over_Bought = 80;
input over_Sold = 20;
input price = close;
input averageType = AverageType.WILDERS;
input line = 50;

def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

plot RSI = 50 * (ChgRatio + 1);
plot lline = 50;


#                ==== THE FIVE ====
RSI.DefineColor("Positive and Up", Color.GREEN);
RSI.DefineColor("Positive and Down", Color.DARK_GREEN);
RSI.DefineColor("Negative and Down", Color.RED);
RSI.DefineColor("Negative and Up", Color.DARK_RED);
RSI.AssignValueColor(if RSI >= 50 then if RSI > RSI[1] then RSI.Color("Positive and Up") else RSI.Color("Positive and Down") else if RSI < RSI[1] then RSI.Color("Negative and Down") else RSI.Color("Negative and Up"));

# RSI 2
input length2 = 21;
input over_Bought2 = 80;
input over_Sold2 = 20;
input price2 = close;
input averageType2 = AverageType.WILDERS;

def NetChgAvg2 = MovingAverage(averageType2, price2 - price2[1], length2);
def TotChgAvg2 = MovingAverage(averageType2, AbsValue(price2 - price2[1]), length2);
def ChgRatio2 = if TotChgAvg2 != 0 then NetChgAvg2 / TotChgAvg2 else 0;

plot RSI2 = 50 * (ChgRatio2 + 1);

RSI2.DefineColor("Positive and Up", Color.GREEN);
RSI2.DefineColor("Positive and Down", Color.DARK_GREEN);
RSI2.DefineColor("Negative and Down", Color.RED);
RSI2.DefineColor("Negative and Up", Color.DARK_RED);
RSI2.AssignValueColor(if RSI2 >= 50 then if RSI2 > RSI2[1] then RSI2.Color("Positive and Up") else RSI2.Color("Positive and Down") else if RSI2 < RSI2[1] then RSI2.Color("Negative and Down") else RSI2.Color("Negative and Up"));


AddCloud(RSI, RSI2, Color.GREEN, Color.RED);


#plot 5;

input AverageTypeBB = {default SMA, EMA, HMA};
input displaceBB = 0;
input lengthBB = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;

plot upperBand;
plot lowerBand;
def midline;


switch (AverageTypeBB) {
case SMA:
    upperBand = reference BollingerBands(RSI, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).UpperBand;
    lowerBand = reference BollingerBands(RSI, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).LowerBand;
    midline   = reference BollingerBands(RSI, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up).Midline;
case EMA:
    upperBand = reference BollingerBands(RSI, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).UpperBand;
    lowerBand = reference BollingerBands(RSI, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).LowerBand;
    midline   = reference BollingerBands(RSI, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).Midline;
case HMA:
    upperBand = reference BollingerBands(RSI, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).UpperBand;
    lowerBand = reference BollingerBands(RSI, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).LowerBand;
    midline   = reference BollingerBands(RSI, displaceBB, lengthBB, Num_Dev_Dn, Num_Dev_up, averageType = AverageType.EXPONENTIAL).Midline;
}

upperBand.SetDefaultColor(Color.GRAY);

lowerBand.SetDefaultColor(Color.GRAY);

def ma = ExpAverage(RSI, 50);
plot upper = ma + koridor;
upper.SetDefaultColor(GetColor(8));
plot lower = ma - koridor;
lower.SetDefaultColor(GetColor(8));

plot k = ma;
k.SetPaintingStrategy(PaintingStrategy.LINE_VS_SQUARES);
k.AssignValueColor(if RSI > upper then Color.GREEN else if RSI < lower then Color.RED else Color.GRAY);
 
T

Thomas

Active member
Tarzan is similar to a strategy of that PETD that was annoying. It's an RSI with only the 50 and the strategy was stay long over 50, short under. I also think it was to demonstrate things are not overbought or oversold just because they are below certain level....
 
AlgofinC

AlgofinC

New member
Hello, I am also very interested in this indicator, it seems very promissing at first glance. I have been playing around with this for 2 days I somehow I think it is awnsome in predicting reversal in forex currency pairs. However, it is never wise to trade reversal in forex pairs because of manipulation of big banks and they are not determined by supply and demand laws, so I am trying to experiment it on some other instruments that have supply and demand based price. Yet, somehow, this indicator just does not fit for those, one example is oil and other commodity like Cotton as well as gold. It does not make any sense, it works pretty well in predicting Spot Forex reversal but not other instruments that are intrinsically determined by supply and demand. Please, I would be happy if someone want to discuss and elaborate on this a little bit more.
 
T

tradebyday

Active member
@AlgofinC In regards to FX, unless you are trading hourly or higher TF then trading reversal is "safe" for scalping. But those higher TF you could be destroyed in the stop raids or other tactics taken by big money in FX, so definitely be careful there. Where I find its usefulness is positioning for reversal to VWAP on /ES futures. Try either on 10,000 tick chart, or 5min chart (if you want you can also do the /MES 4,000 tick chart). Using today, 4/17, as an example on the 10,000 tick chart... Long entry about 11am est, then second long entry at 3:10pm est, closing both out at 3:30pm est when price reached VWAP. /ES likes to touch VWAP AT LEAST once during RTH approx 80% of the time (last time I did a data check on that). So if you miss a VWAP tag play, then I would focus on a different strategy rest of day for /ES
 

Similar threads

Top