Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions components/cruize_data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@
from components import FirebaseDataManager
from utilities import cruize_constants

"""
class :: CruizeDataManager - is used to manage the db data operation for backend. such as storing data on db, getting data form db etc.
"""


class CruizeDataManager(object):

"""
method :: save_asset_tvl - is used to store total TLV of an asset.
params :: asset_data - contain the details of asset TLV.
return :: None
"""

def save_asset_tvl(self, asset_data):
firebase_db_manager_obj = FirebaseDataManager()
asset_tvl_data = firebase_db_manager_obj.fetch_data(
Expand Down Expand Up @@ -32,6 +43,12 @@ def save_asset_tvl(self, asset_data):
data=tvl_data,
)

"""
method :: fetch_user_transactions - is used to fetch the user's transaction from DB .
params :: user_data - contain the details of user's such as user's wallet address.
return :: user's transaction.
"""

def fetch_user_transactions(self, user_data):
firebase_db_obj = FirebaseDataManager()
firebase_data = firebase_db_obj.fetch_sub_collections(
Expand Down
78 changes: 67 additions & 11 deletions components/dydx_order_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import time
from datetime import datetime, timedelta

import pandas
from dateutil.relativedelta import relativedelta
from dydx3 import constants
Expand All @@ -15,7 +14,10 @@
from utilities.enums import AssetCodes


# class: DydxOrderManager - is responsible for managing order on dydx.
"""class::DydxOrderManager is responsible for managing operation on Dydx
such us opening and closing order on dydx, calculating open and close price for position,
computing market volatility for an asset etc, to perform all of these task there are function written is the class
"""


class DydxOrderManager:
Expand All @@ -27,16 +29,27 @@ def __init__(self, dydx_client=None):
self.firebase_data_manager_obj = FirebaseDataManager()
self.dydx_client = dydx_client

"""
method :: create_order - is used to open and close orders on dydx.
params :: order_params - contains all the necessary parameter to open an order on dydx.
params :: dydx_client - is a dydx client. with the help of this
client we perform operation on dydx it's different for the different asset.
"""

def create_order(self, order_params, dydx_client):
dydx_order = DydxOrder()
# we have to keep separate volume of btc and eth to open different position on dydx.
# total_btc_volume * 5 --> would be the open size for the btc
# total_eth_volume * 5 --> would be the open size for eth
order_information = dydx_order.create_order(order_params, dydx_client)
dydx_order_details = vars(order_information)
dydx_order_details = dydx_order_details["data"]["order"]
return dydx_order_details

"""
method :: calculate_open_close_price - is used to calculate the open and close price for orders on dydx.
params :: asset_pair - is the pair of asset such as ETH-USD,BTC-USD.
params :: eth_order_size - it is the size of the order for which we have to calculate the open and close price.
params :: symbol - symbol is the sign for the asset ETH-USD,LINK-USD.
"""

def calculate_open_close_price(self, asset_pair, eth_order_size, symbol):
bids_consumed = 0
dydx_order_obj = DydxOrder()
Expand Down Expand Up @@ -116,10 +129,10 @@ def create_order_params(self, side, market, size, market_price, dydx_client):
return order_params

"""
:method - calculate_asst_volume.
:params - market_price:current market price of asset.
:params - total_volume:total volume of asset.
:return - asset total value in usd.
method :: get_asset_price_and_size - is used to get the total equity of an asset multiply by leverage and the market price of an asset.
params :: asset_address - is the chainlink Oracal address .
params :: dydx_client - is a dydx client. with the help of this client we perform operation on dydx it's different for the different asset.
return :: asset total equity in dydx multiply by leverage and the market price of the asset.
"""

def get_asset_price_and_size(self, asset_address, dydx_client):
Expand All @@ -136,6 +149,12 @@ def get_asset_price_and_size(self, asset_address, dydx_client):

return asset_details

"""
method :: compute_market_volatility - is used compute the market volatility for an asset.
params :: prices_data - an array of 6 months price data.
return :: sigma ema and mu ema for an given asset price data.
"""

def compute_market_volatility(self, prices_data):
volatility_data = {"sigma_ema": None, "mu_ema": None}
percentage_EMA = []
Expand All @@ -149,6 +168,11 @@ def compute_market_volatility(self, prices_data):
volatility_data["mu_ema"] = float(mu_ema)
return volatility_data

"""
method :: market_volatility - is used compute the market volatility for an asset.
params :: symbol - symbol of an asset such as ETH-USD, BTC-USD.
"""

def market_volatility(self, symbol):
volatility_data = {}
months_price_data = []
Expand Down Expand Up @@ -223,6 +247,13 @@ def market_volatility(self, symbol):
data={"ema": volatility_data}, document=symbol, collection_name="ema_data"
)

"""
method :: position_status - is used to get open position status on dydx for an asset.
params :: collection_name - collection for the asset ex ETHBUSD,BTCBUSD.
params :: symbol - symbol of an asset such as ETH-USD, BTC-USD.
return :: True if the short position is open on dydx else False.
"""

def position_status(self, collection_name, symbol):

# fetch data from db
Expand All @@ -238,6 +269,14 @@ def position_status(self, collection_name, symbol):
return False
return position_status[symbol]

"""
method :: set_position_status - is used to set open position status on dydx for an asset.
params :: collection_name - collection for the asset ex ETHBUSD,BTCBUSD.
params :: symbol - symbol of an asset such as ETH-USD, BTC-USD.
params :: status - status of an position ex. True or false.
return :: None.
"""

def set_position_status(self, collection_name, symbol, status):
# store data to db
self.firebase_data_manager_obj.store_data(
Expand All @@ -248,6 +287,13 @@ def deposit_test_fund(self, dydx_client):
dydx_p_client = dydx_client["dydx_instance"]
return dydx_p_client.private.request_testnet_tokens()

"""
method :: deposit_to_dydx - is used to deposit USDC to dydx.
params :: amount - USDC value in number.
params ::dydx_client - is a dydx client. with the help of this client we perform operation on dydx it's different for the different asset.
return :: transaction hash for deposit transaction.
"""

def deposit_to_dydx(self, amount, dydx_client):
try:
dydx_p_client = dydx_client["dydx_instance"]
Expand All @@ -264,6 +310,13 @@ def deposit_to_dydx(self, amount, dydx_client):
except Exception as e:
raise Exception(e)

"""
method :: withdraw_from_dydx - is used to withdraw USDC from dydx.
params :: recipient_address - the account in which we want to withdraw fund.
params ::dydx_client - is a dydx client. with the help of this client we perform operation on dydx it's different for the different asset.
return :: transaction hash for deposit transaction.
"""

def withdraw_from_dydx(self, recipient_address, dydx_client):
try:
dydx_p_client = dydx_client["dydx_instance"]
Expand All @@ -278,8 +331,11 @@ def withdraw_from_dydx(self, recipient_address, dydx_client):
except Exception as e:
raise Exception(e)

# open order on dydx celery task algo
# asset type -- ETH-USD,BTC-USD etc
"""
method :: open_order_on_dydx - is used to open orders on dydx. it has algorithm to open order's on dydx
params :: order_details - contains all the necessary parameter to open an order on dydx.
"""

def open_order_on_dydx(self, order_details):
dydx_asset_instance = asset_dydx_instance[order_details["asset_pair"]]
dydx_order_manager = DydxOrderManager(dydx_asset_instance)
Expand Down
42 changes: 42 additions & 0 deletions components/firebase_data_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,46 @@
from services.firebase_cloud_client import FirebaseClient
from settings_config import firebase_client

"""class :: FirebaseDataManager is used to manage the firebase db oprations such as storing,fetching and updating the
data. """


class FirebaseDataManager(object):
def __init__(self):
self.firebase_client = firebase_client

"""method :: get_firebase_client - is used to get the firebase client.
return :: firebase client.
"""

def get_firebase_client(self):
if self.firebase_client is None:
self.firebase_client = FirebaseClient().get_firebase_client
return self.firebase_client

"""
method :: update_data - is used to update the data on firebase db
return :: None.
"""

def update_data(self, order_id, collection, data):
self.firebase_client = self.get_firebase_client()
self.firebase_client.collection(collection).document(order_id).update(data)

"""
method :: store_data - is used to store the data on firebase db
return :: None.
"""

def store_data(self, data, document, collection_name):
self.firebase_client = self.get_firebase_client()
self.firebase_client.collection(collection_name).document(document).set(data)

"""
method :: bulk_store - is used to store the data on firebase db in bulk .
return :: None.
"""

def bulk_store(self, data, collection_name, field):
self.firebase_client = self.get_firebase_client()
batch = self.firebase_client.batch()
Expand All @@ -30,6 +52,11 @@ def bulk_store(self, data, collection_name, field):
batch.set(write, {field: str(value)})
batch.commit()

"""
method :: store_sub_collections - is used to store sub_collections the data on firebase db .
return :: None.
"""

def store_sub_collections(
self, data, collection, document_name, sub_collection, sub_document
):
Expand All @@ -39,6 +66,11 @@ def store_sub_collections(
sub_collection
).document(sub_document).set(data)

"""
method :: fetch_sub_collections - is used to fetch sub_collections from the firebase db .
return :: data of sub collection.
"""

def fetch_sub_collections(self, collection, document_name, sub_collection):
self.firebase_client = self.get_firebase_client()
firebase_data = (
Expand All @@ -49,6 +81,11 @@ def fetch_sub_collections(self, collection, document_name, sub_collection):
)
return firebase_data

"""
method :: fetch_data - is used to fetch data from the firebase db .
return :: collection data.
"""

def fetch_data(self, collection_name, document_name):
self.firebase_client = self.get_firebase_client()
firebase_data = (
Expand All @@ -60,6 +97,11 @@ def fetch_data(self, collection_name, document_name):
firebase_data = vars(firebase_data)
return firebase_data.get("_data", None)

"""
method :: fetch_collections - is used to fetch collections from the firebase db .
return :: collection .
"""

def fetch_collections(self, collection_name):
self.firebase_client = self.get_firebase_client()
collection_obj = self.firebase_client.collection(collection_name)
Expand Down
22 changes: 21 additions & 1 deletion components/price_floor_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
from services.market_data.coingecko import CoinGecko

firebase_data_manager_obj = FirebaseDataManager()
"""class :: PriceFloorManager is used to manage the price floor data such a setting up price floor , retrieving price floor.
etc.
"""


class PriceFloorManager:
"""
method :: set_price_floor - is used to set the price floor of an given asset.
params :: asset_name - name of the asset such as bitcoin etc.
params :: number_of_days - for how much time you want to calculate the price floor.
"""

def set_price_floor(self, asset_name, number_of_days=30):

coin_gecko = CoinGecko()
Expand All @@ -18,7 +27,7 @@ def set_price_floor(self, asset_name, number_of_days=30):
prices.sort(reverse=True)
asset_peak_price = prices[0]
current_price_floor = asset_peak_price * 0.60
# TODO: if perivous_price_floor < current_price_floor : update the price floor, else don't update
# TODO: if pervious_price_floor < current_price_floor : update the price floor, else don't update
# get price floor
firebase_data_manager_obj.store_data(
data={
Expand All @@ -32,13 +41,24 @@ def set_price_floor(self, asset_name, number_of_days=30):
except Exception as e:
raise Exception(e)

"""
method :: get_asset_price_floor - is used to get the price floor of an given asset.
params :: asset_name - name of the asset such as bitcoin etc.
return :: price floor of given asset.
"""

def get_asset_price_floor(self, asset_name):
asset_price_floor_details = firebase_data_manager_obj.fetch_data(
document_name=asset_name, collection_name="price_floor_data"
)
asset_price_floor_details = asset_price_floor_details.get("price_floor")
return asset_price_floor_details

"""
method :: get_assets_price_floors - is used to get the price floors of an given all asset that are supported by cruize.
return :: price floors of assets.
"""

def get_assets_price_floors(self):
asset_price_floor_details = firebase_data_manager_obj.fetch_collections(
"price_floor_data"
Expand Down
Loading