Exemplo n.º 1
0
def test_remove_not_trading_stocks() :
    stockDayData = cxStockDayData()
    dataList = stockDayData.loadLogDataFromFile()
    remove_not_trading_stocks(dataList)
    del stockDayData.data[0:-1]
    stockDayData.data = dataList
    stockDayData.saveLogDataToFile(fileName = u'cxStockDayDataRemovedNotTradingStock.dat')
    del dataList
    del stockDayData
Exemplo n.º 2
0
def save_data_of_turtle_trading_per_stock() :

    S1_entryCondition = 20
    S1_exitCondition = 10
    S2_entryCondition = 55
    S2_exitCondition = 20
    N_condition = 20

    stockDayData = cxStockDayData()

    print 'start : load stockDayData'
    rawDataList = stockDayData.loadLogDataFromFile(fileName = \
                                    u'cxStockDayDataRemovedNotTradingStock.dat')
    print 'end : load stockDayData'

    if rawDataList == None :
        print 'loading stockDayData : failed'
        return None

    #remove_not_trading_stocks(rawDataList)

    turtleDataPerStockFile = cxFile(u'turtleDataPerStockFile.txt')
    
    stockNum = len(rawDataList)

    #stockNum = 1

    for stockIndex in range(0,stockNum) :
        turtleDataPerStockFile.write(u'%s\n'%rawDataList[stockIndex][0])
        
        dayDataList = rawDataList[stockIndex][1]
        dayDataLen = len(dayDataList)

        for dayIndex in range(0, dayDataLen) :
            ( currentDate, currentMaxPrice, currentMinPrice, 
              currentClosingPrice, currentTradingVolumn, currentTradingCost ) = \
              dayDataList[dayIndex]

            S1_entryPrice = 0
            S1_exitPrice = 0
            S2_entryPrice = 0
            S2_exitPrice = 0
            n = 0               # 오늘의 변동성
            N = 0.0

            if dayIndex >= 1 :
                ( prevDate, prevMaxPrice, prevMinPrice, prevClosingPrice,
                    prevTradingVolumn, prevTradingCost ) = dayDataList[dayIndex-1]

                n = max( [ fabs( currentMaxPrice - currentMinPrice ),
                           fabs( currentMaxPrice - prevClosingPrice ),
                           fabs( prevClosingPrice - currentMinPrice ) ] )

            if dayIndex >= S1_entryCondition :
                S1_entryPrice = \
                    max( [ maxPrice 
                            for date, maxPrice, minPrice, closingPrice,
                                tradingVolumn, tradingCost 
                            in  dayDataList[(dayIndex-S1_entryCondition):dayIndex] ] )

            if dayIndex >= S1_exitCondition :
                S1_exitPrice = \
                    min( [ minPrice 
                            for date, maxPrice, minPrice, closingPrice,
                                tradingVolumn, tradingCost
                            in  dayDataList[(dayIndex-S1_exitCondition):dayIndex] ] )

            if dayIndex >= S2_entryCondition :
                S2_entryPrice = \
                    max( [ maxPrice
                            for date, maxPrice, minPrice, closingPrice,
                                tradingVolumn, tradingCost
                            in  dayDataList[(dayIndex-S2_entryCondition):dayIndex] ] )
            
            if dayIndex >= S2_exitCondition :
                S2_exitPrice = \
                    min( [ minPrice
                            for date, maxPrice, minPrice, closingPrice,
                                tradingVolumn, tradingCost
                            in  dayDataList[(dayIndex-S2_exitCondition):dayIndex] ] )

            if dayIndex >= ( N_condition + 1 ) :

                N = calculate_N( [ [ maxPrice, minPrice, closingPrice ]
                                   for date, maxPrice, minPrice, closingPrice,
                                       tradingVolumn, tradingCost
                                   #in  dayDataList[(dayIndex-N_condition-1):dayIndex] ] , 
                                   in  dayDataList[(dayIndex-N_condition):dayIndex+1] ] , 
                                 N_condition )

            turtleDataPerStockFile.write(u'\t%d %d %d %d %d %d %d %d %d %f\n' \
                    % ( currentDate, currentMaxPrice, currentMinPrice, currentClosingPrice,
                        S1_entryPrice, S1_exitPrice, S2_entryPrice, S2_exitPrice, n, N ) )
   
    del rawDataList
    turtleDataPerStockFile.close()