The resource loading... loading...

TradingViewWebHook signals execution strategies (including tutorials)

Author: Inventors quantify - small dreams, Date: 2020-04-28 17:01:35
Tags: StudyTrade-aided

Related articles:https://www.fmz.com/bbs-topic/5533The video link to the B station:https://www.bilibili.com/video/BV1Wk4y1k7zz/

  • Updated on the 29th of July 2020. Adding SPK and BPK two directives, respectively: sell open positions after selling a flat position and buy open positions after buying a flat position.

/*
- 交互命令字符串格式
  action:amount
  action: buy , sell , long , short , cover_long , cover_short, spk , bpk
- 交易所类型
  eType变量取值: 0 spot , 1 futures

- TV文档链接
  https://www.tradingview.com/pine-script-docs/en/v4/Quickstart_guide.html
  https://cn.tradingview.com/chart/8xfTuX7F/

- TV webhook 发送请求
  https://www.fmz.com/api/v1?access_key=xxx&secret_key=yyyy&method=CommandRobot&args=[186515,"action:amount"]

- 引用类库
  引用数字货币交易类库
*/

// 参数
// var IsMarketOrder = false 
// var QuotePrecision = 2
// var BasePrecision = 2

// 期货参数
// var Ct = ""


// 全局变量
var BUY = "buy"
var SELL = "sell"
var LONG = "long"
var SHORT = "short"
var COVER_LONG = "cover_long"
var COVER_SHORT = "cover_short"
var SPK = "spk"
var BPK = "bpk"


function main() {
    // 清空日志,如不需要,可以删除
    LogReset(1)

	// 设置精度
    exchange.SetPrecision(QuotePrecision, BasePrecision)

    // 识别期货还是现货
    var eType = 0
    var eName = exchange.GetName()
    var patt = /Futures_/
    if (patt.test(eName)) {
        Log("添加的交易所为期货交易所:", eName, "#FF0000")
        eType = 1
        if (Ct == "") {
            throw "Ct 合约设置为空"
        } else {
        	Log(exchange.SetContractType(Ct), "设置合约:", Ct, "#FF0000")
        }
    } else {
    	Log("添加的交易所为现货交易所:", eName, "#32CD32")
    }
    
    var lastMsg = ""
    var acc = _C(exchange.GetAccount)
    while(true) {
        var cmd = GetCommand()
        if (cmd) {
            // 检测交互命令
            lastMsg = "命令:" + cmd + "时间:" + _D()
            var arr = cmd.split(":")
            if (arr.length != 2) {
                Log("cmd信息有误:", cmd, "#FF0000")
                continue
            }

            var action = arr[0]
            var amount = parseFloat(arr[1])

            if (eType == 0) {
                if (action == BUY) {               
                    var buyInfo = IsMarketOrder ? exchange.Buy(-1, amount) : $.Buy(amount)
                    Log("buyInfo:", buyInfo)
                } else if (action == SELL) {        
                    var sellInfo = IsMarketOrder ? exchange.Sell(-1, amount) : $.Sell(amount)
                    Log("sellInfo:", sellInfo)
                } else {
                	Log("现货交易所不支持!", "#FF0000")
                }
            } else if (eType == 1) {
            	var tradeInfo = null
            	var ticker = _C(exchange.GetTicker)
                if (action == LONG) {
                	exchange.SetDirection("buy")
                    tradeInfo = IsMarketOrder ? exchange.Buy(-1, amount) : exchange.Buy(ticker.Sell, amount)
                } else if (action == SHORT) {        
                    exchange.SetDirection("sell")
                    tradeInfo = IsMarketOrder ? exchange.Sell(-1, amount) : exchange.Sell(ticker.Buy, amount)
                } else if (action == COVER_LONG) {        
                    exchange.SetDirection("closebuy")
                    tradeInfo = IsMarketOrder ? exchange.Sell(-1, amount) : exchange.Sell(ticker.Buy, amount)
                } else if (action == COVER_SHORT) {        
                	exchange.SetDirection("closesell")
                	tradeInfo = IsMarketOrder ? exchange.Buy(-1, amount) : exchange.Buy(ticker.Sell, amount)
                } else if (action == SPK) {   // 卖出平多仓,卖出开空仓
                    exchange.SetDirection("closebuy")
                    var tradeInfo1 = IsMarketOrder ? exchange.Sell(-1, amount) : exchange.Sell(ticker.Buy, amount)
                    exchange.SetDirection("sell")
                    var tradeInfo2 = IsMarketOrder ? exchange.Sell(-1, amount) : exchange.Sell(ticker.Buy, amount)
                    tradeInfo = [tradeInfo1, tradeInfo2]
                } else if (action == BPK) {   // 买入平空仓,买入开多仓
                    exchange.SetDirection("closesell")
                    var tradeInfo1 = IsMarketOrder ? exchange.Buy(-1, amount) : exchange.Buy(ticker.Sell, amount)
                    exchange.SetDirection("buy")
                    var tradeInfo2 = IsMarketOrder ? exchange.Buy(-1, amount) : exchange.Buy(ticker.Sell, amount)
                    tradeInfo = [tradeInfo1, tradeInfo2]
                } else {
                	Log("期货交易所不支持!", "#FF0000")
                }
                if (tradeInfo) {
                    Log("tradeInfo:", tradeInfo)
                }
            } else {
            	throw "eType error, eType:" + eType
            }
            acc = _C(exchange.GetAccount)
        }
        var tbl = {
        	type : "table", 
        	title : "状态信息", 
        	cols : ["数据"], 
        	rows : []
        }
        tbl.rows.push([JSON.stringify(acc)])
        LogStatus(_D(), eName, "上次接收到的命令:", lastMsg, "\n", "`" + JSON.stringify(tbl) + "`")
    	Sleep(1000)
    }
}



Related

More

superonI've been looking at it all night and I don't understand it, can you translate it?

zz791377920Which exchanges does this support?

zz791377920The number of spot transactions filled in 10, when the order is placed becomes the amount of 10u, where is the setup problem?

What is it?ReferenceError: 'QuotePrecision' is not defined at main (__FILE__:13)

kukerzzzAsk if you can only set the quantity in the contract transaction, how to write if every time the stock is full.

15937556103Teacher, can you send me a complete strategy for the contract?

15937556103/upload/asset/203b175e3c2c22ac9681c.png This is a list of all the different ways Upload/asset/203b175e3c2c22ac9681c.png is credited in the database. Why is it always like this?

tobey2022Hi, can you put multiple transactions on a single bot right? then the URL of the alert adds the transaction pair name, a bot runs multiple transaction pairs alert

le7marslong, short, cover_long, cover_SHORT, is it to add webhook separately?

sbwffg198212What does this mean, futures exchanges don't support, I want to do bitcoin perpetual contracts BTC_USDT how to set up

sbwffg198212What does this mean, futures exchanges don't support, I want to do bitcoin perpetual contracts BTC_USDT how to set up

sbwffg198212 /upload/asset/1cd7a606f646b8c0d32be.png

Reverse the Q.Brother, can you publish an instructional video on how to add and set up futures contracts, preferably in more detail?

sug210/upload/asset/125e8fed0f51ab1f8122e.jpg Error message with spk command

The night skyHow to solve the problem of TV sending alerts and FMZ occasionally not receiving the signal? In particular, two policies of TV pointing at the same time to send signals to two robots to FMZ, occasionally one robot is not receiving. And two TV accounts have been registered to send, but the problem still occurs, although the servers hosted by FMZ are the same.

The Peninsula is not coolspk, bpk, do you want to add numbers after spk? If the TV signal has multiple alerts, the total number of alerts can be opened at once, just like the blank list.

melo23Contract multiplication Where to choose

Inventors quantify - small dreamsLook at the policy parameters, if you add a futures exchange object, set up a contract. Otherwise the robot doesn't know which contract to operate.

Richie Hello, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I'm sorry, I was just wondering what you were going to say, but I'm not sure what you were going to say.

Inventors quantify - small dreamsAll of this is possible, but the parameters, webhook on TV settings may need to be adjusted depending on the type of TV you are doing.

Inventors quantify - small dreamsThe purchase of the spot market price order, the next order amount is the amount.

Inventors quantify - small dreamsI guess you got it wrong, check the API documentation.

What is it?If you change the key again and it doesn't work, do you have to copy the entire policy to tune it?

Inventors quantify - small dreamsCode 1 is the wrong API KEY, check.

What is it?Now I'm testing within the platform to order, but with the extension API, I get an error message, return, code:1, data:[], I'm directly accessed with a link, similar to a browser opening a link.

Inventors quantify - small dreamsDon't just copy the code, copy the whole strategy.

Inventors quantify - small dreamsThe TV signals, how much is written in it, how much is the strategy. If you want to hip hop, you need to change the strategy.

Inventors quantify - small dreamsThis tactic itself is a perfect example of how to run a futures contract if you use the wrong command, check.

Inventors quantify - small dreamsYou can run the futures, the command you used in the webhook callback is wrong, use the futures command, read the code carefully.

tobey2022 好的,已经在众包板块发布了信息,期望你的成果和合作,thanks.

Inventors quantify - small dreamsThis has not been changed, so you can crowdsource the information and some developers will pick up the order.

tobey2022Can I send you the changes directly? because others may be familiar with them for a while, but I'm not sure if they'll be able to do it.

Inventors quantify - small dreamsThe platform has a crowdfunding platform where you can post your needs.

tobey2022@ChiloChiloDream, can you modify a version of this strategy, if it is possible, of course you can pay to buy it, and expect your results and feedback, thank you.

Inventors quantify - small dreamsI can, but I need to change this policy.

Inventors quantify - small dreamsI don't quite understand what you mean.

le7marsI set up four alerts in long, short, coverlong and covershort, but since it's a strategy, not a study, the four commands are always triggered simultaneously...

le7marsHow do you distinguish between long and short?

Inventors quantify - small dreamsYou can tell the difference in the URL of the alert webhook.

le7marsThe problem was found, only trading alerts from tradingview indicators could be received, but the strategy I wrote, which set up 4 different alerts, but seemed to be unrecognizable, when the signal was generated, all four signals opened simultaneously.

Inventors quantify - small dreamsSend a request to alert on TV What's up? This is a list of all the different ways Access_key=xxx&secret_key=yyyy&method=CommandRobot&args=[186515, "action:amount"] is credited in the database. What's up? In the action section, you write what instructions the robot receives. What's up? This is a list of all the different ways Access_key=xxx&secret_key=yyyy&method=CommandRobot&args=[186515",long:1"] is credited in the database. What's up?

Inventors quantify - small dreamsFirst, check out the FMZ introductory tutorial to operate a futures exchange account, configure the futures exchange; then add the futures exchange object to the robot.

sbwffg198212 /upload/asset/1ccc5603d54593dcba809.png

sbwffg198212I just changed these places, God help me to see where it's wrong, I won't add futures exchange objects, set contracts on the parameters, permanent is swap, the currency base is XXX_USD, U base is XXX_USDT.

Inventors quantify - small dreamsAdd a futures exchange object, set a contract on the parameter, permanent is a swap, the currency base is XXX_USD, the U base is XXX_USDT.

Inventors quantify - small dreamsPlease check if you can call SPK without holding stock.

Inventors quantify - small dreamsThe TV direct link method can be found in the FMZ platform library, https://www.fmz.com/digest-topic/5969.

Inventors quantify - small dreamsThe leverage level can be set on the exchange. Or set the leverage level before placing an order in the strategy code.

Inventors quantify - small dreamsRunning a contract exchange, setting up a contract, or not knowing which contract to operate.

Inventors quantify - small dreamsWhat's up? This is a list of all the different ways Access_key=xxx&secret_key=yyyy&method=CommandRobot&args=[186515, "action:amount"] is credited in the database. What's up? action is written as spk, bpk

The night skyI want to know how to set up SPK and BPK on the TV side.