Implementing Amibroker Flip and Exrem Functions in Python Pandas Structure

Rajandran R
Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for greater than a decade. Designed and revealed 100+ open supply buying and selling methods on varied buying and selling instruments. Strongly consider that market understanding and strong buying and selling frameworks are the important thing to the buying and selling success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

January 18, 2024

2 minutes learn

In AmiBroker Formula Language (AFL), Flip and ExRem are two helpful features usually utilized in buying and selling system improvement. Let’s discover every of them:

Implementing Amibroker Flip and Exrem Functions in Python Pandas Structure
Implementing Amibroker Flip and Exrem Functions in Python Pandas Structure 3

Flip Function

The Flip perform is used to toggle a variable between two states (True and False) primarily based on two given situations. This perform is especially helpful for monitoring the state of a commerce or an indicator between two occasions, similar to entry and exit indicators in a buying and selling technique.

Syntax, Flip(Buy, Sell)

  • Buy and Sell are situations (expressions that return True or False).
  • When Buy is true, Flip returns True.
  • as soon as Flip has flipped to True, it stays True till Sell turns into true.

Example Usage,

Buy = Cross(MA(Close, 5), MA(Close, 20)); // Buy when brief MA crosses above lengthy MA
Sell = Cross(MA(Close, 20), MA(Close, 5)); // Sell when lengthy MA crosses above brief MA
Buycontinue = Flip(Buy, Sell); // Buycontinue is True between Buy and Sell indicators

In this instance, Buycontinue will probably be True from the purpose the place the Buy situation is met till the Sell situation happens.

ExRem Function

The ExRem perform stands for “Exclude Remnant” and is used to get rid of opposing indicators inside a given window. Essentially, it ensures that after a Buy or Sell sign is given, subsequent indicators of the other kind are ignored till an opposing sign is generated.

Syntax, ExRem(Buy, Sell)

  • Buy and Sell are once more situations.
  • ExRem removes residual (additional) Buy indicators that happen after the primary Buy sign and earlier than the primary Sell sign. The similar logic applies to Sell indicators.

Example Usage,

Buy = Cross(MA(Close, 5), MA(Close, 20)); 
Sell = Cross(MA(Close, 20), MA(Close, 5));
Buy = ExRem(Buy, Sell); // Removes additional Buy indicators after the primary Buy and earlier than the primary Sell
Sell = ExRem(Sell, Buy); // Removes additional Sell indicators after the primary Sell and earlier than the primary Buy

Here, ExRem is used to filter out extra Buy indicators that happen after an preliminary Buy and earlier than the subsequent Sell sign, and vice versa for Sell indicators.

each Flip and ExRem are highly effective instruments within the AFL toolbox, notably helpful for sign processing in buying and selling methods to handle entry and exit factors extra successfully. They assist in simplifying the logic and avoiding pointless trades or sign overlaps.

Code Implementation of Flip and Exrem Functions in Pandas Dataframe

The offered code defines two features, exrem and flip, each of that are designed to work with boolean collection in a pandas DataFrame. These features are modeled after the ExRem and Flip features in AmiBroker Formula Language (AFL).

import pandas as pd

def exrem(series1, series2):
    """
    Mimics the ExRem perform.
    After a True sign in series1, it ignores subsequent True indicators in series2 till a brand new True sign in series1.
    """
    in_position = False
    clean_series1 = pd.Series(False, index=series1.index)

    for i in vary(len(series1)):
        if series1.iloc[i] and never in_position:
            clean_series1.iloc[i] = True
            in_position = True
        elif series2.iloc[i] and in_position:
            in_position = False

    return clean_series1

def flip(series1, series2):
    """
    Mimics the Flip perform.
    Toggles a state between True and False primarily based on True indicators in series1 and series2.
    """
    state = False
    flipped_state = pd.Series(False, index=series1.index)

    for i in vary(len(series1)):
        if series1.iloc[i]:
            state = True
        elif series2.iloc[i]:
            state = False
        flipped_state.iloc[i] = state

    return flipped_state

# Example DataFrame
knowledge = {'Buy': [False, True, False,True,False, False, True, True, False, False],
        'Sell': [False, False, False, False, True, True, False, False, True, True]}
df = pd.DataFrame(knowledge)

# Apply the exrem perform
df['ExremData'] = exrem(df['Buy'], df['Sell'])

# Apply the flip perform
df['FlipData'] = flip(df['Buy'], df['Sell'])

print(df)

output

    Buy   Sell  ExremKnowledge  FlipKnowledge
0  False  False      False     False
1   True  False       True      True
2  False  False      False      True
3   True  False      False      True
4  False   True      False     False
5  False   True      False     False
6   True  False       True      True
7   True  False      False      True
8  False   True      False     False
9  False   True      False     False

Source link

#Implementing #Amibroker #Flip #Exrem #Functions #Python #Pandas #Structure