Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BinanceExchange] Add future support #483

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
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
50 changes: 48 additions & 2 deletions Trading/Exchange/binance/binance_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,42 @@
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
import octobot_commons.constants as common_constants
import octobot_trading.enums as trading_enums
import octobot_trading.exchanges as exchanges


class Binance(exchanges.SpotCCXTExchange):
class Binance(exchanges.SpotCCXTExchange, exchanges.FutureCCXTExchange):
DESCRIPTION = ""

BUY_STR = "BUY"
SELL_STR = "SELL"

ACCOUNTS = {
trading_enums.AccountTypes.CASH: 'cash'
trading_enums.AccountTypes.CASH: 'cash',
trading_enums.AccountTypes.FUTURE: 'future'
}

MARK_PRICE_IN_POSITION = True

BINANCE_SYMBOL = "symbol"

BINANCE_FUTURE_QUANTITY = "positionAmt"
BINANCE_FUTURE_UNREALIZED_PNL = "unRealizedProfit"
BINANCE_FUTURE_LIQUIDATION_PRICE = "liquidationPrice"
BINANCE_FUTURE_VALUE = "liquidationPrice"

BINANCE_MARGIN_TYPE = "marginType"
BINANCE_MARGIN_TYPE_ISOLATED = "ISOLATED"
BINANCE_MARGIN_TYPE_CROSSED = "CROSSED"

BINANCE_FUNDING_RATE = "fundingRate"
BINANCE_LAST_FUNDING_TIME = "fundingTime"
BINANCE_FUNDING_DURATION = 8 * common_constants.HOURS_TO_SECONDS

BINANCE_TIME = "time"
BINANCE_MARK_PRICE = "markPrice"
BINANCE_ENTRY_PRICE = "entryPrice"

@classmethod
def get_name(cls):
Expand Down Expand Up @@ -98,3 +119,28 @@ def _fill_order_missing_data(self, order, trades):
if not order[trading_enums.ExchangeConstantsOrderColumns.FEE.value] and order_id in trades:
order[trading_enums.ExchangeConstantsOrderColumns.FEE.value] = \
trades[order_id][trading_enums.ExchangeConstantsOrderColumns.FEE.value]

def parse_funding(self, funding_dict, from_ticker=False):
try:
last_funding_time = self.connector.get_uniform_timestamp(
self.connector.client.safe_float(funding_dict, self.BINANCE_LAST_FUNDING_TIME))
funding_dict = {
trading_enums.ExchangeConstantsFundingColumns.LAST_FUNDING_TIME.value: last_funding_time,
trading_enums.ExchangeConstantsFundingColumns.FUNDING_RATE.value:
self.connector.client.safe_float(funding_dict, self.BINANCE_FUNDING_RATE),
trading_enums.ExchangeConstantsFundingColumns.NEXT_FUNDING_TIME.value:
last_funding_time + self.BINANCE_FUNDING_DURATION
}
except KeyError as e:
self.logger.error(f"Fail to parse funding dict ({e})")
return funding_dict

def parse_mark_price(self, mark_price_dict, from_ticker=False):
try:
mark_price_dict = {
trading_enums.ExchangeConstantsMarkPriceColumns.MARK_PRICE.value:
self.connector.client.safe_float(mark_price_dict, self.BINANCE_MARK_PRICE, 0)
}
except KeyError as e:
self.logger.error(f"Fail to parse mark_price dict ({e})")
return mark_price_dict