SHARE
TWEET

Untitled

a guest Dec 11th, 2019 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Binance = require('binance-api-node').default
  2. const binanceclient = Binance();
  3. const FTXRest = require('./');
  4. const BitMEXClient = require('./indexBitmex.js');
  5. const rp = require('request-promise');
  6. const FTXWs = require('ftx-api-ws');
  7. const now = require('performance-now');
  8. var crypto = require('crypto');
  9. const sleep = ms => new Promise(r => setTimeout(r, ms));
  10. // FIX account credentials
  11. const API = require('./api.js');
  12.  
  13. let api = new API({key:"", secret:"", subaccount:""});
  14.  
  15. var start;
  16. var end;
  17. var sendOrderCount = 0;
  18. var responseCount = 0;
  19. var FIXconnected = false;
  20.  
  21. const ftxws = new FTXWs({
  22.     key: "",
  23.     secret: "",
  24.     subaccount: ""
  25. });
  26.  
  27. const ftx = new FTXRest({
  28.     key: "",
  29.     secret: "",
  30.     subaccount: ""
  31. });
  32. var FTXbidPrice = 0;
  33. var FTXaskPrice = 0;
  34. var BinanceBTCBid = 0;
  35. var BinanceBTCAsk = 0;
  36.  
  37. async function WSstart() {
  38.     await ftxws.connect();
  39.     ftxws.subscribe('ticker', 'BTC-PERP');
  40.     ftxws.on('BTC-PERP::ticker', function(e) {
  41.         try {
  42.                 FTXbidPrice = e.bid
  43.                 FTXaskPrice = e.ask;
  44.         } catch (e) {
  45.             console.error('BTC-PERP::ticker ' + e);
  46.         }
  47.     });
  48.     await this.api.ftxFix.connect()
  49.     this.api.ftxFix.on("message", message=>{
  50.         console.log(message.encode())
  51.     });
  52. }
  53.  
  54. WSstart();
  55.  
  56.  
  57.  
  58. var tradePrice = 0;
  59. var tradeSize = 0;
  60.  
  61. var premiumBuyHisotry = [];
  62. var premiumSellHistory = [];
  63.  
  64. var averageBuyPremium = 0;
  65. var averageSellPremium = 0;
  66.  
  67. function calculatepremiumHistory()
  68. {
  69.     if(BinanceBTCBid!=0 && FTXbidPrice!=0 && BinanceBTCAsk!=0 && FTXaskPrice!=0)
  70.     {
  71.         var buyPremium = FTXbidPrice - BinanceBTCBid;
  72.         premiumBuyHisotry.push(buyPremium);
  73.         var sellPremium =  FTXaskPrice - BinanceBTCAsk;
  74.         premiumSellHistory.push(sellPremium);
  75.     }
  76.     if(premiumBuyHisotry.length>5)
  77.     {
  78.         var tempaverage = 0;
  79.         for(var i = 0; i<premiumBuyHisotry.length ; i++)
  80.         {
  81.             tempaverage = tempaverage+premiumBuyHisotry[i];
  82.         }
  83.         tempaverage = tempaverage/premiumBuyHisotry.length;
  84.         averageBuyPremium = tempaverage;
  85.         premiumBuyHisotry.shift();
  86.  
  87.     }
  88.     if(premiumSellHistory.length>5)
  89.     {
  90.         var tempaverage = 0;
  91.         for(var i = 0; i<premiumSellHistory.length ; i++)
  92.         {
  93.             tempaverage = tempaverage+premiumSellHistory[i];
  94.         }
  95.         tempaverage = tempaverage/premiumSellHistory.length;
  96.         averageSellPremium = tempaverage;
  97.         premiumSellHistory.shift();
  98.  
  99.     }  
  100. }
  101.  
  102. binanceclient.ws.ticker('BTCUSDT', ticker => {
  103.     try {
  104.         BinanceBTCBid = ticker.bestBid;
  105.         BinanceBTCAsk = ticker.bestAsk;
  106.         calculatepremiumHistory();
  107.     } catch (e) {
  108.         console.log("BTCUSDT " + e);
  109.     }
  110. });
  111.  
  112. binanceclient.ws.trades(['BTCUSDT'], trade => {
  113.         try {
  114.   tradePrice = parseFloat(trade.price);
  115.   tradeSize = parseFloat(trade.quantity);
  116.   if(parseFloat(tradePrice+averageBuyPremium) > FTXaskPrice*(1.001) && tradeSize > 1 && FTXbidPrice !=0 && FTXaskPrice!=0 && averageBuyPremium!=0)
  117.   {
  118.       if (FIXconnected == true)
  119.       {
  120.           console.log("sentFix Order");
  121.         this.api.ftxFix.placeOrder('buy', 'BTC-PERP', FTXaskPrice+5, 0.005, false, true);
  122.       }
  123.       else
  124.       {
  125.         sendOrderFTX('buy', 'BTC-PERP', FTXaskPrice+5, 0.005, false, true);
  126.       }
  127.   }
  128.   if(parseFloat(tradePrice+averageSellPremium) < FTXbidPrice*(0.999) && tradeSize > 1 && FTXbidPrice !=0 && FTXaskPrice!=0 && averageSellPremium!=0)
  129.   {
  130.       if (FIXconnected == true)
  131.       {
  132.           console.log("sentFix Order");
  133.           this.api.ftxFix.placeOrder('sell', 'BTC-PERP', FTXbidPrice-5, 0.005, false, true);
  134.       }
  135.       else
  136.       {  
  137.         sendOrderFTX('sell', 'BTC-PERP', FTXbidPrice-5, 0.005, false, true);
  138.       }
  139.   }
  140.  
  141.     } catch (e) {
  142.         console.log("BTCUSDT " + e);
  143.     }  
  144. });
  145.  
  146. async function sendOrderFTX(side, market, price, size, postOnly, ioc) {
  147.     try {
  148.  
  149.         const data = await ftx.request({
  150.             method: 'POST',
  151.             path: '/orders',
  152.             data: {
  153.                 'market': market,
  154.                 'side': side,
  155.                 'price': price,
  156.                 'order_type': 'limit',
  157.                 'size': size,
  158.                 'postOnly': postOnly,
  159.                 'ioc': ioc
  160.             }
  161.         });
  162.         console.log("Sent Order", side, price, size);
  163.     } catch (err) {
  164.         console.log(side, market, price, size, postOnly, ioc);
  165.         console.log('sendOrderFTX ', err);
  166.     }
  167. }
  168.  
  169. setInterval(async () => {
  170.     FIXconnected = api.ftxFix.connected;
  171.     console.log(api.ftxFix.connected);
  172. }, 10000);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top