예제 #1
0
    def init(self):
        self.pubsub_timeout = int(config.get('system', 'pubsub_timeout'), 0)
        self.loop_count = 100
        self.job_periodic = tornado.ioloop.PeriodicCallback(self._periodic_callback, 1000)
        self.job_periodic.start()

        if not config.has_section("redis"):
            return

        redis_enable = config.get('redis', 'enable')
        if redis_enable != "true":
            return

        redis_host = config.get('redis', 'host')
        redis_port = int(config.get('redis', 'port'), 0)
        redis_password = config.get('redis', 'password')
        self.redis_sub = tornadoredis.Client(
            host=redis_host,
            port=redis_port,
            password=redis_password)

        self.redis_pub = tornadoredis.Client(
            host=redis_host,
            port=redis_port,
            password=redis_password)
        self.redis_pub.connect()

        yield tornado.gen.Task(self.redis_sub.subscribe, self.REDIS_PUBSU_CHANEL_ID)
        self.redis_sub.listen(self.on_redis_message)
예제 #2
0
def do_send_email(title: str, content: str, config: ConfigParser):
    # 登录
    logging.info("开始发送邮件")
    smtp = SMTP_SSL(config.get('email', 'host_server'))
    smtp.set_debuglevel(0)
    smtp.ehlo(config.get('email', 'host_server'))
    passwords = config.get('email', 'password').split(",")
    import random
    k = random.randint(0, len(passwords) - 1)
    password = passwords[k]
    smtp.login(config.get('email', 'username'), password)
    if 'ib_account' in config.sections():
        name = config.get("ib_account", "name")
    elif 'td_account' in config.sections():
        name = config.get("td_account", "name")
    else:
        raise RuntimeError("config error")
    title = '[{}]{}'.format(name, title)
    sender_email = config.get('email', 'sender_email')
    receiver = config.get('email', 'receiver')
    # 替换掉content中的< >字符
    content = content.replace('<', '[').replace('>', ']')
    msg = MIMEText(content, "plain", 'utf-8')
    msg["Subject"] = Header(title, 'utf-8')
    msg["From"] = sender_email
    msg["To"] = receiver
    smtp.sendmail(sender_email, receiver, msg.as_string())
    smtp.quit()
예제 #3
0
    def _init_handlers(self):
        """
        @summary: 加载应用程序模块

        @return: 初始化成功返回True,否则返回False
        """
        handlers = []
        if config.has_section("services"):
            if config.has_option("services", "enable_pub") and \
                            config.get("services", "enable_pub", "0") == "1":
                handlers.append(((r"/sub/(.+)"), SubHandler))
                handlers.append(((r"/pub/(.+)"), PubHandler))
                
                #
                # 批量订阅支持
                #
                handlers.append(((r"/subs"), SubsHandler))
                handlers.append(((r"/pubs"), PubsHandler))

        handlers.append(((r"/logs"), LogHandler))
        handlers.append(((r"/watch"), WatchHandler))
        host_pattern = ".*$"
        if len(handlers) <= 0:
            return False

        self.add_handlers(host_pattern, handlers)
        return True
예제 #4
0
    def init(self):
        """
        @summary: 初始化所有部分

        @return: 初始化成功返回
        """
        #
        # 初始化处理句柄
        #
        ret = self._init_handlers()
        if not ret:
            return ret

        #
        # 初始化redis监控句柄
        #
        self.pub_sub.init()

        #
        # 初始化piwik统计模块
        #
        self._init_piwik()
        
        if not config.has_section("system"):
            return ret

        #
        # 添加ip检测开关量
        #
        if config.has_option("system", "enable_ip_auth"):
            enable_ip_auth = config.get("system", "trust_ips", "false")
            if enable_ip_auth == "true":
                self.ip_auth = True

        if not config.has_option("system", "trust_ips"):
            return ret

        trust_ips = config.get("system", "trust_ips", "0")
        trust_ip_list = trust_ips.split('|')
        for ip in trust_ip_list:
            if ip in self.trust_ips:
                continue
            self.trust_ips.append(ip)
        return ret
예제 #5
0
    def __init__(self, application, request, **kwargs):
        web.RequestHandler.__init__(self, application, request, **kwargs)
        self.request.remote_ip = self.request.headers.get("X-Forwarded-For", self.request.remote_ip)

        if self.application.enable_piwik:
            headers = {
                'HTTP_USER_AGENT': self.request.headers.get("User-Agent", "Default"),
                'REMOTE_ADDR': self.request.remote_ip,
                #'HTTP_REFERER': 'http://referer.com/somewhere/',
                'HTTP_ACCEPT_LANGUAGE': 'en-US',
                'SERVER_NAME': self.request.host,
                'PATH_INFO': self.request.path,
                'QUERY_STRING': self.request.query,
                'HTTPS': False,
            }
            request = FakeRequest(headers)
            self.piwik = PiwikTracker(config.get("piwik", "app_id", 1), request)
            self.piwik.set_api_url(config.get("piwik", "app_url", ""))
            self.piwik.set_ip(self.request.remote_ip) # Optional, to override the IP
            self.piwik.set_token_auth(config.get("piwik", "token_auth", ""))  # Optional, to override the IP
            self.tracked = False
예제 #6
0
    def _init_piwik(self):
        """
        @summary: 初始化统计模块piwik

        @return: 初始化成功返回True,否则返回False
        """
        if not config.has_section("piwik") or \
            not config.has_option("piwik", "enable"):
            return False

        enable_piwik = config.get("piwik", "enable", "false")
        if enable_piwik != "true":
            return True

        self.enable_piwik = True
        return True
예제 #7
0
def send_email(title: str, content: str):
    from se import config
    if config.get("email", 'activate') == 'false':
        return

    def send_with_retry():
        retry_limit = 10
        for i in range(retry_limit):
            try:
                do_send_email(title, content, config)
                break
            except:
                import traceback
                logging.warning("发送邮件失败 {},邮件配置:{}".format(
                    traceback.format_exc(), config.__dict__))
                import time
                time.sleep(10)

    threading.Thread(name='send_email', target=send_with_retry).start()
예제 #8
0
from ibapi.tag_value import TagValue
from pandas._libs.tslibs.timestamps import Timestamp

os.environ[
    'config.dir'] = "/Users/zhang/PycharmProjects/strategy_engine_v2/interface"
# 如果没有日志目录的话,则创建
if not os.path.exists("log"):
    os.makedirs("log")

from se.domain2.time_series.time_series import TimeSeries, TimeSeriesRepo, TimeSeriesSubscriber, TSData
from se.domain2.engine.engine import DataPortal
from se import config, BeanContainer
from ibapi.order import Order as IBOrder
from se.infras.ib import IBAccount, IBClient, Request

client: IBClient = IBClient.find_client(config.get('ib_data', 'host'),
                                        config.getint('ib_data', 'port'),
                                        config.getint('ib_data', 'client_id'))
if not client:
    client: IBClient = IBClient(config.get('ib_data', 'host'),
                                config.getint('ib_data', 'port'),
                                config.getint('ib_data', 'client_id'))
contract: Contract = Contract()
contract.symbol = 'CL'
contract.secType = 'FUT'
contract.exchange = "NYMEX"
# contract.multiplier = 5000
contract.lastTradeDateOrContractMonth = "202104"
# contract: Contract = Contract()
# contract.symbol = '700'
# contract.secType = 'STK'
예제 #9
0
from trading_calendars import get_calendar

from se import config, BeanContainer, AccountRepo
from se.domain2.engine.engine import Engine, Scope
from se.infras.td import TDAccount
from st import SPCEStrategy

engine = Engine()
scope = Scope(["SPCE_STK_USD_SMART"], trading_calendar=get_calendar("NYSE"))
strategy = SPCEStrategy(scope)

account_name = "td_local_spce"
repo: AccountRepo = BeanContainer.getBean(AccountRepo)
acc: TDAccount = repo.find_one(account_name)
if not acc:
    acc = TDAccount(account_name, 100)

acc.with_order_callback(strategy).with_client(
    config.get("td_account", "client_id"),
    config.get("td_account", 'redirect_url'),
    config.get("td_account", 'credentials_path'),
    config.get("td_account", 'account_id'))

acc.start_save_thread()

engine.run(strategy, acc)
예제 #10
0
from trading_calendars import get_calendar

from se import config, AccountRepo, BeanContainer
from se.domain2.engine.engine import Engine, Scope
from se.infras.ib import IBAccount
from strategies.strategy import TestStrategy3

engine = Engine()
scope = Scope(["GSX_STK_USD_SMART"], trading_calendar=get_calendar("NYSE"))
strategy = TestStrategy3(scope)

account_name = "ib_real2"
repo: AccountRepo = BeanContainer.getBean(AccountRepo)
acc = repo.find_one(account_name)
if not acc:
    acc = IBAccount(account_name, 10000)

acc.with_order_callback(strategy).with_client(
    config.get('ib_account', 'host'), config.getint('ib_account', 'port'),
    config.getint('ib_account', 'client_id'))
acc.start_save_thread()
acc.start_sync_order_executions_thread()

engine.run(strategy, acc)
예제 #11
0
from trading_calendars import get_calendar

from se import config, BeanContainer, AccountRepo
from se.domain2.engine.engine import Engine, Scope
from se.infras.ib import IBAccount
from strategies.spce import SPCEStrategy

engine = Engine()
scope = Scope(["SPCE_STK_USD_SMART"], trading_calendar=get_calendar("NYSE"))
strategy = SPCEStrategy(scope)

account_name = "ib_real1"
repo: AccountRepo = BeanContainer.getBean(AccountRepo)
acc = repo.find_one(account_name)
if not acc:
    acc = IBAccount(account_name, 10000)

acc.with_order_callback(strategy).with_client(config.get('ib_account', 'host'), config.getint('ib_account', 'port'),
                                              config.getint('ib_account', 'client_id'))
acc.valid_scope(scope)
acc.start_save_thread()
acc.start_sync_order_executions_thread()

engine.run(strategy, acc)
예제 #12
0
from trading_calendars import get_calendar

from se import config, BeanContainer, AccountRepo
from se.domain2.engine.engine import Engine, Scope
from se.infras.td import TDAccount
from strategies_td.spce import SPCEStrategy

engine = Engine()
scope = Scope(["ACB_STK_USD_SMART"], trading_calendar=get_calendar("NYSE"))
strategy = SPCEStrategy(scope)

account_name = "td_real1"
repo: AccountRepo = BeanContainer.getBean(AccountRepo)
acc: TDAccount = repo.find_one(account_name)
if not acc:
    acc = TDAccount(account_name, 50)

acc.with_order_callback(strategy).with_client(config.get("td_account", "client_id"),
                                              config.get("td_account", 'redirect_url'),
                                              config.get("td_account", 'credentials_path'),
                                              config.get("td_account", 'account_id'))

acc.start_save_thread()

engine.run(strategy, acc)
예제 #13
0
import os
os.environ[
    'config.dir'] = "/Users/zhangzheng/Quantitative/stratege_engine/interface3"

from se.infras.models import *
from cassandra.cqlengine.management import sync_table, sync_type
from se import config

ks_name = config.get("cassandra", "session_keyspace")

sync_type(ks_name, UserOrderExecutionModel)
sync_type(ks_name, UserOrderModel)
sync_type(ks_name, DataRecordModel)
sync_table(AccountModel)
sync_table(TimeSeriesModel)
sync_table(TimeSeriesDataModel)
예제 #14
0
from se import config
from ibapi.order import Order as IBOrder
from se.infras.ib import IBAccount


class TestStrategy(AbstractStrategy):
    def do_order_status_change(self, order, account):
        pass

    def do_initialize(self, engine: Engine, data_portal: DataPortal):
        pass


account_name = "test"
acc = IBAccount(account_name, 10000)
acc.with_client(config.get('ib_account', 'host'),
                config.getint('ib_account', 'port'),
                config.getint('ib_account', 'client_id'))

acc.with_order_callback(TestStrategy(None))
code = '700_STK_HKD_SEHK'
contract = acc.cli.code_to_contract(code)
ib_order: IBOrder = IBOrder()
ib_order.orderType = "MKT"
ib_order.totalQuantity = 100
# ib_order.lmtPrice = 85
ib_order.action = 'SELL'
# ib_order.outsideRth = True
# ib_order.tif = "GTD"
# ib_order.goodAfterTime = '18:45:00'
# ib_order.goodTillDate = '19:56:00'