{"cells": [{"metadata": {}, "cell_type": "markdown", "source": "#\u6d4b\u8bd5\u7814\u7a76\u73af\u5883\nhttps://www.fmz.com/digest-topic/4459\n\u4e09\u89d2\u5bf9\u51b2\u7ec6\u8282\uff0c\u4ee5\u53ca\u624b\u7eed\u8d39"}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var fmz = require(\"fmz\");\nvar task = fmz.VCtx({\n    start: '2020-01-01 00:00:00',\n    end: '2020-01-20 18:00:00',\n    period: '1d',\n    exchanges: [{'eid':'Huobi','currency':'ETH_BTC',\"balance\":1, \"stocks\":10},\n               {'eid':'OKEX', 'currency':'ETH_USDT',\"balance\":10000, \"stocks\":1},\n               {'eid' : 'OKEX', 'currency':'BTC_USDT', \"balance\": 10000, \"stocks\":1}]\n});", "execution_count": 78, "outputs": []}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var accA = exchanges[0].GetAccount()\naccA", "execution_count": 16, "outputs": [{"output_type": "execute_result", "execution_count": 16, "data": {"text/plain": "{ Balance: 1, FrozenBalance: 0, Stocks: 10, FrozenStocks: 0 }"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var accB = exchanges[1].GetAccount();\naccB", "execution_count": 17, "outputs": [{"output_type": "execute_result", "execution_count": 17, "data": {"text/plain": "{ Balance: 10000, FrozenBalance: 0, Stocks: 1, FrozenStocks: 0 }"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var accC = exchanges[2].GetAccount();\naccC", "execution_count": 18, "outputs": [{"output_type": "execute_result", "execution_count": 18, "data": {"text/plain": "{ Balance: 10000, FrozenBalance: 0, Stocks: 1, FrozenStocks: 0 }"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var initSumBTC = accA.Balance + accC.Stocks\ninitSumBTC", "execution_count": 19, "outputs": [{"output_type": "execute_result", "execution_count": 19, "data": {"text/plain": "2"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var initSumETH = accA.Stocks + accB.Stocks\ninitSumETH", "execution_count": 20, "outputs": [{"output_type": "execute_result", "execution_count": 20, "data": {"text/plain": "11"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var initSumUSDT = accB.Balance + accC.Balance\ninitSumUSDT", "execution_count": null, "outputs": []}, {"metadata": {}, "cell_type": "markdown", "source": "### \u53ef\u4ee5\u770b\u5230\uff0c\u521d\u59cbBTC\u603b\u6570\u662f2\u4e2a\uff0cETH\u603b\u6570\u662f11\u4e2a\uff0cUSDT\u603b\u6570\u662f20000\u4e2a\uff0c\u4e0b\u9762\u6211\u4eec\u83b7\u53d6\u4e00\u4e0b\u5f53\u524d\u7684\u5404\u4e2a\u4ea4\u6613\u5bf9\u7684\u884c\u60c5\u4fe1\u606f"}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var tickerA = exchanges[0].GetTicker()\ntickerA", "execution_count": 29, "outputs": [{"output_type": "execute_result", "execution_count": 29, "data": {"text/plain": "{ Time: 1577839560000,\n  High: 0.01792601,\n  Low: 0.01792599,\n  Sell: 0.01792601,\n  Buy: 0.01792599,\n  Last: 0.017926,\n  Volume: 256.2683,\n  OpenInterest: 0 }"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "_D(tickerA.Time)", "execution_count": 23, "outputs": [{"output_type": "execute_result", "execution_count": 23, "data": {"text/plain": "'2020-01-01 00:00:00'"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var tickerB = exchanges[1].GetTicker()\ntickerB", "execution_count": 30, "outputs": [{"output_type": "execute_result", "execution_count": 30, "data": {"text/plain": "{ Time: 1577840280000,\n  High: 128.90000001,\n  Low: 128.89999999,\n  Sell: 128.90000001,\n  Buy: 128.89999999,\n  Last: 128.9,\n  Volume: 4036.4944,\n  OpenInterest: 0 }"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "_D(tickerB.Time)", "execution_count": 25, "outputs": [{"output_type": "execute_result", "execution_count": 25, "data": {"text/plain": "'2020-01-01 00:00:00'"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var tickerC = exchanges[2].GetTicker()\ntickerC", "execution_count": 28, "outputs": [{"output_type": "execute_result", "execution_count": 28, "data": {"text/plain": "{ Time: 1577839024800,\n  High: 7176.30000001,\n  Low: 7176.29999999,\n  Sell: 7176.30000001,\n  Buy: 7176.29999999,\n  Last: 7176.3,\n  Volume: 120.3639,\n  OpenInterest: 0 }"}, "metadata": {}}]}, {"metadata": {}, "cell_type": "markdown", "source": "### as we can see, exchange A & B trades ETH, while C trades USDT\nA/B\u4ea4\u6613\u6240\u53ef\u4ee5\u8fdb\u884c\u95f4\u63a5\u5bf9\u51b2\u64cd\u4f5c\uff0c\u65e0\u6cd5\u76f4\u63a5\u5bf9\u51b2\uff0c\u539f\u56e0\u5728\u4e8e\u8ba1\u4ef7\u5e01\u79cd\u4e0d\u540c\u3002A\u4ea4\u6613\u6240\u7684\u8ba1\u4ef7\u5e01\u79cd\u662fBTC\uff0cB\u4ea4\u6613\u6240\u7684\u8ba1\u4ef7\u5e01\u79cd\u662fUSDT\u3002\u5047\u8bbe\u5bf9\u4e8eA\u4ea4\u6613\u6240\u4e70\u5165\uff0cexchanges[0].Buy(price, amount);\n\u4e70\u5165\u65f6\u6839\u636e\u5bf9\u624b\u5356\u51fa\u7684\u4ef7\u683c\u4f5c\u4e3a\u4e0b\u5355\u4ef7\u683c\uff0c\u5373ticker.Sell. B\u4ea4\u6613\u6240\u4e3a\u4e86\u5bf9\u51b2\uff0c\u5c31\u8981\u8fdb\u884c\u5356\u51fa\u64cd\u4f5c\uff0c\u5373exchanges[1].Sell(price, amount); \u5356\u51fa\u65f6\u6839\u636e\u5bf9\u624b\u8981\u4e70\u5165\u7684\u4ef7\u683c\u4f5c\u4e3a\u4e0b\u5355\u4ef7\u683c\uff0c\u5373\uff1a tickerB.Buy\u3002 \u56e0\u4e3aA\u4ea4\u6613\u6240\u4e70\u5165\u65f6\u6d88\u8017\u7684\u662fBTC\uff0cB\u4ea4\u6613\u6240\u5356\u51fa\u65f6\u5f97\u5230USDT\uff0c\u9700\u8981\u628aUSDT\u6362\u6210BTC\uff0c\u5373\u8865\u7f3aA\u4ea4\u6613\u6240\u6d88\u8017\u4f7f\u7528\u7684BTC\uff0c\u5373C\u4ea4\u6613\u6240\u6267\u884cexchanges[2].Buy(price, amount),\u628aUSDT\u6362\u6210BTC\uff0cC\u4ea4\u6613\u6240\u4e70\u5165\uff0c\u9700\u8981\u770b\u5bf9\u624b\u7684\u5356\u51fa\u4ef7\u683c\uff0c\u5373`ticker.Sell`\u3002 A\u4ea4\u6613\u6240\u4e70\u5165\u7684\u4ef7\u683c\u4e00\u5b9a\u8981\u6bd4B\u4ea4\u6613\u6240\u5356\u51fa\u7684\u4ef7\u683c\u66f4\u4f4e\uff0c\u90a3\u4e48C\u4ea4\u6613\u6240\u5c31\u53ea\u7528\u82b1\u5c11\u4e00\u70b9\u7684USDT\u5c31\u80fd\u4e70\u56deA\u4ea4\u6613\u6240\u6d88\u8017\u6389\u7684BTC\uff0c\u5c31\u8d5a\u5230\u4e86USDT\u91d1\u989d\u5dee\n\u6240\u4ee5\u5982\u4e0b\uff1a"}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var diffB2A = tickerA.Sell - tickerB.Buy / tickerC.Sell\ndiffB2A", "execution_count": 32, "outputs": [{"output_type": "execute_result", "execution_count": 32, "data": {"text/plain": "-0.000035892371670689927"}, "metadata": {}}]}, {"metadata": {}, "cell_type": "markdown", "source": "### \u8bf4\u660eA\u4ea4\u6613\u6240\u4e70\u5165\u7684\u4ef7\u683c\u6bd4BC\u4ea4\u6613\u6240\u7ec4\u5408\u6210\u7684\u4ef7\u683c\u8981\u4f4e\uff0c\u5bf9\u51b2\u4ea7\u751f\u7684USDT\u5dee\u989d\u4e3adiffB2A\n"}, {"metadata": {}, "cell_type": "markdown", "source": "\u53e6\u4e00\u4e2a\u5bf9\u51b2\u65b9\u5411\u540c\u7406\uff0cA\u4ea4\u6613\u6240\u6267\u884c\u5356\u51faETH\u7684\u64cd\u4f5c\uff0cB\u4ea4\u6613\u6240\u6267\u884c\u4e70\u5165ETH\u7684\u64cd\u4f5c\uff0cC\u4ea4\u6613\u6240\u6267\u884c\u5356\u51faBTC\u7684\u64cd\u4f5c\u6362\u56deUSDT\u3002\u8fbe\u5230ETH\uff0cBTC\u90fd\u662f\u5e73\u8861\u7684\uff0c\u53ea\u6709USDT\u589e\u51cf"}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var diffA2B = tickerA.Buy - tickerB.Sell/tickerC.Buy\ndiffA2B", "execution_count": 33, "outputs": [{"output_type": "execute_result", "execution_count": 33, "data": {"text/plain": "-0.00003591237450770515"}, "metadata": {}}]}, {"metadata": {}, "cell_type": "markdown", "source": "\u6211\u4eec\u6267\u884c\u4e00\u6b21tickerA.Buy - tickerB.Sell / tickerC.Buy \u7684\u64cd\u4f5c\uff0c\u5bf9\u5176\u4e2d\u7684\u5404\u4e2a\u6570\u503c\u53d8\u5316\u67e5\u770b"}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var idA = exchanges[0].Sell(tickerA.Buy, 1);\nvar nowAccA = exchanges[0].GetAccount()\nnowAccA", "execution_count": 79, "outputs": [{"output_type": "execute_result", "execution_count": 79, "data": {"text/plain": "{ Balance: 1.01792407,\n  FrozenBalance: 0,\n  Stocks: 9,\n  FrozenStocks: 0 }"}, "metadata": {}}]}, {"metadata": {}, "cell_type": "markdown", "source": "\u901a\u8fc7\u67e5\u770b\u8d26\u6237\u53d8\u5316\u6765\u770b\u5356\u51faETH\u7684\u5f71\u54cd"}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "diffBalanceA = nowAccA.Balance - accA.Balance\ndiffBalanceA", "execution_count": 51, "outputs": [{"output_type": "execute_result", "execution_count": 51, "data": {"text/plain": "0.01792407000000007"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "nowAccA.Stocks - accA.Stocks", "execution_count": 46, "outputs": [{"output_type": "execute_result", "execution_count": 46, "data": {"text/plain": "-1"}, "metadata": {}}]}, {"metadata": {}, "cell_type": "markdown", "source": "\u901a\u8fc7\u67e5\u770b\u8ba2\u5355\u6765\u770b\u5356\u51faETH\u7684\u5f71\u54cd"}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var orderA = exchanges[0].GetOrder(idA)\norderA", "execution_count": 47, "outputs": [{"output_type": "execute_result", "execution_count": 47, "data": {"text/plain": "{ Id: 1,\n  Price: 0.01792599,\n  Amount: 1,\n  DealAmount: 1,\n  AvgPrice: 0.01795999,\n  Type: 1,\n  Offset: 0,\n  Status: 1,\n  ContractType: 'ETH_BTC_Huobi' }"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "orderA.Price*orderA.Amount - diffBalanceA", "execution_count": 61, "outputs": [{"output_type": "execute_result", "execution_count": 61, "data": {"text/plain": "0.000001919999999929617"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var feeRatioA = 0.00107\nvar feeA = orderA.DealAmount * orderA.AvgPrice * feeRatioA\nfeeA", "execution_count": 67, "outputs": [{"output_type": "execute_result", "execution_count": 67, "data": {"text/plain": "0.000019217189299999998"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var idB = exchanges[1].Buy(tickerB.Sell, 1);\nvar nowAccB = exchanges[1].GetAccount();\nnowAccB", "execution_count": 80, "outputs": [{"output_type": "execute_result", "execution_count": 80, "data": {"text/plain": "{ Balance: 9870.84219998,\n  FrozenBalance: 129.15780001,\n  Stocks: 1,\n  FrozenStocks: 0 }"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var orderB = exchanges[1].GetOrder(idB)\norderB", "execution_count": 72, "outputs": [{"output_type": "execute_result", "execution_count": 72, "data": {"text/plain": "{ Id: 2,\n  Price: 128.90000001,\n  Amount: 1,\n  DealAmount: 0,\n  AvgPrice: 0,\n  Type: 0,\n  Offset: 0,\n  Status: 0,\n  ContractType: 'ETH_USDT_OKEX' }"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var feeRatioB = 0.002\nvar feeB = orderB.Amount * orderB.Price * feeRatioB\nfeeB", "execution_count": 73, "outputs": [{"output_type": "execute_result", "execution_count": 73, "data": {"text/plain": "0.25780000002000003"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var idC = exchanges[2].Sell(tickerC.Buy, nowAccA.Balance - accA.Balance);\nidC", "execution_count": 81, "outputs": [{"output_type": "execute_result", "execution_count": 81, "data": {"text/plain": "1"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var nowAccC = exchanges[2].GetAccount()\nnowAccC", "execution_count": 82, "outputs": [{"output_type": "execute_result", "execution_count": 82, "data": {"text/plain": "{ Balance: 10128.53291899,\n  FrozenBalance: 0,\n  Stocks: 0.9821,\n  FrozenStocks: 0 }"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var orderC = exchanges[2].GetOrder(idC)\norderC", "execution_count": 83, "outputs": [{"output_type": "execute_result", "execution_count": 83, "data": {"text/plain": "{ Id: 1,\n  Price: 7176.29999999,\n  Amount: 0.0179,\n  DealAmount: 0.0179,\n  AvgPrice: 7194.99999999,\n  Type: 1,\n  Offset: 0,\n  Status: 1,\n  ContractType: 'BTC_USDT_OKEX' }"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var feeRatioC = 0.002\nvar feeC = orderC.DealAmount * orderC.AvgPrice * feeRatioC / tickerC.Last\nfeeC", "execution_count": 86, "outputs": [{"output_type": "execute_result", "execution_count": 86, "data": {"text/plain": "0.00003589328762727896"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var nowSumBTC = nowAccA.Balance + nowAccC.Stocks\nnowSumBTC", "execution_count": 87, "outputs": [{"output_type": "execute_result", "execution_count": 87, "data": {"text/plain": "2.0000240700000003"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var nowSumETH = nowAccA.Stocks + nowAccB.Stocks + nowAccA.FrozenStocks\nnowSumETH", "execution_count": 90, "outputs": [{"output_type": "execute_result", "execution_count": 90, "data": {"text/plain": "10"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "var nowSumUSDT = nowAccB.Balance + nowAccC.Balance\nnowSumUSDT", "execution_count": 91, "outputs": [{"output_type": "execute_result", "execution_count": 91, "data": {"text/plain": "19999.375118969998"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "nowSumBTC- initSumBTC", "execution_count": 92, "outputs": [{"output_type": "execute_result", "execution_count": 92, "data": {"text/plain": "0.000024070000000264713"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "nowSumETH - initSumETH", "execution_count": 93, "outputs": [{"output_type": "execute_result", "execution_count": 93, "data": {"text/plain": "-1"}, "metadata": {}}]}, {"metadata": {"trusted": true}, "cell_type": "code", "source": "nowSumUSDT - initSumUSDT", "execution_count": 94, "outputs": [{"output_type": "execute_result", "execution_count": 94, "data": {"text/plain": "-0.6248810300021432"}, "metadata": {}}]}], "metadata": {"kernelspec": {"name": "javascript", "display_name": "Javascript (Node.js)", "language": "javascript"}, "language_info": {"name": "javascript", "version": "8.10.0", "mimetype": "application/javascript", "file_extension": ".js"}}, "nbformat": 4, "nbformat_minor": 2}