예제 #1
0
class Forceorder():
    def __init__(self):
        self.ev = EventEngine()
        self.ev.start()
        self.broke = BinanceFutures(self.ev)
        self.broke.start("btcusdt", [FORCEORDER])
        self.ev.register(FORCEORDER, self.callback)

    def callback(self, event):
        """
        {'e': 'forceOrder', 'E': 1594107015086,
        'o': {'s': 'BTCUSDT',
        'S': 'SELL',
        'o': 'LIMIT',
        'f': 'IOC',
        'q': '1.300',
        'p': '9206.39',
        'ap': '9242.01',
        'X': 'FILLED',
        'l': '1.084',
        'z': '1.300',
        'T': 1594107015081}}

        :param event:
        :return:
        """
        e = event.data
        try:
            data = e["o"]
            db.insert_one("forceorder", data)
        except Exception as e:
            log.debug(e)
예제 #2
0
파일: main.py 프로젝트: ynzheng/alphaquant
 def __init__(self, engine):
     super().__init__(engine)
     self.bar = {}
     self.t = {}
     cfg = config.exchange
     self.broker = BinanceFutures(cfg)
     self.symbols = cfg.symbols
     self.timeframe = cfg.timeframe
     self.update()
예제 #3
0
 def __init__(self):
     self.ev = EventEngine()
     self.maker = BinanceFutures(self.ev)
     self.taker = Ftx(self.ev)
     self.maker_price = 0
     self.taker_price = 0
     self.data = []
     RepeatingTimer(1, self.savedb).start()
예제 #4
0
 def __init__(self, engine, setting):
     super().__init__(engine, setting)
     self.symbol=""
     self.kma_time=45
     self.rsi_time=14
     self.rsi_fastk=5
     self.rsi_fastd=3
     self.rsi_fastd_matype=0
     cfg=config.binance
     self.broker=BinanceFutures(cfg)
     self.symbol=cfg.symbol[0]
     self.timeframe="15m"
     self.qty=0.001
     self.bar=None
     self.tp=30.00
     self.sl=30.00
     RepeatingTimer(60, self.start).start()
예제 #5
0
class OpenInterest():
    symbol = 'BTCUSDT'
    starttime = None

    def __init__(self):
        self.ev = EventEngine()
        self.ev.start()
        self.broke = BinanceFutures(self.ev)
        # self.broke.start("btcusdt",[FORCEORDER])
        # self.ev.register(FORCEORDER, self.callback)
    def loop(self):
        # db.table_name.aggregate({"$group": {_id: "max", max_value: {"$max": "$column_name"}}})
        rs = db.db["openinterest"].find().sort([("timestamp", -1)]).limit(1)
        rs = list(rs)
        if len(rs) > 0:
            self.starttime = float(rs[0]["timestamp"]) + 1000 * 60
        else:
            self.starttime = get_cur_timestamp_ms() - 1000 * 30 * 288 * 5 * 60
        while True:
            self.loaddata()
            if (get_cur_timestamp_ms() - self.starttime) > 1000 * 60 * 5:
                time.sleep(0.5)
            else:
                time.sleep(60)

    def loaddata(self):
        endtime = self.starttime + 500 * 1000 * 60 * 5
        t = get_cur_timestamp_ms()
        if endtime > t:
            endtime = t
        data = self.broke.get_open_interest(self.symbol, '5m', self.starttime,
                                            endtime)
        try:
            db.insert_many("openinterest", data)
            self.starttime = data[-1]["timestamp"]
        except Exception as e:
            log.error(e)
예제 #6
0
 def __init__(self):
     self.ev = EventEngine()
     self.ev.start()
     self.broke = BinanceFutures(self.ev)
예제 #7
0
 def __init__(self):
     self.ev = EventEngine()
     self.ev.start()
     self.broke = BinanceFutures(self.ev)
     self.broke.start("btcusdt", [FORCEORDER])
     self.ev.register(FORCEORDER, self.callback)
예제 #8
0
class Stcstrategy(BaseStrategy):

    def __init__(self, engine, setting):
        super().__init__(engine, setting)
        self.symbol=""
        self.kma_time=45
        self.rsi_time=14
        self.rsi_fastk=5
        self.rsi_fastd=3
        self.rsi_fastd_matype=0
        cfg=config.binance
        self.broker=BinanceFutures(cfg)
        self.symbol=cfg.symbol[0]
        self.timeframe="15m"
        self.qty=0.001
        self.bar=None
        self.tp=30.00
        self.sl=30.00
        RepeatingTimer(60, self.start).start()

    def start(self):
        bar=self.broker.get_bar(self.symbol,self.timeframe)
        self.bar=bar
        self.on_bar(bar[-1])

    def on_bar(self, event):
        if not self.broker.get_positions(self.symbol):
            self.trade()

    def trend(self):
        kma=talib.KAMA(self.bar.close,self.kma_time*16)
        close=self.bar.close
        return close[-1]>kma[-1]

    def on_trade(self,trade):
        position=self.broker.get_positions(self.symbol)
        if position:
            price=position.price
            side=position.direction
            if side==Direction.LONG:
                if trade.price-price>self.tp or price-trade.price>self.sl:
                    self.broker.close_position(position)

            else:
                if price-trade.price>self.tp or trade.price-price>self.sl:
                    self.broker.close_position(position)


    def trade(self):
        rsi,k,d = talib.STOCHRSI(self.bar.close, self.rsi_time, self.rsi_fastk, self.rsi_fastd, self.rsi_fastd_matype);
        if self.trend():
            if crossover(k,d) and k <50:
                price=self.bar.close[-1]
                self.broker.buy(self.symbol,self.qty,price)
        else:
            if crossunder(k, d) and k > 50:
                price = self.bar.close[-1]
                self.broker.sell(self.symbol,self.qty,price)
예제 #9
0
파일: main.py 프로젝트: ynzheng/alphaquant
class SHstrategy(BaseStrategy):
    """
    StrengthHedge,趋势强度对冲策略
    根据4小时TSI信号,0轴向上做多,0轴向下做空,
    多空仓位保持平衡
    """
    def __init__(self, engine):
        super().__init__(engine)
        self.bar = {}
        self.t = {}
        cfg = config.exchange
        self.broker = BinanceFutures(cfg)
        self.symbols = cfg.symbols
        self.timeframe = cfg.timeframe
        self.update()

    def start(self):
        bar = self.broker.get_bar(self.symbol, self.timeframe)
        self.bar = bar
        self.on_bar(bar[-1])

    def on_bar(self, event):
        if not self.broker.get_positions(self.symbol):
            self.trade()

    def trend(self):
        kma = talib.KAMA(self.bar.close, self.kma_time * 16)
        close = self.bar.close
        return close[-1] > kma[-1]

    def on_trade(self, trade):
        position = self.broker.get_positions(self.symbol)
        if position:
            price = position.price
            side = position.direction
            if side == Direction.LONG:
                if trade.price - price > self.tp or price - trade.price > self.sl:
                    self.broker.close_position(position)

            else:
                if price - trade.price > self.tp or trade.price - price > self.sl:
                    self.broker.close_position(position)

    def update(self):
        tsi = pd.DataFrame()
        for s in self.symbols:
            self.bar[s] = self.broker.get_bar(s, self.timeframe)
            close = self.bar[s].close
            tsi[s] = self.tsi(close)

        print(tsi.tail())

    def tsi(self, data):
        a = talib.EMA(data, 5)
        b = talib.EMA(data, 8)
        c = talib.EMA(data, 13)
        d = talib.EMA(data, 21)
        d1 = a / b - 1
        d2 = b / c - 1
        d3 = c / d - 1
        t = (d1 + d2 + d3) * 1000
        return talib.EMA(t, 6)

    def trade(self):
        rsi, k, d = talib.STOCHRSI(self.bar.close, self.rsi_time,
                                   self.rsi_fastk, self.rsi_fastd,
                                   self.rsi_fastd_matype)
        if self.trend():
            if crossover(k, d) and k < 50:
                price = self.bar.close[-1]
                self.broker.buy(self.symbol, self.qty, price)
        else:
            if crossunder(k, d) and k > 50:
                price = self.bar.close[-1]
                self.broker.sell(self.symbol, self.qty, price)

    def on_order(self, event):
        pass

    def on_position(self, event):
        pass

    def on_ticker(self, event):
        pass
예제 #10
0
import os, sys
baseroot = os.path.dirname(os.path.abspath(__file__))
sys.path.append('/alphaquant/')
from aq.broker.binancefutures import BinanceFutures
from aq.broker.huobi import HuobiFutures
from aq.common.constant import *
from aq.engine.baseStrategy import BaseStrategy
from aq.engine.config import config
from aq.common.logger import log
from aq.engine.event import EventEngine


def on_orderbook(msg):
    print(msg)


lcfg = config.long
scfg = config.short
ev = EventEngine()
long = BinanceFutures(ev)
subscribe = {ORDERBOOK: on_orderbook}
# long.subscribe(lcfg.symbols,subscribe)

short = HuobiFutures(ev)
short.subscribe((scfg.symbols, subscribe))
예제 #11
0
파일: main.py 프로젝트: ynzheng/alphaquant
 def __init__(self, engine):
     super().__init__(engine)
     self.ev = engine
     cfg = config.short
     self.short = BinanceFutures(self.ev)
     self.long = HuobiFutures(self.ev)
예제 #12
0
파일: main.py 프로젝트: ynzheng/alphaquant
class Feeabrstrategy(BaseStrategy):
    """
    资金费率套利
    从二个市场或多个市场出找出费率差最大的交易对
    买入费率低的,卖出费率高的交易对
    定时调整二个账户的资金余额
    """
    def __init__(self, engine):
        super().__init__(engine)
        self.ev = engine
        cfg = config.short
        self.short = BinanceFutures(self.ev)
        self.long = HuobiFutures(self.ev)

    def start(self):
        bar = self.broker.get_bar(self.symbol, self.timeframe)
        self.bar = bar
        self.on_bar(bar[-1])

    def on_bar(self, event):
        if not self.broker.get_positions(self.symbol):
            self.trade()

    def trend(self):
        kma = talib.KAMA(self.bar.close, self.kma_time * 16)
        close = self.bar.close
        return close[-1] > kma[-1]

    def on_trade(self, trade):
        position = self.broker.get_positions(self.symbol)
        if position:
            price = position.price
            side = position.direction
            if side == Direction.LONG:
                if trade.price - price > self.tp or price - trade.price > self.sl:
                    self.broker.close_position(position)

            else:
                if price - trade.price > self.tp or trade.price - price > self.sl:
                    self.broker.close_position(position)

    def update(self):
        tsi = pd.DataFrame()
        for s in self.symbols:
            self.bar[s] = self.broker.get_bar(s, self.timeframe)
            close = self.bar[s].close
            tsi[s] = self.tsi(close)

        print(tsi.tail())

    def on_order(self, event):
        pass

    def on_position(self, event):
        pass

    def on_ticker(self, event):
        pass

    def get_fee(self):
        short = self.short.get_fees()
        long = self.long.get_fees()
        data = []
        for l in long:
            symbol = l.split("-")[0] + "USDT"
            r = short.get(symbol, 0)
            if r != 0:
                rs = [l, long[l], r]
                data.append(rs)
        df = pd.DataFrame(data)
        df.columns = ["symbil", "huobi", "binance"]
        df["dif"] = df["huobi"] - df["binance"]
예제 #13
0
from aq.broker.binancefutures import BinanceFutures
from aq.engine.event import EventEngine
import pandas as pd
ev = EventEngine()
b = BinanceFutures(ev)
bar = b.get_bar("BTCUSDT", "1m")
data = pd.DataFrame(bar)
data.to_csv("btcusdt1m.csv")