PlurexSignalIntegration
// 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
library("PlurexSignalIntegration")

LONG = "LONG"
SHORT = "SHORT"
CLOSE_LONGS = "CLOSE_LONGS"
CLOSE_SHORTS = "CLOSE_SHORTS"
CLOSE_ALL = "CLOSE_ALL"

// @function Build a Plurex market 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 simple 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 simple Plurex Signal Messages
// @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].
// @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 = "") =>
    market = marketOverride != "" ? marketOverride : tickerToPlurexMarket()
    "{\"secret\": \""+secret+"\", \"action\": \""+action+"\", \"market\": \""+market+"\"}"
    
// @function Executes strategy actions with Plurex Signal messages
// @param secret The secret for your Signal on plurex
// @param openLong Strategy should open long if true, aggregated with other boolean values
// @param openShort Strategy should open short if true, aggregated with other boolean values
// @param closeLongs Strategy should close longs if true, aggregated with other boolean values
// @param closeShorts Strategy should close shorts if true, aggregated with other boolean values
// @param marketOverride Optional, defaults to the syminfo for the ticker. Use the `plurexMarket` function to build your own.
export executeStrategy(string secret, bool openLong, bool openShort, bool closeLongs, bool closeShorts, string marketOverride = "") =>
    actuallyOpenLong = openLong and not closeLongs and not openShort
    actuallyOpenShort = openShort and not closeShorts and not openLong
    
    if actuallyOpenLong
        strategy.entry("long", strategy.long, alert_message=simpleMessage(secret=secret, action=LONG, marketOverride=marketOverride))
    
    if actuallyOpenShort
        strategy.entry("short", strategy.short, alert_message=simpleMessage(secret=secret, action=SHORT, marketOverride=marketOverride))
    
    if closeLongs and closeShorts
        strategy.close_all(alert_message=simpleMessage(secret=secret, action=CLOSE_ALL, marketOverride=marketOverride))
    else if closeLongs
        strategy.close("long", alert_message=simpleMessage(secret=secret, action=CLOSE_LONGS, marketOverride=marketOverride))
    else if closeShorts
        strategy.close("short", alert_message=simpleMessage(secret=secret, action=CLOSE_SHORTS, marketOverride=marketOverride))



