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

Simple 3-EMA Crossover Trading Strategy - Free Profitable TradingView Strategy Script (+ Source Code)

SIMPLE 3-EMA CROSSOVER TRADING STRATEGY FOR BEGINNERS Risk Disclaimer ❗ The information provided on this post does not constitute investment advice, financial advice, trading advice, or any other sort of advice, and you should not treat any of the posts's content as such. Free Trading View Scripts does not recommend any cryptocurrency should be bought, sold or held by you. Do conduct your own due diligence and consult your financial advisor before making any investment decisions. Introduction In this post, I’m gonna show you a simple yet profitable trading strategy that anyone can use to trade crypto. We’ll only be using one type of indicator and that is the Exponential Moving Average. Later in this post i’ll help you code this strategy using tradingview pinescript so that we can easily backtest historical data and see if this strategy has a chance or not. Before we start, you might want to use my referral link on Binance

How to Add an ATR Trailing Stoploss to Your Strategy - TradingView Script (+ Source Code)

How to Add an ATR Trailing Stoploss to Your Strategy in TradingView Risk Disclaimer ❗ The information provided on this post does not constitute investment advice, financial advice, trading advice, or any other sort of advice, and you should not treat any of the posts's content as such. Free Trading View Scripts does not recommend any cryptocurrency should be bought, sold or held by you. Do conduct your own due diligence and consult your financial advisor before making any investment decisions. Introduction As promised, in this post we are going to add a trailing stoploss strategy to avoid giving back the profits to the market. Out of all the strategies available for trailing stop, I am choosing the ATR . The main reason I chose this is that the ATR changes based on the volatility of the candles. It takes into account the huge movements of the candle to avoid being whipsawed more often . You’ll see what I’m t

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