REX Oscillator for TOS

A

abcsthinkscript

New member
Hello Fellows!

I am new to this forum and I don't know how to code so just wondering if any expert here can help convert this REX Oscillator indicator code into thinkscript?

I poked around and found this link of the MT4 code....

Code:
//+------------------------------------------------------------------+
//|                                                          Rex.mq4 |
//|                               Copyright © 2014, Gehtsoft USA LLC |
//|                                            http://fxcodebase.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2014, Gehtsoft USA LLC"
#property link      "http://fxcodebase.com"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Green
#property indicator_color2 Red

extern int Smoothing_Length=14;
extern int Smoothing_Method=0;  // 0 - SMA
                                // 1 - EMA
                                // 2 - SMMA
                                // 3 - LWMA
extern int Signal_Length=14;
extern int Signal_Method=0;  // 0 - SMA
                             // 1 - EMA
                             // 2 - SMMA
                             // 3 - LWMA

double Rex[], Signal[];
double TVB[];

int init()
{
IndicatorShortName("Rex oscillator");
IndicatorDigits(Digits);
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,Rex);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,Signal);
SetIndexStyle(2,DRAW_NONE);
SetIndexBuffer(2,TVB);

return(0);
}

int deinit()
{

return(0);
}

int start()
{
if(Bars<=3) return(0);
int ExtCountedBars=IndicatorCounted();
if (ExtCountedBars<0) return(-1);
int limit=Bars-2;
if(ExtCountedBars>2) limit=Bars-ExtCountedBars-1;
int pos;
pos=limit;
while(pos>=0)
{
  TVB[pos]=3.*Close[pos]-(Low[pos]+Open[pos]+High[pos]);

  pos--;
}

pos=limit;
while(pos>=0)
{
  Rex[pos]=iMAOnArray(TVB, 0, Smoothing_Length, 0, Smoothing_Method, pos)/Point;

  pos--;
}

pos=limit;
while(pos>=0)
{
  Signal[pos]=iMAOnArray(Rex, 0, Signal_Length, 0, Signal_Method, pos);

  pos--;
}

return(0);
}



and this one on trading view.....



//@version=4
study("REX", overlay=false)
// Best used as an exit indicator at default settings.
// Also doing well in entering positions by using TEMA(14) for REX and SMA(14) as signal.
// You need to tune it yourself according to your trading style.
// Use at your own risk.
//
// Vitelot/Yanez/Vts Oct 2019
//
// Oct 2019: added zero line and OS/OB lines (currently commented out)
//
rex_ma_type = input(type=input.string, defval="SMA", options=["EMA","SMA", "Tenkan","TEMA"], title="REX MA type")
smoothing = input(14, title="Smoothing length", minval=1)
sig_ma_type = input(type=input.string, defval="SMA", options=["EMA","SMA", "Tenkan","TEMA"], title="Signal MA type")
smoothing_sig = input(14, title="Signal smoothing length", minval=1)
tenkan(sig, len) =>
0.5 * (highest(sig, len) + lowest(sig, len))
// triple ema
tema(src, len) =>
v2 = ema(src, len)
v = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential
v
ma(t, sig, len) =>
sss = float(na)
if t == "SMA"
sss := sma(sig, len)
if t == "EMA"
sss := ema(sig, len)
if t == "TEMA"
sss := tema(sig, len)
if t == "Tenkan"
sss := tenkan(sig, len)
sss
tvb = 3*close-open-high-low
rex = ma(rex_ma_type, tvb, smoothing)
sig = ma(sig_ma_type, rex, smoothing_sig)

plot(rex, color=#3342c1ff, linewidth=2)
plot(sig, color=#d2a344ff)
hline(0, title="Zero line", color=#ffffff80)
//stddev_period = 200
//bup = 2*stdev(rex,stddev_period)+sma(rex,stddev_period)
//bdn = -2*stdev(rex,stddev_period)+sma(rex,stddev_period)
//plot(bup, color=#ffff0080, style=plot.style_circles, title="Band up")
//plot(bdn, color=#ffff0080, style=plot.style_circles, title="Band down")





Many thanks in advance!:)

PS: I believe the default settings will have to be tweaked a bit. Hope you guys can find success with it as well!
 
Last edited:
T

TrueDepth

Member
VIP
"
The Rex Oscillator is a study that measures market behavior based on the relationship of the close to the open, high and low values of the same bar. The theory behind the Rex Oscillator is that a big difference between the high and close on a bar indicates weakness. Conversely, wide disparity between the low and close indicates strength. The difference between open and close also indicates market performance.

The True Value of a Bar (TVB) gives us an indication of how healthy the market is. It is possible to have a negative close and a positive TVB, and vice versa. This indicates that the market is building strength on the opposing side of the trend. The Rex Oscillator is a moving average of the TVB, indicating the inertia of the market. When the Rex Oscillator turns positive in a bearish trend, a reversal is indicated. Likewise, Rex turning negative in a bull market indicates a reversal to the downside.

The TVB is defined as:

TVB = (Close-Low) + (Close - Open) - (High - Close), or

= 3 * Close - (Low + Open + High)

Rex Oscillator (n periods) = moving average (n periods) of TVB "


Looks like it is just the TVB line
and then a 14 period simple moving average of the the TVB line.
 
CoachT

CoachT

New member
New Trader to the family guys. Trying to create what @abcsthinkscript asked you guys to do for him. Plz help. I felt like it was a simple code to create and though the code seems complete, it doesn't plot correctly when I use it on my chart.
Code:
declare lower;

input price = close;
input length = 8;
input averagetype = AverageType.SIMPLE;
input showBreakoutSignals = no;

def Exit = (close - low) + (close - open) - (high - close);

plot Exit_Line = MovingAverage(averagetype, Exit, length);
plot Main_Line = MovingAverage(averagetype, price, length);
plot Go_Time = price crosses above Main_Line;
plot Sow_Time = price crosses below Main_Line;

Go_Time.SetHiding(!showBreakoutSignals);
Sow_Time.SetHiding(!showBreakoutSignals);

Main_Line.SetDefaultColor(GetColor(1));
Exit_Line.SetDefaultColor(GetColor(0));
Go_Time.SetDefaultColor(Color.GREEN);
Go_Time.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Sow_Time.SetDefaultColor(Color.RED);
Sow_Time.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
 
horserider

horserider

Well-known member
VIP
@CoachT No idea if calculations are correct. Just commenting on how you did the code.
Look at your calculations. You want to plot the TBV ..... TVB = (Close-Low) + (Close - Open) - (High - Close) . Is that what you did?
Then plot the REX. Rex Oscillator (n periods) = moving average of TVB over (n periods) Is that what you plotted?

Get the plots then you can figure out the problem with the arrows.
 
CoachT

CoachT

New member
Im not really concerned about the arrows, i've been hung up on being able to plot the TVB and the the moving average over it. When it shows in my study they are on complete opposite sides of the chart not plotting anything at all.

When i plot the TVB by itself it works fine i think. But adding a separate SMA over it does not work correctly because it re-adjusts depending on the time perspective.
 
horserider

horserider

Well-known member
VIP
@CoachT Check what you are plotting. A SMA of price values will be much larger so that is why the big difference. And I do not think you are doing TBV correctly. Look at calculations closely. It is all in the previous post.
 
CoachT

CoachT

New member
@horserider I'm not sure if i'm getting any closer or further away with this code. I added different Price inputs to replace the CLOH and defined them. Like i mentioned earlier, If i only define the TVB and plot it i think it shows fine. But i'm struggling with adding the MovAvg

declare lower;


input price1 = close;
input price2 = Low;
input price3 = Open;
input price4 = High;
input length = 120;
input averagetype = AverageType.SIMPLE;
input showBreakoutSignals = no;

def Exit = (price1 - price2) + (price1 - price3) - (price4 - price1);

plot Exit_Line = Exit;
plot Main_Line = MovingAverage(averagetype, price1, length);
 
horserider

horserider

Well-known member
VIP
Nope. Read post #4 carefully. You are making it way too complicated.
def Exit = (close - low) + (close - open) - (high - close); instead maybe plot exit =
and then a MA of exit. Make sure your math calculations match what you are trying to plot. As in post # 4.
 
CoachT

CoachT

New member
Nope. Read post #4 carefully. You are making it way too complicated.
def Exit = (close - low) + (close - open) - (high - close); instead maybe plot exit =
and then a MA of exit. Make sure your math calculations match what you are trying to plot. As in post # 4.
Can you provide me an example? I'll be able to calculate it correctly once i'm able to see its structure. I just can't visible when you say 'Plot a MA of the Exit'. Every time i make another plot the lines get all messed up. Or maybe its my calculations that make the lines look like this ?
 
horserider

horserider

Well-known member
VIP
plot Exit = (close - low) + (close - open) - (high - close);
plot Main_Line = MovingAverage(averagetype, exit, length);
 
CoachT

CoachT

New member
plot Exit = (close - low) + (close - open) - (high - close);
plot Main_Line = MovingAverage(averagetype, exit, length);
Thank you @horserider for attempting to teach me. I was able to the study to chart, amazing what I couple word changes can do. I dont think it's as smoove as I had imagined, but i think it works. Going to be testing it tonight
 
horserider

horserider

Well-known member
VIP
@CoachT You are welcome. No idea if it works or even if the formulas given above are correct. Just trying to help with the code you were doing.
 

Similar threads

Top