def collect_stock_info(): ebest = EBest("PROD") mongodb = MongoDBHandler() ebest.login() code_list = mongodb.find_items({}, "stocklab", "code_info") target_code = set([item["단축코드"] for item in code_list]) #today = datetime.today().strftime("%Y%m%d") today = datetime.today().strftime("%Y%m%d") print(today) collect_list = mongodb.find_items({"날짜":today}, "stocklab", "price_info") \ .distinct("code") for col in collect_list: target_code.remove(col) for code in target_code: time.sleep(1) print(">>> code:", code) result_price = ebest.get_stock_price_by_code(code, "365") if len(result_price) > 0: #print(result_price) mongodb.insert_items(result_price, "stocklab", "price_info") result_credit = ebest.get_credit_trend_by_code(code, today) if len(result_credit) > 0: mongodb.insert_items(result_credit, "stocklab", "credit_info") result_short = ebest.get_short_trend_by_code(code, sdate=today, edate=today) if len(result_short) > 0: mongodb.insert_items(result_short, "stocklab", "short_info") '''
def setUp(self): self.mongodb = MongoDBHandler() self.mongodb._client["stocklab_test"]["corp_info"].drop() docs = [ { "item": "SamSung SDS", "related": "SamSung", "qty": 25, "tags": ["blank", "red"], "account": [14, 21], }, { "item": "LG CNS", "related": "LG", "qty": 50, "tags": ["red", "blank"], "account": [14, 21], }, { "item": "SK Telecom", "related": "SK", "qty": 100, "tags": ["red", "blank", "plain"], "account": [14, 21], }, { "item": "HYUNDAI MOBIS", "related": "HYUNDAI", "qty": 75, "tags": ["blank", "red"], "account": [22.85, 30], }, { "item": "SamSung SDI", "related": "SamSung", "qty": 25, "tags": ["blank", "red"], "account": [14, 21], }, { "item": "LG Telecom", "related": "LG", "qty": 50, "tags": ["red", "blank"], "account": [14, 21], }, { "item": "SK Innovation", "related": "SK", "qty": 50, "tags": ["red", "blank"], "account": [14, 21], }, ] self.mongodb._client["stocklab_test"]["corp_info"].insert_many(docs)
def collect_code_list(): ebest = EBest("DEMO") mongodb = MongoDBHandler() ebest.login() result = ebest.get_code_list("ALL") mongodb.delete_items({}, "stocklab", "code_info") mongodb.insert_items(result, "stocklab", "code_info")
def collect_stock_info(): ebest = EBest("DEMO") mongodb = MongoDBHandler() ebest.login() code_list = mongodb.find_item({}, "stocklab_ace", "code_info") target_code = set([item["단축코드"] for item in code_list]) today = datetime.today().strftime("%Y%m%d") print(today) collect_list = mongodb.find_item({"날짜": today}, "stocklab_ace", "price_info") \ .distinct("code") for col in collect_list: target_code.remove(col) for code in target_code: time.sleep(1) print("code:", code) """ result_price = ebest.get_stock_price_by_code(code, "1") if len(result_price) > 0: print(result_price) mongodb.insert_item_many(result_price, "stocklab_ace", "price_info") result_credit = ebest.get_credit_trend_by_code(code, today) if len(result_credit) > 0: mongodb.insert_item_many(result_credit, "stocklab_ace", "credit_info") """ result_short = ebest.get_short_trend_by_code(code, sdate=today, edate=today) if len(result_short) > 0: mongodb.insert_item_many(result_short, "stocklab_ace", "short_info") result_agent = ebest.get_agent_trend_by_code(code, fromdt=today, todt=today) if len(result_agent) > 0: mongodb.insert_item_many(result_agent, "stocklab_ace", "agent_info") # ebest.logout()
import os import sys sys.path.append(os.path.dirname(os.path.abspath(os.path.dirname(__file__)))) from flask import Flask, request from flask_restful import reqparse, abort, Api, Resource, fields, marshal_with import json # from bson import json_util from bson.objectid import ObjectId from stocklab.db_handler.mongodb_handler import MongoDBHandler mongodb = MongoDBHandler() class User(Resource): def get(self, id=None, users=None): if id: print("specific user") return "specific user" else: status = request.args.get('status', default="all", type=str) if status == 'all': result_list = list( mongodb.find_items({"isdeleted": "0"}, "stocklab", "user")) elif status in [ "buy_ordered", "buy_completed", "sell_ordered", "sell_completed" ]: result_list = list( mongodb.find_items({"status": status}, "stocklab", "user"))
class MongoDBHandlerTest(unittest.TestCase): def setUp(self): print("setup") self.mongodb = MongoDBHandler() self.mongodb._client["stocklab_test"]["corp_info"].drop() docs = [ { "item": "SamSung SDS", "related": "SamSung", "qty": 25, "tags": ["blank", "red"], "account": [ 14, 21 ] }, { "item": "LG CNS", "related": "LG", "qty": 50, "tags": ["red", "blank"], "account": [ 14, 21 ] }, { "item": "SK Telecom", "related": "SK", "qty": 100, "tags": ["red", "blank", "plain"], "account": [ 14, 21 ] }, { "item": "HYUNDAI MOBIS", "related": "HYUNDAI", "qty": 75, "tags": ["blank", "red"], "account": [ 22.85, 30 ] }, { "item": "SamSung SDI", "related": "SamSung", "qty": 25, "tags": ["blank", "red"], "account": [ 14, 21 ] }, { "item": "LG Telecom", "related": "LG", "qty": 50, "tags": ["red", "blank"], "account": [ 14, 21 ] }, { "item": "SK Innovation", "related": "SK", "qty": 50, "tags": ["red", "blank"], "account": [ 14, 21 ] } ] self.mongodb._client["stocklab_test"]["corp_info"].insert_many(docs) # def test_insert_item(self): # print(inspect.stack()[0][3]) # doc={"item":"Hyundai Card","related":"Hyundai","qty":25,"tags":["green","red"],"account":[10,11]} # _id = self.mongodb.insert_item(doc,"stocklab_test","corp_info") # assert _id # print(_id) # def test_insert_items(self): # print(inspect.stack()[0][3]) # docs = [ # { "item": "LG", "related": "LG", "qty": 25, # "tags": ["red"], "account": [ 10, 11 ] }, # { "item": "LG 화학", "related": "LG", "qty": 25, # "tags": ["green", "red"], "account": [ 10, 11 ] } # ] # ids = self.mongodb.insert_items(docs, "stocklab_test", "corp_info") # assert ids # print(ids) # def test_find_item(self): # print(inspect.stack()[0][3]) # doc = self.mongodb.find_item({"related": "LG"}, "stocklab_test", "corp_info") # pprint(doc) # def test_find_items(self): # print(inspect.stack()[0][3]) # cursor = self.mongodb.find_items({"tags.1": "red"}, "stocklab_test", "corp_info") # assert cursor # for doc in cursor: # pprint(doc) # def test_delete_items(self): # print(inspect.stack()[0][3]) # result = self.mongodb.delete_items({"related": "SamSung"}, "stocklab_test", "corp_info") # assert result # print(result.deleted_count) # def test_update_items(self): # print(inspect.stack()[0][3]) # result = self.mongodb.update_items({"item": "LG Telecom"}, {"$set": {"qty": 300}}, # "stocklab_test", "corp_info") # assert result # print("matched_count:" + str(result.matched_count)) # print("modified_count:" + str(result.modified_count)) # def test_aggregate(self): # print(inspect.stack()[0][3]) # pipeline = [ # { # "$match": { # "tags.1": "red"} # }, # { # "$group": { # "_id": "$related", # "sum_val": {"$sum": "$qty"} # } # } # ] # result = self.mongodb.aggregate(pipeline, "stocklab_test", "corp_info") # assert result # for item in result: # pprint(item) def test_text_search(self): print(inspect.stack()[0][3]) index_result = self.mongodb._client["stocklab_test"]["corp_info"].create_index( [('item', 'text'), ('related', 'text'), ('tags', 'text')]) print(index_result) result = self.mongodb.text_search("blank", "stocklab_test", "corp_info") assert result for item in result: pprint(item) def tearDown(self): pass
def get(self, id=None): if id: print("get method id :" + str(id)) # 종목코드 가져오기 if id == 1: ebest = EBest("DEMO") ebest.login() result_cod = ebest.get_code_list("ALL") print("get_code_list", len(result_cod)) result_ext_all = [] i = 0 if len(result_cod) > 0: print("t8407 주식현재가 시작") str_codes = "" for code in result_cod: str_codes = str_codes + code["단축코드"] i = i + 1 if len(str_codes) >= 300 or len(result_cod) == i: result_ext = ebest.get_current_price_by_shcodes( str_codes) result_ext_all.extend(result_ext) print("result_ext_all 건수", len(result_ext_all)) str_codes = "" # 코드정보와 주식현재가 병합 for code in result_cod: for extend in result_ext_all: if code["단축코드"] == extend["종목코드"]: code.update(extend) print("result_cod 건수", len(result_cod)) mongodb = MongoDBHandler() mongodb.delete_items({}, "stocklab", "code_info") mongodb.insert_items(result_cod, "stocklab", "code_info") # collect_code_list() # collect_stock_info() return { "errcode": 0, "errmsg": str(len(result_cod)) + " 건이 정상처리 되었습니다." } # 종목가격정보 가져오기 elif id == 2: ebest = EBest("DEMO") ebest.login() code_list = mongodb.find_items({}, "stocklab", "code_info") target_code = set([item["단축코드"] for item in code_list]) today = datetime.today().strftime("%Y%m%d") collect_list = mongodb.find_items({ "날짜": today }, "stocklab", "price_info").distinct("code") for col in collect_list: target_code.remove(col) for code in target_code: result_price = ebest.get_stock_price_by_code(code, "1") time.sleep(1) if len(result_price) > 0: mongodb.insert_items(result_price, "stocklab", "price_info") return { "errcode": 0, "errmsg": str(len(result)) + " 건이 정상처리 되었습니다." } # 모든 종목정보 가져오기 elif id == 3: ebest = EBest("DEMO") mongodb = MongoDBHandler() ebest.login() code_list = mongodb.find_items({}, "stocklab", "code_info") target_code = set([item["단축코드"] for item in code_list]) today = datetime.today().strftime("%Y%m%d") print(today) collect_list = mongodb.find_items({ "날짜": today }, "stocklab", "price_info").distinct("code") for col in collect_list: target_code.remove(col) for code in target_code: time.sleep(1) print("code:", code) result_price = ebest.get_stock_price_by_code(code, "1") if len(result_price) > 0: print(result_price) mongodb.insert_items(result_price, "stocklab", "price_info") result_credit = ebest.get_credit_trend_by_code(code, today) if len(result_credit) > 0: mongodb.insert_items(result_credit, "stocklab", "credit_info") result_short = ebest.get_short_trend_by_code(code, sdate=today, edate=today) if len(result_short) > 0: mongodb.insert_items(result_short, "stocklab", "short_info") result_agent = ebest.get_agent_trend_by_code(code, fromdt=today, todt=today) if len(result_agent) > 0: mongodb.insert_items(result_agent, "stocklab", "agent_info") # 일자별 주가정보에서 평균 거래량 가져오기(거래량 증가 종목 찾기) elif id == 4: ebest = EBest("DEMO") ebest.login() mongodb = MongoDBHandler() code_list = list( mongodb.find_items({}, "stocklab", "code_info")) target_code = set([item["단축코드"] for item in code_list]) # print(target_code) today = datetime.today().strftime("%Y%m%d") fromday = (datetime.today() - timedelta(days=30)).strftime("%Y%m%d") print(len(code_list)) inc_codes = [] vol_codes = [] loop_cnt = 0 for code in code_list: if int(code["누적거래량"]) < 10000: continue loop_cnt = loop_cnt + 1 if loop_cnt % 100 == 0 and len(inc_codes) > 0: print(str(loop_cnt % 100) + "//" + str(len(inc_codes))) print(code["단축코드"] + "진행율 : " + str((loop_cnt / len(code_list) * 100))) mongodb.insert_items(inc_codes, "stocklab", "check_volume") inc_codes.clear() results = ebest.get_stock_chart_by_code( code["단축코드"], "2", fromday, today) time.sleep(1) if len(results) > 0: # 평균 거래량 계산 tot_volume = 0 i_count = 0 for result in results: if int(result['거래량']) != 0: tot_volume = tot_volume + int(result['거래량']) i_count = i_count + 1 if i_count == 0 or tot_volume == 0: continue avg_volume = tot_volume / i_count inc_rate = int(result['거래량']) / avg_volume inc_code = { "code": code["단축코드"], "종목명": code["종목명"], "sdate": today, "avg_volume": int(avg_volume), "volume": int(result['거래량']) } vol_codes.append(inc_code) print("체크 종목 :" + code["종목명"] + " 거래량 [" + result['거래량'] + "] 평균 [" + str(avg_volume) + "] 비율[" + str(inc_rate) + "]") # 거래량이 5배 이상이면 종목 추가 if inc_rate > 5: inc_codes.append(inc_code) print("추가된 종목 :" + code["종목명"] + " 건수 : " + str(len(inc_codes))) if len(inc_codes) > 0: mongodb.insert_items(inc_codes, "stocklab", "check_volume") mongodb.insert_items(vol_codes, "stocklab", "volume") return { "errcode": 0, "errmsg": str(len(vol_codes)) + " 건이 정상처리 되었습니다." } elif id == 5: print("Error Id : " + str(id)) else: print("Error Id : " + str(id)) else: print("list of users") # collect_code_list() # collect_stock_info() return "list of users"
import time from datetime import datetime, timedelta from apscheduler.schedulers.background import BackgroundScheduler from stocklab.agent.ebest import EBest from stocklab.db_handler.mongodb_handler import MongoDBHandler from multiprocessing import Process ebest_demo = EBest("DEMO") ebest_demo.login() mongo = MongoDBHandler() def run_process_trading_scenario(code_list): p = Process(target=trading_scenario, args=(code_list,)) p.start() p.join() print("run process join") def check_buy_completed_order(code): """매수완료된 주문은 매도 주문 """ buy_completed_order_list = list(mongo.find_items({"$and":[ {"code": code}, {"status": "buy_completed"} ]}, "stocklab_demo", "order")) """매도 주문 """ for buy_completed_order in buy_completed_order_list: buy_price = buy_completed_order["매수완료"]["주문가격"] buy_order_no = buy_completed_order["매수완료"]["주문번호"] tick_size = ebest_demo.get_tick_size(int(buy_price))
class MongoDBHandlerTestCase(unittest.TestCase): def setUp(self): self.mongodb = MongoDBHandler() self.mongodb._client["stocklab_test"]["corp_info"].drop() docs = [ { "item": "SamSung SDS", "related": "SamSung", "qty": 25, "tags": ["blank", "red"], "account": [14, 21], }, { "item": "LG CNS", "related": "LG", "qty": 50, "tags": ["red", "blank"], "account": [14, 21], }, { "item": "SK Telecom", "related": "SK", "qty": 100, "tags": ["red", "blank", "plain"], "account": [14, 21], }, { "item": "HYUNDAI MOBIS", "related": "HYUNDAI", "qty": 75, "tags": ["blank", "red"], "account": [22.85, 30], }, { "item": "SamSung SDI", "related": "SamSung", "qty": 25, "tags": ["blank", "red"], "account": [14, 21], }, { "item": "LG Telecom", "related": "LG", "qty": 50, "tags": ["red", "blank"], "account": [14, 21], }, { "item": "SK Innovation", "related": "SK", "qty": 50, "tags": ["red", "blank"], "account": [14, 21], }, ] self.mongodb._client["stocklab_test"]["corp_info"].insert_many(docs) def test_insert_item(self): print(inspect.stack()[0][3]) doc = { "item": "SamSung Card", "related": "SamSung", "qty": 25, "tags": ["green", "red"], "account": [10, 11], } _id = self.mongodb.insert_item(doc, "stocklab_test", "corp_info") assert _id print(_id) def test_insert_items(self): print(inspect.stack()[0][3]) docs = [ { "item": "LG", "related": "LG", "qty": 25, "tags": ["red"], "account": [10, 11], }, { "item": "LG 화학", "related": "LG", "qty": 25, "tags": ["green", "red"], "account": [10, 11], }, ] ids = self.mongodb.insert_items(docs, "stocklab_test", "corp_info") assert ids print(ids) def test_find_item(self): print(inspect.stack()[0][3]) doc = self.mongodb.find_item({"related": "LG"}, "stocklab_test", "corp_info") pprint(doc) def test_find_items(self): print(inspect.stack()[0][3]) cursor = self.mongodb.find_items({"tags.1": "red"}, "stocklab_test", "corp_info") assert cursor for doc in cursor: pprint(doc) def test_delete_items(self): print(inspect.stack()[0][3]) result = self.mongodb.delete_items({"related": "SamSung"}, "stocklab_test", "corp_info") assert result print(result.deleted_count) def test_update_items(self): print(inspect.stack()[0][3]) result = self.mongodb.update_items({"item": "LG Telecom"}, {"$set": { "qty": 300 }}, "stocklab_test", "corp_info") assert result print("matched_count:" + str(result.matched_count)) print("modified_count:" + str(result.modified_count)) def test_aggregate(self): print(inspect.stack()[0][3]) pipeline = [ { "$match": { "tags.1": "red" } }, { "$group": { "_id": "$related", "sum_val": { "$sum": "$qty" } } }, ] result = self.mongodb.aggregate(pipeline, "stocklab_test", "corp_info") assert result for item in result: pprint(item) def test_text_search(self): print(inspect.stack()[0][3]) index_result = self.mongodb._client["stocklab_test"][ "corp_info"].create_index([("item", "text"), ("related", "text"), ("tags", "text")]) print(index_result) result = self.mongodb.text_search("blank", "stocklab_test", "corp_info") assert result for item in result: pprint(item) def tearDown(self): pass
def collect_stock_info(): ebest = EBest("DEMO") mongodb = MongoDBHandler() ebest.login() code_list = mongodb.find_items({},'stocklab','code_info') target_code = set([item["단축코드"] for item in code_list]) today = datetime.today().strftime("%Y%m%d") collect_list = mongodb.find_items({"날짜": today}, "stocklab", "price_info").distinct("code") print('중복발생', collect_list) for col in collect_list: target_code.remove(col) for code in target_code: time.sleep(1) print('code: ',code) result_price = ebest.get_stock_price_by_code(code, "1") if len(result_price) > 0: mongodb.insert_items(result_price, "stocklab", "price_info") result_credit = ebest.get_credit_trend_by_code(code,today) if len(result_credit) > 0: mongodb.insert_items(result_credit,"stocklab",'credit_info') result_short = ebest.get_short_trend_by_code(code,sdate=today,edate=today) if len(result_short) > 0: mongodb.insert_items(result_short,"stocklab",'short_info') result_agent = ebest.get_agent_trend_by_code(code,fromdt=today,todt=today) if len(result_agent) > 0: mongodb.insert_items(result_agent,"stocklab",'agent_info')
def collect_stock_info(): # 주식 가격을 수집하는 메서드 ebest = EBest("DEMO") mongodb = MongoDBHandler() ebest.login() code_list = mongodb.find_items({}, "stocklab", "code_info") target_code = set([item["단축코드"] for item in code_list]) today = datetime.today().strftime("%Y%m%d") print(today) collect_list = mongodb.find_items({"날짜": today}, "stocklab", "price_info").distinct("code") for col in collect_list: target_code.remove(col) # 오늘 날짜로 수집된 데이터가 있다면 제외시킨다. 데이터 중복을 방지. for code in target_code: time.sleep(1) print("code:", code) result_price = ebest.get_stock_price_by_code(code, "1") # 1일 치 데이터 수집 if len(result_price) > 0: print(result_price) mongodb.insert_items(result_price, "stocklab", "price_info") result_credit = ebest.get_credit_trend_by_code(code, today) if len(result_credit) > 0: mongodb.insert_items(result_credit, "stocklab", "credit_info") result_short = ebest.get_short_trend_by_code(code, sdate=today, edate=today) if len(result_short) > 0: mongodb.insert_items(result_short, "stocklab", "short_info") result_agent = ebest.get_agent_trend_by_code(code, fromdt=today, todt=today) if len(result_agent) > 0: mongodb.insert_items(result_agent, "stocklab", "agent_info")
def collect_stock_info(): ebest = EBest("DEMO") mongodb = MongoDBHandler() ebest.login() mongodb.delete_items({}, "stocklab", "price_info") mongodb.delete_items({}, "stocklab", "credit_info") mongodb.delete_items({}, "stocklab", "short_info") mongodb.delete_items({}, "stocklab", "agent_info") code_list = mongodb.find_items({}, "stocklab", "code_info") target_code = set([item["단축코드"] for item in code_list]) today = datetime.today().strftime("%Y%m%d") print(today) collect_list = mongodb.find_items({ "날짜": today }, "stocklab", "price_info").distinct("code") for col in collect_list: target_code.remove(col) for code in target_code: print(code) time.sleep(1) result_price = ebest.get_stock_price_by_code(code, "500") if len(result_price) > 0: print(result_price) mongodb.insert_items(result_price, "stocklab", "price_info") result_credit = ebest.get_credit_trend_by_code(code, today) if len(result_credit) > 0: print(result_credit) mongodb.insert_items(result_credit, "stocklab", "credit_info") result_short = ebest.get_short_trend_by_code(code, sdate="20180101", edate=today) if len(result_short) > 0: print(result_short) mongodb.insert_items(result_short, "stocklab", "short_info") result_agent = ebest.get_agent_trend_by_code(code, fromdt="20180101", todt=today) if len(result_agent) > 0: print(result_agent) mongodb.insert_items(result_agent, "stocklab", "agent_info")