도메인/금융공학

이동평균선의 통계적 근거

늘근이 2015. 4. 28. 21:04

그래.. 다돌렸다


2010 년 ~ 2015년 평균 수익률 24.93 % 

5일 - 120일 이동평균선 골드크로스 돌파시 매수 전략 평균 수익률 119.14 %

차이가 꽤난다. 


index.html


 

<head>

{% load staticfiles%}


</head>



<body>

<h1>고락가락 웹 분석기</h1>




<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.min.js"></script>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.27/angular.min.js"></script>

<script type="text/javascript" src="{% static 'django_ajax/js/jquery.ajax.min.js' %}"></script>


<script>

$(document).ready(function() {





$("#btn_update").on("click",function() {



//숫자가 끝날때까지


$(".stock").each(function() {



ajaxGet('/calcAjax', { 'targetStock' : $(this).text() },function(content){

console.log(content)

//주식코드, 초기가격, 마지막가격, 그냥수익률, 5-120선투자 수익률, 차이

var array = content.split(',')


stock_code = array[0]


//주식이 제대로 들어왔나 확인

var stockv_class = ".stockv" + "." + stock_code

$(stockv_class).text(array[0])


var initial_price_class = ".initial_price" + "." + stock_code

$(initial_price_class).text(array[1])


var adjc_class = ".adjc" + "." + stock_code

$(adjc_class).text(array[2])


var realRate_class = ".realRate" + "." + stock_code

$(realRate_class).text(array[3])


var rate_class = ".rate" + "." + stock_code

$(rate_class).text(array[4])


var diff_class = ".diff" + "." + stock_code

$(diff_class).text(array[5])


}); //end ajax

}); //end each function

}); //update button

    }); //end jquery


</script>


<button id="btn_update">update</button>


<p id="test"></p>


<table>


<tr>

    <th>targetStock</th>

    <th>v</th>

    <th>desc</th>

    <th>initial_price</th>

    <th>adjc</th>

    <th>realRate</th>

    <th>rate</th>

    <th>diff</th>

</tr>


<a id="anchor"></a>


{% for stock in stockNumber %}

<tr>

<td class="stock {{forloop.counter}} {{stock}}">{{stock}}</td>

<td class="stockv {{forloop.counter}} {{stock}}"></td>

<td class="desc {{forloop.counter}} {{stock}}" ></td>

<td class="initial_price {{forloop.counter}} {{stock}}"></td>

<td class="adjc {{forloop.counter}} {{stock}}"></td>

<td class="realRate {{forloop.counter}} {{stock}}"></td>

<td class="rate {{forloop.counter}} {{stock}}"></td>

<td class="diff {{forloop.counter}} {{stock}}"></td>

</tr>

{% endfor %}


</table>


</body>


views.py


@ajax

def calcAjax(request):


beg = datetime.datetime(2010,1,1)

end = datetime.datetime(2015,4,1)


resultList = []

exception = 0;

adjc = 1

initial_price = 0

rate = 0.00


try:


targetStock = request.GET.get('targetStock')

print(targetStock)


data = web.DataReader(targetStock + '.KS',"yahoo",beg,end) # Samsung Electronics

data['mean5'] = panda.stats.moments.rolling_mean(data['Adj Close'], 5)

data['mean10'] = panda.stats.moments.rolling_mean(data['Adj Close'], 10)

data['mean20'] = panda.stats.moments.rolling_mean(data['Adj Close'], 20)

data['mean60'] = panda.stats.moments.rolling_mean(data['Adj Close'], 60)

data['mean120'] = panda.stats.moments.rolling_mean(data['Adj Close'], 120)

data['diff'] = data['mean5']-data['mean120']

data['status'] = ""

data['cash'] = 0

# data['stock'] = 0

data['asset'] = 0


starttime = datetime.datetime.now()


i = 0 # loop initial value

cash = 0

stock = 0

asset = 0

temp = 0

flag = False

stockList = []

status = ""

adjc = 0

initial_flag = True # 초기 투자비용 계산용

initial_price = 0

rate = 0.00



while i < len(data):

diff = data['diff'].ix[i]

adjc = data['Adj Close'].ix[i] 

val = diff * temp 



if val < 0 and temp > 0 : ### buy

cash = cash - adjc # buying stock, 

stock = stock + adjc

flag = True

status = "BUY"


if initial_flag == True:

initial_price = adjc


elif val < 0 and temp <= 0: ### sell

cash = cash + adjc # selling stock, 

stock = 0

flag = False

status = "SELL"


else :

status = ""


#adjusting data

if flag == True:

stock = adjc


asset = cash + stock


if initial_price == 0 :

rate = 0

else :

rate = asset / initial_price * 100



# stockList.append(Stock(data.index[i],adjc,cash,stock,asset,rate,status))

temp = diff

i = i + 1 # no iterator, thus manual index increasing


realRate = (adjc-initial_price) / initial_price * 100


resultList.append(Result(targetStock, "" ,initial_price, adjc, realRate, rate, rate-realRate))

result = Result(targetStock, "" ,initial_price, adjc, realRate, rate, rate-realRate)


except :

tb = traceback.format_exc()

print(tb)

print(exception)

exception = exception + 1



# h = data.to_html(escape=False)

# ctx = Context({'data' : data,  'htmlData' : h, 'stockList' : stockList})

print(result.rate)

return HttpResponse(str(result.targetStock) + "," + str(result.initial_price) + ","

+ str(result.adjc) + "," + str(result.realRate) + ","

 + str(result.rate) + "," + str(result.diff)