Skip to content

Instantly share code, notes, and snippets.

@m-root
Last active March 25, 2023 04:30
Show Gist options
  • Select an option

  • Save m-root/bc10e00ea202f45167a70fad59e3999a to your computer and use it in GitHub Desktop.

Select an option

Save m-root/bc10e00ea202f45167a70fad59e3999a to your computer and use it in GitHub Desktop.

Revisions

  1. m-root revised this gist Mar 25, 2023. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions basisPerpSpotSpread.js
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    const {CoinMClient} = require('binance');
    const RestClientV5 = require('bybit-api').RestClientV5;

    // Set the API keys for the Binance and Bybit APIs
    // Set the API keys for the Binance and Bybit APIs
    const apiKeyBinance = 'your-binance-api-key';
    const apiSecretBinance = 'your-binance-api-secret';
    @@ -21,15 +22,15 @@ const clientBybit = new RestClientV5({
    secret: apiSecretBybit,
    });

    // Define symbol for Binance Spot and Bybit Perpetual Contracts
    const spotBinanceSymbol = 'BTCUSD_PERP';
    const perpBybitSymbol = 'BTCUSDT';
    // Define symbol for Binance Perpetual Contracts and Bybit Spot
    const perpBinanceSymbol = 'BTCUSD_PERP';
    const spotBybitSymbol = 'BTCUSDT';

    // Define the formula for calculating the basis
    const calculateBasis = (spotResponse, prepResponse) => {
    const calculateBasis = ( prepResponse , spotResponse ) => {
    // Basis = Perpetual contract price - Spot price
    const perpAsk = parseFloat(prepResponse[0]['askPrice']);
    const perpBid = parseFloat(prepResponse[0]['bidPrice']);
    const perpAsk = parseFloat(prepResponse[0].askPrice);
    const perpBid = parseFloat(prepResponse[0].bidPrice);
    const spotAsk = parseFloat(spotResponse.result.a[0][0]);
    const spotBid = parseFloat(spotResponse.result.b[0][0]);

    @@ -44,14 +45,13 @@ const calculateBasis = (spotResponse, prepResponse) => {
    const fetchSpotFuturesPrices = async () => {
    try {
    const [prepResponse, spotResponse] = await Promise.all(

    [
    clientBinance.getSymbolOrderBookTicker({symbol : spotBinanceSymbol }),// Perpertual
    clientBybit.getOrderbook({ category: 'linear', symbol: perpBybitSymbol }) // Spot
    clientBinance.getSymbolOrderBookTicker({symbol : perpBinanceSymbol }),// Perpertual
    clientBybit.getOrderbook({ category: 'linear', symbol: spotBybitSymbol }) // Spot
    ]
    );

    console.log(`Basis Perp-Spot Spread: ${ calculateBasis(spotResponse, prepResponse) }`);
    console.log(`Basis Perp-Spot Spread: ${ calculateBasis(prepResponse, spotResponse) }`);
    } catch (error) {
    console.log(error)
    }
  2. m-root created this gist Mar 24, 2023.
    60 changes: 60 additions & 0 deletions basisPerpSpotSpread.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    // Importing Binance and Bybit Node Packages
    const {CoinMClient} = require('binance');
    const RestClientV5 = require('bybit-api').RestClientV5;

    // Set the API keys for the Binance and Bybit APIs
    const apiKeyBinance = 'your-binance-api-key';
    const apiSecretBinance = 'your-binance-api-secret';
    const apiKeyBybit = 'your-bybit-api-key';
    const apiSecretBybit = 'your-bybit-api-secret';


    // Create a new instance of Binance and Bybit APIs
    const clientBinance = new CoinMClient({
    api_key: apiKeyBinance,
    api_secret: apiSecretBinance,
    }
    );

    const clientBybit = new RestClientV5({
    key: apiKeyBybit,
    secret: apiSecretBybit,
    });

    // Define symbol for Binance Spot and Bybit Perpetual Contracts
    const spotBinanceSymbol = 'BTCUSD_PERP';
    const perpBybitSymbol = 'BTCUSDT';

    // Define the formula for calculating the basis
    const calculateBasis = (spotResponse, prepResponse) => {
    // Basis = Perpetual contract price - Spot price
    const perpAsk = parseFloat(prepResponse[0]['askPrice']);
    const perpBid = parseFloat(prepResponse[0]['bidPrice']);
    const spotAsk = parseFloat(spotResponse.result.a[0][0]);
    const spotBid = parseFloat(spotResponse.result.b[0][0]);

    const perpPrice = (perpBid + perpAsk) / 2;
    const spotPrice = (spotBid + spotAsk) / 2;

    return perpPrice - spotPrice;

    }

    // Fetch the spot and futures prices from Binance and Bybit
    const fetchSpotFuturesPrices = async () => {
    try {
    const [prepResponse, spotResponse] = await Promise.all(

    [
    clientBinance.getSymbolOrderBookTicker({symbol : spotBinanceSymbol }),// Perpertual
    clientBybit.getOrderbook({ category: 'linear', symbol: perpBybitSymbol }) // Spot
    ]
    );

    console.log(`Basis Perp-Spot Spread: ${ calculateBasis(spotResponse, prepResponse) }`);
    } catch (error) {
    console.log(error)
    }
    }

    setInterval(fetchSpotFuturesPrices, 2000)