コード例 #1
0
ファイル: rrEstimate.py プロジェクト: e94046165/2019NTUcourse
def computeReturnRate(priceVec, stockType):
	capital=1000	# Initial available capital
	capitalOrig=capital	 # original capital
	dataCount=len(priceVec)				# day size
	suggestedAction=np.zeros((dataCount,1))	# Vec of suggested actions
	stockHolding=np.zeros((dataCount,1))  	# Vec of stock holdings
	total=np.zeros((dataCount,1))	 	# Vec of total asset
	realAction=np.zeros((dataCount,1))	# Real action, which might be different from suggested action. For instance, when the suggested action is 1 (buy) but you don't have any capital, then the real action is 0 (hold, or do nothing). 
	# Run through each day
	for ic in range(dataCount):
		currentPrice=priceVec[ic]	# current price
		suggestedAction[ic]=myStrategy(priceVec[0:ic], currentPrice, stockType)		# Obtain the suggested action
		# get real action by suggested action
		if ic>0:
			stockHolding[ic]=stockHolding[ic-1]	# The stock holding from the previous day
		if suggestedAction[ic]==1:	# Suggested action is "buy"
			if stockHolding[ic]==0:		# "buy" only if you don't have stock holding
				stockHolding[ic]=capital/currentPrice # Buy stock using cash
				capital=0	# Cash
				realAction[ic]=1
		elif suggestedAction[ic]==-1:	# Suggested action is "sell"
			if stockHolding[ic]>0:		# "sell" only if you have stock holding
				capital=stockHolding[ic]*currentPrice # Sell stock to have cash
				stockHolding[ic]=0	# Stocking holding
				realAction[ic]=-1
		elif suggestedAction[ic]==0:	# No action
			realAction[ic]=0
		else:
			assert False
		total[ic]=capital+stockHolding[ic]*currentPrice	# Total asset, including stock holding and cash 
	returnRate=(total[-1]-capitalOrig)/capitalOrig		# Return rate of this run
	return returnRate
コード例 #2
0
def calculateReturnRate(file, stocksType):
    # stocksType = "SPY" or "IAU" or "DSI" , "LQD"
    # read file
    df = pd.read_csv(file)
    adjClose = df["Adj Close"].values  # get adj close
    dataCount = len(adjClose)  # day size

    # init.
    capital = 1  # 持有資金
    capitalOrig = capital  # cost
    suggestedAction = np.zeros((dataCount, 1))  # 判斷action
    stockHolding = np.zeros((dataCount, 1))  # 持有股票
    total = np.zeros((dataCount, 1))  # 結算資金
    realAction = np.zeros((dataCount, 1))  # 實際action

    # run each day
    for ic in range(dataCount):
        currPrice = adjClose[ic]  # 當天價格
        suggestedAction[ic] = myStrategy(adjClose[0:ic], currPrice,
                                         stocksType)  # 取得當天action

        # get real action by suggested action
        if ic > 0:
            # 更新手上持有股票
            stockHolding[ic] = stockHolding[ic - 1]
        if suggestedAction[ic] == 1:
            # 若未持有股票: 買
            if stockHolding[ic] == 0:
                stockHolding[ic] = capital / currPrice  # 買入股票
                capital = 0  # 持有資金
                realAction[ic] = 1
        elif suggestedAction[ic] == -1:
            # 若持有股票: 賣
            if stockHolding[ic] > 0:
                capital = stockHolding[ic] * currPrice  # 賣出股票
                stockHolding[ic] = 0  # 持有股票
                realAction[ic] = -1
        elif suggestedAction[ic] == 0:
            # 不買不賣
            realAction[ic] = 0
        else:
            assert False
        # 當天結算資金
        total[ic] = capital + stockHolding[ic] * currPrice
    # 最終盈利率
    returnRate = (total[-1] - capitalOrig) / capitalOrig
    return returnRate
コード例 #3
0
evalDays = 14
action = np.zeros((evalDays,1))
realAction = np.zeros((evalDays,1))
total = np.zeros((evalDays,1))
total[0] = capital
Holding = 0.0
openPricev = dailyOhlcv["open"].tail(evalDays).values
clearPrice = dailyOhlcv.iloc[-3]["close"]

for ic in range(evalDays,0,-1):
    flag = 1
    dailyOhlcvFile = dailyOhlcv.head(len(dailyOhlcv)-ic)
    dateStr = dailyOhlcvFile.iloc[-1,0]
    minutelyOhlcvFile = minutelyOhlcv.head((np.where(minutelyOhlcv.iloc[:,0].str.split(expand=True)[0].values==dateStr))[0].max()+1)
    
    action[evalDays-ic] = myStrategy(dailyOhlcvFile,minutelyOhlcvFile,openPricev[evalDays-ic])
    currPrice = openPricev[evalDays-ic]
    if action[evalDays-ic] == 1:
        if Holding == 0 and capital > transFee:
            Holding = (capital-transFee)/currPrice
            capital = 0
            realAction[evalDays-ic] = 1
    elif action[evalDays-ic] == -1:
        if Holding > 0 and Holding*currPrice > transFee:
            capital = Holding*currPrice - transFee
            Holding = 0
            realAction[evalDays-ic] = -1
    elif action[evalDays-ic] == 0:
        realAction[evalDays-ic] = 0
    else:
        assert False
コード例 #4
0
import pandas as pd
from myStrategy import myStrategy

df = pd.read_csv(sys.argv[1])
adjClose = df["Adj Close"].values
capital = 1
capitalOrig = capital
dataCount = len(adjClose)
suggestedAction = np.zeros((dataCount, 1))
stockHolding = np.zeros((dataCount, 1))
total = np.zeros((dataCount, 1))
realAction = np.zeros((dataCount, 1))
total[0] = capital
for ic in range(dataCount):
    currPrice = adjClose[ic]
    suggestedAction[ic] = myStrategy(adjClose[0:ic], currPrice)
    if ic > 0:
        stockHolding[ic] = stockHolding[ic - 1]
    if suggestedAction[ic] == 1:
        if stockHolding[ic] == 0:
            stockHolding[ic] = capital / currPrice
            capital = 0
            realAction[ic] = 1
    elif suggestedAction[ic] == -1:
        if stockHolding[ic] > 0:
            capital = stockHolding[ic] * currPrice
            stockHolding[ic] = 0
            realAction[ic] = -1
    elif suggestedAction[ic] == 0:
        realAction[ic] = 0
    else: