此网页仅供信息参考之用。部分服务和功能可能在您所在的司法辖区不可用

OKX信号策略:如何使用TradingView警报创建您的信号策略 - 附录

发布于 2023年8月21日更新于 2024年6月27日阅读时长 2 分钟

附录

A. 案例1:带有AlertCondition() + Alert手动设置的MACD脚本

要创建一个警报,需要定义触发它的条件。这个条件可以基于各种因素,如价格、指标或自定义逻辑。以下派语言使用MACD技术指标作为示例。

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="")

在这个例子中,我们首先使用macd函数计算MACD指标,该函数接受输入序列(在本例中为收盘价)和MACD的参数(12、26、9)。然后我们分别使用crossover和crossunder函数定义了金叉和死叉的条件。当MACD线穿过信号线时,发生金叉;而当MACD线穿过信号线下方时,发生死叉。

B. 案例2:带有Alert()消息嵌入的MACD脚本

不使用alertcondition,我们使用alert()函数在满足条件时直接生成警报。

JavaScript
//@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 = "欧易 Alert - Credential"
signalToken = input("", "Signal Token", inline = "11", group = ALERTGRP_CRED)

var ALERTGRP_ENTER = "欧易 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 = "欧易 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 = 'ENTER_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 = 'EXIT_LONG', instrument = syminfo.ticker, signalToken = signalToken, orderType = exitOrderType, orderPriceOffset = exitOrderPriceOffset, investmentType = exitInvestmentType, amount = exitAmount)
    alert(sell警报信息, freq = alert.freq_once_per_bar)

get警报信息函数是为了生成一个定制的JSON负载,来按照欧易消息规格的格式发送警报。输入包括signalToken、enterOrderType、enterOrderPriceOffset、enterInvestmentType、enterAmount、exitOrderType、exitOrderPriceOffset、exitInvestmentType和exitAmount。这些输入用于指定警报的订单类型、价格偏移量、投资类型和金额。

然后,代码使用buySignal和sellSignal条件来检查买入和卖出信号。如果发生买入信号,它通过调用带有适当参数的get警报信息函数生成买入警报消息。警报消息包括行动('ENTER_LONG')、交易工具(当前的股票符号)、信号令牌和指定的输入变量的信息。同样,如果发生卖出信号,它会使用get警报信息函数生成卖出警报消息,并使用警报函数发送消息。

作为用户,如果你想从MACD金叉/死叉切换到另一种逻辑,你需要做的就是更换buySignal和sellSignal的定义,其他所有的东西都可以直接复用。说得更直白一些,如果你想改变买卖信号的判断依据,只需要修改买卖信号的定义就行了,其他的都不用改。