Exemplo n.º 1
0
    def sell(self, code, unit_price=0, cnt=0, desc=""):
        kl = self.kls[code]
        data = kl.get_kabuka()
        (indexes, dates, open, high, low, close, volume) = self.data

        if self.stocks.has_key(code) == False:
            return False

        if unit_price == 0:
            unit_price = close[indexes[self.date]]

        if cnt == 0:
            cnt = self.stocks[code].get_cnt()

        s = self.stocks[code]
        buy_price = s.get_buy_price()
        price = unit_price * cnt
        pm = price - buy_price * cnt
        ret = s.sell(unit_price, cnt)
        if ret < 0:
            return False
        if ret == 0:
            del self.stocks[code]

        self.balance = self.balance + price - kf.get_charge(price)
        f.log(
            "[sell] C=%s, UP=%d, CNT=%d, P=%d, B=%d, PM=%d: R=[%s]"
            % (code, unit_price, cnt, price, self.balance, pm, desc)
        )
Exemplo n.º 2
0
    def buy(self, code, unit_price, cnt, desc=""):
        price = unit_price * cnt
        tmp_balance = self.balance - price - kf.get_charge(price)
        if tmp_balance < 0:
            return False

        if self.stocks.has_key(code):
            self.stocks[code].buy(unit_price, cnt)
        else:
            self.stocks[code] = StockInfo2(code, self.date, self.kls[code], unit_price, cnt)
        self.balance = tmp_balance
        f.log("[buy] C=%s, UP=%d, CNT=%d, P=%d, B=%d: R=[%s]" % (code, unit_price, cnt, price, self.balance, desc))
        return True
Exemplo n.º 3
0
    def trade(self, code):
        trader = self.traders[code]
        unit_price = trader.get_open(self.date)
        high = trader.get_high(self.date)
        low = trader.get_low(self.date)
        trade_mode_str = trader.get_curr_trade_mode()
        tmp_loan = self.loan
        tmp_balance = self.balance
        enter_ok = True
    
        if unit_price <= 0:
            enter_ok = False
        cnt = self._get_enter_cnt(code, unit_price)
        if cnt == 0:
            enter_ok = False
    
        if trade_mode_str == "SELL":
            tmp_price = low*cnt
            charge = kf.get_charge(tmp_price)
            tmp_loan = self.loan + tmp_price
            tmp_balance -= charge
        elif trade_mode_str == "BUY":
            tmp_price = high*cnt
            charge = kf.get_charge(tmp_price)
            tmp_balance = self.balance - tmp_price - charge
        if tmp_balance < 0:
            enter_ok = False
        
        tmp_loan = self.loan
        tmp_balance = self.balance
    
        
        (traded_cnt, traded_price, trade_mode_str) = trader.trade_1step(self.date, cnt, enter_ok)
        curr_holding_n = trader.get_curr_holding_n()
        charge = kf.get_charge(abs(traded_price))
        if curr_holding_n <= 0:
            del self.traders[code]
        if trade_mode_str == "SELL":
            tmp_loan = self.loan + traded_price
            tmp_balance -= charge
        elif trade_mode_str == "BUY":
            tmp_balance = self.balance - traded_price - charge
        else:
            return False

        self.loan = tmp_loan
        self.balance = tmp_balance
         
        if traded_cnt > 0:
            enter_release = "enter"
        elif traded_cnt < 0:
            enter_release = "release"
        else:
            enter_release = ""
        if enter_release != "":
            desc = trader.get_desc()
            (eval_price, holding_n) = self.get_eval()
            geneid = trader.get_geneid()
            fieldid = trader.get_fieldid()
            f.log("[%s:%s] C=%s, UP=%d, CNT=%d, P=%d, EV=%d, H=%d R=[%s:%s:%s]" %
                  (enter_release, trade_mode_str, code, abs(traded_price/traded_cnt), 
                   abs(traded_cnt), abs(traded_price), eval_price, holding_n, desc, geneid, fieldid))
    
        return True