Skip to content

Commit

Permalink
add functionality to binance futures exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
matiaskotlik committed Mar 7, 2022
1 parent 7197194 commit 59de43f
Show file tree
Hide file tree
Showing 17 changed files with 499 additions and 183 deletions.
2 changes: 2 additions & 0 deletions blankly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import blankly.indicators as indicators
from blankly.utils import time_builder

from blankly.enums import *

from blankly.deployment.reporter_headers import Reporter as __Reporter_Headers
is_deployed = False
_screener_runner = None
Expand Down
48 changes: 48 additions & 0 deletions blankly/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import enum


class MarginType(str, enum.Enum):
CROSSED = 'crossed'
ISOLATED = 'isolated'


class Side(str, enum.Enum):
BUY = 'buy'
SELL = 'sell'


class PositionMode(str, enum.Enum):
BOTH = 'both'
LONG = 'long'
SHORT = 'short'


class TimeInForce(str, enum.Enum):
GTC = 'GTC' # Good Till Cancelled
FOK = 'FOK' # Fill or Kill
IOC = 'IOC' # Immediate or Cancel


class HedgeMode(str, enum.Enum):
HEDGE = 'hedge'
ONEWAY = 'oneway'


class OrderStatus(str, enum.Enum):
NEW = 'new'
PARTIALLY_FILLED = 'partially_filled'
FILLED = 'filled'
CANCELED = 'canceled'
EXPIRED = 'expired'


class OrderType(str, enum.Enum):
MARKET = 'market'
LIMIT = 'limit'
STOP = 'stop'
TAKE_PROFIT = 'take_profit'
LIQUIDATION = 'liquidation'


class ContractType(str, enum.Enum):
PERPETUAL = 'perpetual'
73 changes: 28 additions & 45 deletions blankly/exchanges/futures/futures_strategy_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,63 +15,46 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from blankly import FuturesStrategy
from abc import ABC

from blankly.enums import PositionMode, Side
from blankly.exchanges.interfaces.futures_exchange_interface import FuturesExchangeInterface
from blankly.exchanges.orders.futures.futures_limit_order import FuturesLimitOrder
from blankly.exchanges.orders.futures.futures_market_order import FuturesMarketOrder
from blankly.utils.utils import AttributeDict


# TODO
class FuturesStrategyLogger(FuturesExchangeInterface):
class FuturesStrategyLogger:
interface: FuturesExchangeInterface
strategy: FuturesStrategy
strategy: 'FuturesStrategy'

def __init__(self, interface=None, strategy=None):
self.interface = interface
self.strategy = strategy

def init_exchange(self):
return self.interface.init_exchange()

def get_products(self) -> list:
return self.interface.get_products()

def get_account(self, symbol: str = None) -> AttributeDict:
return self.interface.get_account(symbol)

def market_order(self, symbol: str, side: str, size: float,
position: str) -> FuturesMarketOrder:
def __getattribute__(self, item):
# run the overidden function in this class if it exists, or default to the method in self.interface
try:
# this is NOT recursive, it will get the *actual* attribute
return object.__getattribute__(self, item)
except AttributeError:
return self.interface.__getattribute__(item)

def market_order(
self,
symbol: str,
side: Side,
size: float,
position: PositionMode = PositionMode.BOTH) -> FuturesMarketOrder:
# TODO log order
return self.interface.market_order(symbol, side, size, position)

def limit_order(self, symbol: str, side: str, price: float, size: float,
position: str) -> FuturesLimitOrder:
def limit_order(
self,
symbol: str,
side: Side,
price: float,
size: float,
position: PositionMode = PositionMode.BOTH) -> FuturesLimitOrder:
# TODO log order
return self.interface.limit_order(symbol, side, price, size, position)

def cancel_order(self, order_id: str) -> dict:
return self.interface.cancel_order(order_id)

def get_open_orders(self, symbol: str = None) -> list:
return self.interface.get_open_orders(symbol)

def get_order(self, order_id: str) -> dict:
return self.interface.get_order(order_id)

def get_price(self, symbol: str) -> float:
return self.interface.get_price(symbol)

@property
def account(self) -> AttributeDict:
return self.interface.account

@property
def orders(self) -> list:
return self.interface.orders

@property
def cash(self) -> float:
return self.interface.cash

def get_product_history(self, symbol, epoch_start, epoch_stop, resolution):
return self.interface.get_product_history(symbol, epoch_start,
epoch_stop, resolution)
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ def __init__(self,
self.__calls = Client(
api_key=auth.keys['API_KEY'],
api_secret=auth.keys['API_SECRET'],
# pretty sure this breaks futures
# tld=self.preferences["settings"]['binance']["binance_tld"],
testnet=self.preferences['settings']['use_sandbox'])
testnet=auth.keys['sandbox'])
# TODO does futures even have a tld switch?

self.__interface = BinanceFuturesInterface(self.exchange_type,
self.calls)
Expand Down
Loading

0 comments on commit 59de43f

Please sign in to comment.