Files
ai-econ/db/export_to_mongo.py
T

58 lines
1.7 KiB
Python

from pymongo import MongoClient
import datetime
cmt="current_market_trades"
cma="current_market_asks"
cmb="current_market_bids"
class MongoExporter():
def __init__(self,conn_string,db,econ) -> None:
# Provide the mongodb atlas url to connect python to mongodb using pymongo
# Create a connection using MongoClient. You can import MongoClient or use pymongo.MongoClient
client = MongoClient(conn_string)
# Create the database for our example (we will use the same database throughout the tutorial
self.client=client[db]
self.econ=econ
def reset_market(self):
"""Resets the current market data in the analytics stack"""
c=self.client
c[cmt].drop()
c[cma].drop()
c[cmb].drop()
def submit_full_market(self):
c=self.client
market=self.econ.get_component("ContinuousDoubleAuction")
self.reset_market()
base=datetime.datetime.fromtimestamp(0)
delta=datetime.timedelta(days=1)
for i in range(len(market.executed_trades)):
step=market.executed_trades[i]
timestep=base+(i*delta)
if len(step)>0:
for transaction in step:
transaction["step"]=i
c[cmt].insert_one(transaction)
for com,asks in market.asks.items():
for ask in asks:
ask["commodity"]=com
c[cma].insert_one(ask)
for com,bids in market.bids.items():
for bid in bids:
bid["commodity"]=com
c[cmb].insert_one(bid)
# This is added so that many files can reuse the function get_database()