text: const request = require('request')
  const crypto = require('crypto')
  var SocksProxyAgent = require('socks-proxy-agent')
  var proxy = 'socks://127.0.0.1:1080'
  var agent = new SocksProxyAgent(proxy)
  var baseURL = 'https://www.bitmex.com'
  
  exports.GetOrderDepth = GetOrderDepth
  exports.GetTicker = GetTicker
  exports.GetPosition = GetPosition
  exports.AddOrder = AddOrder
  exports.CancelOrder = CancelOrder
  exports.CancelAllOrder = CancelAllOrder
  
  function GetOrderDepth(symbol, depth, callback) {
      symbol = symbol || 'BTCUSD'
      depth = depth || 200
      var url = baseURL + '/api/v1/orderBook/L2?symbol=' + symbol + '&depth=' + depth
      request.get({
          url: url,
          agent: agent
      }, function (err, res, body) {
          if (!err && res.statusCode == 200 && body) {
              try {
                  var data = JSON.parse(body)
                  var result = {
                      bids: [],
                      asks: []
                  }
                  for (var item of data) {
                      if (item.side == 'Sell') {
                          result.asks.push([item.price, item.size])
                      } else if (item.side == 'Buy') {
                          result.bids.push([item.price, item.size])
                      }
                  }
                  callback(null, result)
              } catch (error) {
                  return callback(error)
              }
          }
          callback(err)
      })
  }
  
  function GetTicker(symbol, options, callback) {
      symbol = symbol
      options = options || {}
      var url = baseURL + '/api/v1/quote?reverse=true&count=1&symbol=' + symbol
      request.get({
          url: url,
          agent: agent
      }, function (err, res, body) {
          if (!err && res.statusCode == 200 && body) {
              try {
                  var data = JSON.parse(body)
                  if (data.length > 0) {
                      callback(null, data[0])
                  } else {
                      callback('unexpect result')
                  }
              } catch (error) {
                  return callback(error)
              }
          }
          callback(err)
      })
  }
  
  function GetPosition(order, options, callback) {
      if (!options) return
      callback = callback || function () {}
  
      var apiKey = options.apiKey
      var apiSecret = options.apiSecret
  
      var verb = 'GET',
          path = '/api/v1/position',
          expires = new Date().getTime() + (60 * 1000)
      var postBody = ''
      if (order) {
          postBody = JSON.stringify(order)
      }
  
      var signature = crypto.createHmac('sha256', apiSecret).update(verb + path + expires + postBody).digest('hex');
  
      var headers = {
          'content-type': 'application/json',
          'Accept': 'application/json',
          'X-Requested-With': 'XMLHttpRequest',
          'api-expires': expires,
          'api-key': apiKey,
          'api-signature': signature
      }
  
      const requestOptions = {
          headers: headers,
          url: baseURL + path,
          method: verb,
          body: postBody,
          agent: agent
      }
  
      request(requestOptions, function (error, response, body) {
          callback(error, response, body)
      })
  }
  
  function CancelOrder(order, options, callback) {
      if (!options) return
      callback = callback || function () {}
  
      var apiKey = options.apiKey
      var apiSecret = options.apiSecret
  
      var verb = 'DELETE',
          path = '/api/v1/order',
          expires = new Date().getTime() + (60 * 1000)
      var postBody = ''
      if (order) {
          postBody = JSON.stringify(order)
      }
  
      var signature = crypto.createHmac('sha256', apiSecret).update(verb + path + expires + postBody).digest('hex');
  
      var headers = {
          'content-type': 'application/json',
          'Accept': 'application/json',
          'X-Requested-With': 'XMLHttpRequest',
          'api-expires': expires,
          'api-key': apiKey,
          'api-signature': signature
      }
  
      const requestOptions = {
          headers: headers,
          url: baseURL + path,
          method: verb,
          body: postBody,
          agent: agent
      }
  
      request(requestOptions, function (error, response, body) {
          callback(error, response, body)
      })
  }
  
  function AddOrder(order, options, callback) {
      if (!order || !options) return
      callback = callback || function () {}
  
  
      var apiKey = options.apiKey
      var apiSecret = options.apiSecret
  
      var verb = 'POST',
          path = '/api/v1/order',
          expires = new Date().getTime() + (60 * 1000)
      order.ordType = order.ordType || "Limit"
      var postBody = JSON.stringify(order)
  
      var signature = crypto.createHmac('sha256', apiSecret).update(verb + path + expires + postBody).digest('hex');
  
      var headers = {
          'content-type': 'application/json',
          'Accept': 'application/json',
          'X-Requested-With': 'XMLHttpRequest',
          'api-expires': expires,
          'api-key': apiKey,
          'api-signature': signature
      }
  
      const requestOptions = {
          headers: headers,
          url: baseURL + path,
          method: verb,
          body: postBody,
          agent: agent
      }
  
      request(requestOptions, function (error, response, body) {
          callback(error, response, body)
      })
  }
  
  function CancelAllOrder(order, options, callback) {
      if (!options) return
      callback = callback || function () {}
  
      var apiKey = options.apiKey
      var apiSecret = options.apiSecret
  
      var verb = 'DELETE',
          path = '/api/v1/order/all',
          expires = new Date().getTime() + (60 * 1000)
      var postBody = ''
      if (order) {
          postBody = JSON.stringify(order)
      }
  
      var signature = crypto.createHmac('sha256', apiSecret).update(verb + path + expires + postBody).digest('hex');
  
      var headers = {
          'content-type': 'application/json',
          'Accept': 'application/json',
          'X-Requested-With': 'XMLHttpRequest',
          'api-expires': expires,
          'api-key': apiKey,
          'api-signature': signature
      }
  
      const requestOptions = {
          headers: headers,
          url: baseURL + path,
          method: verb,
          body: postBody,
          agent: agent
      }
  
      request(requestOptions, function (error, response, body) {
          callback(error, response, body)
      })
  }