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

// AUTO-TRANSPILE //

// Note: examples or implementations are subject to possible change in future. Please subscribe to CCXT-announcements telegram/discord channel to be informed of related & important updates.

async function example () {
    const exchange = new ccxt['binance']({'apiKey': 'xxx', 'secret': 'xxx'});
    // set target symbol
    const symbol = 'BUSD/USDT';
    // which asset you want to use for margin-borrow collateral
    const collateral_coin = 'USDT'; 
    // which coin to borrow
    const borrow_coin = 'BUSD'; 
    // how many coins to sell
    const amount_to_trade = 20;
    // at what limit-price you want to sell (set undefined/null/None if market-order)
    const limit_price = 0.98;
    // what is the target margin. This might be obtainable from exchange automatically, using available endpoints, but for example purposes, we set here manually
    const margin_magnitude = 10; 
    // for example purposes, let's also check available balance at first
    const balance_margin = await exchange.fetchBalance ({'defaultType': 'margin', 'marginMode': 'isolated'}); // 'isolated' or 'cross'.
    // if we don't have enought coins, then we have to borrow at first
    if (balance_margin[symbol][borrow_coin]['free'] < amount_to_trade) {
        console.log ('hmm, I dont have enough margin balance, I should borrow at first.');
        // To initate a borrow, at first, check if we have enough collateral
        const needed_collateral_amount = amount_to_trade/ (margin_magnitude - 1); // as we sell-short, we need '-1' to keep for collateral currency
        // if we don't have any collateral, then at first, we need to transfer it from spot
        if (balance_margin[symbol][collateral_coin]['free'] < needed_collateral_amount) {
            console.log ('hmm, at first I should transfer some collateral from spot');
            // let's check if we have spot balance at all
            const balance_spot = await exchange.fetchBalance ({'type': 'spot'});
            if (balance_spot[collateral_coin]['free'] < needed_collateral_amount) {
                console.log ('hmm, I neither do have enough balance on spot');
                return;
            }  else {
                console.log ('Transferring some ' + collateral_coin + ' to margin account');
                await exchange.transfer (collateral_coin, needed_collateral_amount, 'spot', 'isolated', {'symbol': symbol});
                console.log ('Transfer complete.');
            }
        }
        // now, as we have enough margin collateral, initiate borrow
        console.log ('Initiating margin borrow');
        const borrowResult = await exchange.borrowMargin ('BUSD', amount_to_trade, 'BUSD/USDT', {'marginMode': 'isolated'});
        console.log ('Borrow complete.');
    }
    console.log ("Submitting order.");
    const order = await exchange.createOrder (symbol, 'market', 'sell', amount_to_trade, limit_price, {'type': 'margin', 'marginMode': 'isolated'});
    console.log ("Order was submitted !");
}

example();<commit_msg>examples - margin buy/sell update<commit_after>'use strict';
const ccxt = require ('../../ccxt.js');

// AUTO-TRANSPILE //

async function example () {
    // ########## user inputs ##########
    const exchange = new ccxt['binance']({'apiKey': 'xxx', 'secret': 'xxx'});
    // set target symbol
    const symbol = 'BUSD/USDT';
    // margin mode (cross or isolated)
    const marginMode = 'isolated';
    // which asset you want to use for margin-borrow collateral
    const collateral_coin = 'USDT'; 
    // which coin to borrow
    const borrow_coin = 'BUSD'; 
    // which side to trade
    const order_side = 'sell';
    // how many coins to sell
    const amount_to_trade = 20;
    // order type (can be market, limit or etc)
    const order_type = 'limit';
    // at what limit-price you want to sell (set undefined/null/None if market-order)
    const limit_price = 0.99;
    // what is the target margin. This might be obtainable from exchange automatically, using available endpoints, but for example purposes, we set here manually
    const margin_magnitude = 10; 
    // ########## end of user-inputs ##########

    // for example purposes, let's also check available balance at first
    const balance_margin = await exchange.fetchBalance ({'defaultType': 'margin', 'marginMode': marginMode}); // use `defaultType` because of temporary bug, otherwise, after several days, you can use `type` too.
    // if we don't have enought coins, then we have to borrow at first
    let needed_amount_to_borrow = undefined;  // will be auto-set below
    if ( amount_to_trade - balance_margin[symbol][borrow_coin]['free']) {
        needed_amount_to_borrow = amount_to_trade - balance_margin[symbol][borrow_coin]['free'];
        console.log ('hmm, I have only ' + balance_margin[symbol][borrow_coin]['free'] + ' ' + borrow_coin + ' in margin balance, and still need additional ' + needed_amount_to_borrow + ' to make an order. Lets borrow it.');
        // To initate a borrow, at first, check if we have enough collateral (for this example, as we make a sell-short, we need '-1' to keep for collateral currency)
        const needed_collateral_amount = needed_amount_to_borrow / (margin_magnitude - 1);
        // Check if we have any collateral to get permission for borrow, 
        if (balance_margin[symbol][collateral_coin]['free'] < needed_collateral_amount) {
            // If we don't have enough collateral, then let's try to transfer collateral-asset from spot-balance to margin-balance
            console.log ('hmm, I have only ' + balance_margin[symbol][collateral_coin]['free'] + ' in balance, but ' + needed_collateral_amount + ' collateral is needed. I should transfer ' + needed_collateral_amount + ' from spot');
            // let's check if we have spot balance at all
            const balance_spot = await exchange.fetchBalance ({'type': 'spot'});
            if (balance_spot[collateral_coin]['free'] < needed_collateral_amount) {
                console.log ('hmm, I neither do have enough balance on spot - only ' + balance_spot[collateral_coin]['free'] +'. Script can not continue...');
                return;
            }  else {
                console.log ('Transferring  ' + needed_collateral_amount + ' to margin account');
                await exchange.transfer (collateral_coin, needed_collateral_amount, 'spot', marginMode, {'symbol': symbol}); // because of temporary bug, you have to round "needed_collateral_amount" manually to 8 decimals. will be fixed a few days later
            }
        }
        // now, as we have enough margin collateral, initiate borrow
        console.log ('Initiating margin borrow of ' + needed_amount_to_borrow + ' ' + borrow_coin);
        const borrowResult = await exchange.borrowMargin (borrow_coin, needed_amount_to_borrow, symbol, {'marginMode': marginMode});
    }
    console.log ('Submitting order.');
    const order = await exchange.createOrder (symbol, order_type, order_side, amount_to_trade, limit_price, {'marginMode': marginMode});
    console.log ('Order was submitted !');


    // ...
    // ...
    // some time later, if you want to repay the loan back ('close the position')...
    // ...
    // ...

    // set the "repay-back" amount (for this example snippet, this will be same amount that we borrowed above)
    if (needed_amount_to_borrow !== undefined) {
        const amount_to_repay_back = needed_amount_to_borrow; 
        // At first, you need to get back the borrowed coin, by making an opposide trade
        console.log ('Making purchase back of ' + amount_to_repay_back + ' ' + borrow_coin + ' to repay it back.');
        const purchase_back_price = 1.01;
        const order_2 = await exchange.createOrder (symbol, order_type, (order_side === 'buy' ? 'sell' : 'buy'), amount_to_repay_back, purchase_back_price, {'marginMode': marginMode});
        console.log ('Now, repaying the loan.');
        const repayResult = await exchange.repayMargin (borrow_coin, amount_to_repay_back, symbol, {'marginMode': marginMode});
    }
}

example();
{'fraction_non_alphanumeric': 0.11446078431372549, 'fraction_numerical': 0.002818627450980392, 'mean_word_length': 3.1196365471983847, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4147749', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2187, 'n_words': 810}