-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
295 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# cython: language_level=3 | ||
# Drakkar-Software OctoBot-Trading | ||
# Copyright (c) Drakkar-Software, All rights reserved. | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 3.0 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library. | ||
|
||
from octobot_trading.channels.exchange_channel cimport ExchangeChannel, ExchangeChannelProducer | ||
|
||
|
||
cdef class FundingProducer(ExchangeChannelProducer): | ||
pass | ||
|
||
cdef class FundingChannel(ExchangeChannel): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Drakkar-Software OctoBot-Trading | ||
# Copyright (c) Drakkar-Software, All rights reserved. | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 3.0 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library. | ||
from asyncio import CancelledError | ||
|
||
from octobot_channels.constants import CHANNEL_WILDCARD | ||
from octobot_trading.channels.exchange_channel import ExchangeChannel, ExchangeChannelProducer, ExchangeChannelConsumer | ||
|
||
|
||
class FundingProducer(ExchangeChannelProducer): | ||
async def push(self, symbol, funding_rate, next_funding_time, timestamp): | ||
await self.perform(symbol, funding_rate, next_funding_time, timestamp) | ||
|
||
async def perform(self, symbol, funding_rate, next_funding_time, timestamp): | ||
try: | ||
if self.channel.get_filtered_consumers( | ||
symbol=CHANNEL_WILDCARD) or self.channel.get_filtered_consumers(symbol=symbol): | ||
await self.channel.exchange_manager.get_symbol_data(symbol) \ | ||
.handle_funding_update(funding_rate=funding_rate, | ||
next_funding_time=next_funding_time, | ||
timestamp=timestamp) | ||
await self.send(symbol=symbol, | ||
funding_rate=funding_rate, | ||
next_funding_time=next_funding_time, | ||
timestamp=timestamp) | ||
except CancelledError: | ||
self.logger.info("Update tasks cancelled.") | ||
except Exception as e: | ||
self.logger.exception(e, True, f"Exception when triggering update: {e}") | ||
|
||
async def send(self, symbol, funding_rate, next_funding_time, timestamp): | ||
for consumer in self.channel.get_filtered_consumers(symbol=symbol): | ||
await consumer.queue.put({ | ||
"exchange": self.channel.exchange_manager.exchange_name, | ||
"exchange_id": self.channel.exchange_manager.id, | ||
"symbol": symbol, | ||
"funding_rate": funding_rate, | ||
"next_funding_time": next_funding_time, | ||
"timestamp": timestamp | ||
}) | ||
|
||
|
||
class FundingChannel(ExchangeChannel): | ||
PRODUCER_CLASS = FundingProducer | ||
CONSUMER_CLASS = ExchangeChannelConsumer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# cython: language_level=3 | ||
# Drakkar-Software OctoBot-Trading | ||
# Copyright (c) Drakkar-Software, All rights reserved. | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 3.0 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library. | ||
from octobot_trading.util.initializable cimport Initializable | ||
|
||
cdef class FundingManager(Initializable): | ||
cdef object logger | ||
|
||
cdef public double funding_rate | ||
cdef public double next_updated | ||
cdef public double last_updated | ||
|
||
cpdef void reset_funding(self) | ||
|
||
cpdef funding_update(self, double funding_rate, double next_funding_time, double timestamp) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Drakkar-Software OctoBot-Trading | ||
# Copyright (c) Drakkar-Software, All rights reserved. | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 3.0 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library. | ||
from math import nan | ||
|
||
from octobot_commons.logging.logging_util import get_logger | ||
from octobot_trading.util.initializable import Initializable | ||
|
||
|
||
class FundingManager(Initializable): | ||
def __init__(self): | ||
super().__init__() | ||
self.logger = get_logger(self.__class__.__name__) | ||
self.funding_rate = nan | ||
self.next_updated = 0 | ||
self.last_updated = 0 | ||
self.reset_funding() | ||
|
||
async def initialize_impl(self): | ||
self.reset_funding() | ||
|
||
def reset_funding(self): | ||
self.funding_rate = nan | ||
self.next_updated = 0 | ||
self.last_updated = 0 | ||
|
||
def funding_update(self, funding_rate, next_funding_time, timestamp): | ||
if funding_rate and next_funding_time: | ||
self.funding_rate = funding_rate | ||
self.next_updated = next_funding_time | ||
self.last_updated = timestamp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# cython: language_level=3 | ||
# Drakkar-Software OctoBot-Trading | ||
# Copyright (c) Drakkar-Software, All rights reserved. | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 3.0 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library. | ||
|
||
from octobot_trading.channels.funding cimport FundingProducer | ||
|
||
|
||
cdef class FundingUpdater(FundingProducer): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# pylint: disable=E0611 | ||
# Drakkar-Software OctoBot-Trading | ||
# Copyright (c) Drakkar-Software, All rights reserved. | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 3.0 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library. | ||
import asyncio | ||
import time | ||
|
||
from ccxt.base.errors import NotSupported | ||
from octobot_commons.constants import HOURS_TO_SECONDS | ||
|
||
from octobot_trading.constants import FUNDING_CHANNEL | ||
from octobot_trading.channels.funding import FundingProducer | ||
from octobot_trading.enums import ExchangeConstantsFundingColumns | ||
|
||
|
||
class FundingUpdater(FundingProducer): | ||
CHANNEL_NAME = FUNDING_CHANNEL | ||
|
||
FUNDING_REFRESH_TIME = 2 * HOURS_TO_SECONDS | ||
FUNDING_REFRESH_TIME_MIN = 0.2 * HOURS_TO_SECONDS | ||
FUNDING_REFRESH_TIME_MAX = 8 * HOURS_TO_SECONDS | ||
|
||
def __init__(self, channel): | ||
super().__init__(channel) | ||
|
||
async def start(self): | ||
if not self._should_run(): | ||
return | ||
|
||
while not self.should_stop and not self.channel.is_paused: | ||
next_funding_time = None | ||
sleep_time = self.FUNDING_REFRESH_TIME | ||
try: | ||
for pair in self.channel.exchange_manager.exchange_config.traded_symbol_pairs: | ||
funding: dict = await self.channel.exchange_manager.exchange.get_funding_rate(pair) | ||
|
||
if funding: | ||
next_funding_time = funding[ExchangeConstantsFundingColumns.NEXT_FUNDING_TIME] | ||
await self.push(symbol=pair, | ||
funding_rate=funding[ExchangeConstantsFundingColumns.FUNDING_RATE], | ||
next_funding_time=next_funding_time, | ||
timestamp=funding[ExchangeConstantsFundingColumns.TIMESTAMP]) | ||
except NotSupported: | ||
self.logger.warning(f"{self.channel.exchange_manager.exchange_name} is not supporting updates") | ||
await self.pause() | ||
except Exception as e: | ||
sleep_time = self.FUNDING_REFRESH_TIME_MIN | ||
self.logger.exception(e, True, f"Fail to update funding rate : {e}") | ||
finally: | ||
if next_funding_time: | ||
should_sleep_time = time.time() - next_funding_time | ||
sleep_time = should_sleep_time if should_sleep_time < self.FUNDING_REFRESH_TIME_MAX else \ | ||
self.FUNDING_REFRESH_TIME | ||
await asyncio.sleep(sleep_time) | ||
|
||
def _should_run(self) -> bool: | ||
if not self.channel.exchange_manager.is_margin: | ||
return False | ||
else: | ||
return not self.channel.exchange_manager.exchange.FUNDING_IN_TICKER and \ | ||
not self.channel.exchange_manager.exchange.FUNDING_WITH_MARK_PRICE | ||
|
||
async def resume(self) -> None: | ||
if not self._should_run(): | ||
return | ||
await super().resume() | ||
if not self.is_running: | ||
await self.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.