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

//@version=5

// @description General purpose functions and helpers for use in more specific Plurex Signal alerting scripts and libraries
library("PlurexSignalCore")

// @function Build a Plurex market string from a base and quote asset symbol.
// @returns A market string that can be used in Plurex Signal messages.
export plurexMarket(string base, string quote) => base + "-" + quote

// @function Builds Plurex market string from the syminfo
// @returns A market string that can be used in Plurex Signal messages.
export tickerToPlurexMarket() => plurexMarket(syminfo.basecurrency, syminfo.currency)

// @function Builds Plurex Signal Message json to be sent to a Signal webhook
// @param secret The secret for your Signal on plurex
// @param action The action of the message. One of [LONG, SHORT, CLOSE_LONGS, CLOSE_SHORTS, CLOSE_ALL, CLOSE_LAST_LONG, CLOSE_LAST_SHORT, CLOSE_FIRST_LONG, CLOSE_FIRST_SHORT].
// @param marketOverride Optional, defaults to the syminfo for the ticker. Use the `plurexMarket` function to build your own.
// @returns A json string message that can be used in alerts to send messages to Plurex.
export simpleMessage(string secret, string action, string marketOverride = na) =>
    market = na(marketOverride) ? tickerToPlurexMarket() : marketOverride 
    "{\"secret\": \""+secret+"\", \"action\": \""+action+"\", \"market\": \""+market+"\"}"

// @function Builds Plurex Signal Entry Message json to be sent to a Signal webhook with optional parameters for budget and price limits.
// @param secret The secret for your Signal on plurex
// @param isLong The action of the message. true for LONG, false for SHORT.
// @param budgetPercentage Optional, The percentage of budget to use in the entry.
// @param priceLimit Optional, The worst price to accept for the entry.
// @param marketOverride Optional, defaults to the syminfo for the ticker. Use the `plurexMarket` function to build your own.
// @returns A json string message that can be used in alerts to send messages to Plurex.
export entryMessage(string secret, bool isLong, float budgetPercentage = na, float priceLimit = na, string marketOverride = na) =>
    market = na(marketOverride) ? tickerToPlurexMarket() : marketOverride
	action = isLong ? "LONG" : "SHORT"
	baseMessage = "{\"secret\": \""+secret+"\", \"action\": \""+action+"\", \"market\": \""+market+"\""
	if not na(budgetPercentage)
		baseMessage := baseMessage+", \"budgetPercentage\": "+str.tostring(budgetPercentage)
	if not na(priceLimit)
		baseMessage := baseMessage+", \"priceLimit\": "+str.tostring(priceLimit)
	baseMessage+"}"

// @function Builds Plurex Signal LONG Message json to be sent to a Signal webhook with optional parameters for budget and price limits.
// @param secret The secret for your Signal on plurex
// @param budgetPercentage Optional, The percentage of budget to use in the entry.
// @param priceLimit Optional, The worst price to accept for the entry.
// @param marketOverride Optional, defaults to the syminfo for the ticker. Use the `plurexMarket` function to build your own.
// @returns A json string message that can be used in alerts to send messages to Plurex.
export long(string secret, float budgetPercentage = na, float priceLimit = na, string marketOverride = na) =>
    entryMessage(secret = secret, isLong = true, budgetPercentage = budgetPercentage, priceLimit = priceLimit, marketOverride = marketOverride)

// @function Builds Plurex Signal SHORT Message json to be sent to a Signal webhook with optional parameters for budget and price limits.
// @param secret The secret for your Signal on plurex
// @param budgetPercentage Optional, The percentage of budget to use in the entry.
// @param priceLimit Optional, The worst price to accept for the entry.
// @param marketOverride Optional, defaults to the syminfo for the ticker. Use the `plurexMarket` function to build your own.
// @returns A json string message that can be used in alerts to send messages to Plurex.
export short(string secret, float budgetPercentage = na, float priceLimit = na, string marketOverride = na) =>
    entryMessage(secret = secret, isLong = false, budgetPercentage = budgetPercentage, priceLimit = priceLimit, marketOverride = marketOverride)

// @function Builds Plurex Signal CLOSE_ALL Message json to be sent to a Signal webhook.
// @param secret The secret for your Signal on plurex
// @param marketOverride Optional, defaults to the syminfo for the ticker. Use the `plurexMarket` function to build your own.
// @returns A json string message that can be used in alerts to send messages to Plurex.
export closeAll(string secret, string marketOverride = na) =>
    simpleMessage(secret = secret, action="CLOSE_ALL", marketOverride = marketOverride)

// @function Builds Plurex Signal CLOSE_SHORTS Message json to be sent to a Signal webhook.
// @param secret The secret for your Signal on plurex
// @param marketOverride Optional, defaults to the syminfo for the ticker. Use the `plurexMarket` function to build your own.
// @returns A json string message that can be used in alerts to send messages to Plurex.
export closeShorts(string secret, string marketOverride = na) =>
    simpleMessage(secret = secret, action="CLOSE_SHORTS", marketOverride = marketOverride)

// @function Builds Plurex Signal CLOSE_LONGS Message json to be sent to a Signal webhook.
// @param secret The secret for your Signal on plurex
// @param marketOverride Optional, defaults to the syminfo for the ticker. Use the `plurexMarket` function to build your own.
// @returns A json string message that can be used in alerts to send messages to Plurex.
export closeLongs(string secret, string marketOverride = na) =>
    simpleMessage(secret = secret, action="CLOSE_LONGS", marketOverride = marketOverride)

// @function Builds Plurex Signal CLOSE_FIRST_LONG Message json to be sent to a Signal webhook.
// @param secret The secret for your Signal on plurex
// @param marketOverride Optional, defaults to the syminfo for the ticker. Use the `plurexMarket` function to build your own.
// @returns A json string message that can be used in alerts to send messages to Plurex.
export closeFirstLong(string secret, string marketOverride = na) =>
    simpleMessage(secret = secret, action="CLOSE_FIRST_LONG", marketOverride = marketOverride)

// @function Builds Plurex Signal CLOSE_LAST_LONG Message json to be sent to a Signal webhook.
// @param secret The secret for your Signal on plurex
// @param marketOverride Optional, defaults to the syminfo for the ticker. Use the `plurexMarket` function to build your own.
// @returns A json string message that can be used in alerts to send messages to Plurex.
export closeLastLong(string secret, string marketOverride = na) =>
    simpleMessage(secret = secret, action="CLOSE_LAST_LONG", marketOverride = marketOverride)

// @function Builds Plurex Signal CLOSE_FIRST_SHORT Message json to be sent to a Signal webhook.
// @param secret The secret for your Signal on plurex
// @param marketOverride Optional, defaults to the syminfo for the ticker. Use the `plurexMarket` function to build your own.
// @returns A json string message that can be used in alerts to send messages to Plurex.
export closeFirstShort(string secret, string marketOverride = na) =>
    simpleMessage(secret = secret, action="CLOSE_FIRST_SHORT", marketOverride = marketOverride)

// @function Builds Plurex Signal CLOSE_LAST_SHORT Message json to be sent to a Signal webhook.
// @param secret The secret for your Signal on plurex
// @param marketOverride Optional, defaults to the syminfo for the ticker. Use the `plurexMarket` function to build your own.
// @returns A json string message that can be used in alerts to send messages to Plurex.
export closeLastShort(string secret, string marketOverride = na) =>
    simpleMessage(secret = secret, action="CLOSE_LAST_SHORT", marketOverride = marketOverride)