WebhookJsonMsg
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © GlassCat

//@version=5

// @description This webhook json message library provides convenient functions for building JSON messages
// Used to manage automatic transaction orders and positions
library("WebhookJsonMsg", overlay = true)

// @type Define some constant values
// @field <string> OPEN_LONG open_long. Used in WebhookMessage.orderSide.
// @field <string> OPEN_SHORT open_short. Used in WebhookMessage.orderSide.
// @field <string> CLOSE_LONG close_long. Used in WebhookMessage.orderSide.
// @field <string> CLOSE_SHORT close_short. Used in WebhookMessage.orderSide.
// @field <string> LIMIT limit. Used in WebhookMessage.orderType.
// @field <string> MARKET market. Used in WebhookMessage.orderType.
// @field <string> U_MARGIN U-Margin. Used in WebhookMessage.symbolType.
// @field <string> C_MARGIN C-Margin. Used in WebhookMessage.symbolType.
// @field <string> SPOT SPOT. Used in WebhookMessage.symbolType.
export type Dict
	OPEN_LONG       = "open_long"
	OPEN_SHORT      = "open_short"
	CLOSE_LONG      = "close_long"
	CLOSE_SHORT    	= "close_short"
	LIMIT 			= "limit"
	MARKET			= "market"
	U_MARGIN 		= "U-Margin"
	C_MARGIN 		= "C-Margin"
	SPOT 			= "Spot"

DictObj = Dict.new()


// @type Webhook message structure.
// @field <string> strategyId - your strategy id.
// @field <string> signalNo - Signal No, used to track current signals.
// @field <string> relatedSignalNo - Related Signal No, used to track previous signals, you can close specified order using this field.
// @field <string> symbol - symbol.
// @field <string> symbolType - symbol type.
// @field <string> orderSide - order side.
// @field <string> price - price for limit order type.
// @field <string> orderType - order type.
// @field <string> takeProfitPrice - take profit price.
// @field <string> stopLossPrice - stop loss price.
// @field <string> timestamp - Unix timestamp (millisecond).
// @field <string> accessKey - your strategy access key.
// @field <string> positionRatio - control position ratio.
export type WebhookMessage
	string strategyId
	string signalNo
	string relatedSignalNo
	string symbol
	string symbolType
	string orderSide
	string price
	string orderType
	string takeProfitPrice
	string stopLossPrice
	string timestamp
	string accessKey
	string positionRatio


// @function Builds the final JSON payload from a WebhookMessage type.
// @param WebhookMessage msg A prepared WebhookMessage.
// @returns <string> A JSON Payload.
export method buildWebhookJson(WebhookMessage msg) =>
	json = "{"
	json += '"library": "GlassCat/WebhookMessage"'
	json += ',"strategyId": "' + msg.strategyId + '"'

	if (msg.signalNo != "")
		json += ',"signalNo": "' + msg.signalNo + '"'

	if (msg.relatedSignalNo != "")
		json += ',"relatedSignalNo": "' + msg.relatedSignalNo + '"'

	json += ',"symbol": "' + (msg.symbol == "" ? syminfo.basecurrency + syminfo.currency : msg.symbol) + '"'
	json += ',"symbolType": "' + (msg.symbolType == "" ?  DictObj.U_MARGIN : msg.symbolType) + '"'
	json += ',"orderSide": "' + msg.orderSide + '"'
	json += ',"accessKey": "' + msg.accessKey + '"'

	if (msg.orderType != "")
		json += ',"orderType": "' + msg.orderType + '"'
		if (msg.orderType ==  DictObj.LIMIT)
			json += ',"price": "' + msg.price + '"'

	if (msg.timestamp != "")
		json += ',"timestamp": "' + msg.timestamp + '"'
	else
		json += ',"timestamp": "' + str.tostring(timenow) + '"'

	if (msg.takeProfitPrice != "")
		json += ',"takeProfitPrice": "' + msg.takeProfitPrice + '"'

	if (msg.stopLossPrice != "")
		json += ',"stopLossPrice": "' + msg.stopLossPrice + '"'

	if (msg.positionRatio != "")
		json += ',"positionRatio": "' + msg.positionRatio + '"'

	json += "}"
	json



//plot nothing to pass publishing rules
plot(na)