<commit_before>const ccxt = require('../../ccxt');

console.log ('CCXT Version:', ccxt.version);

function getMaxLeverage (market, positionSize, price) {
    /**
     * Equation taken from https://www.delta.exchange/contracts/
     * @param market: CCXT market
     * @param positionSize: The value of the position in quote currency
     * @param price: The latest market price
     * @return: The maximum leverage available for the market for the given position size
     */
    const info = market['info'];
    const maxLeverageNotional = Number (info['max_leverage_notional']);
    const initialMarginScalingFactor = Number (info['initial_margin_scaling_factor']);
    let initialMargin = Number (info['initial_margin']);
    const positionSizeLimit = Number (info['position_size_limit']);

    if (positionSize <= maxLeverageNotional) {
        const initialMarginRatio = initialMargin / 100;
        return 1 / initialMarginRatio;
    } else if (positionSize <= (positionSizeLimit * market['contractSize'] * price)) {
        initialMargin = initialMargin + (initialMarginScalingFactor * (positionSize - maxLeverageNotional));
        const initialMarginRatio = initialMargin / 100;
        return 1 / initialMarginRatio;
    } else {
        throw new Error ('positionSize is too high for ' + market['symbol']);
    }
}

function getMaintenanceMarginRate (market, positionSize, price) {
    /**
     * Equation taken from https://www.delta.exchange/contracts/
     * @param market: CCXT market
     * @param positionSize: The value of the position in quote currency
     * @param price: The latest market price
     * @return: The maintenance margin rate as a percentage for the market with the given position size
     */
    const info = market['info'];
    const maxLeverageNotional = Number (info['max_leverage_notional']);
    const maintenanceMarginScalingFactor = Number (info['maintenance_margin_scaling_factor']);
    const maintenanceMargin = Number (info['maintenance_margin']);
    const positionSizeLimit = Number (info['position_size_limit']);

    if (positionSize <= maxLeverageNotional) {
        return maintenanceMargin;
    } else if (positionSize <= (positionSizeLimit * market['contractSize'] * price)) {
        return maintenanceMargin + (maintenanceMarginScalingFactor * (positionSize - maxLeverageNotional));
    } else {
        throw new Error ('positionSize is too high for ' + market['symbol']);
    }
}

async function main () {

    const exchange = new ccxt.delta();
    await exchange.loadMarkets ();
    
    const symbol = 'ADA/USDT:USDT';
    const market = exchange.market (symbol);
    const ticker = exchange.fetchTicker (symbol);

    // Gets the maximum leverage and maintenance margin rate for a position worth 100,000 USDT on the ADA/USDT:USDT market
    const maxLeverage = getMaxLeverage(market, 100000, ticker['close']);
    const maintenanceMarginRate = getMaintenanceMarginRate(market, 100000, ticker['close']);
    console.log(maxLeverage, maintenanceMarginRate);<commit_msg>removed price param from examples/js/delta-maintenance-margin-rate-max-leverage.js<commit_after>const ccxt = require('../../ccxt');

console.log ('CCXT Version:', ccxt.version);

function getMaxLeverage (market, positionSize) {
    /**
     * Equation taken from https://www.delta.exchange/contracts/
     * @param market: CCXT market
     * @param positionSize: The value of the position in quote currency
     * @return: The maximum leverage available for the market for the given position size
     */
    const info = market['info'];
    const maxLeverageNotional = Number (info['max_leverage_notional']);
    const initialMarginScalingFactor = Number (info['initial_margin_scaling_factor']);
    let initialMargin = Number (info['initial_margin']);

    if (positionSize <= maxLeverageNotional) {
        const initialMarginRatio = initialMargin / 100;
        return 1 / initialMarginRatio;
    } else {
        initialMargin = initialMargin + (initialMarginScalingFactor * (positionSize - maxLeverageNotional));
        const initialMarginRatio = initialMargin / 100;
        return 1 / initialMarginRatio;
    }
}

function getMaintenanceMarginRate (market, positionSize) {
    /**
     * Equation taken from https://www.delta.exchange/contracts/
     * @param market: CCXT market
     * @param positionSize: The value of the position in quote currency
     * @return: The maintenance margin rate as a percentage for the market with the given position size
     */
    const info = market['info'];
    const maxLeverageNotional = Number (info['max_leverage_notional']);
    const maintenanceMarginScalingFactor = Number (info['maintenance_margin_scaling_factor']);
    const maintenanceMargin = Number (info['maintenance_margin']);

    if (positionSize <= maxLeverageNotional) {
        return maintenanceMargin;
    } else {
        return maintenanceMargin + (maintenanceMarginScalingFactor * (positionSize - maxLeverageNotional));
    }
}

async function main () {

    const exchange = new ccxt.delta();
    await exchange.loadMarkets ();
    
    const symbol = 'ADA/USDT:USDT';
    const market = exchange.market (symbol);

    // Gets the maximum leverage and maintenance margin rate for a position worth 100,000 USDT on the ADA/USDT:USDT market
    const maxLeverage = getMaxLeverage(market, 100000);
    const maintenanceMarginRate = getMaintenanceMarginRate(market, 100000);
    console.log(maxLeverage, maintenanceMarginRate);
{'fraction_non_alphanumeric': 0.09950702939565456, 'fraction_numerical': 0.009494248676282636, 'mean_word_length': 3.843501326259947, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3831444', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1467, 'n_words': 437}