💼 영웅문4 OpenAPI & VB.NET 자동매매 – 4단계: 실전 자동매매 시스템 구축 (고급편)

반응형

4단계: 실전 자동매매 시스템 구축 (고급편)

✅ STEP 4-1: 조건식 기반 실시간 자동매매

📌 조건식 목록 불러오기 

vb.net

axKH.GetConditionLoad()
vb.net

Private Sub axKH_OnReceiveConditionVer(...) Handles axKH.OnReceiveConditionVer
    Dim count = axKH.GetConditionCount()
    For i = 0 To count - 1
        Dim name = axKH.GetConditionName(i).Split("^"c)(0)
        Dim index = axKH.GetConditionName(i).Split("^"c)(1)
        lstConditions.Items.Add($"{name} [{index}]")
    Next
End Sub

📌 조건 실시간 등록 및 감지

vb.net

axKH.SendCondition("조건실시간", "급등주포착", 1, 1)
vb.net

Private Sub axKH_OnReceiveRealCondition(...) Handles axKH.OnReceiveRealCondition
    If e.strType = "I" Then ' 진입
        AutoBuy(e.sTrCode)
    ElseIf e.strType = "D" Then ' 이탈
        AutoSell(e.sTrCode)
    End If
End Sub

✅ STEP 4-2: 손절 / 익절 조건 설정

vb.net

Dim entryPrice As Double = 65000
Dim currentPrice As Double = axKH.GetCommRealData(code, 10)
Dim stopLoss = entryPrice * 0.97 ' -3%
Dim takeProfit = entryPrice * 1.05 ' +5%

If currentPrice <= stopLoss Then
    axKH.SendOrder("손절매", "1004", account, 2, code, quantity, 0, "03", "")
ElseIf currentPrice >= takeProfit Then
    axKH.SendOrder("익절매", "1005", account, 2, code, quantity, 0, "03", "")
End If

✅ STEP 4-3: 주문 및 체결 로그 CSV 저장

vb.net

Dim logPath As String = "C:\StockLogs\log.csv"
Using sw As New StreamWriter(logPath, True)
    sw.WriteLine($"{DateTime.Now},{code},{price},{volume},{orderType}")
End Using
markdown

📁 C:\StockLogs\
    └─ 2025-04-07_log.csv

✅ STEP 4-4: Telegram으로 체결 알림 보내기

📌 Telegram Bot API 호출

vb.net

Sub SendTelegram(message As String)
    Dim botToken = "YOUR_TELEGRAM_BOT_TOKEN"
    Dim chatId = "YOUR_CHAT_ID"
    Dim url = $"https://api.telegram.org/bot{botToken}/sendMessage?chat_id={chatId}&text={message}"
    Dim client As New WebClient()
    client.DownloadString(url)
End Sub
vb.net

SendTelegram($"[자동체결] {code} / {price}원 / {volume}주")

🧠 최종 구조 요약

기능구현 메서드설명
조건 실시간 등록 SendCondition, OnReceiveRealCondition 조건식 진입 시 자동매매 수행
손절/익절 조건 현재가 계산 후 조건부 매도 -3%, +5% 등 전략 설정 가능
로그 저장 StreamWriter.WriteLine() CSV 자동 로그 저장
Telegram 알림 Telegram API 호출 체결 시 실시간 메시지 발송

키워드 최적화: 키움증권 자동매매, 조건 실시간 매매, 손절 익절, Telegram 주식 알림, 자동매매 로그 저장, VB.NET 자동주문

반응형