import numpy as np # A simple greedy approach def myActionSimple(priceMat, transFeeRate): # Explanation of my approach: # 1. Technical indicator used: Watch next day price # 2. if next day price > today price + transFee ==> buy # * buy the best stock # if next day price < today price + transFee ==> sell # * sell if you are holding stock # 3. You should sell before buy to get cash each day # default cash = 1000 hold = 0 # user definition nextDay = 1 dataLen, stockCount = priceMat.shape # day size & stock count stockHolding = np.zeros((dataLen,stockCount)) # Mat of stock holdings actionMat = [] # An k-by-4 action matrix which holds k transaction records. for day in range( 0, dataLen-nextDay ) : dayPrices = priceMat[day] # Today price of each stock nextDayPrices = priceMat[ day + nextDay ] # Next day price of each stock if day > 0: stockHolding[day] = stockHolding[day-1] # The stock holding from the previous action day buyStock = -1 # which stock should buy. No action when is -1 buyPrice = 0 # use how much cash to buy sellStock = [] # which stock should sell. No action when is null sellPrice = [] # get how much cash from sell bestPriceDiff = 0 # difference in today price & next day price of "buy" stock stockCurrentPrice = 0 # The current price of "buy" stock # Check next day price to "sell" for stock in range(stockCount) : todayPrice = dayPrices[stock] # Today price nextDayPrice = nextDayPrices[stock] # Next day price holding = stockHolding[day][stock] # how much stock you are holding if holding > 0 : # "sell" only when you have stock holding if nextDayPrice < todayPrice*(1+transFeeRate) : # next day price < today price, should "sell" sellStock.append(stock) # "Sell" sellPrice.append(holding * todayPrice) cash = holding * todayPrice*(1-transFeeRate) # Sell stock to have cash stockHolding[day][sellStock] = 0 # Check next day price to "buy" if cash > 0 : # "buy" only when you have cash for stock in range(stockCount) : todayPrice = dayPrices[stock] # Today price nextDayPrice = nextDayPrices[stock] # Next day price if nextDayPrice > todayPrice*(1+transFeeRate) : # next day price > today price, should "buy" diff = nextDayPrice - todayPrice*(1+transFeeRate) if diff > bestPriceDiff : # this stock is better bestPriceDiff = diff buyStock = stock stockCurrentPrice = todayPrice # "Buy" the best stock if buyStock >= 0 : buyPrice = cash stockHolding[day][buyStock] = cash*(1-transFeeRate) / stockCurrentPrice # Buy stock using cash cash = 0 # Save your action this day if buyStock >= 0 or len(sellStock) > 0 : action = [] if len(sellStock) > 0 : for i in range( len(sellStock) ) : action = [day, sellStock[i], -1, sellPrice[i]] actionMat.append( action ) if buyStock >= 0 : action = [day, -1, buyStock, buyPrice] actionMat.append( action ) return actionMat # A DP-based approach to obtain the optimal return def myAction01(priceMat, transFeeRate): actionMat = [] # An k-by-4 action matrix which holds k transaction records. return actionMat # An approach that allow non-consecutive K days to hold all cash without any stocks def myAction02(priceMat, transFeeRate, K): actionMat = [] # An k-by-4 action matrix which holds k transaction records. return actionMat # An approach that allow consecutive K days to hold all cash without any stocks def myAction03(priceMat, transFeeRate, K): actionMat = [] # An k-by-4 action matrix which holds k transaction records. return actionMat