Example #1
0
    def execute(self, job):
        broker = job.broker
        balance = broker.balance()
        enable_balance = balance[0].enable_balance

        q = Quotation()
        quote = q.get_realtime_quotes([self.GC001, self.R001])
        # for (k,v) in quote.items():
        #    string = v.__str__()
        #    print string.encode('utf-8')

        # job.result.invest = []
        job.result.clear()
        if quote[self.GC001].buy > quote[self.R001].buy and enable_balance >= self.GC001_UNIT * self.HAND:
            amount = int(enable_balance / self.HAND / self.GC001_UNIT) * self.GC001_UNIT
            # invest = Invest({'name':'GC001', 'code':self.GC001, 'amount':amount, 'price':quote[self.GC001].buy})
            invest = {'code': self.GC001, 'name': 'GC001', 'amount': amount, 'price': quote[self.GC001].buy, 'deal': Invest.INVEST_DEAL_SELL}
            job.result.append(invest)
            enable_balance = enable_balance - amount * self.HAND

        quote = q.get_realtime_quotes([self.GC001, self.R001])
        amount = int(enable_balance / self.HAND / self.R001_UNIT) * self.R001_UNIT
        # invest = Invest({'name':'R001', 'code':self.R001, 'amount':amount, 'price':quote[self.R001].buy})
        invest = {'code': self.R001, 'name': 'R001', 'amount': amount, 'price': quote[self.R001].buy, 'deal': Invest.INVEST_DEAL_SELL}
        job.result.append(invest)

        return True
Example #2
0
    def test_get_realtime_quotes(self):
        q = Quotation()
        d = q.get_realtime_quotes('sh')
        self.assertIn('sh', d)
        self.assertEqual(d['sh'].symbol, 'sh000001')
        self.assertEqual(d['sh'].code, 'sh')
        self.assertEqual(d['sh'].name, '上证指数')

        d = q.get_realtime_quotes(['000002', '601992'])
        self.assertIn('000002', d)
        self.assertIn('601992', d)
Example #3
0
    def execute(self, job):
        t = Trader.get_instance(job['trader'])
        balance = t.balance()
        enable_balance = balance[0].enable_balance

        q = Quotation()
        quote = q.get_realtime_quotes([self.GC001, self.R001])
        for (k,v) in quote.items():
            string = v.__str__()
            print string.encode('utf-8')

        if quote[self.GC001].buy > quote[self.R001].buy and enable_balance >= self.GC001_UNIT * self.HAND:
            amount=int(enable_balance/self.HAND/self.GC001_UNIT)*self.GC001_UNIT
            ret = t.sell(self.GC001, price=quote[self.GC001].buy, amount=int(enable_balance/self.HAND/self.GC001_UNIT)*self.GC001_UNIT)
            job.notice(str(ret))
            job.trade(str(ret))

        quote = q.get_realtime_quotes([self.GC001, self.R001])
        amount=int(enable_balance/self.HAND/self.R001_UNIT)*self.R001_UNIT
        ret = t.sell(self.R001, price=quote[self.R001].buy, amount=amount)

        job.notice(str(ret))
        job.trade(str(ret))


        #t = Trader.get_instance(job['trader'])
        #d = t.position()
        ##d = t.entrust()
        ##d = t.buy('601288', price=3.1, amount=100)
        ##d = t.sell('601288', price=3.5, amount=100)
        ##d = t.entrust()
        ##d = t.check_available_cancels()
        ##d = t.cancel_all_entrust()
        ##d = t.cancel_entrust('500', '601288')
        ##d = t.get_deal('2017-04-11')
        #job.notice(str(d))
        #job.trade(str(d))

        return 0
Example #4
0
    def execute(self, job):
        q = Quotation()
        hgt = q.get_hgt_capital()
        hgt_estimate = estimate_to_close(hgt)

        #沪港通指标
        buy = 0
        if hgt > self.HGT_LIM:
            buy = 1
        if get_exchange_time(
        ) > 30 * 60 and hgt_estimate > self.HGT_ESTIMATE_LIM:
            buy = 1

        if buy == 0:
            job.status = 0
            job.result.clear()
            return 0

        if buy == 1:
            job.status = 1

        t = Trader.get_instance(job['trader'])
        position = t.position()
        balance = t.balance()
        enable_balance = balance[0].enable_balance

        codes = []
        for i, v in enumerate(job.result):
            codes.append(v.code)
        quotes = q.get_realtime_quotes(codes)

        temp_result = copy.copy(job.result)
        job.result.clear()
        for i, v in enumerate(temp_result):
            #找到该股的持仓数据
            v_position = ''
            for p in position:
                if p.stock_code == v.code:
                    v_position = p
            #达到持仓上限
            if v_position != '' and v_position.current_amount >= v.amount:
                continue
            #设置买入参数
            v.price = quotes[v.code].buy
            if get_turnover_brokerage(v.price * v.amount) > enable_balance:
                continue
            enable_balance -= get_turnover_brokerage(v.price * v.amount)
            job.result.append(v)

        return 0
    def execute(self, job):
        q = Quotation()
        hgt = q.get_hgt_capital()
        hgt_estimate = estimate_to_close(hgt)

        #沪港通指标
        sell = 0
        if hgt < self.HGT_LIM:
            sell = 1
        if get_exchange_time(
        ) > 30 * 60 and hgt_estimate < self.HGT_ESTIMATE_LIM:
            sell = 1
        if hgt < self.HGT_KEEP_LIM:
            sell = 2
        if get_exchange_time(
        ) > 30 * 60 and hgt_estimate < self.HGT_KEEP_ESTIMATE_LIM:
            sell = 2

        if sell == 0:
            job.status = 0
            job.result.clear()
            return 0

        if sell != 0:
            job.status = 1

        t = Trader.get_instance(job['trader'])
        position = t.position()

        codes = []
        for i, v in enumerate(job.result):
            codes.append(v.code)
        quotes = q.get_realtime_quotes(codes)

        temp_result = copy.copy(job.result)
        job.result.clear()
        for i, v in enumerate(temp_result):
            #找到该股的持仓数据
            v_position = ''
            for p in position:
                if p.stock_code == v.code:
                    v_position = p
            #没有持仓
            if v_position == '':
                continue
            #盈利卖出
            if sell == 1:
                v.price = max(
                    quotes[v.code].buy,
                    round(
                        v_position.keep_cost_price *
                        (1 + self.INCOME_RATION_LIM), 2))
            #保本卖出
            elif sell == 2:
                v.price = v_position.keep_cost_price

            #设置卖出参数
            v.amount = v_position.enable_amount
            job.result.append(v)

        return 0
    def execute(self, job):
        t = Trader.get_instance(job['trader'])
        balance = t.balance()
        enable_balance = balance[0].enable_balance

        q = Quotation()
        quote = q.get_realtime_quotes([self.GC001, self.R001])
        #for (k,v) in quote.items():
        #    string = v.__str__()
        #    print string.encode('utf-8')

        #job.result.invest = []
        job.result.clear()
        if quote[self.GC001].buy > quote[self.R001].buy and enable_balance >= self.GC001_UNIT * self.HAND:
            amount=int(enable_balance/self.HAND/self.GC001_UNIT)*self.GC001_UNIT
            #invest = Invest({'name':'GC001', 'code':self.GC001, 'amount':amount, 'price':quote[self.GC001].buy})
            invest = {'name':'GC001', 'code':self.GC001, 'amount':amount, 'price':quote[self.GC001].buy}
            job.result.append(invest)
            enable_balance = enable_balance - amount * self.HAND

        quote = q.get_realtime_quotes([self.GC001, self.R001])
        amount=int(enable_balance/self.HAND/self.R001_UNIT)*self.R001_UNIT
        #invest = Invest({'name':'R001', 'code':self.R001, 'amount':amount, 'price':quote[self.R001].buy})
        invest = {'name':'R001', 'code':self.R001, 'amount':amount, 'price':quote[self.R001].buy}
        job.result.append(invest)


        #q = Quotation()
        #quote = q.get_realtime_quotes([self.GC001, self.R001])
        #for (k,v) in quote.items():
        #    string = v.__str__()
        #    print string.encode('utf-8')

        #if quote[self.GC001].buy > quote[self.R001].buy and enable_balance >= self.GC001_UNIT * self.HAND:
        #    amount=int(enable_balance/self.HAND/self.GC001_UNIT)*self.GC001_UNIT
        #    ret = t.sell(self.GC001, price=quote[self.GC001].buy, amount=int(enable_balance/self.HAND/self.GC001_UNIT)*self.GC001_UNIT)
        #    job.notice(str(ret))
        #    job.trade(str(ret))

        ##不要过于频繁操作
        #time.sleep(2)
        #balance = t.balance()
        #enable_balance = balance[0].enable_balance
        #quote = q.get_realtime_quotes([self.GC001, self.R001])
        #amount=int(enable_balance/self.HAND/self.R001_UNIT)*self.R001_UNIT
        #ret = t.sell(self.R001, price=quote[self.R001].buy, amount=amount)

        #job.notice(str(ret))
        #job.trade(str(ret))


        #t = Trader.get_instance(job['trader'])
        #d = t.position()
        ##d = t.entrust()
        ##d = t.buy('601288', price=3.1, amount=100)
        ##d = t.sell('601288', price=3.5, amount=100)
        ##d = t.entrust()
        ##d = t.check_available_cancels()
        ##d = t.cancel_all_entrust()
        ##d = t.cancel_entrust('500', '601288')
        ##d = t.get_deal('2017-04-11')
        #job.notice(str(d))
        #job.trade(str(d))

        return 0