Skip to main content

How To Convert Amibroker's ExRem and Flip Functions to TradingView Pinescript?

 How To Convert Amibroker's ExRem and Flip Functions to TradingView Pinescript?

First, before we can convert ExRem and Flip Functions to TradingView Pinescript, we need first to understand what they do. We are very lucky since Amibroker has good documentation.


ExRem

So what does the ExRem function in Amibroker does? In simple terms, ExRem removes excessive signals. ExRem accepts two parameters: series1 and series2.

ExRem(series1, series2) 

ExRem returns true on the first occurrence of series1 then it returns false until series2 is true even if series1 is true. It sounds confusing but I can explain it better with pictures.

So for example we have a chart with sets of buy and sell signals:



So if we use:

buy = ExRem(buy, sell)
sell = ExRem(sell, buy)

What will happen is ExRem will remove the second occurrences of buy and sell signals. This is how it will look like:





So now that we understand the ExRem we can now convert it to pinescript.

Converting ExRem into TradingView Pinescript

To convert ExRem we will first need to know the following:
  1. index of the second most recent occurrence of true in series1
  2. index of the first most recent occurrence of true in series2
We can use the valuewhen in pinescript to get the indices we need:

series1_index = valuewhen(series1, bar_index, 1)
series2_index = valuewhen(series2, bar_index, 0)

Now that we get the indices, if series1 is true and series1_index is lesser than series2_index then return true otherwise return false. In simple terms, if the current value of series1 is true we want the previous truthy value of series1 to be before the truthy value of series2 so that it's alternating.

If we code it in TradingView pinescript this will be the result:


Then we can then use it like in Amibroker:

buy := ExRem(buy, sell)
sell := ExRem(sell, buy)

Then the excessive buy and sell signals before will look like this:


Now, that we are done with ExRem, we will not jump on to Flip.

Flip

Now flip is more of a switch-like function. Once it's on, it's always on till it is turned off. So basically, it is similar to ExRem in that it has 2 parameters too.

Flip(series1, series2)

Flip returns true after the first occurrence of true in series1 until a true signal is received on series2 which resets the state back to false until the next true is detected in series1.

We are going to use Flip in order for us to have a quick glance if we have a current position during those times or not. We will want to color the background green to indicate the holding time or period between the buy and sell signal.

So if we use:
hasPosition = Flip(buy, sell)

we should be able to produce something like this:



Now that we know what Flip does, we can now convert it into TradinView pinescript.

Converting Flip into TradingView Pinescript

With Flip, we need to first have the following:
  1. Index of the first most recent occurrence of true in series1
  2. Index of the first most recent occurrence of true in series2
Like in ExRem we will also use valuewhen to get what we need:

series1_index = valuewhen(series1, bar_index, 0)
series2_index = valuewhen(series2, bar_index, 0)

Now that we got those indices, we can now use this to create a comparison condition that when series1_index is greater or more recent than series2_index then return true otherwise return false. After we do this, it will now work as a switch.


Then we will use it like this:

hasPosition := Flip(buy, sell)

After that, this will be the final look of our chart:


Conclusion

As you can see, we can now use both ExRem and Flip to create our trading indicator w/ TradingView pinescript. We used ExRem to filter out the unnecessary buy and sell signals. And, we used Flip as a switch to store the state if we currently have a position or not.

Source Code

You can view the full source code of the samples here: source code

A Little Favor From You 😅

Since I give everything for free in this blog, please have time to support the blog by following or subscribing to some of my social media accounts:

👉 Youtube Channel

👉 Blog

Comments

Popular posts from this blog

Source Code - Simple 3-EMA Crossover + ATR Trailing Stoploss - TradingView Strategy

Source Code of Simple 3-EMA Crossover + ATR Trailing Stoploss TradingView Strategy Script Related links Link to the blog post Youtube video Source Code A Little Favor From You 😅 Since I give everything for free in this blog, please have time to support the blog by following or subscribing to some of my social media accounts: 👉 Youtube Channel 👉 Blog

How to Draw Lines in TradingView Pinescript?

 How to draw lines in TradingView pinescript? There are 3 ways that I know of on how to draw lines in TradingView  pinescript. And, these are: hline() plot() line.new() hline() The hline() or Levels are used to plot horizontal lines across the chart. It is commonly used in RSI to indicate the oversold and overbought levels . Let's say we have an oscillator that is very similar to RSI and we want to plot the oversold and overbought levels,  we can do that easily by calling the hline function: hline (price, title, color, linestyle, linewidth, editable) → hline And here's the example of trying to use the function: And here's the result when plotted to chart RSI + Levels (30, 50, 70) plot() The plot() function is often used to draw almost all lines in TradingView pinescript.

Source Code - Simple 3-EMA Crossover TradingView Strategy

Source Code of Simple 3-EMA Crossover TradingView Strategy Script Related links Link to the blog post Link to youtube video version Source Code A Little Favor From You 😅 Since I give everything for free in this blog, please have time to support the blog by following or subscribing to some of my social media accounts: 👉 Youtube Channel 👉 Blog