나만의 로보 어드바이저(robo-advisor) 만들기 [5]

금융 2016. 9. 23. 16:23

 

 

 

 

오늘은 Python과 CybosPlus를 이용하여 장중 실시간 종목 가격 변동을 모니터링하고 특정 조건(매수 혹은 매도가 도달)만족시 이벤트를 생성하는 방법을 알아보자. 

 

먼저 첨부된 python 코드는, 깔끔하지는 않지만, 장중 정해준 종목을 계속 모니터링하다가 정해진 이벤트(트레이딩)이 발생할 경우 발생한 트레이딩 정보와 변동률을 화면에 뿌려주는 기능을 한다. 

이 코드를 조금 수정하면 이벤트가 발생한 경우에 사용자 펑션 호출하여 원하는 조건(매수 혹은 매도 조건 도달, 특정 가격이상 변동, 급작스런 가격변동 탐지등)을 만족하는가를 체크하고 만족하는 경우 특정 행동을 하도록 할 수 있을것이다.

 

특정 조건 만족시 매수, 혹은 매도를 실행하는 코드를 추가하면 실시간 트레이딩이 가능해지는데 이 부분은 다음 시간에 다루기로 한다.

 

 

 

코드 설명:

 

 

 

 

 

__author__ = 'Dr. Deeeep'

import win32com.client

import io

 

 

""" Memo

02: change rate

13: currentprice

18: currenttime

"""

activeFlag = [        1,        1,        0,       0,       0,       0,      0]

itemcode =   ['A017680','A052790','A032860', 'A900110', 'A033130']

############### deco, actoz, steel, EAH, cho

crArray =    [       '',       '',       '',      '',      '',  '']

 

class CpEvent0:

    #print("E0: flag=", activeFlag[0])

    instance = None

    def OnReceived(self):

        #print("E0: flag=", activeFlag[0])

        if (activeFlag[0]>0):

            currentprice = CpEvent0.instance.GetHeaderValue(13)

            changeprice = CpEvent0.instance.GetHeaderValue(2)

            changerate = round(((changeprice / (currentprice - changeprice)) * 100.0),2)

            crArray[0] = changerate

            prt_style_all(0, changerate, currentprice, changeprice)

            #prt_style(0, changerate, currentprice, changeprice)

            #print(itemcode[0], " ", changerate, "         E0: ", currentprice, "  ", changeprice);  

        

class CpEvent1:

    #print("E1: flag=", activeFlag[1])

    instance = None

    def OnReceived(self):

        #print("E1: flag=", activeFlag[1])

        if (activeFlag[1]>0):

            currentprice = CpEvent1.instance.GetHeaderValue(13)

            changeprice = CpEvent1.instance.GetHeaderValue(2)

            changerate = round(((changeprice / (currentprice - changeprice)) * 100.0),2)

            crArray[1] = changerate

            prt_style_all(1, changerate, currentprice, changeprice)

            #prt_style(1, changerate, currentprice, changeprice)

            #print(itemcode[1], "      ", changerate, "     E1: ", currentprice, "  ", changeprice);  

        

class CpEvent2:

    #print("E2: flag=", activeFlag[2])

    instance = None

    def OnReceived(self):

        #print("E2: flag=", activeFlag[2])

        if (activeFlag[2]>0):

            currentprice = CpEvent2.instance.GetHeaderValue(13)

            changeprice = CpEvent2.instance.GetHeaderValue(2)

            changerate = round(((changeprice / (currentprice - changeprice)) * 100.0),2)

            crArray[2] = changerate

            prt_style_all(2, changerate, currentprice, changeprice)

            #prt_style(2, changerate, currentprice, changeprice)

            #print(itemcode[2], "          ", changerate, " E2: ", currentprice, "  ", changeprice);  

 

class CpEvent3:

    #print("E3: flag=", activeFlag[3])

    instance = None

    def OnReceived(self):

        #print("E3: flag=", activeFlag[3])

        if (activeFlag[3]>0):

            currentprice = CpEvent3.instance.GetHeaderValue(13)

            changeprice = CpEvent3.instance.GetHeaderValue(2)

            changerate = round(((changeprice / (currentprice - changeprice)) * 100.0),2)

            crArray[3] = changerate

            prt_style_all(3, changerate, currentprice, changeprice)

            #prt_style(3, changerate, currentprice, changeprice)

            #print(itemcode[3], "          ", changerate, " E3: ", currentprice, "  ", changeprice);  

 

class CpEvent4:

    #print("E4: flag=", activeFlag[4])

    instance = None

    def OnReceived(self):

        #print("E4: flag=", activeFlag[4])

        if (activeFlag[4]>0):

            currentprice = CpEvent4.instance.GetHeaderValue(13)

            changeprice = CpEvent4.instance.GetHeaderValue(2)

            changerate = round(((changeprice / (currentprice - changeprice)) * 100.0),2)

            crArray[4] = changerate

            prt_style_all(4, changerate, currentprice, changeprice)

            #prt_style(4, changerate, currentprice, changeprice)

            #print(itemcode[4], "          ", changerate, " E4: ", currentprice, "  ", changeprice);  

 

def prt_style_all(classnum, chrate, curprice, chprice):

    print(classnum, ", ", enter_crArray(), curprice, ",", chprice)

 

def prt_style(classnum, chrate, curprice, chprice):

    print(classnum, enter_spc(classnum), chrate, ",", exit_spc(classnum), ",", curprice, ",", chprice)

    #print(itemcode[classnum], enter_spc(classnum), chrate, ", ", curprice, ", ", chprice)

 

def enter_crArray():

    crnum = len(crArray)

    rtnline = ""

    for n in range(0, crnum):

        rtnline = rtnline + str(crArray[n]) + ","

    return rtnline

    

def enter_spc(classn):

    spc = ""

    for n in range(0, classn):

        spc = spc + "     "

    return spc

 

def exit_spc(classn):

    spc = ""

    rev = 4 - classn

    for n in range(0, rev):

        spc = spc + "     "

    return spc

 

#not working

def sprintf(buf, fmt, args):

    buf.write(fmt % args)

 

    

inst0 = win32com.client.Dispatch("dscbo1.StockCur")

inst1 = win32com.client.Dispatch("dscbo1.StockCur")

inst2 = win32com.client.Dispatch("dscbo1.StockCur")

inst3 = win32com.client.Dispatch("dscbo1.StockCur")

inst4 = win32com.client.Dispatch("dscbo1.StockCur")

 

win32com.client.WithEvents(inst0, CpEvent0)

win32com.client.WithEvents(inst1, CpEvent1)

win32com.client.WithEvents(inst2, CpEvent2)

win32com.client.WithEvents(inst3, CpEvent3)

win32com.client.WithEvents(inst4, CpEvent4)

 

inst0.SetInputValue(0,itemcode[0])

inst1.SetInputValue(0,itemcode[1])

inst2.SetInputValue(0,itemcode[2])

inst3.SetInputValue(0,itemcode[3])

inst4.SetInputValue(0,itemcode[4])

 

CpEvent0.instance = inst0

CpEvent1.instance = inst1

CpEvent2.instance = inst2

CpEvent3.instance = inst3

CpEvent4.instance = inst4

 

inst0.Subscribe()

inst1.Subscribe()

inst2.Subscribe()

inst3.Subscribe()

inst4.Subscribe()

 

 

 

이벤트 발생시 해당 종목의 가격을 체크하여 정해진 STOPLOSS 보다 작거나 STOPGAIN보다 큰 경우 적절한 행동을 하도록 하려면 다음과 같은 class를 정해주면 된다.  

 

 

class CpEvent000:

    instance = None

    monitoring = 1  # 0 no, 1 yes

    def OnReceived(self):

        if (monitoring = 1)

            value13_cprice = CpEvent1.instance.GetHeaderValue(13)

            value18_ctime = CpEvent1.instance.GetHeaderValue(18)

            print("E1: ",value18_ctime,", ", value13_cprice)

            if (value13_cprice >= stopgain_price)

                print("E1: STOPGAIN, Exit")

                monitoring = 0 # 정해진 일을 이곳에 코딩

            elif (value13_cprice <= stoploss_price)

                print("E1: STOPLOASS, Exit")

                monitoring = 0  # 정해진 일을 이곳에 코딩

 

 

<to be continued..>

 

 

 

posted by Dr.Deeeep