def init(context):
    stbase.controller.init('z:/ams/fisher')
    stbase.stocks.setup(ams.ASM_StockMarket().init(context), ams.ASM_StockTrader().init(context))
    # stbase.stocks.market.setupRecorder(stbase.MarketFileRecorder(stbase.controller.data_path)) # 安裝行情記錄器
    stbase.stocks.market.setupRecorder(strecoder.MarketMongoDBRecorder(db_prefix='AMS_Stocks',host='192.168.1.252')) # 安裝行情記錄器
    stbase.controller.addStrategy(ASM_Strategy('ASM_Strategy',stbase.stocks).init())
    stbase.controller.run()
    stbase.println("Strategy : Market MongoDB Recorder Started..")
Exemplo n.º 2
0
def init(context):
    stbase.controller.init('z:/ams/fisher')
    stbase.stocks.setup(ams.ASM_StockMarket().init(context),
                        ams.ASM_StockTrader().init(context))
    stbase.stocks.market.setupRecorder(
        stbase.MarketFileRecorder(stbase.controller.data_path))  # 安裝行情記錄器
    stbase.controller.addStrategy(
        ASM_Strategy('ASM_Strategy', stbase.stocks).init())
    stbase.controller.run()
    stbase.println("Strategy : Sample Started..")
Exemplo n.º 3
0
    def getYdClosePrice(self, code):
        """查詢昨日 收盤價
         ? 如果盤后查詢,則取最後一根而不是倒數第二

        """
        import stutils
        if not stutils.Stocks.in_trading_time():
            stbase.println(" not in trading time ..")
            return self.getHistoryBars(code, 'd', 2, True)[-1].Close
        return self.getHistoryBars(code, 'd', 2)[-1].Close
Exemplo n.º 4
0
    def start(self):
        stbase.Strategy.start(self)
        stbase.println("Strategy : Sample Started..")

        code = '0600000'
        to = stbase.stocks.getTradeObject(code)
        pos = self.product.getPosition(code)
        stbase.println(pos.dict())
        amount = self.product.getAmountUsable()
        asset = self.product.getAmountAsset()
        stbase.println('amount:{}'.format(amount) )
        stbase.println('asset:{}'.format(asset)  )
Exemplo n.º 5
0
def strategy_inday(code, num=100, limit=0.02, base_price=0):
    """日内涨跌幅策略
    @:param code: 股票代码
    @:param num :买卖数量
    @:param limit: 价格浮动限
    base_price : 参考基准价格 , 0 表示采用昨收盘价格

    当日仅仅允许买卖各触发一次

    """

    stock = stbase.TradeManager().getStock(code)

    if base_price == 0:
        zf = stock.last_price / stock.yesterday_close_price - 1
    else:
        zf = stock.last_price / base_price - 1

    st_price = stock.yesterday_close_price * (1 + limit)
    st_price = round(st_price, 2)
    # stbase.println('st_price:%s  '%st_price)
    stbase.println('zf:{} limit:{} diff:{}'.format(zf, limit, zf - limit))

    if zf <= -limit and stock.any.flag_buy:
        stbase.print_line(
            '(strategy_inday) zf:%s last_price:%s  base_price:%s' %
            (zf, stock.last_price, base_price))

        stbase.record_signal(code, '=={}=='.format(code))
        stbase.record_signal(code,
                             'strategy_inday signal occur. (zf <= -limit)')
        stbase.record_signal(code, 'zf:{} limit:{}'.format(zf, limit))
        #跌幅过限
        amount = stbase.TradeManager().xy_proxy.get_stock_amount_useable()
        pos_sum = stock.pos.net_total
        #
        if stock.pos.post_cost_amount <= amount * 0.1:
            """持仓资金占总资金 <= 10% """
            stbase.record_signal(
                code, 'do buy: {} ,{}, {}'.format(code, st_price, num))
            stbase.TradeManager().xy_proxy.buy(code, st_price, num)
            strategy_inday_buy_count[code] = 1
            stock.any.flag_buy = 0
            stock.any.flag_sell = 1

    if zf >= limit and stock.any.flag_sell:
        # print '=*'*20
        stbase.print_line(
            '(strategy_inday) zf:%s last_price:%s  base_price:%s' %
            (zf, stock.last_price, base_price))

        stbase.TM.record_signal(code, '=={}=='.format(code))
        stbase.TM.record_signal(code, '-*' * 20)
        stbase.TM.record_signal(code,
                                'strategy_inday signal occur. (zf >= -limit)')
        stbase.TM.record_signal(code, 'zf:{} limit:{}'.format(zf, limit))
        stbase.TM.record_signal(
            code, 'net_total:{} net_yd:{}'.format(stock.pos.net_total,
                                                  stock.pos.net_yd))
        if stock.pos.net_total >= num:
            stbase.TM.record_signal(
                code, 'do sell: {} ,{}, {}'.format(code, st_price, num))
            stbase.TradeManager().xy_proxy.sell(code, st_price, num)
            strategy_inday_sell_count[code] = 1
            stock.any.flag_sell = 0
            stock.any.flag_buy = 1
Exemplo n.º 6
0
def strategy_inday(code, num=100, limit=0.02):
    """日内涨跌幅策略
    @:param code: 股票代码
    @:param num :买卖数量
    @:param limit: 价格浮动限

    当日仅仅允许买卖各触发一次

    """
    global strategy_inday_buy_count
    global strategy_inday_sell_count

    stock = stbase.TradeManager().getStock(code)

    zf = stock.last_price / stock.yesterday_close_price - 1
    # stbase.print_line('(strategy_inday) zf:%s last_price:%s  yd_close_price:%s'%(zf,stock.last_price,stock.yesterday_close_price),stdout=False )

    # stbase.print_line('strategy_inday,=={}=='.format(code))

    strategy_name = 'strategy_inday'
    st_price = stock.yesterday_close_price * (1 + limit)
    st_price = round(st_price, 2)
    # stbase.println('st_price:%s  '%st_price)
    stbase.println('zf:{} limit:{} diff:{}'.format(zf, limit, zf - limit))
    if zf <= -limit:
        stbase.print_line(
            '(strategy_inday) zf:%s last_price:%s  yd_close_price:%s' %
            (zf, stock.last_price, stock.yesterday_close_price),
            stdout=False)

        stbase.record_signal(code, '=={}=='.format(code))
        stbase.record_signal(code,
                             'strategy_inday signal occur. (zf <= -limit)')
        stbase.record_signal(code, 'zf:{} limit:{}'.format(zf, limit))
        #跌幅过限
        amount = stbase.TradeManager().xy_proxy.get_stock_amount_useable()
        pos_sum = stock.pos.net_total
        #
        if stock.pos.post_cost_amount <= amount * 0.1 and strategy_inday_buy_count.get(
                code, 0) == 0:
            """持仓资金占总资金 <= 10% """
            stbase.record_signal(
                code, 'do buy: {} ,{}, {}'.format(code, st_price, num))
            stbase.TradeManager().xy_proxy.buy(code, st_price, num)
            strategy_inday_buy_count[code] = 1

    if zf >= limit:
        # print '=*'*20
        stbase.print_line(
            '(strategy_inday) zf:%s last_price:%s  yd_close_price:%s' %
            (zf, stock.last_price, stock.yesterday_close_price))

        stbase.TM.record_signal(code, '=={}=='.format(code))
        stbase.TM.record_signal(code, '-*' * 20, 'strategy_inday')
        stbase.TM.record_signal(code,
                                'strategy_inday signal occur. (zf >= -limit)')
        stbase.TM.record_signal(code, 'zf:{} limit:{}'.format(zf, limit))
        stbase.TM.record_signal(
            code, 'net_total:{} net_yd:{}'.format(stock.pos.net_total,
                                                  stock.pos.net_yd))
        if stock.pos.net_total >= num and strategy_inday_sell_count.get(
                code, 0) == 0:
            stbase.TM.record_signal(
                code, 'do sell: {} ,{}, {}'.format(code, st_price, num))
            stbase.TradeManager().xy_proxy.sell(code, st_price, num)
            strategy_inday_sell_count[code] = 1
Exemplo n.º 7
0
 def onExit(self):
     import stbase
     stbase.println('onExit()..')
     stbase.controller.stop()
     stbase.println('-- Reached End. System Shutdown. -- ')