Skip to content

Strategy Usage Examples

A. Example 1: MACD Script with AlertCondition() + Manual Alert Setup

To create an alert, you need to define the condition that triggers it. This condition can be based on various factors such as price, indicators, or custom logic. The Pine Script example below uses the MACD technical indicator as an illustration.

JavaScript

//@version=5
indicator('MACD Sample Script 1', overlay=true)

// Calculate MACD
[macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)

// Define the golden cross condition
goldenCross = ta.crossover(macdLine, signalLine)

// Define the death cross condition
deathCross = ta.crossunder(macdLine, signalLine)

// Use the alertcondition function to generate alerts
alertcondition(condition=goldenCross, title="MACD Golden Cross", message="")
alertcondition(condition=deathCross, title="MACD Death Cross", message="")

In this example, we first calculate the MACD indicator using the macd function, which takes an input series (in this case, the closing price) and the MACD parameters (12, 26, 9). We then define the conditions for a golden cross and a death cross using the crossover and crossunderfunctions, respectively. A golden cross occurs when the MACD line crosses above the signal line, while a death cross occurs when the MACD line crosses below the signal line.

B. Example 2: MACD Script with Alert() Message Embedded

Instead of using alertcondition(), we use the alert() function to generate an alert directly when the condition is met.

//@version=5 indicator('MACD Sample Script 2', overlay=true)

// Calculate MACD [macdLine, signalLine, _] = ta.macd(close, 12, 26, 9)

// Define the golden cross condition buySignal = ta.crossover(macdLine, signalLine)

// Define the death cross condition sellSignal = ta.crossunder(macdLine, signalLine)

// Function to generate custom JSON payload get警报信息(action, instrument, signalToken, orderType, orderPriceOffset, investmentType, amount) => str = '{' str := str + '"action": "' + action + '", ' str := str + '"instrument": "' + instrument + '", ' str := str + '"signalToken": "' + signalToken + '", ' str := str + '"timestamp": "' + str.tostring(timenow) + '", ' str := str + '"orderType": "' + orderType + '", ' str := str + '"orderPriceOffset": "' + str.tostring(orderPriceOffset) + '", ' str := str + '"investmentType": "' + investmentType + '", ' str := str + '"amount": "' + str.tostring(amount) + '"' str := str + '}' str

var ALERTGRP_CRED = "BitFrog Alert - Credential" signalToken = input("", "Signal Token", inline = "11", group = ALERTGRP_CRED)

var ALERTGRP_ENTER = "BitFrog Alert - ENTER Signal"

enterOrderType = input.string("market", "Order Type", options = ["market", "limit"], inline = "21", group = ALERTGRP_ENTER) enterOrderPriceOffset = input.float(0, "Order Price Offset", minval = 0, maxval = 100, step = 0.01, inline = "21", group = ALERTGRP_ENTER)

enterInvestmentType = input.string("percentage_balance", "Investment Type", options = ["margin", "contract", "percentage_balance", "percentage_investment"], inline = "31", group = ALERTGRP_ENTER) enterAmount = input.float(100, "Amount", minval = 0.01, inline = "31", group = ALERTGRP_ENTER)

var ALERTGRP_EXIT = "BitFrog Alert - EXIT Signal"

exitOrderType = input.string("market", "Order Type", options = ["market", "limit"], inline = "41", group = ALERTGRP_EXIT) exitOrderPriceOffset = input.float(0, "Order Price Offset", minval = 0, maxval = 100, step = 0.01, inline = "41", group = ALERTGRP_EXIT)

exitInvestmentType = input.string("percentage_position", "Investment Type", options = ["percentage_position"], inline = "51", group = ALERTGRP_EXIT) exitAmount = input.float(100, "Amount", minval = 0.01, maxval = 100, step = 0.01, inline = "51", group = ALERTGRP_EXIT)

if buySignal buy警报信息 = get警报信息(action = 'open_long', instrument = syminfo.ticker, signalToken = signalToken, orderType = enterOrderType, orderPriceOffset = enterOrderPriceOffset, investmentType = enterInvestmentType, amount = enterAmount) alert(buy警报信息, freq = alert.freq_once_per_bar)

if sellSignal sell警报信息 = get警报信息(action='close'/action='open_short', instrument = syminfo.ticker, signalToken = signalToken, orderType = exitOrderType, orderPriceOffset = exitOrderPriceOffset, investmentType = exitInvestmentType, amount = exitAmount) alert(sell警报信息, freq = alert.freq_once_per_bar)

The getAlertMessage() function is used to generate a custom JSON payload that sends alerts in the format specified by the Bitfrog message specifications. The inputs include signalToken, enterOrderType, enterOrderPriceOffset, enterInvestmentType, enterAmount, exitOrderType, exitOrderPriceOffset, exitInvestmentType, and exitAmount. These inputs are used to specify the order type, price offset, investment type, and amount for the alert.

The code then uses buySignal and sellSignal conditions to check for buy and sell signals. If a buy signal occurs, it generates a buy alert message by calling the getAlertMessage() function with the appropriate parameters. The alert message includes information such as the action ('open_long'), trading instrument (current symbol), signal token, and the specified input variables. Similarly, if a sell signal occurs, it generates a sell alert message using the getAlertMessage()function and sends the message using the alert() function.

As a user, if you want to switch from MACD golden cross/death cross to another logic, all you need to do is replace the definitions of buySignal and sellSignal. Everything else can be reused directly. To put it more simply, if you want to change the basis for determining buy and sell signals, you only need to modify the definitions of the buy and sell signals – nothing else needs to be changed.

BitFrog User Documentation