0% found this document useful (0 votes)
21 views

01 Market Data

Uploaded by

sergiosantana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

01 Market Data

Uploaded by

sergiosantana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

01 Market Data

October 2, 2023

1 TRADINGCLUB.CL

2 01 Market Data
2.1 Introducción
Dentro del mundo del sistematic trading, me voy a referir a 3 grupos con objetivos y herramientas
muy distintas.
1. Inversionista: Asset Allocation
Este inversionista aloca su dinero entre fondos y entre variados asset-classes. Lo ideal es
utilizar métodos cuantitativos para encontrar los weights correspondientes. Pueden ser lazy
& wize investors o institutional como fondos de pensiones. La premisa básica es que no existe
alpha via HFT y buscan incorporar su visión en el portafolio. Generalmente tradeamos los
asset-class a través de un ETF proxy.
2. Semi-Automatic Trader
Son aquellos que viven en un mundo de oportunidades en un set fluido de activos y precios.
Realizan algo que conocemos como educated-guess tal que tratan de ganarle al mercado. Es-
tas apuestas están enmarcadas en un framework sistemático tal que sus posiciones y riesgo
esta manejado correctamente. Esto, apalanca el tiempo libre para dedicarse a crear valor.
Generalmente, se siente en confianza con apalancamiento y el uso de derivados. Long/short
portfolios más que solamente long-only.
3. Systems Trader
Un believer de los beneficios de un trading completamente sistemático. Mezcla perfecta de
frameworks de riesgo y reglas de trading tal que nunca el portafolio explote. El alpha no esta
disponible haciendo estrategias de buy and hold. Buscan alpha y/o alternative beta.
Fieles al backtesting, simulaciónes y métodos cuantitativos. Set de reglas e infrastructura
desarrollada para llevar el portafolio.

2.2 API REST


A RESTful API is an architectural style for an application program interface (API) that uses HTTP
requests to access and use data.
El principal concepto es que funciona a través de requests, es decir, le tenemos que pedir data
cada vez que queremos saber el estado actual. El principal problema es que normalmente perde-

1
mos mucha data que no vemos entre requests y es muy intensivo en uso de tiempo de com-
puto/network/etc.
Diferencia de forma gráfica.

[7]: import os

# Set SSL verification False


# CMD: set CURL_CA_BUNDLE= // or in env variables in pycharm // import os, os.
↪environ['CURL_CA_BUNDLE'] = ''

os.environ['CURL_CA_BUNDLE'] = ''

import urllib3
urllib3.disable_warnings()

Received eth_mxn 24458.000000000 - 24474.000000000


Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000

2
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24458.000000000 - 24474.000000000
Received eth_mxn 24457.000000000 - 24474.000000000
Received eth_mxn 24457.000000000 - 24474.000000000
Received eth_mxn 24439.000000000 - 24474.000000000
Received eth_mxn 24439.000000000 - 24474.000000000
Received eth_mxn 24440.000000000 - 24474.000000000
Received eth_mxn 24440.000000000 - 24474.000000000Received eth_btc 0.074708000 -
0.074790000

Received eth_mxn 24440.000000000 - 24474.000000000


Received eth_mxn 24441.000000000 - 24474.000000000
Received eth_mxn 24441.000000000 - 24474.000000000
Received eth_mxn 24441.000000000 - 24474.000000000
Received eth_mxn 24441.000000000 - 24474.000000000
Received eth_mxn 24441.000000000 - 24474.000000000
Received eth_mxn 24441.000000000 - 24474.000000000
Received eth_btc 0.074708000 - 0.074790000

[20]: # Mkd Data Binance


import requests

tickers = ['BTCUSDT','ETHUSDT']

def mkt_data(ticker):
key = "https://api.binance.com/api/v3/ticker/24hr?symbol="
r = key + str(ticker)
r = requests.get(r)
prices = r.json()
best_bid = prices['bidPrice']
best_bid_q = prices['bidQty']

3
best_offer = prices['askPrice']
best_offer_q = prices['askQty']
flag = {'bid': best_bid, 'bid_q': best_bid_q, 'ask': best_offer, 'ask_q':␣
↪best_offer_q}

return flag

for ticker in tickers:


flag = mkt_data(ticker)
print(flag)

{'bid': '16859.75000000', 'bid_q': '0.01775000', 'ask': '16860.20000000',


'ask_q': '0.01786000'}
{'bid': '1254.20000000', 'bid_q': '58.91460000', 'ask': '1254.21000000',
'ask_q': '9.50130000'}

[21]: # Mkt Data Buda

import requests

tickers = ['BTC-CLP','ETH-CLP']

def mkt_data(ticker):
r = requests.get(f'https://www.buda.com/api/v2/markets/{ticker}/order_book')
prices = r.json()
best_bid = prices['order_book']['bids'][0][0]
best_bid_q = prices['order_book']['bids'][0][1]
best_offer = prices['order_book']['asks'][0][0]
best_offer_q = prices['order_book']['asks'][0][1]
flag = {'bid': best_bid, 'bid_q': best_bid_q, 'ask': best_offer, 'ask_q':␣
↪best_offer_q}

return flag, prices

for ticker in tickers:


flag, prices = mkt_data(ticker)
print(flag)

{'bid': '14458844.0', 'bid_q': '0.04149709', 'ask': '14475804.0', 'ask_q':


'0.9986'}
{'bid': '1069231.0', 'bid_q': '0.001247', 'ask': '1074997.0', 'ask_q':
'0.028506538'}

[22]: # Veamos la estructura de precios


prices['order_book']

4
[22]: {'asks': [['1074997.0', '0.028506538'],
['1075000.0', '0.339712698'],
['1078400.0', '0.513479285'],
['1078404.0', '1.433947422'],
['1078450.0', '0.022441547'],
['1078550.0', '0.033'],
['1078650.0', '0.033'],
['1078750.0', '0.033'],
['1078800.0', '0.033'],
['1078850.0', '0.033'],
['1078900.0', '0.033'],
['1078940.0', '0.033'],
['1079000.0', '0.033'],
['1079050.0', '0.033'],
['1079100.0', '0.033'],
['1079150.0', '0.033'],
['1079200.0', '0.033'],
['1079250.0', '0.033'],
['1079300.0', '0.033'],
['1079325.0', '4.993'],
['1079350.0', '0.033'],
['1079400.0', '0.033'],
['1080000.0', '0.62168178'],
['1082000.0', '0.046373022'],
['1084000.0', '1.067645912'],
['1089999.0', '9.7008'],
['1090000.0', '8.062257929'],
['1091239.0', '0.5'],
['1100000.0', '2.886845853'],
['1105311.0', '0.9976514'],
['1113590.0', '0.009057975'],
['1115000.0', '3.203661702'],
['1116000.0', '2.10355709'],
['1119000.0', '9.97'],
['1120000.0', '2.716159819'],
['1130000.0', '0.149449089'],
['1138000.0', '0.035839721'],
['1139990.0', '0.996500003'],
['1140000.0', '2.860925234'],
['1145000.0', '2.04'],
['1149000.0', '1.0'],
['1150000.0', '6.895404425'],
['1155000.0', '8.11067511'],
['1155333.0', '0.03736727'],
['1160000.0', '1.6'],
['1169977.0', '1.0'],
['1175175.0', '2.0'],

5
['1179497.0', '0.09755574'],
['1180000.0', '4.322264691'],
['1187000.0', '0.038062948'],
['1198873.0', '0.02322747'],
['1199000.0', '0.100519409'],
['1199999.0', '0.53199604'],
['1200000.0', '23.088978191'],
['1200270.0', '0.011588341'],
['1203873.0', '0.02380787'],
['1205409.0', '0.1823798'],
['1212752.0', '0.09259259'],
['1215000.0', '0.03703704'],
['1215409.0', '0.18297792'],
['1218864.0', '0.04563422'],
['1220000.0', '0.5'],
['1222222.0', '1.0'],
['1223645.0', '0.090793955'],
['1230950.0', '0.28047894'],
['1239482.0', '0.04417765'],
['1242752.0', '0.09248717'],
['1250000.0', '0.399157774'],
['1250100.0', '0.002427395'],
['1250250.0', '1.0'],
['1260000.0', '0.03571428'],
['1278873.0', '0.02325206'],
['1279219.0', '1.0'],
['1299601.0', '0.074103612'],
['1300000.0', '3.521251261'],
['1305000.0', '0.03448276'],
['1310000.0', '0.113143648'],
['1313313.0', '2.0'],
['1320000.0', '0.159273455'],
['1333333.0', '2.0'],
['1350000.0', '1.90858745'],
['1351500.0', '0.007428901'],
['1375375.0', '2.000077179'],
['1380000.0', '0.073777778'],
['1385000.0', '0.0318726'],
['1394890.0', '0.21676825'],
['1395000.0', '0.03225806'],
['1399999.0', '0.5'],
['1400000.0', '0.300988457'],
['1420000.0', '0.7'],
['1440000.0', '0.08125'],
['1448951.0', '0.2782923'],
['1450000.0', '2.538491878'],
['1462009.0', '0.07270448'],

6
['1480000.0', '0.088832068'],
['1485000.0', '0.03030304'],
['1496900.0', '0.36938729'],
['1500000.0', '15.960333105'],
['1502726.0', '0.050706068'],
['1510000.0', '3.063643214'],
['1523000.0', '0.34655584'],
['1530000.0', '0.02941176'],
['1535000.0', '0.261765969'],
['1540000.0', '0.037650925'],
['1545000.0', '0.906715441'],
['1550000.0', '5.437289586'],
['1555691.0', '0.06790064'],
['1557455.0', '0.06790064'],
['1560000.0', '0.010646897'],
['1563000.0', '0.971018997'],
['1575000.0', '0.02857142'],
['1580000.0', '3.75434563'],
['1589912.0', '0.031863574'],
['1590000.0', '0.353589564'],
['1593500.0', '0.66129654'],
['1598578.0', '0.15275947'],
['1600000.0', '9.850586269'],
['1600022.0', '2.33953379'],
['1610000.0', '1.33984968'],
['1620000.0', '0.02777778'],
['1623556.0', '0.07099969'],
['1649999.0', '0.750769443'],
['1650000.0', '0.838260263'],
['1657000.0', '0.25786408'],
['1660000.0', '0.563451555'],
['1665000.0', '0.02702702'],
['1680000.0', '0.124564192'],
['1689000.0', '0.621383465'],
['1700000.0', '8.257959364'],
['1701000.0', '0.626729561'],
['1710000.0', '0.02631578'],
['1720000.0', '0.029186512'],
['1722151.0', '0.01605547'],
['1740000.0', '7.240296792'],
['1748752.0', '0.00332882'],
['1750000.0', '1.640669804'],
['1753544.0', '0.300369706'],
['1755000.0', '0.31633852'],
['1770000.0', '0.3272035'],
['1780000.0', '0.119'],
['1790000.0', '0.078600828'],

7
['1792850.0', '0.15'],
['1800000.0', '5.028951419'],
['1806000.0', '0.1'],
['1815000.0', '0.002765884'],
['1820878.0', '1.0'],
['1830000.0', '1.0'],
['1845000.0', '0.02439024'],
['1847808.0', '0.00679071'],
['1850000.0', '2.642471462'],
['1865000.0', '0.005383464'],
['1870000.0', '0.05637523'],
['1880000.0', '1.0'],
['1887911.0', '0.01609339'],
['1890000.0', '0.02380952'],
['1892000.0', '1.0'],
['1893969.0', '0.003011565'],
['1900000.0', '2.007926442'],
['1935000.0', '0.02325582'],
['1945000.0', '0.010324072'],
['1980000.0', '0.02272728'],
['1982278.0', '5.362618498'],
['1992426.0', '0.002736769'],
['2000000.0', '2.392022853'],
['2020000.0', '0.484663073'],
['2025000.0', '0.02222222'],
['2050000.0', '1.1'],
['2070000.0', '0.02173914'],
['2075000.0', '0.012096578'],
['2100000.0', '0.489968573'],
['2101918.0', '0.279138216'],
['2130765.0', '0.087267259'],
['2132857.0', '0.003168534'],
['2157949.0', '1.0'],
['2169000.0', '0.5'],
['2170000.0', '2.3'],
['2190000.0', '0.01377548'],
['2199000.0', '0.006848677'],
['2200000.0', '1.86835495'],
['2235000.0', '0.00982895'],
['2299999.0', '0.01166174'],
['2300000.0', '5.545975639'],
['2332205.0', '0.004081575'],
['2391880.0', '0.428082146'],
['2399000.0', '0.103396286'],
['2400000.0', '5.936851227'],
['2413977.0', '0.002154867'],
['2450000.0', '0.0097329'],

8
['2460571.0', '0.002154866'],
['2477777.0', '0.99685874'],
['2480000.0', '0.04860975'],
['2486881.0', '0.00229356'],
['2487454.0', '0.002091215'],
['2500000.0', '5.092907471'],
['2505000.0', '0.00875813'],
['2506170.0', '0.002175754'],
['2509000.0', '0.0480241'],
['2517603.0', '2.668048'],
['2520533.0', '0.002163356'],
['2550000.0', '0.04745238'],
['2551999.0', '0.1'],
['2552888.0', '0.002869809'],
['2560000.0', '0.89212405'],
['2567171.0', '0.002431844'],
['2596300.0', '0.75419388'],
['2600000.0', '0.991850772'],
['2600500.0', '0.003983204'],
['2605971.0', '0.001255609'],
['2620026.0', '0.001191393'],
['2630000.0', '0.04632558'],
['2650000.0', '0.250156475'],
['2655000.0', '3.219811389'],
['2660000.0', '0.04568808'],
['2660051.0', '0.00121574'],
['2667991.0', '0.002533003'],
['2668680.0', '0.00120391'],
['2700000.0', '0.822174019'],
['2737000.0', '0.007294381'],
['2747269.0', '0.053064913'],
['2750000.0', '1.993033487'],
['2756938.0', '0.003452769'],
['2784606.0', '2.669388'],
['2795738.0', '0.00766154'],
['2796790.0', '0.001148763'],
['2800000.0', '0.342567112'],
['2804400.0', '0.001145646'],
['2805000.0', '0.042931005'],
['2810000.0', '0.00156177'],
['2815000.0', '0.042778497'],
['2820000.0', '0.01116592'],
['2825000.0', '0.042627069'],
['2835000.0', '0.042476709'],
['2840000.0', '0.001060577'],
['2845000.0', '0.042327406'],
['2850000.0', '0.760568589'],

9
['2855000.0', '0.042179149'],
['2860000.0', '0.140351367'],
['2865000.0', '0.042031927'],
['2870000.0', '0.035034331'],
['2875000.0', '0.041885729'],
['2885000.0', '0.041740544'],
['2889960.0', '0.35286781'],
['2895000.0', '0.041596363'],
['2900000.0', '7.231385868'],
['2902859.0', '0.005948987'],
['2909000.0', '1.000936831'],
['2931467.0', '0.157911028'],
['2945000.0', '0.687479118'],
['2950000.0', '0.04422736'],
['2959150.0', '0.106397525'],
['2983000.0', '2.5'],
['2990585.0', '0.005774479'],
['2999368.0', '0.438640118'],
['3000000.0', '6.693766958'],
['3010000.0', '0.254890398'],
['3030000.0', '3.31790153'],
['3048744.0', '0.183856428'],
['3050000.0', '1.001994282'],
['3051000.0', '0.368197348'],
['3056999.0', '0.10798966'],
['3064320.0', '0.043882895'],
['3096000.0', '0.362845643'],
['3100000.0', '2.158703294'],
['3104771.0', '0.002869658'],
['3129038.0', '0.005775667'],
['3140000.0', '0.357761182'],
['3150000.0', '0.557839846'],
['3157949.0', '1.0'],
['3185000.0', '0.352706471'],
['3200000.0', '2.309316189'],
['3230000.0', '0.347792604'],
['3240000.0', '0.009296444'],
['3244000.0', '2.5'],
['3270000.0', '0.013805603'],
['3275000.0', '0.284615778'],
['3278000.0', '0.03214516'],
['3285000.0', '0.158231836'],
['3289903.0', '0.01994375'],
['3300000.0', '4.011884491'],
['3347220.0', '0.03507541'],
['3370000.0', '0.09388248'],
['3400000.0', '0.140315147'],

10
['3427000.0', '2.0'],
['3450000.0', '0.510255944'],
['3463000.0', '0.048663938'],
['3463900.0', '1.0'],
['3465899.0', '0.37943881'],
['3480000.0', '0.01071512'],
['3482000.0', '0.335823499'],
['3488996.0', '0.143883235'],
['3490000.0', '0.001447668'],
['3499999.0', '0.008024402'],
['3500000.0', '7.937905572'],
['3500003.0', '0.194408604'],
['3505000.0', '2.5'],
['3510000.0', '0.682253184'],
['3519000.0', '0.00314096'],
['3530000.0', '0.01291309'],
['3552000.0', '0.014135372'],
['3560000.0', '2.261120401'],
['3575000.0', '0.58445752'],
['3580000.0', '0.099233234'],
['3582251.0', '0.159871594'],
['3595000.0', '0.00739158'],
['3599680.0', '0.312074993'],
['3600000.0', '9.730725821'],
['3610000.0', '0.01001326'],
['3619840.0', '0.484242115'],
['3630000.0', '0.00655507'],
['3640000.0', '0.00621185'],
['3650000.0', '0.013753644'],
['3672321.0', '0.001389561'],
['3680000.0', '0.02145289'],
['3690000.0', '0.55027624'],
['3695000.0', '0.078095531'],
['3700000.0', '0.190849128'],
['3712000.0', '0.07773011'],
['3718000.0', '0.718999854'],
['3720000.0', '0.1993'],
['3727000.0', '0.154832196'],
['3729784.0', '0.035'],
['3730000.0', '0.09965'],
['3734000.0', '0.47848986'],
['3745000.0', '0.006702377'],
['3766000.0', '2.5'],
['3772321.0', '0.001358422'],
['3780000.0', '0.177725'],
['3795326.0', '0.004064646'],
['3800000.0', '4.75117368'],

11
['3807333.0', '1.940280152'],
['3811000.0', '0.3'],
['3815000.0', '0.286575117'],
['3820000.0', '1.582539712'],
['3830000.0', '0.749389137'],
['3847000.0', '0.997'],
['3849000.0', '0.765631562'],
['3850000.0', '0.62625816'],
['3860000.0', '2.65292563'],
['3870000.0', '0.02501642'],
['3870001.0', '0.155057'],
['3885000.0', '0.257715489'],
['3890543.0', '0.047260683'],
['3900000.0', '0.192404272'],
['3916001.0', '1.083647252'],
['3918750.0', '0.05'],
['3920000.0', '1.252397041'],
['3922875.0', '0.05'],
['3927000.0', '0.05'],
['3930000.0', '0.01232532'],
['3931125.0', '0.05'],
['3935250.0', '0.05'],
['3939375.0', '0.05'],
['3943500.0', '0.05'],
['3946000.0', '0.1510006'],
['3947625.0', '0.05'],
['3950000.0', '0.540382311'],
['3951750.0', '0.05'],
['3955875.0', '0.05'],
['3957000.0', '0.013212974'],
['3960000.0', '0.05'],
['3964125.0', '0.05'],
['3966496.0', '1.0286472'],
['3968250.0', '0.05'],
['3972375.0', '0.05'],
['3974880.0', '0.027908476'],
['3976500.0', '0.05'],
['3980000.0', '0.0026535'],
['3980625.0', '0.05'],
['3984750.0', '0.05'],
['3985000.0', '0.3'],
['3988875.0', '0.05'],
['3993000.0', '0.05'],
['3997125.0', '0.05'],
['3998769.0', '0.276806248'],
['3998787.0', '0.518293159'],
['3999000.0', '0.007174242'],

12
['3999799.0', '0.036404087'],
['3999998.0', '0.1993'],
['3999999.0', '2.056257395'],
['4000000.0', '11.722411595'],
['4001250.0', '0.05'],
['4002342.0', '0.001439884'],
['4005375.0', '0.05'],
['4007500.0', '0.027628752'],
['4009500.0', '0.05'],
['4013125.0', '0.05'],
['4017750.0', '0.05'],
['4021875.0', '0.05'],
['4026000.0', '0.05'],
['4027000.0', '2.5'],
['4030000.0', '1.0'],
['4030125.0', '0.05'],
['4034250.0', '0.05'],
['4038375.0', '0.05'],
['4040000.0', '0.002483941'],
['4042500.0', '0.05'],
['4046625.0', '0.05'],
['4050000.0', '0.15'],
['4050750.0', '0.05'],
['4054875.0', '0.05'],
['4059000.0', '0.05'],
['4061750.0', '0.027259734'],
['4063125.0', '0.05'],
['4067250.0', '0.05'],
['4071375.0', '0.05'],
['4075500.0', '0.05'],
['4079625.0', '0.05'],
['4083750.0', '0.05'],
['4087650.0', '0.027028127'],
['4087875.0', '0.05'],
['4092000.0', '0.05'],
['4096125.0', '0.05'],
['4100000.0', '4.969107085'],
['4100250.0', '0.05'],
['4104375.0', '0.05'],
['4105000.0', '0.1'],
['4108500.0', '0.05'],
['4109637.0', '0.026207412'],
['4112625.0', '0.05'],
['4116750.0', '0.05'],
['4120875.0', '0.050481486'],
['4139685.0', '0.027070661'],
['4150000.0', '0.15'],

13
['4156163.0', '0.0243'],
['4167800.0', '0.026453061'],
['4199999.0', '13.973941378'],
['4200000.0', '3.730745996'],
['4249575.0', '0.026476321'],
['4250000.0', '0.008'],
['4288000.0', '2.5'],
['4300000.0', '0.30977179'],
['4310000.0', '0.99852659'],
['4375852.0', '0.025584109'],
['4400000.0', '0.06944444'],
['4500000.0', '5.900283843'],
['4549000.0', '2.5'],
['4721490.0', '0.072082667'],
['4800000.0', '4.727268542'],
['4810000.0', '2.5'],
['4822783.0', '1.241869728'],
['4882200.0', '0.020564827'],
['4990000.0', '1.039327842'],
['5000000.0', '7.952729333'],
['5484986.0', '3.135996218'],
['5500000.0', '0.0017978'],
['5599999.0', '0.002999999'],
['5600000.0', '10.0'],
['5630000.0', '1.0'],
['5800000.0', '2.42049499'],
['6000000.0', '0.655766903'],
['6300000.0', '0.160333666'],
['6390000.0', '0.048907096'],
['6476000.0', '1.0'],
['6500000.0', '0.939687505'],
['7000000.0', '0.101833864'],
['7700000.0', '2.0'],
['8000000.0', '6.600558468'],
['8100000.0', '0.009727703'],
['9000000.0', '3.855295857'],
['9100000.0', '0.532860695'],
['10000000.0', '0.147315118'],
['11200000.0', '0.958219401'],
['12000000.0', '0.026746'],
['19999999.0', '0.005'],
['20000000.0', '0.859836342'],
['31700000.0', '0.008170286'],
['33000000.0', '0.039575557'],
['50000000.0', '0.079538945']],
'bids': [['1069231.0', '0.001247'],
['1069230.0', '4.122814507'],

14
['1060000.0', '0.047169916'],
['1059221.0', '0.808055043'],
['1057375.0', '0.0887'],
['1057317.0', '0.0671'],
['1056899.0', '0.0163'],
['1056890.0', '0.012'],
['1056858.0', '0.0931'],
['1056778.0', '0.0688'],
['1056729.0', '0.1875'],
['1056728.0', '0.0315'],
['1056692.0', '0.1229'],
['1056677.0', '0.0162'],
['1056000.0', '0.3'],
['1050000.0', '0.95095095'],
['1040065.0', '0.288443511'],
['1040000.0', '2.2029737'],
['1039343.0', '0.009621463'],
['1036588.0', '0.610851389'],
['1032000.0', '9.512639775'],
['1030695.0', '0.033428657'],
['1021499.0', '1.174744175'],
['1020000.0', '0.058823628'],
['1010000.0', '0.373987644'],
['1000234.0', '0.119372067'],
['1000000.0', '8.29584235'],
['995000.0', '0.010050252'],
['990000.0', '1.558080909'],
['985000.0', '6.052556305'],
['981000.0', '0.01529052'],
['950000.0', '0.115789474'],
['945000.0', '0.02380952'],
['938200.0', '0.140694948'],
['931395.0', '3.623239679'],
['915000.0', '0.010928962'],
['902850.0', '0.012064019'],
['900114.0', '0.055548519'],
['900100.0', '0.515961801'],
['900000.0', '3.452396978'],
['891125.0', '2.0'],
['885000.0', '2.016949153'],
['880000.0', '0.072089363'],
['865432.0', '0.057774614'],
['856000.0', '0.11682243'],
['855000.0', '0.02631579'],
['850101.0', '0.588763195'],
['850100.0', '0.588166098'],
['850000.0', '0.6'],

15
['833951.0', '0.119911122'],
['810000.0', '0.02777778'],
['805000.0', '0.02484472'],
['804646.0', '0.006213913'],
['800100.0', '1.24984377'],
['800001.0', '0.024999969'],
['800000.0', '6.399792988'],
['790000.0', '2.852053797'],
['785000.0', '0.897435897'],
['780000.0', '16.41636214'],
['775000.0', '0.903225806'],
['770500.0', '0.908500973'],
['770000.0', '0.909090909'],
['767000.0', '0.260756193'],
['765000.0', '0.94444444'],
['760000.0', '0.921052632'],
['755000.0', '0.966887417'],
['750501.0', '0.266746707'],
['750500.0', '0.932711526'],
['750100.0', '0.733235569'],
['750000.0', '2.829395933'],
['745000.0', '0.939597315'],
['740000.0', '0.094594595'],
['735550.0', '0.951668819'],
['735000.0', '0.952380952'],
['730500.0', '0.992907801'],
['730000.0', '0.962328768'],
['725000.0', '0.965517241'],
['720000.0', '1.003472222'],
['715000.0', '0.979020979'],
['712665.0', '19.533373984'],
['710000.0', '0.985915493'],
['705000.0', '0.992907801'],
['703230.0', '15.6526812'],
['700000.0', '10.187877828'],
['698450.0', '19.156704131'],
['676000.0', '0.157164201'],
['675000.0', '0.03333333'],
['650000.0', '15.0'],
['639023.0', '0.078244445'],
['635000.0', '1.57480315'],
['630000.0', '0.03571429'],
['622222.0', '0.160714343'],
['620000.0', '1.0'],
['603000.0', '0.04145937'],
['600001.0', '0.199998'],
['600000.0', '3.000000117'],

16
['585000.0', '0.03846154'],
['557000.0', '0.26929982'],
['550000.0', '0.610114546'],
['545000.0', '0.004587156'],
['540000.0', '0.04166667'],
['526000.0', '0.190114068'],
['500001.0', '1.213886972'],
['500000.0', '3.025'],
['495000.0', '0.04545455'],
['476000.0', '0.210084034'],
['450000.0', '1.25'],
['422222.0', '0.23684223'],
['420000.0', '1.19047619'],
['405000.0', '0.05555556'],
['400511.0', '0.049936206'],
['378500.0', '4.01822502'],
['365000.0', '0.006849315'],
['360000.0', '0.0625'],
['332100.0', '0.301114122'],
['315000.0', '0.07142857'],
['300001.0', '0.099999667'],
['300000.0', '0.008500767'],
['270000.0', '0.08333333'],
['261377.0', '0.003825891'],
['260420.0', '0.008327586'],
['250000.0', '2.0'],
['233000.0', '4.287553648'],
['232100.0', '0.430848772'],
['231000.0', '0.004329004'],
['225000.0', '0.1'],
['216010.0', '0.5'],
['202510.0', '1.5'],
['200001.0', '4.417623912'],
['189010.0', '1.0'],
['185710.0', '0.00538474'],
['183000.0', '2.732240437'],
['182304.0', '0.109706864'],
['180000.0', '0.125'],
['175510.0', '4.0'],
['172100.0', '0.581057525'],
['162010.0', '1.0'],
['162000.0', '0.011645741'],
['155000.0', '0.07'],
['148510.0', '9.0'],
['135010.0', '20.00590522'],
['135000.0', '0.16666667'],
['132001.0', '0.151514004'],

17
['130000.0', '0.006930538'],
['98300.0', '0.01017294'],
['90610.0', '0.011036309'],
['90000.0', '0.011111111'],
['80000.0', '0.451344375'],
['40003.0', '0.499962503'],
['40002.0', '14.999339783'],
['38000.0', '0.011497105'],
['12325.0', '0.02'],
['10000.0', '1.0'],
['9578.0', '1.000001044'],
['5538.0', '3.611412062'],
['5537.0', '1.000014448'],
['1401.0', '35.688793719'],
['1100.0', '0.93'],
['76.0', '12.289473684'],
['10.0', '1000.0'],
['9.0', '1111.111111111'],
['4.0', '125.0'],
['3.0', '1342.21'],
['2.0', '1339.225']],
'market_id': 'ETH-CLP'}

[23]: # Veamos la estructura de precios


prices['order_book']['asks']

[23]: [['1074997.0', '0.028506538'],


['1075000.0', '0.339712698'],
['1078400.0', '0.513479285'],
['1078404.0', '1.433947422'],
['1078450.0', '0.022441547'],
['1078550.0', '0.033'],
['1078650.0', '0.033'],
['1078750.0', '0.033'],
['1078800.0', '0.033'],
['1078850.0', '0.033'],
['1078900.0', '0.033'],
['1078940.0', '0.033'],
['1079000.0', '0.033'],
['1079050.0', '0.033'],
['1079100.0', '0.033'],
['1079150.0', '0.033'],
['1079200.0', '0.033'],
['1079250.0', '0.033'],
['1079300.0', '0.033'],
['1079325.0', '4.993'],
['1079350.0', '0.033'],

18
['1079400.0', '0.033'],
['1080000.0', '0.62168178'],
['1082000.0', '0.046373022'],
['1084000.0', '1.067645912'],
['1089999.0', '9.7008'],
['1090000.0', '8.062257929'],
['1091239.0', '0.5'],
['1100000.0', '2.886845853'],
['1105311.0', '0.9976514'],
['1113590.0', '0.009057975'],
['1115000.0', '3.203661702'],
['1116000.0', '2.10355709'],
['1119000.0', '9.97'],
['1120000.0', '2.716159819'],
['1130000.0', '0.149449089'],
['1138000.0', '0.035839721'],
['1139990.0', '0.996500003'],
['1140000.0', '2.860925234'],
['1145000.0', '2.04'],
['1149000.0', '1.0'],
['1150000.0', '6.895404425'],
['1155000.0', '8.11067511'],
['1155333.0', '0.03736727'],
['1160000.0', '1.6'],
['1169977.0', '1.0'],
['1175175.0', '2.0'],
['1179497.0', '0.09755574'],
['1180000.0', '4.322264691'],
['1187000.0', '0.038062948'],
['1198873.0', '0.02322747'],
['1199000.0', '0.100519409'],
['1199999.0', '0.53199604'],
['1200000.0', '23.088978191'],
['1200270.0', '0.011588341'],
['1203873.0', '0.02380787'],
['1205409.0', '0.1823798'],
['1212752.0', '0.09259259'],
['1215000.0', '0.03703704'],
['1215409.0', '0.18297792'],
['1218864.0', '0.04563422'],
['1220000.0', '0.5'],
['1222222.0', '1.0'],
['1223645.0', '0.090793955'],
['1230950.0', '0.28047894'],
['1239482.0', '0.04417765'],
['1242752.0', '0.09248717'],
['1250000.0', '0.399157774'],

19
['1250100.0', '0.002427395'],
['1250250.0', '1.0'],
['1260000.0', '0.03571428'],
['1278873.0', '0.02325206'],
['1279219.0', '1.0'],
['1299601.0', '0.074103612'],
['1300000.0', '3.521251261'],
['1305000.0', '0.03448276'],
['1310000.0', '0.113143648'],
['1313313.0', '2.0'],
['1320000.0', '0.159273455'],
['1333333.0', '2.0'],
['1350000.0', '1.90858745'],
['1351500.0', '0.007428901'],
['1375375.0', '2.000077179'],
['1380000.0', '0.073777778'],
['1385000.0', '0.0318726'],
['1394890.0', '0.21676825'],
['1395000.0', '0.03225806'],
['1399999.0', '0.5'],
['1400000.0', '0.300988457'],
['1420000.0', '0.7'],
['1440000.0', '0.08125'],
['1448951.0', '0.2782923'],
['1450000.0', '2.538491878'],
['1462009.0', '0.07270448'],
['1480000.0', '0.088832068'],
['1485000.0', '0.03030304'],
['1496900.0', '0.36938729'],
['1500000.0', '15.960333105'],
['1502726.0', '0.050706068'],
['1510000.0', '3.063643214'],
['1523000.0', '0.34655584'],
['1530000.0', '0.02941176'],
['1535000.0', '0.261765969'],
['1540000.0', '0.037650925'],
['1545000.0', '0.906715441'],
['1550000.0', '5.437289586'],
['1555691.0', '0.06790064'],
['1557455.0', '0.06790064'],
['1560000.0', '0.010646897'],
['1563000.0', '0.971018997'],
['1575000.0', '0.02857142'],
['1580000.0', '3.75434563'],
['1589912.0', '0.031863574'],
['1590000.0', '0.353589564'],
['1593500.0', '0.66129654'],

20
['1598578.0', '0.15275947'],
['1600000.0', '9.850586269'],
['1600022.0', '2.33953379'],
['1610000.0', '1.33984968'],
['1620000.0', '0.02777778'],
['1623556.0', '0.07099969'],
['1649999.0', '0.750769443'],
['1650000.0', '0.838260263'],
['1657000.0', '0.25786408'],
['1660000.0', '0.563451555'],
['1665000.0', '0.02702702'],
['1680000.0', '0.124564192'],
['1689000.0', '0.621383465'],
['1700000.0', '8.257959364'],
['1701000.0', '0.626729561'],
['1710000.0', '0.02631578'],
['1720000.0', '0.029186512'],
['1722151.0', '0.01605547'],
['1740000.0', '7.240296792'],
['1748752.0', '0.00332882'],
['1750000.0', '1.640669804'],
['1753544.0', '0.300369706'],
['1755000.0', '0.31633852'],
['1770000.0', '0.3272035'],
['1780000.0', '0.119'],
['1790000.0', '0.078600828'],
['1792850.0', '0.15'],
['1800000.0', '5.028951419'],
['1806000.0', '0.1'],
['1815000.0', '0.002765884'],
['1820878.0', '1.0'],
['1830000.0', '1.0'],
['1845000.0', '0.02439024'],
['1847808.0', '0.00679071'],
['1850000.0', '2.642471462'],
['1865000.0', '0.005383464'],
['1870000.0', '0.05637523'],
['1880000.0', '1.0'],
['1887911.0', '0.01609339'],
['1890000.0', '0.02380952'],
['1892000.0', '1.0'],
['1893969.0', '0.003011565'],
['1900000.0', '2.007926442'],
['1935000.0', '0.02325582'],
['1945000.0', '0.010324072'],
['1980000.0', '0.02272728'],
['1982278.0', '5.362618498'],

21
['1992426.0', '0.002736769'],
['2000000.0', '2.392022853'],
['2020000.0', '0.484663073'],
['2025000.0', '0.02222222'],
['2050000.0', '1.1'],
['2070000.0', '0.02173914'],
['2075000.0', '0.012096578'],
['2100000.0', '0.489968573'],
['2101918.0', '0.279138216'],
['2130765.0', '0.087267259'],
['2132857.0', '0.003168534'],
['2157949.0', '1.0'],
['2169000.0', '0.5'],
['2170000.0', '2.3'],
['2190000.0', '0.01377548'],
['2199000.0', '0.006848677'],
['2200000.0', '1.86835495'],
['2235000.0', '0.00982895'],
['2299999.0', '0.01166174'],
['2300000.0', '5.545975639'],
['2332205.0', '0.004081575'],
['2391880.0', '0.428082146'],
['2399000.0', '0.103396286'],
['2400000.0', '5.936851227'],
['2413977.0', '0.002154867'],
['2450000.0', '0.0097329'],
['2460571.0', '0.002154866'],
['2477777.0', '0.99685874'],
['2480000.0', '0.04860975'],
['2486881.0', '0.00229356'],
['2487454.0', '0.002091215'],
['2500000.0', '5.092907471'],
['2505000.0', '0.00875813'],
['2506170.0', '0.002175754'],
['2509000.0', '0.0480241'],
['2517603.0', '2.668048'],
['2520533.0', '0.002163356'],
['2550000.0', '0.04745238'],
['2551999.0', '0.1'],
['2552888.0', '0.002869809'],
['2560000.0', '0.89212405'],
['2567171.0', '0.002431844'],
['2596300.0', '0.75419388'],
['2600000.0', '0.991850772'],
['2600500.0', '0.003983204'],
['2605971.0', '0.001255609'],
['2620026.0', '0.001191393'],

22
['2630000.0', '0.04632558'],
['2650000.0', '0.250156475'],
['2655000.0', '3.219811389'],
['2660000.0', '0.04568808'],
['2660051.0', '0.00121574'],
['2667991.0', '0.002533003'],
['2668680.0', '0.00120391'],
['2700000.0', '0.822174019'],
['2737000.0', '0.007294381'],
['2747269.0', '0.053064913'],
['2750000.0', '1.993033487'],
['2756938.0', '0.003452769'],
['2784606.0', '2.669388'],
['2795738.0', '0.00766154'],
['2796790.0', '0.001148763'],
['2800000.0', '0.342567112'],
['2804400.0', '0.001145646'],
['2805000.0', '0.042931005'],
['2810000.0', '0.00156177'],
['2815000.0', '0.042778497'],
['2820000.0', '0.01116592'],
['2825000.0', '0.042627069'],
['2835000.0', '0.042476709'],
['2840000.0', '0.001060577'],
['2845000.0', '0.042327406'],
['2850000.0', '0.760568589'],
['2855000.0', '0.042179149'],
['2860000.0', '0.140351367'],
['2865000.0', '0.042031927'],
['2870000.0', '0.035034331'],
['2875000.0', '0.041885729'],
['2885000.0', '0.041740544'],
['2889960.0', '0.35286781'],
['2895000.0', '0.041596363'],
['2900000.0', '7.231385868'],
['2902859.0', '0.005948987'],
['2909000.0', '1.000936831'],
['2931467.0', '0.157911028'],
['2945000.0', '0.687479118'],
['2950000.0', '0.04422736'],
['2959150.0', '0.106397525'],
['2983000.0', '2.5'],
['2990585.0', '0.005774479'],
['2999368.0', '0.438640118'],
['3000000.0', '6.693766958'],
['3010000.0', '0.254890398'],
['3030000.0', '3.31790153'],

23
['3048744.0', '0.183856428'],
['3050000.0', '1.001994282'],
['3051000.0', '0.368197348'],
['3056999.0', '0.10798966'],
['3064320.0', '0.043882895'],
['3096000.0', '0.362845643'],
['3100000.0', '2.158703294'],
['3104771.0', '0.002869658'],
['3129038.0', '0.005775667'],
['3140000.0', '0.357761182'],
['3150000.0', '0.557839846'],
['3157949.0', '1.0'],
['3185000.0', '0.352706471'],
['3200000.0', '2.309316189'],
['3230000.0', '0.347792604'],
['3240000.0', '0.009296444'],
['3244000.0', '2.5'],
['3270000.0', '0.013805603'],
['3275000.0', '0.284615778'],
['3278000.0', '0.03214516'],
['3285000.0', '0.158231836'],
['3289903.0', '0.01994375'],
['3300000.0', '4.011884491'],
['3347220.0', '0.03507541'],
['3370000.0', '0.09388248'],
['3400000.0', '0.140315147'],
['3427000.0', '2.0'],
['3450000.0', '0.510255944'],
['3463000.0', '0.048663938'],
['3463900.0', '1.0'],
['3465899.0', '0.37943881'],
['3480000.0', '0.01071512'],
['3482000.0', '0.335823499'],
['3488996.0', '0.143883235'],
['3490000.0', '0.001447668'],
['3499999.0', '0.008024402'],
['3500000.0', '7.937905572'],
['3500003.0', '0.194408604'],
['3505000.0', '2.5'],
['3510000.0', '0.682253184'],
['3519000.0', '0.00314096'],
['3530000.0', '0.01291309'],
['3552000.0', '0.014135372'],
['3560000.0', '2.261120401'],
['3575000.0', '0.58445752'],
['3580000.0', '0.099233234'],
['3582251.0', '0.159871594'],

24
['3595000.0', '0.00739158'],
['3599680.0', '0.312074993'],
['3600000.0', '9.730725821'],
['3610000.0', '0.01001326'],
['3619840.0', '0.484242115'],
['3630000.0', '0.00655507'],
['3640000.0', '0.00621185'],
['3650000.0', '0.013753644'],
['3672321.0', '0.001389561'],
['3680000.0', '0.02145289'],
['3690000.0', '0.55027624'],
['3695000.0', '0.078095531'],
['3700000.0', '0.190849128'],
['3712000.0', '0.07773011'],
['3718000.0', '0.718999854'],
['3720000.0', '0.1993'],
['3727000.0', '0.154832196'],
['3729784.0', '0.035'],
['3730000.0', '0.09965'],
['3734000.0', '0.47848986'],
['3745000.0', '0.006702377'],
['3766000.0', '2.5'],
['3772321.0', '0.001358422'],
['3780000.0', '0.177725'],
['3795326.0', '0.004064646'],
['3800000.0', '4.75117368'],
['3807333.0', '1.940280152'],
['3811000.0', '0.3'],
['3815000.0', '0.286575117'],
['3820000.0', '1.582539712'],
['3830000.0', '0.749389137'],
['3847000.0', '0.997'],
['3849000.0', '0.765631562'],
['3850000.0', '0.62625816'],
['3860000.0', '2.65292563'],
['3870000.0', '0.02501642'],
['3870001.0', '0.155057'],
['3885000.0', '0.257715489'],
['3890543.0', '0.047260683'],
['3900000.0', '0.192404272'],
['3916001.0', '1.083647252'],
['3918750.0', '0.05'],
['3920000.0', '1.252397041'],
['3922875.0', '0.05'],
['3927000.0', '0.05'],
['3930000.0', '0.01232532'],
['3931125.0', '0.05'],

25
['3935250.0', '0.05'],
['3939375.0', '0.05'],
['3943500.0', '0.05'],
['3946000.0', '0.1510006'],
['3947625.0', '0.05'],
['3950000.0', '0.540382311'],
['3951750.0', '0.05'],
['3955875.0', '0.05'],
['3957000.0', '0.013212974'],
['3960000.0', '0.05'],
['3964125.0', '0.05'],
['3966496.0', '1.0286472'],
['3968250.0', '0.05'],
['3972375.0', '0.05'],
['3974880.0', '0.027908476'],
['3976500.0', '0.05'],
['3980000.0', '0.0026535'],
['3980625.0', '0.05'],
['3984750.0', '0.05'],
['3985000.0', '0.3'],
['3988875.0', '0.05'],
['3993000.0', '0.05'],
['3997125.0', '0.05'],
['3998769.0', '0.276806248'],
['3998787.0', '0.518293159'],
['3999000.0', '0.007174242'],
['3999799.0', '0.036404087'],
['3999998.0', '0.1993'],
['3999999.0', '2.056257395'],
['4000000.0', '11.722411595'],
['4001250.0', '0.05'],
['4002342.0', '0.001439884'],
['4005375.0', '0.05'],
['4007500.0', '0.027628752'],
['4009500.0', '0.05'],
['4013125.0', '0.05'],
['4017750.0', '0.05'],
['4021875.0', '0.05'],
['4026000.0', '0.05'],
['4027000.0', '2.5'],
['4030000.0', '1.0'],
['4030125.0', '0.05'],
['4034250.0', '0.05'],
['4038375.0', '0.05'],
['4040000.0', '0.002483941'],
['4042500.0', '0.05'],
['4046625.0', '0.05'],

26
['4050000.0', '0.15'],
['4050750.0', '0.05'],
['4054875.0', '0.05'],
['4059000.0', '0.05'],
['4061750.0', '0.027259734'],
['4063125.0', '0.05'],
['4067250.0', '0.05'],
['4071375.0', '0.05'],
['4075500.0', '0.05'],
['4079625.0', '0.05'],
['4083750.0', '0.05'],
['4087650.0', '0.027028127'],
['4087875.0', '0.05'],
['4092000.0', '0.05'],
['4096125.0', '0.05'],
['4100000.0', '4.969107085'],
['4100250.0', '0.05'],
['4104375.0', '0.05'],
['4105000.0', '0.1'],
['4108500.0', '0.05'],
['4109637.0', '0.026207412'],
['4112625.0', '0.05'],
['4116750.0', '0.05'],
['4120875.0', '0.050481486'],
['4139685.0', '0.027070661'],
['4150000.0', '0.15'],
['4156163.0', '0.0243'],
['4167800.0', '0.026453061'],
['4199999.0', '13.973941378'],
['4200000.0', '3.730745996'],
['4249575.0', '0.026476321'],
['4250000.0', '0.008'],
['4288000.0', '2.5'],
['4300000.0', '0.30977179'],
['4310000.0', '0.99852659'],
['4375852.0', '0.025584109'],
['4400000.0', '0.06944444'],
['4500000.0', '5.900283843'],
['4549000.0', '2.5'],
['4721490.0', '0.072082667'],
['4800000.0', '4.727268542'],
['4810000.0', '2.5'],
['4822783.0', '1.241869728'],
['4882200.0', '0.020564827'],
['4990000.0', '1.039327842'],
['5000000.0', '7.952729333'],
['5484986.0', '3.135996218'],

27
['5500000.0', '0.0017978'],
['5599999.0', '0.002999999'],
['5600000.0', '10.0'],
['5630000.0', '1.0'],
['5800000.0', '2.42049499'],
['6000000.0', '0.655766903'],
['6300000.0', '0.160333666'],
['6390000.0', '0.048907096'],
['6476000.0', '1.0'],
['6500000.0', '0.939687505'],
['7000000.0', '0.101833864'],
['7700000.0', '2.0'],
['8000000.0', '6.600558468'],
['8100000.0', '0.009727703'],
['9000000.0', '3.855295857'],
['9100000.0', '0.532860695'],
['10000000.0', '0.147315118'],
['11200000.0', '0.958219401'],
['12000000.0', '0.026746'],
['19999999.0', '0.005'],
['20000000.0', '0.859836342'],
['31700000.0', '0.008170286'],
['33000000.0', '0.039575557'],
['50000000.0', '0.079538945']]

2.3 WEBSOCKETS
A diferencia de una API REST, los websockets nos permiten funcionar como una radio. Una vez
conectados al canal, simplemente escuchamos los cambios de eventos. Optimiza el uso de recursos,
nos acercamos a frecuencia real-time y no pierde datos entre requests. Basado en eventos.

[2]: import requests


import base64
import hmac
import time
import requests.auth
import websocket

class BudaHMACAuth(requests.auth.AuthBase):
"""Adjunta la autenticación HMAC de Buda al objeto Request."""

def __init__(self, api_key: str, secret: str):


self.api_key = api_key
self.secret = secret

def get_nonce(self) -> str:

28
# 1. Generar un nonce (timestamp en microsegundos)
return str(int(time.time() * 1e6))

def sign(self, r, nonce: str) -> str:


# 2. Preparar string para firmar
components = [r.method, r.path_url]
if r.body:
encoded_body = base64.b64encode(r.body).decode()
components.append(encoded_body)
components.append(nonce)
msg = ' '.join(components)
# 3. Obtener la firma
h = hmac.new(key=self.secret.encode(),
msg=msg.encode(),
digestmod='sha384')
signature = h.hexdigest()
return signature

def __call__(self, r):


nonce = self.get_nonce()
signature = self.sign(r, nonce)
# 4. Adjuntar API-KEY, nonce y firma al header del request
r.headers['X-SBTC-APIKEY'] = self.api_key
r.headers['X-SBTC-NONCE'] = nonce
r.headers['X-SBTC-SIGNATURE'] = signature
return r

url = f'https://www.buda.com/api/v2/me'

# PRIVATE KEYS

pubsub_key = ""

# wss://realtime.buda.com/sub?channel=[channel]
# wss://realtime.buda.com/sub?channel=[channel]@[pubsub_key]

# Libro de Órdenes
# market_id = ethbtc, btcclp, etc

def on_message(ws, message):


print(message)
ws.close()
print("socket-closed")

def on_open(ws):

29
print("socket-connected")

def channels():
pubsub_key = "pubsub_key"
chs = [
"book%40btcclp",
"book%40ethclp",
"trades%40btcclp",
f"balances%40{pubsub_key}",
f"orders%40{pubsub_key}"
]
return ",".join(chs)

def main():
SOCKET = f"wss://realtime.buda.com/sub?channel={channels()}"
ws = websocket.WebSocketApp(SOCKET, on_message = on_message)
ws.on_open = on_open
ws.run_forever(ping_interval=10)

if __name__ == "__main__":
main()

socket-connected
{"mk":"ETH-CLP","ts":"1672855950.805219","ev":"book-
changed","change":["asks","1092429.37","7.858379"]}
socket-closed

2.4 FIX
El protocolo más utilizado en producción es el FIX. Financial Information Exchange Protocol.
Se basa en un diccionario y tags, ejemplo el tag 44 = 90.5 es el precio equivalente a 90.5.
1) FIX Protocol Architecture
– Session Layer - connectivity, handshaking, data integrity & message sequencing
– Application Layer - order execution, market data subscription, other business-specific func-
tionality, etc
– FIX Connection Types (Pricing, Trading)
2) FIX Field Structure (name, number, value, e.g. 8=FIX.4.4)
3) FIX Message Structure (Standard Header, Message Body, Standard Trailer)

2.5 THREADDING
El principal problema que nos enfrentamos cuando avanzamos en la programación, es que debemos
olvidarnos la estructura inicial y pensar en eventos.

30
• En python, si avanzamos secuencial, en breve vamos a ver como quedamos con market data
y ruteo encolado.
• Es por aquello que vamos a crear hilos.
• Cada hilo o thread va a ser encargado de llevar una tarea. Puede ser un libro, market data,
mandar órdenes etc.
• En general lo usaremos para escuchar varios channel de un websocket.
• En memoria, estará en cada clase los precios del activo, así podemos llamarlos desde otros
hilos.

[6]: import time


import datetime
import logging
import sys

import json
import threading
from websocket import create_connection

class mktdata(threading.Thread):

def __init__(self, ticket):


threading.Thread.__init__(self)
self.ticket = ticket
self.ws = create_connection("wss://ws.bitso.com") # URL puede ser param
self.bid = None
self.ask = None

def subscribe(self):
self.x = self.ws.send(json.dumps({"action": "subscribe","type":␣
↪"orders","book": self.ticket})) # libro tiene q ser un param Y UN THREAD

while True:
self.result = self.ws.recv()
self.result = json.loads(self.result)
if 'payload' in self.result.keys():
self.bid = self.result['payload']['bids'][0]['r']
self.ask = self.result['payload']['asks'][0]['r']
print("Received %s %.9f - %.9f" % (self.ticket, self.bid, self.
↪ask))
else:
print("Socket %s : %s " % (self.ticket, self.result))

def unsubscribe(self):
self.ws.send(json.dumps({"action": "unsubscribe","type":␣
↪"orders","book": self.ticket})) #¿como le paso cual RECV parar?

print("%s Unsubscribed" % (self.ticket))

31
def run(self):
self.subscribe()

if __name__ == "__main__":

ticketA = mktdata("eth_btc")
ticketA.start()

print("Subscribe mkt data 1")

ticketB = mktdata("eth_mxn")
ticketB.start()

print("Subscribe mkt data 2")

print(ticketA.bid)
time.sleep(1)
print(ticketA.bid)
time.sleep(1)
print(ticketA.bid)
time.sleep(1)
print(ticketB.ask)
ticketA.unsubscribe()
ticketB.unsubscribe()

Subscribe mkt data 1


Socket eth_btc : {'action': 'subscribe', 'response': 'ok', 'time':
1672855047671, 'type': 'orders'}
Received eth_btc 0.074582000 - 0.074788000
Subscribe mkt data 2
0.074582
Socket eth_mxn : {'action': 'subscribe', 'response': 'ok', 'time':
1672855048246, 'type': 'orders'}
Received eth_btc 0.074582000 - 0.074729000
Received eth_btc 0.074582000 - 0.074729000
Received eth_btc 0.074582000 - 0.074728000
0.074582
Received eth_mxn 24380.000000000 - 24442.000000000
Received eth_mxn 24381.000000000 - 24442.000000000
Received eth_mxn 24381.000000000 - 24442.000000000
Received eth_mxn 24381.000000000 - 24442.000000000
Received eth_mxn 24381.000000000 - 24441.000000000
Received eth_mxn 24382.000000000 - 24441.000000000
Received eth_mxn 24382.000000000 - 24441.000000000
0.074582
Received eth_mxn 24382.000000000 - 24441.000000000

32
Received eth_mxn 24382.000000000 - 24441.000000000
Received eth_mxn 24382.000000000 - 24445.000000000
Received eth_mxn 24382.000000000 - 24445.000000000
Received eth_mxn 24382.000000000 - 24444.000000000
Received eth_mxn 24383.000000000 - 24444.000000000
Received eth_mxn 24383.000000000 - 24440.000000000
Received eth_mxn 24383.000000000 - 24440.000000000
Received eth_mxn 24383.000000000 - 24440.000000000
Received eth_mxn 24383.000000000 - 24440.000000000
Received eth_mxn 24383.000000000 - 24440.000000000
Received eth_mxn 24384.000000000 - 24440.000000000
Received eth_mxn 24384.000000000 - 24439.000000000
Received eth_mxn 24384.000000000 - 24439.000000000
Received eth_mxn 24384.000000000 - 24439.000000000
Received eth_mxn 24384.000000000 - 24439.000000000
Received eth_mxn 24384.000000000 - 24445.000000000
Received eth_mxn 24384.000000000 - 24438.000000000
Received eth_mxn 24384.000000000 - 24438.000000000
Received eth_mxn 24385.000000000 - 24438.000000000
Received eth_mxn 24385.000000000 - 24438.000000000
Received eth_mxn 24385.000000000 - 24437.000000000
Received eth_mxn 24386.000000000 - 24437.000000000Received eth_btc 0.074582000 -
0.074728000

Received eth_mxn 24386.000000000 - 24437.000000000


Received eth_mxn 24386.000000000 - 24437.000000000
Received eth_mxn 24386.000000000 - 24437.000000000
24437
eth_btc Unsubscribed
eth_mxn Unsubscribed
Received eth_mxn 24386.000000000 - 24437.000000000
Received eth_mxn 24386.000000000 - 24441.000000000
Received eth_btc 0.074582000 - 0.074728000
Received eth_mxn 24386.000000000 - 24440.000000000
Received eth_mxn 24386.000000000 - 24436.000000000
Received eth_mxn 24386.000000000 - 24436.000000000
Socket eth_btc : {'action': 'unsubscribe', 'response': 'ok', 'time':
1672855051248, 'type': 'orders'}
Socket eth_mxn : {'action': 'unsubscribe', 'response': 'ok', 'time':
1672855051251, 'type': 'orders'}
Exception in thread Thread-6:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\threading.py", line 917, in
_bootstrap_inner
self.run()
File "<ipython-input-6-c97bbae08fc4>", line 38, in run
self.subscribe()

33
File "<ipython-input-6-c97bbae08fc4>", line 24, in subscribe
self.result = self.ws.recv()
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_core.py", line
314, in recv
opcode, data = self.recv_data()
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_core.py", line
331, in recv_data
opcode, frame = self.recv_data_frame(control_frame)
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_core.py", line
344, in recv_data_frame
frame = self.recv_frame()
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_core.py", line
378, in recv_frame
return self.frame_buffer.recv_frame()
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_abnf.py", line
361, in recv_frame
self.recv_header()
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_abnf.py", line
309, in recv_header
header = self.recv_strict(2)
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_abnf.py", line
396, in recv_strict
bytes_ = self.recv(min(16384, shortage))
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_core.py", line
453, in _recv
return recv(self.sock, bufsize)
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_socket.py", line
115, in recv
"Connection is already closed.")
websocket._exceptions.WebSocketConnectionClosedException: Connection is already
closed.

Exception in thread Thread-7:


Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\threading.py", line 917, in
_bootstrap_inner
self.run()
File "<ipython-input-6-c97bbae08fc4>", line 38, in run
self.subscribe()
File "<ipython-input-6-c97bbae08fc4>", line 24, in subscribe
self.result = self.ws.recv()
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_core.py", line
314, in recv
opcode, data = self.recv_data()
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_core.py", line
331, in recv_data
opcode, frame = self.recv_data_frame(control_frame)
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_core.py", line

34
344, in recv_data_frame
frame = self.recv_frame()
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_core.py", line
378, in recv_frame
return self.frame_buffer.recv_frame()
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_abnf.py", line
361, in recv_frame
self.recv_header()
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_abnf.py", line
309, in recv_header
header = self.recv_strict(2)
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_abnf.py", line
396, in recv_strict
bytes_ = self.recv(min(16384, shortage))
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_core.py", line
453, in _recv
return recv(self.sock, bufsize)
File "C:\ProgramData\Anaconda3\lib\site-packages\websocket\_socket.py", line
115, in recv
"Connection is already closed.")
websocket._exceptions.WebSocketConnectionClosedException: Connection is already
closed.

2.6 FAST Binary Trees


Una de las formas más eficientes de manejar market data es utilizando la siguiente estructura:
1. Generar un snapshot del libro inicial
2. Escuchar los incrementales
3. Ir ubicando los incrementales en la posición del libro correspondiente: delete, new (insert),
update.
4. Mantener sincronizado el libro en memoria
El punto 3 requiere ir ubicando cada dato dentro de un arreglo de bids y uno de asks. Existen
formas más eficientes de manejar la data como Árboles AVL.

[ ]: !conda install -c conda-forge bintrees

[ ]: !pip install bintrees

[5]: # Requires custom dependencies


from srcorderbook.order_book import OrderBook

'''
Basic liquidity providing principles
'''

book = OrderBook()

35
# 0 0 0
print("Initial Book is empty")
print("Bid Size - Ask Size - Volume Traded")
print(book.bid_size, book.ask_size, book.total_volume_traded)

# LMT sell 1900pcs @11.5


print("New Order SELL")
book.submit_order('lmt', 'ask', 1900, 11.5, 1)

# LMT buy 1500pcs @10.5


print("New Order BID")
book.submit_order('lmt', 'bid', 1500, 10.5, 2)

# bid ask total_traded = 0


print("Bid Size - Ask Size - Volume Traded")
print(book.bid_size, book.ask_size, book.total_volume_traded)

# Satisfies market demand on both sides


print("Let's send market orders on both sides.")
book.submit_order('lmt', 'bid', 1000, 20, 3)
book.submit_order('lmt', 'ask', 500, 5, 3)

# Print Final Book


print("Bid Size - Ask Size - Volume Traded")
print(book.bid_size, book.ask_size, book.total_volume_traded)

Initial Book is empty


Bid Size - Ask Size - Volume Traded
0 0 0
New Order SELL
New Order BID
Bid Size - Ask Size - Volume Traded
1500 1900 0
Let's send market orders on both sides.
Bid Size - Ask Size - Volume Traded
1000 900 1500

[4]: '''
Market microstructure
---
Implementation is not making concrete orders public
only order sizes on given level
'''

book = OrderBook()

36
# Get 5 levels of order book
# [[], []]
print("Get 5 levels of order book // Empty")
print(book.get_mkt_depth(5))

book.submit_order('lmt', 'ask', 1500, 12, 1)


book.submit_order('lmt', 'ask', 4000, 12.5, 1)
book.submit_order('lmt', 'ask', 6000, 13.0, 1)

book.submit_order('lmt', 'bid', 1500, 9.9, 2)


book.submit_order('lmt', 'bid', 5000, 9.89, 2)
book.submit_order('lmt', 'bid', 8000, 9.87, 2)

# [[[Price, Size] * n], [[Price, Size] * n]]


print("Get 5 levels of order book")
print(book.get_mkt_depth(5))

# ([IDs], [{ID: (size, side, priority_id)}])


# OrderBook respects time priority. It means that if
# trader A submitted LMT buy @10 and trader B did same
# then trader A is traded first

print(book.get_participant_orders(1))

Get 5 levels of order book // Empty


[[], []]
Get 5 levels of order book
[[[12, 1500], [12.5, 4000], [13.0, 6000]], [[9.9, 1500], [9.89, 5000], [9.87,
8000]]]
([1, 2, 3], {1: (12, 'ask', 1500), 2: (12.5, 'ask', 4000), 3: (13.0, 'ask',
6000)})

[2]: !export PATH=/Library/TeX/texbin:$PATH

[ ]:

37

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy