<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>챗GPT와 Gemini의 코딩 기술을 이용해 python 코딩 &#8211; 준우블로그</title>
	<atom:link href="https://junewoo.com/tag/%EC%B1%97gpt%EC%99%80-gemini%EC%9D%98-%EC%BD%94%EB%94%A9-%EA%B8%B0%EC%88%A0%EC%9D%84-%EC%9D%B4%EC%9A%A9%ED%95%B4-python-%EC%BD%94%EB%94%A9/feed/" rel="self" type="application/rss+xml" />
	<link>https://junewoo.com</link>
	<description>또 하나의 준우블로그 사이트</description>
	<lastBuildDate>Sat, 16 Mar 2024 11:03:21 +0000</lastBuildDate>
	<language>ko-KR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
<site xmlns="com-wordpress:feed-additions:1">199246569</site>	<item>
		<title>주식 자동 매매 API 환경 구축 2</title>
		<link>https://junewoo.com/%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-api-%ed%99%98%ea%b2%bd-%ea%b5%ac%ec%b6%95-2/</link>
					<comments>https://junewoo.com/%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-api-%ed%99%98%ea%b2%bd-%ea%b5%ac%ec%b6%95-2/#respond</comments>
		
		<dc:creator><![CDATA[cvilla]]></dc:creator>
		<pubDate>Fri, 16 Feb 2024 12:05:02 +0000</pubDate>
				<category><![CDATA[인공지능]]></category>
		<category><![CDATA[App key]]></category>
		<category><![CDATA[App secret 값을 설정]]></category>
		<category><![CDATA[config.yaml 설정]]></category>
		<category><![CDATA[KoreaStockAutoTrade.py 설정]]></category>
		<category><![CDATA[주식 자동 매매 API 환경 구축 2]]></category>
		<category><![CDATA[챗GPT와 Gemini의 코딩 기술을 이용해 python 코딩]]></category>
		<category><![CDATA[편집 도구 Visual Studio Code 설정]]></category>
		<guid isPermaLink="false">https://junewoo.com/?p=1275</guid>

					<description><![CDATA[<p>Contents 0. 인공지능을 이용한 주식 자동 매매 코딩 연구 1. 한국투자증권 계좌, KIS Developers 서비스 신청, Python 설치, Visual Studio Code 설치 2. Visual Studio ... <a title="주식 자동 매매 API 환경 구축 2" class="read-more" href="https://junewoo.com/%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-api-%ed%99%98%ea%b2%bd-%ea%b5%ac%ec%b6%95-2/" aria-label="주식 자동 매매 API 환경 구축 2에 대해 더 자세히 알아보세요">더 읽어보기</a></p>
<p>The post <a rel="nofollow" href="https://junewoo.com/%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-api-%ed%99%98%ea%b2%bd-%ea%b5%ac%ec%b6%95-2/">주식 자동 매매 API 환경 구축 2</a> appeared first on <a rel="nofollow" href="https://junewoo.com">준우블로그</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Contents</p>
<table style="border-collapse: collapse; width: 100.131%; height: 178px;">
<tbody>
<tr style="height: 51px;">
<td style="width: 100%; height: 51px; background-color: #f7f7f2;"><a href="https://junewoo.com/%ec%9d%b8%ea%b3%b5%ec%a7%80%eb%8a%a5%ec%9d%84-%ec%9d%b4%ec%9a%a9%ed%95%9c-%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-%ec%bd%94%eb%94%a9-%ec%97%b0%ea%b5%ac/"><span style="color: #000000;">0. 인공지능을 이용한 주식 자동 매매 코딩 연구</span></a></td>
</tr>
<tr style="height: 51px;">
<td style="width: 100%; height: 51px;"><a href="https://junewoo.com/%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-api-%ed%99%98%ea%b2%bd-%ea%b5%ac%ec%b6%95/">1. 한국투자증권 계좌, KIS Developers 서비스 신청, Python 설치, <span style="font-size: inherit; font-family: inherit;">Visual Studio Code 설치</span></a></td>
</tr>
<tr style="height: 51px;">
<td style="width: 100%; height: 51px; background-color: #f5da0c;"><a href="https://junewoo.com/%EC%A3%BC%EC%8B%9D-%EC%9E%90%EB%8F%99-%EB%A7%A4%EB%A7%A4-api-%ED%99%98%EA%B2%BD-%EA%B5%AC%EC%B6%95-2/">2. Visual Studio Code 설정, config.yaml 설정, KoreaStockAutoTrade.py 설정, 주식 종목 정리</a></td>
</tr>
<tr style="height: 25px;">
<td style="width: 100%; height: 25px;"><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://junewoo.com/%EC%A3%BC%EC%8B%9D-%EC%9E%90%EB%8F%99-%EB%A7%A4%EB%A7%A4-api-%ED%99%98%EA%B2%BD-%EA%B5%AC%EC%B6%95-3/">3. Visual Studio Code 편집기 path 설정, <span style="font-size: inherit; font-family: inherit;">윈도우(Windows) 10 환경 변수 Path 설정, </span><span style="font-size: inherit; font-family: inherit;">주식 종목 발굴과 선택</span></a></span></td>
</tr>
</tbody>
</table>
<h2>주식 자동 매매 API 환경 구축 2</h2>
<p>주식 자동 매매 API 환경 구축 2 : 주식 매매에서 전략이 70% 전술이 30%입니다. 전략은 종목 선택과 자금 운용 방법이고, 전술은 종목 수익률 결정, 손절 비율과 매도 결정이라 할 수 있습니다. 주식을 하면서 제일 힘든 부분이 &#8220;매도&#8221;로 수익 또는 손실의 실현입니다. 자동 매매를 적절히 사용하면서 개인의 장점을 잘 살리는 것이 인공지능 시대에 적응하고 경제적 자유를 얻는 방법이라 생각합니다. python 코딩을 이용해서 자동 매매를 하면서 매일 종목 List와 운용 자금 규모는 사용자가 정합니다. 매수 시점은 시가를 기준으로 어느 정도(0.01~3%) 상승 시로 정하며, 매도 시점은 시가를 기준으로 (2~10%) 상승 시로 정해줍니다. 장 종료 시 까지 프로그램을 작동 시키면 그날의 정리 매매가 자동으로 이루어지게 코딩 되어 있습니다. 프로그램을 종료 시키거나, Ctrl+C를 누르면 자동 매매 python API가 종료됩니다. 여러분의 투자 방법을 인공지능 코딩의 도움으로 조금씩 개선해 나간다면 훌륭한 매매 전문가로 만들 수 있습니다.</p>
<p>준우블로그 메뉴&gt; 게시판 &gt; 자료실에  기본 파일config.yaml과 KoreaStockAutoTrade.py이 있습니다.</p>
<p>MS의 챗GPT 4.0, 2024년 구글의 Gemini로 &#8220;python 코딩&#8221;을 해 보았습니다.</p>
<p>부족한 부분은 계속 보완해 나가며, 지속적인 학습과 관심으로 더 좋은 방법을 만들어 보려 합니다.</p>
<p>챗GPT와 Gemini의 코딩 기술을 이용해 python 코딩을 하고 이를 이용해 주식 자동 매매를 하려 합니다.</p>
<p>인공지능의 코딩(python 코딩) 기술이 전문가 이상으로 좋습니다.</p>
<p>종목 발굴과 당일 상승 종목 유망주를 인공지능이 분석하는 python 코딩도 만들어 보려 합니다.</p>
<h2>2-1. 편집 도구 Visual Studio Code 설정</h2>
<p>a. 자료실에 있는 python 구동 파일 2개를 사용자 PC의 C:\루트에 압축을 풀면 C:\python&gt;에 config.yaml 과 KoreaStockAutoTrade.py이 생성됩니다.</p>
<p>b. 먼저 Visual Studio Code를 실행합니다.</p>
<p>c. Visual Studio Code 메뉴에서 File&gt;Open Folder 에서 C:\python&gt;를 선택합니다.</p>
<p>d. C:\python&gt;폴더의 config.yaml을 엽니다.</p>
<div>
<div>e. 한국투자증권 API 서비스 신청 시 받은 App key, App secret 값을 설정합니다.</div>
<div></div>
<div>APP_KEY: &#8220;***&#8221;</div>
<div>APP_SECRET: &#8220;***&#8221;</div>
<div>본인의 계좌번호 앞 8자리를 입력합니다.</div>
<div>CANO: &#8220;********&#8221;</div>
</div>
<p>Ctrl+S key를 눌러 저장합니다.</p>
<p>f. Visual Studio Code 실행창에서 아래 붉은색 부분을 수정 후 Ctrl+S key로 저장합니다.</p>
<pre id="code_1633400140094" class="bash" style="margin: 20px auto 0px; display: block; overflow: auto; padding: 20px; color: #383a42; background: #f8f8f8; font-size: 14px; font-family: 'SF Mono', Menlo, Consolas, Monaco, monospace; border: 1px solid #ebebeb; line-height: 1.71; cursor: default; z-index: 1; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" data-ke-language="bash" data-ke-type="codeblock"><code class="hljs">#홈페이지에서 API서비스 신청시 받은 Appkey, Appsecret 값 설정
<span style="color: #ff0000;">APP_KEY: "TEST"</span>
<span style="color: #ff0000;">APP_SECRET: "TEST"</span>

#계좌번호 앞 8자리
<span style="color: #ff0000;">CANO: "TEST"</span>
#계좌번호 뒤 2자리
ACNT_PRDT_CD: "01"

#실전투자
URL_BASE: "https://openapi.koreainvestment.com:9443"
#모의투자
# URL_BASE: "https://openapivts.koreainvestment.com:29443"

#디스코드 웹훅 URL
DISCORD_WEBHOOK_URL: ""</code></pre>
<p><img data-recalc-dims="1" fetchpriority="high" decoding="async" class="alignnone wp-image-1280" src="https://i0.wp.com/junewoo.com/wp-content/uploads/2024/02/noname2.png?resize=860%2C881&#038;ssl=1" alt="" width="860" height="881" srcset="https://i0.wp.com/junewoo.com/wp-content/uploads/2024/02/noname2.png?resize=293%2C300&amp;ssl=1 293w, https://i0.wp.com/junewoo.com/wp-content/uploads/2024/02/noname2.png?resize=1000%2C1024&amp;ssl=1 1000w, https://i0.wp.com/junewoo.com/wp-content/uploads/2024/02/noname2.png?resize=768%2C786&amp;ssl=1 768w, https://i0.wp.com/junewoo.com/wp-content/uploads/2024/02/noname2.png?w=1391&amp;ssl=1 1391w" sizes="(max-width: 860px) 100vw, 860px" /></p>
<h2>2-2. KoreaStockAutoTrade.py 설정</h2>
<p>a. C:\python&gt;폴더의 KoreaStockAutoTrade.py을 엽니다. 터미널 창에서 설치합니다.</p>
<p>b. 설치 안 된 import request는 C:\python&gt;pip install requests로 설치합니다.</p>
<p>c. 설치 안 된 import yaml은 C:\python&gt;pip install pyyaml로 설치합니다.<br />
(맥 사용자는 C:\python&gt;pip3 install *** 와 같이 pip3 명령어를 사용합니다.)</p>
<p>d. 코드 실행은 오른쪽 위 삼각형 ▷ 모양을 누르면 실행됩니다.</p>
<p>e. 자동 매매 종료는 터미널 창에서 Ctrl+C를 누르거나 프로그램을 종료합니다.</p>
<p><img data-recalc-dims="1" decoding="async" class="alignnone wp-image-1284" src="https://i0.wp.com/junewoo.com/wp-content/uploads/2024/02/noname3-1.png?resize=860%2C869&#038;ssl=1" alt="" width="860" height="869" srcset="https://i0.wp.com/junewoo.com/wp-content/uploads/2024/02/noname3-1.png?resize=297%2C300&amp;ssl=1 297w, https://i0.wp.com/junewoo.com/wp-content/uploads/2024/02/noname3-1.png?resize=1012%2C1024&amp;ssl=1 1012w, https://i0.wp.com/junewoo.com/wp-content/uploads/2024/02/noname3-1.png?resize=768%2C777&amp;ssl=1 768w, https://i0.wp.com/junewoo.com/wp-content/uploads/2024/02/noname3-1.png?w=1391&amp;ssl=1 1391w" sizes="(max-width: 860px) 100vw, 860px" /></p>
<h2>2-3. KoreaStockAutoTrade.py 코드 내용 살펴보기</h2>
<pre id="code_1633400140094" class="bash" style="margin: 20px auto 0px; display: block; overflow: auto; padding: 20px; color: #383a42; background: #f8f8f8; font-size: 14px; font-family: 'SF Mono', Menlo, Consolas, Monaco, monospace; border: 1px solid #ebebeb; line-height: 1.71; cursor: default; z-index: 1; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" data-ke-language="bash" data-ke-type="codeblock"><code class="hljs">
import requests
import json
import datetime
import time
import yaml

with open('config.yaml', encoding='UTF-8') as f:
    _cfg = yaml.load(f, Loader=yaml.FullLoader)
APP_KEY = _cfg['APP_KEY']
APP_SECRET = _cfg['APP_SECRET']
ACCESS_TOKEN = ""
CANO = _cfg['CANO']
ACNT_PRDT_CD = _cfg['ACNT_PRDT_CD']
DISCORD_WEBHOOK_URL = _cfg['DISCORD_WEBHOOK_URL']
URL_BASE = _cfg['URL_BASE']

def send_message(msg):
    """디스코드 메세지 전송"""
    now = datetime.datetime.now()
    message = {"content": f"[{now.strftime('%Y-%m-%d %H:%M:%S')}] {str(msg)}"}
    requests.post(DISCORD_WEBHOOK_URL, data=message)
    print(message)

def get_access_token():
    """토큰 발급"""
    headers = {"content-type":"application/json"}
    body = {"grant_type":"client_credentials",
    "appkey":APP_KEY, 
    "appsecret":APP_SECRET}
    PATH = "oauth2/tokenP"
    URL = f"{URL_BASE}/{PATH}"
    res = requests.post(URL, headers=headers, data=json.dumps(body))
    ACCESS_TOKEN = res.json()["access_token"]
    return ACCESS_TOKEN
    
def hashkey(datas):
    """암호화"""
    PATH = "uapi/hashkey"
    URL = f"{URL_BASE}/{PATH}"
    headers = {
    'content-Type' : 'application/json',
    'appKey' : APP_KEY,
    'appSecret' : APP_SECRET,
    }
    res = requests.post(URL, headers=headers, data=json.dumps(datas))
    hashkey = res.json()["HASH"]
    return hashkey

def get_current_price(code="005930"):
    """현재가 조회"""
    PATH = "uapi/domestic-stock/v1/quotations/inquire-price"
    URL = f"{URL_BASE}/{PATH}"
    headers = {"Content-Type":"application/json", 
            "authorization": f"Bearer {ACCESS_TOKEN}",
            "appKey":APP_KEY,
            "appSecret":APP_SECRET,
            "tr_id":"FHKST01010100"}
    params = {
    "fid_cond_mrkt_div_code":"J",
    "fid_input_iscd":code,
    }
    res = requests.get(URL, headers=headers, params=params)
    return int(res.json()['output']['stck_prpr'])

def get_target_price(code="005930"):
    """변동성 돌파 전략으로 매수 목표가 조회"""
    PATH = "uapi/domestic-stock/v1/quotations/inquire-daily-price"
    URL = f"{URL_BASE}/{PATH}"
    headers = {"Content-Type":"application/json", 
        "authorization": f"Bearer {ACCESS_TOKEN}",
        "appKey":APP_KEY,
        "appSecret":APP_SECRET,
        "tr_id":"FHKST01010400"}
    params = {
    "fid_cond_mrkt_div_code":"J",
    "fid_input_iscd":code,
    "fid_org_adj_prc":"1",
    "fid_period_div_code":"D"
    }
    res = requests.get(URL, headers=headers, params=params)
    stck_oprc = int(res.json()['output'][0]['stck_oprc']) #오늘 시가
    stck_hgpr = int(res.json()['output'][1]['stck_hgpr']) #전일 고가
    stck_lwpr = int(res.json()['output'][1]['stck_lwpr']) #전일 저가
    <span style="color: #ff0000;">target_price = stck_oprc + (stck_hgpr - stck_lwpr) * 0.1</span>
    return target_price

def get_stock_balance():
    """주식 잔고조회"""
    PATH = "uapi/domestic-stock/v1/trading/inquire-balance"
    URL = f"{URL_BASE}/{PATH}"
    headers = {"Content-Type":"application/json", 
        "authorization":f"Bearer {ACCESS_TOKEN}",
        "appKey":APP_KEY,
        "appSecret":APP_SECRET,
        "tr_id":"TTTC8434R",
        "custtype":"P",
    }
    params = {
        "CANO": CANO,
        "ACNT_PRDT_CD": ACNT_PRDT_CD,
        "AFHR_FLPR_YN": "N",
        "OFL_YN": "",
        "INQR_DVSN": "02",
        "UNPR_DVSN": "01",
        "FUND_STTL_ICLD_YN": "N",
        "FNCG_AMT_AUTO_RDPT_YN": "N",
        "PRCS_DVSN": "01",
        "CTX_AREA_FK100": "",
        "CTX_AREA_NK100": ""
    }
    res = requests.get(URL, headers=headers, params=params)
    stock_list = res.json()['output1']
    evaluation = res.json()['output2']
    stock_dict = {}
    send_message(f"====주식 보유잔고====")
    for stock in stock_list:
        if int(stock['hldg_qty']) &gt; 0:
            stock_dict[stock['pdno']] = stock['hldg_qty']
            send_message(f"{stock['prdt_name']}({stock['pdno']}): {stock['hldg_qty']}주")
            time.sleep(0.1)
    send_message(f"주식 평가 금액: {evaluation[0]['scts_evlu_amt']}원")
    time.sleep(0.1)
    send_message(f"평가 손익 합계: {evaluation[0]['evlu_pfls_smtl_amt']}원")
    time.sleep(0.1)
    send_message(f"총 평가 금액: {evaluation[0]['tot_evlu_amt']}원")
    time.sleep(0.1)
    send_message(f"=================")
    return stock_dict

def get_balance():
    """현금 잔고조회"""
    PATH = "uapi/domestic-stock/v1/trading/inquire-psbl-order"
    URL = f"{URL_BASE}/{PATH}"
    headers = {"Content-Type":"application/json", 
        "authorization":f"Bearer {ACCESS_TOKEN}",
        "appKey":APP_KEY,
        "appSecret":APP_SECRET,
        "tr_id":"TTTC8908R",
        "custtype":"P",
    }
    params = {
        "CANO": CANO,
        "ACNT_PRDT_CD": ACNT_PRDT_CD,
        "PDNO": "005930",
        "ORD_UNPR": "65500",
        "ORD_DVSN": "01",
        "CMA_EVLU_AMT_ICLD_YN": "Y",
        "OVRS_ICLD_YN": "Y"
    }
    res = requests.get(URL, headers=headers, params=params)
    cash = res.json()['output']['ord_psbl_cash']
    send_message(f"주문 가능 현금 잔고: {cash}원")
    return int(cash)

def buy(code="005930", qty="1"):
    """주식 시장가 매수"""  
    PATH = "uapi/domestic-stock/v1/trading/order-cash"
    URL = f"{URL_BASE}/{PATH}"
    data = {
        "CANO": CANO,
        "ACNT_PRDT_CD": ACNT_PRDT_CD,
        "PDNO": code,
        "ORD_DVSN": "01",
        "ORD_QTY": str(int(qty)),
        "ORD_UNPR": "0",
    }
    headers = {"Content-Type":"application/json", 
        "authorization":f"Bearer {ACCESS_TOKEN}",
        "appKey":APP_KEY,
        "appSecret":APP_SECRET,
        "tr_id":"TTTC0802U",
        "custtype":"P",
        "hashkey" : hashkey(data)
    }
    res = requests.post(URL, headers=headers, data=json.dumps(data))
    if res.json()['rt_cd'] == '0':
        send_message(f"[매수 성공]{str(res.json())}")
        return True
    else:
        send_message(f"[매수 실패]{str(res.json())}")
        return False

def sell(code="005930", qty="1"):
    """주식 시장가 매도"""
    PATH = "uapi/domestic-stock/v1/trading/order-cash"
    URL = f"{URL_BASE}/{PATH}"
    data = {
        "CANO": CANO,
        "ACNT_PRDT_CD": ACNT_PRDT_CD,
        "PDNO": code,
        "ORD_DVSN": "01",
        "ORD_QTY": qty,
        "ORD_UNPR": "0",
    }
    headers = {"Content-Type":"application/json", 
        "authorization":f"Bearer {ACCESS_TOKEN}",
        "appKey":APP_KEY,
        "appSecret":APP_SECRET,
        "tr_id":"TTTC0801U",
        "custtype":"P",
        "hashkey" : hashkey(data)
    }
    res = requests.post(URL, headers=headers, data=json.dumps(data))
    if res.json()['rt_cd'] == '0':
        send_message(f"[매도 성공]{str(res.json())}")
        return True
    else:
        send_message(f"[매도 실패]{str(res.json())}")
        return False

# 자동매매 시작
try:
    ACCESS_TOKEN = get_access_token()

    <span style="color: #ff0000;">symbol_list = ["036540","001780","900250","252990","033230"] # 매수 희망 종목 리스트</span>
    bought_list = [] # 매수 완료된 종목 리스트
    total_cash = get_balance() # 보유 현금 조회
    stock_dict = get_stock_balance() # 보유 주식 조회
    for sym in stock_dict.keys():
        bought_list.append(sym)
    <span style="color: #ff0000;">target_buy_count = 3 # 매수할 종목 수</span>
    <span style="color: #ff0000;">buy_percent = 0.33 # 종목당 매수 금액 비율</span>
    buy_amount = total_cash * buy_percent  # 종목별 주문 금액 계산
    soldout = False
    <span style="color: #ff0000;">profit_percent = 0.03 # 매도 기준 수익률</span>

    send_message("===국내 주식 자동매매 프로그램을 시작합니다===")
    while True:
        t_now = datetime.datetime.now()
        t_9 = t_now.replace(hour=9, minute=0, second=0, microsecond=0)
        t_start = t_now.replace(hour=9, minute=5, second=0, microsecond=0)
        t_sell = t_now.replace(hour=15, minute=15, second=0, microsecond=0)
        t_exit = t_now.replace(hour=15, minute=20, second=0,microsecond=0)
        today = datetime.datetime.today().weekday()
        if today == 5 or today == 6:  # 토요일이나 일요일이면 자동 종료
            send_message("주말이므로 프로그램을 종료합니다.")
            break
        if t_9 &lt; t_now &lt; t_start and soldout == False: # 잔여 수량 매도
            for sym, qty in stock_dict.items():
                sell(sym, qty)
            soldout == True
            bought_list = []
            stock_dict = get_stock_balance()
        if t_start &lt; t_now &lt; t_sell :  # AM 09:05 ~ PM 03:15 : 매수
            for sym in symbol_list:
                if len(bought_list) &lt; target_buy_count:
                    if sym in bought_list:
                        continue
                    target_price = get_target_price(sym)
                    current_price = get_current_price(sym)
                    if target_price &lt; current_price: buy_qty = 0 # 매수할 수량 초기화 buy_qty = int(buy_amount // current_price) if buy_qty &gt; 0:
                            send_message(f"{sym} 목표가 달성({target_price} &lt; {current_price}) 매수를 시도합니다.") result = buy(sym, buy_qty) if result: soldout = False bought_list.append(sym) get_stock_balance() elif current_price &gt;= target_price * (1+profit_percent) and sym in bought_list:
                        <span style="color: #ff0000;">sell_qty = int(stock_dict[sym] * 0.4)</span>
                        sell(sym, sell_qty)
                        send_message(f"{sym} 수익률({current_price} / {target_price}) 달성 매도를 시도합니다.")            
                        get_stock_balance()
            time.sleep(1)
            if t_now.minute == 30 and t_now.second &lt;= 5: 
                get_stock_balance()
                time.sleep(5)
        if t_sell &lt; t_now &lt; t_exit:  # PM 03:15 ~ PM 03:20 : 일괄 매도
            if soldout == False:
                stock_dict = get_stock_balance()
                for sym, qty in stock_dict.items():
                    sell(sym, qty)
                soldout = True
                bought_list = []
                time.sleep(1)
        if t_exit &lt; t_now:  # PM 03:20 ~ :프로그램 종료
            send_message("프로그램을 종료합니다.")
            break
except Exception as e:
    send_message(f"[오류 발생]{e}")
    time.sleep(1)
</code></pre>
<p>빨간색으로 표시된 부분을 중심으로 설명합니다.</p>
<p><code class="hljs"><span style="color: #ff0000;">target_price = stck_oprc + (stck_hgpr - stck_lwpr) * 0.1</span></code></p>
<p>0.1의 값은 시가 보다 0.1% 상승 시 매수로 정한 값입니다.</p>
<p><code class="hljs"><span style="color: #ff0000;">profit_percent = 0.03 # 매도 기준 수익률</span></code></p>
<p>0.03의 값은 매수가 기준 3% 수익 돌파 시 매도로 정한 값입니다.</p>
<p><code class="hljs"><span style="color: #ff0000;">sell_qty = int(stock_dict[sym] * 0.4)</span></code></p>
<p>0.4의 값은 매수 값 0.1% + 매도 값 0.3% = 0.4% 값입니다.</p>
<p>위 세 가지 값은 매일 종목의 예상 변동 폭에 따라 장 시작 전 오전 8시경 정합니다.</p>
<p>값 변경 후 Ctrl+S로 저장한 후 실행 시켜 봅니다.</p>
<p>테스트 후 Ctrl+C로 프로그램을 중지합니다.</p>
<h2>2-4. 주식 종목 정리 방법</h2>
<p><code class="hljs"><span style="color: #ff0000;">symbol_list = ["036540","001780","900250","252990","033230"] # 매수 희망 종목 리스트</span></code></p>
<p>종목 리스트는 엑셀이나 메모장에 정리해서 매매 당일 오전 8시경 수정합니다.</p>
<p>&#8220;900250&#8221;,&#8221;027580&#8243;,&#8221;033230&#8243;,&#8221;036540&#8243;,&#8221;252990&#8243;,&#8221;001780&#8243;</p>
<p>크리스탈신소재 900250<br />
상보 027580<br />
인성정보 033230<br />
SFA반도체 036540<br />
샘씨엔에스 252990<br />
알루코 001780</p>
<p><code class="hljs"><span style="color: #ff0000;">target_buy_count = 3 # 매수할 종목 수</span></code></p>
<p>당일 매매할 종목의 수를 나타내며 전략에 따라서 1~200정도로 합니다.</p>
<p><code class="hljs"><span style="color: #ff0000;">buy_percent = 0.33 # 종목 당 매수 금액 비율</span></code></p>
<p>매매 종목당 비율로 100만 원 운용 시 종목당 33만 원이 정해집니다.</p>
<h2><a href="https://junewoo.com/%EC%A3%BC%EC%8B%9D-%EC%9E%90%EB%8F%99-%EB%A7%A4%EB%A7%A4-api-%ED%99%98%EA%B2%BD-%EA%B5%AC%EC%B6%95-3/">주식 자동 매매 API 환경 구축 3</a></h2>
<div class='kakaotalk_link' style='float:right;'><a href="javascript:SendKakao('준우블로그', '주식 자동 매매 API 환경 구축 2', 'https://junewoo.com/%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-api-%ed%99%98%ea%b2%bd-%ea%b5%ac%ec%b6%95-2/');"><img data-recalc-dims="1" src="https://i0.wp.com/junewoo.com/wp-content/plugins/kakao-talk-link/kakaotalk.png?w=860&#038;ssl=1" alt='Smart phone only'></a></div><div class="korea-sns"><div class="korea-sns-post korea-sns-pos-left"><div class="korea-sns-button korea-sns-facebook" OnClick="SendSNS('facebook', '주식 자동 매매 API 환경 구축 2 - 준우블로그', 'https://junewoo.com/%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-api-%ed%99%98%ea%b2%bd-%ea%b5%ac%ec%b6%95-2/', '', 0);"  style="background-image:url('https://junewoo.com/wp-content/plugins/korea-sns/icons/facebook.png');"></div><div class="korea-sns-button korea-sns-twitter" OnClick="SendSNS('twitter', '주식 자동 매매 API 환경 구축 2 - 준우블로그', 'https://junewoo.com/%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-api-%ed%99%98%ea%b2%bd-%ea%b5%ac%ec%b6%95-2/', '', 0);"  style="background-image:url('https://junewoo.com/wp-content/plugins/korea-sns/icons/twitter.png');"></div><div class="korea-sns-button korea-sns-telegram" OnClick="SendSNS('telegram', '주식 자동 매매 API 환경 구축 2 - 준우블로그', 'https://junewoo.com/%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-api-%ed%99%98%ea%b2%bd-%ea%b5%ac%ec%b6%95-2/', '', 0);"  style="background-image:url('https://junewoo.com/wp-content/plugins/korea-sns/icons/telegram.png');"></div><div class="korea-sns-button korea-sns-naverband" OnClick="SendSNS('naverband', '주식 자동 매매 API 환경 구축 2 - 준우블로그', 'https://junewoo.com/%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-api-%ed%99%98%ea%b2%bd-%ea%b5%ac%ec%b6%95-2/', '', 0);"  style="background-image:url('https://junewoo.com/wp-content/plugins/korea-sns/icons/naverband.png');"></div><div class="korea-sns-button korea-sns-naverblog" OnClick="SendSNS('naverblog', '주식 자동 매매 API 환경 구축 2 - 준우블로그', 'https://junewoo.com/%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-api-%ed%99%98%ea%b2%bd-%ea%b5%ac%ec%b6%95-2/', '', 0);"  style="background-image:url('https://junewoo.com/wp-content/plugins/korea-sns/icons/naverblog.png');"></div><div class="korea-sns-button korea-sns-copyurl" OnClick="SendSNS('copyurl', '주식 자동 매매 API 환경 구축 2 - 준우블로그', 'https://junewoo.com/%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-api-%ed%99%98%ea%b2%bd-%ea%b5%ac%ec%b6%95-2/', '', 0);"  style="background-image:url('https://junewoo.com/wp-content/plugins/korea-sns/icons/copyurl.png');"></div></div><div style="clear:both;"></div></div><p>The post <a rel="nofollow" href="https://junewoo.com/%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-api-%ed%99%98%ea%b2%bd-%ea%b5%ac%ec%b6%95-2/">주식 자동 매매 API 환경 구축 2</a> appeared first on <a rel="nofollow" href="https://junewoo.com">준우블로그</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://junewoo.com/%ec%a3%bc%ec%8b%9d-%ec%9e%90%eb%8f%99-%eb%a7%a4%eb%a7%a4-api-%ed%99%98%ea%b2%bd-%ea%b5%ac%ec%b6%95-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1275</post-id>	</item>
	</channel>
</rss>
