myAutoviewAlerts
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © incryptowetrust100k
//@version=5

// @description My Alerts Functions - To use with autoview
// @returns - These functions returns a string to use in alerts to send commands to autoview. You can open an order, place a stop or take order, close an opened order or a opened position, or open a hedge position.
library("myAutoviewAlerts")

// @param a     = String  - Account Identification
// @param e     = String  - Exchange
// @param s     = String  - Symbol
// @param b     = String  - Book Side
// @param q     = Float   - Quantity
// @param fp    = Float   - Fixed Price
// @param delay = Integer - In Seconds
// @param i     = Integer - Account Index (to multiple accounts allerts)
// @param base  = String  - Base Currency (bitmex) - "Tether" or "Bitcoin"
// @param fsl   = Float   - Stop Loss Limit Price
// @param c     = String  - Close -> "order" or "position"
// @param ro    = Bool    - Reduce Only
// @param sl    = Bool    - Stop Loss -> bitfinex
// @param t     = String  - Type -> "market" or "limit"

// @function f_order         => Open Orders
// @function f_stop          => Set Stop Loss Order
// @function f_take          => Set Take Order
// @function f_closeOrder    => Close Open Orders
// @function f_closePosition => Close Open Positions
// @function f_hedge         => To Open a Hedge Position (short 100% of balance)

export f_order(string a, string e, string s, string b, float q, float fp, int delay, int i, string base) =>
    _q                  =           switch base
        'TETHER'        =>          q * fp / 100
        'FIAT'          =>          math.round(math.round(q * close) / 100) * 100
        'ASSET'         =>          q
    _u                  =           base == "TETHER" ? " u=currency"   : ""
    _delay              =           i == 0 ? " delay=" + str.tostring(delay) + " " : " "
    _msg                =           " a=" + a + " e=" + e + " s=" + s + _delay + " b=" + b + " q=" + str.tostring(_q) + _u + " fp=" + str.tostring(fp) + " | "
    _msg

export f_start(string a, string e, string s, string b, float q, float fp, float fsl, int delay, int i, string base) =>
    _q                  =           switch base
        'TETHER'        =>          q * fp / 100
        'FIAT'          =>          math.round(math.round(q * close) / 100) * 100
        'ASSET'         =>          q
    _u                  =           base == "TETHER" ? " u=currency"   : ""
    _delay              =           i == 0 ? " delay=" + str.tostring(delay) + " " : " "
    _msg                =           e != "BITFINEX" ? 
     " a=" + a + " e=" + e + " s=" + s + _delay + " b=" + b + " q=" + str.tostring(_q) + _u + " fsl=" + str.tostring(fsl) + " fp=" + str.tostring(fp) + " | "
     :
     " a=" + a + " e=" + e + " s=" + s + _delay + " b=" + b + " q=" + str.tostring(_q) + _u + " sl=1" + " fp=" + str.tostring(fp) + " | "
    _msg

export f_stop(string a, string e, string s, float q, float fp, float fsl, int delay, int i, string base) =>
    _q                  =           switch base
        'TETHER'        =>          q * fp / 100
        'FIAT'          =>          math.round(math.round(q * close) / 100) * 100
        'ASSET'         =>          q
    _u                  =           base == "TETHER" ? " u=currency"   : ""
    _delay              =           i == 0 ? " delay=" + str.tostring(delay) + " " : " "
    _msg                =           e != "BITFINEX" ? 
     " a=" + a + " e=" + e + " s=" + s + _delay + " q=" + str.tostring(_q) + _u + " c=position fsl=" + str.tostring(fsl) + " fp=" + str.tostring(fp) + " ro=1 | "
     :
     " a=" + a + " e=" + e + " s=" + s + _delay + " q=" + str.tostring(_q) + _u + " c=position sl=1" + " fp=" + str.tostring(fsl) + " ro=1 | "
    _msg

export f_take(string a, string e, string s, float q, float fp, int delay, int i, string base) =>
    _q                  =           switch base
        'TETHER'        =>          q * fp / 100
        'FIAT'          =>          math.round(math.round(q * close) / 100) * 100
        'ASSET'         =>          q
    _u                  =           base == "TETHER" ? " u=currency"   : ""
    _delay              =           i == 0 ? " delay=" + str.tostring(delay) + " " : " "
    _msg                =           " a=" + a + " e=" + e + " s=" + s + _delay + " q=" + str.tostring(_q) + _u + " c=position fp=" + str.tostring(fp) + " ro=1 | "
    _msg

export f_closeOrder(string a, string e, string s, int delay, int i) =>
    _delay              =           i == 0 ? " delay=" + str.tostring(delay) + " " : " "
    _msg                =           " a=" + a + " e=" + e + " s=" + s + _delay + " c=order | "
    _msg

export f_closePosition(string a, string e, string s, int delay, int i) =>
    _delay              =           i == 0 ? " delay=" + str.tostring(delay) + " " : " "
    _msg                =           " a=" + a + " e=" + e + " s=" + s + _delay + " c=position t=market ro=1 | "
    _msg

export f_hedge(string a, string e, string s, int delay, int i) =>
    _delay              =           i == 0 ? " delay=" + str.tostring(delay) + " " : " "
    _msg                =           " a=" + a + " e=" + e + " s=" + s + _delay + " b=short q=100% t=limit | "
    _msg
