# 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 `crossunder`functions, 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.bitfrog.io/en/trading-instructions/strategy-trading/strategy-usage-examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
