def try_sell(self, code: str, what: str, order_price: int = None): return # fixme if not self.wallet.has(code): return holding = self.wallet.get(code) if not order_price: detail = list(stocks.get_details([code]))[0] order_price = detail.price order_total = order_price * holding.quantity holding_total = holding.price * holding.quantity try: traders.sell(code=code, price=order_price, count=holding.quantity) self.wallet.delete(code) OrderRecord( what=what, order_type='SELL', code=code, name=stocks.get_name(code), order_price=order_price, order_count=holding.quantity, total=order_total, earning_price=order_total - holding_total, earning_rate=calc.earnings_ratio(holding.price, order_price) ).summit(logger=self.logger, warren_session=self.warren_session) except: logging.exception('Failed to try to sell ' + code)
def try_buy(self, code: str, what: str, order_price: int = None, memo: str = None): if self.wallet.has(code): return if len(self.wallet.holdings) >= self.max_holding_count: logging.warning('The number of holdings has reached the maximum holdings.') return if not order_price: detail = list(stocks.get_details([code]))[0] order_price = detail.price order_count = int(100_0000 / order_price) order_total = order_price * order_count try: traders.buy(code=code, price=order_price, count=order_count) self.wallet.put(Holding(created=datetime.now(), code=code, count=order_count, price=order_price)) OrderRecord( what=what, order_type='BUY', code=code, name=stocks.get_name(code), order_price=order_price, order_count=order_count, total=order_total, memo=memo ).summit(logger=self.logger, warren_session=self.warren_session) except: logging.exception('Failed to try to buy ' + code)
def check_stop_line(self): details: Dict[str, stocks.StockDetail2] = { detail.code: detail for detail in stocks.get_details([holding.code for holding in self.wallet.holdings]) } for holding in self.wallet.holdings: detail = details.get(holding.code) if not detail: logging.warning('The detail is null: ' + holding.code) continue if not detail.price: logging.warning('The price is 0: ' + holding.code) continue cur_price = detail.price earnings_rate = calc.earnings_ratio(holding.price, cur_price) try: if earnings_rate < self.stop_line: # 손절 self.try_sell(code=holding.code, what=f'손절 {self.stop_line}%', order_price=detail.price) elif earnings_rate > self.bend_line: # 익절 self.try_sell(code=holding.code, what=f'익절 {self.bend_line}%', order_price=detail.price) except: logging.exception(f'Failed to sell {detail.code}')
def main(): cybos = win32com.client.Dispatch('CpUtil.CpCybos') # 크레온에서 씨발놈들아 'CpUtil.CpCybos' 이거다 assert cybos.IsConnect, 'Disconnected' assert ctypes.windll.shell32.IsUserAnAdmin(), 'Not administrator' connect() # 모든 종목 코드 가져오기 all_codes = [stock.code for stock in stocks.ALL_STOCKS] # 모든 종목에 대한 상세 정보 가져오기 details = stocks.get_details(all_codes) x = [] # 차트 데이터 가져오기 for code in all_codes: chart = charts.request_by_term( code=code, chart_type=charts.ChartType.MINUTE, begin=date.today() - timedelta(days=1), end=date.today() ) db.push(chart) print()
def main(): holdings = list(get_holdings()) details = st.get_details([code for code, _, _ in holdings]) details = {detail.code: detail for detail in details} for code, count, _ in holdings: price = details.get(code).price tr.sell(code=code, count=count, price=price)
def run(self): whitelist = [] for code in self.codes: try: ma_calc = metrics.get_calculator(code) ma_5_yst = ma_calc.get(5, pos=-1) ma_20_yst = ma_calc.get(20, pos=-1) ma_20_yst_2 = ma_calc.get(20, pos=-2) ma_60_yst = ma_calc.get(60, pos=-1) ma_120_yst = ma_calc.get(120, pos=-1) if ma_20_yst > ma_5_yst > ma_60_yst > ma_120_yst \ and ma_20_yst > ma_20_yst_2 \ and len([candle for candle in ma_calc.chart[-5:] if candle.open < candle.close]) > 0: whitelist.append(code) except: continue details = stocks.get_details(whitelist) # 모든 취급 종목에 대해... for detail in details: try: # 전일 기준 5MA, 20MA 구한다 ma_calc = metrics.get_calculator(detail.code) ma_5_yst = ma_calc.get(5, pos=-1) if detail.open < ma_5_yst <= detail.price < ma_5_yst * 1.025: self.try_buy( code=detail.code, what='5MA 상향돌파', order_price=detail.price, # memo=f''' # ma_20_yst > ma_5_yst > ma_60_yst > ma_120_yst # and ma_20_yst > ma_20_yst_2 # and len([candle for candle in ma_calc.chart[-5:] if candle.open < candle.close]) > 0 # and open < ma_5_yst <= cur_price < ma_5_yst * 1.025 # ''' ) except: logging.exception(f'Failed to simulate for {detail.code} in {self.name}')
sys.path.append(os.path.join(os.path.dirname(__file__), 'stocktock')) from model import Candle from utils import calc, log log.init(logging.DEBUG) import creon.provider from creon import charts, stocks logger = logging.getLogger() available_codes = stocks.get_availables() details: Dict[str, stocks.StockDetail2] = { detail.code: detail for detail in stocks.get_details(available_codes) } @dataclass class Holding: code: str count: int price: int is_5_beneath: bool = False is_10_beneath: bool = False max_price: int = 0 BUY_LIMIT = 100_0000