FinandyHookLib
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Hamster-Coder

//@version=5

// @description TODO: add library description here
library("FinandyHookLib", true)

import Hamster-Coder/OrderLib/41 as orderLib
import Hamster-Coder/TextLib/6 as textLib

createOpenElementJson(orderLib.tradePositionModel model, textLib.textFormatOptions options) =>
    string result = ""
    result := textLib.concatLine(result, "  \"open\": {")
    result := textLib.concatLine(result, "    \"amountType\": \"sumUsd\",")
    result := textLib.concatLine(result, str.format("    \"amount\": \"{0}\",", str.tostring(model.enter.amount.value, options.currency_format)))
    result := textLib.concatLine(result, str.format("    \"leverage\": \"{0}\",", model.leverage))
    result := textLib.concatLine(result, str.format("    \"price\": \"{0}\"", str.tostring(model.enter.price.value, options.currency_format)))
    result := textLib.concatLine(result, "  }")
    result

createStopLossJson(orderLib.tradePositionModel model, textLib.textFormatOptions options) =>
    string result = ""
    if (na(model.stop_loss) == false)
        result := textLib.concatLine(result, "  \"sl\": {")
        result := textLib.concatLine(result, str.format("    \"price\": \"{0}\"", str.tostring(model.stop_loss.price.value, options.currency_format)))
        result := textLib.concatLine(result, "  }")
    result

createSlxJson(orderLib.tradePositionModel model, textLib.textFormatOptions options) =>
    string result = ""  
    if (na(model.breakeven) == false or na(model.trailing_stop) == false)      
        result := textLib.concatLine(result, "  \"slx\": {")
        result := textLib.concatLine(result, "    \"mode\": \"trailing\",")
        result := textLib.concatLine(result, str.format("    \"trailingBreakevenProfit\": \"{0}\",", na(model.breakeven) ? "" : str.tostring(model.breakeven.target.price.offset, options.percent_format)))
        result := textLib.concatLine(result, str.format("    \"trailingBreakeven\": \"{0}\",", na(model.breakeven) ? "" : str.tostring(model.breakeven.trigger.offset, options.percent_format)))
        result := textLib.concatLine(result, str.format("    \"trailingProfit\": \"{0}\",", na(model.trailing_stop) ? "" : str.tostring(model.trailing_stop.trigger.offset, options.percent_format)))
        result := textLib.concatLine(result, str.format("    \"trailingLag\": \"{0}\",", na(model.trailing_stop) ? "" : str.tostring(model.trailing_stop.lag_offset, options.percent_format)))
        result := textLib.concatLine(result, "    \"trailingMode\": \"lp\"")
        result := textLib.concatLine(result, "  }")
    result

createTakeProfitElementJson(orderLib.orderPointModel tp, textLib.textFormatOptions options) =>
    string result = ""
    result := textLib.concatLine(result, "      {")
    result := textLib.concatLine(result, str.format("        \"price\": \"{0}\",", str.tostring(tp.price.value, options.currency_format)))
    result := textLib.concatLine(result, str.format("        \"piece\": \"{0}\"", tp.amount.position_piece_prc))
    result := textLib.concatLine(result, "      }")
    result

createTakeProfitElementJson(orderLib.tradePositionModel model, textLib.textFormatOptions options) =>
    string result = ""
    if (na(model.take_profit_collection) == false)
        if (model.take_profit_collection.size() > 0)
            result := textLib.concatLine(result, "  \"tp\": {")
            result := textLib.concatLine(result, "    \"orders\": [")
            for [index, tp] in model.take_profit_collection            
                if (index > 0) 
                    result := textLib.concatLine(result, ",")
                result := textLib.concatLine(result, createTakeProfitElementJson(tp, options = options))
            result := textLib.concatLine(result, "    ],")
            result := textLib.concatLine(result, str.format("  \"qty\": \"{0}\"", model.take_profit_collection.size()))            
            result := textLib.concatLine(result, "  }")        
    result

addPrefixComma(string value) =>
    string result = value
    if (value != "")
        result := str.format(",{0}", value)
    result

export createOrderJson(orderLib.tradePositionModel model, string hook_secret, textLib.textFormatOptions options) =>
    string result = ""
    result := textLib.concatLine(result, "{")    
    result := textLib.concatLine(result, str.format("  \"secret\": \"{0}\",", hook_secret))
    result := textLib.concatLine(result, str.format("  \"side\": \"{0}\",", model.side == 1 ? "buy" : "sell"))
    result := textLib.concatLine(result, str.format("  \"symbol\": \"{0}\",", model.ticker))    
    // add open element    
    result := textLib.concatLine(result, createOpenElementJson(model, options = options))
    // add stop loss element    
    result := textLib.concatLine(result, addPrefixComma(createStopLossJson(model, options = options)))
    // add slx element
    result := textLib.concatLine(result, addPrefixComma(createSlxJson(model, options = options)))
    // add take profit element
    result := textLib.concatLine(result, addPrefixComma(createTakeProfitElementJson(model, options = options)))
    result := textLib.concatLine(result, "}")
    result
