import sys
import numpy as np
import pandas as pd
from student_test import myOptimAction

# Compute return rate over a given price Matrix & action Matrix
def computeReturnRate(priceMat, transFeeRate, actionMat):
    capital = 1000    # Initial available capital
    capitalOrig = capital     # original capital
    stockCount = len(priceMat[0])   # stack size
    suggestedAction = actionMat    # Mat of suggested actions
    actionCount = len(suggestedAction)
    
    stockHolding = np.zeros((actionCount,stockCount))   # Mat of stock holdings
    realAction = np.zeros((actionCount,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). 
    preDay = 0  # previous action day
    
    # Run through each action, should order by day
    for i in range(actionCount):
        actionVec = actionMat[ i ]
        day = actionVec[0] # The index of day
        a = actionVec[1] # The index of "from" stock
        b = actionVec[2] # The index of "to" stock
        z = actionVec[3] # The equivalent cash for such transaction.
        currentPriceVec = priceMat[day]  # current priceVec
        
        # check action day and equivalent cash
        if day >= preDay and day >= 0 and z > 0 :
            # get real action by suggested action
            if i > 0:
                stockHolding[i] = stockHolding[i-1]  # The stock holding from the previous action day
                preDay = day  # previous action day
            
            if a == -1 and b >= 0 and capital > 0 :  # Suggested action is "buy"
                currentPrice = currentPriceVec[b]  # The current price of stock
                if capital < z :  # "buy" allonly if you don't have enough capital
                    z = capital
                stockHolding[i][b] += z*(1-transFeeRate) / currentPrice # Buy stock using cash
                capital = capital - z  # Cash
                realAction[i] = 1
            elif b == -1 and a >= 0 and stockHolding[i][a] > 0 :  # Suggested action is "sell"
                currentPrice = currentPriceVec[a]  # The current price of stock
                sellStock = z / currentPrice
                if stockHolding[i][a] < sellStock :  # "sell" all only if you don't have enough stock holding
                    sellStock = stockHolding[i][a]
                getCash = sellStock * currentPrice*(1-transFeeRate)  # Sell stock to have cash
                capital = capital + getCash  # get cash from sell stock
                stockHolding[i][a] -= sellStock  # Stocking holding
                realAction[i] = -1
            elif a >= 0 and b >= 0 and stockHolding[i][a] > 0 :  # Suggested action is "buy" and "sell"
                currentPriceSell = currentPriceVec[a]  # The current price of sell stock
                currentPriceBuy = currentPriceVec[b]  # The current price of buy stock
                sellStock = z / currentPriceSell
                if stockHolding[i][a] < sellStock :  # "sell" all only if you don't have enough stock holding
                    sellStock = stockHolding[i][a]
                getCash = sellStock * currentPriceSell*(1-transFeeRate)  # Sell stock to have cash
                stockHolding[i][a] -= sellStock  # Stocking holding
                stockHolding[i][b] += getCash*(1-transFeeRate) / currentPriceBuy # Buy stock using cash
                realAction[i] = 2
            else:
                print( actionVec , stockHolding[i] )
                assert False
        else:
            print( actionVec )
            assert False
            
    # calculate total cash you get at last day
    total = capital
    print( capital, stockHolding[-1] )
    for stock in range(stockCount) :
        currentPriceVec = priceMat[ actionMat[-1][0] ]
        total += stockHolding[-1][stock] * currentPriceVec[stock]*(1-transFeeRate)  # Total asset, including stock holding and cash 
    print( total )
    returnRate=(total-capitalOrig)/capitalOrig  # Return rate of this run
    return returnRate
    
if __name__ == "__main__":
    file = sys.argv[1]    # input file
    df = pd.read_csv(file, delimiter=' ')
    transFeeRate= float(sys.argv[2])    # Rate for transaction fee
    priceMat = df.values    # Get price as the m×n matrix which holds n stocks' price over m days
    actionMat = myOptimAction(priceMat, transFeeRate)   # Obtain the suggested action
    # print( np.array(actionMat) )
    for action in actionMat :
        print( action )
    returnRate = computeReturnRate(priceMat, transFeeRate, actionMat)  # Compute return rate
    print( 'returnRate : ' + str(returnRate) )
e3634529424d2afd23b469ff962c9e2a4d0de201
<mask token>

<mask token>


def computeReturnRate(priceMat, transFeeRate, actionMat):
    capital = 1000
    capitalOrig = capital
    stockCount = len(priceMat[0])
    suggestedAction = actionMat
    actionCount = len(suggestedAction)
    stockHolding = np.zeros((actionCount, stockCount))
    realAction = np.zeros((actionCount, 1))
    preDay = 0
    for i in range(actionCount):
        actionVec = actionMat[i]
        day = actionVec[0]
        a = actionVec[1]
        b = actionVec[2]
        z = actionVec[3]
        currentPriceVec = priceMat[day]
        if day >= preDay and day >= 0 and z > 0:
            if i > 0:
                stockHolding[i] = stockHolding[i - 1]
                preDay = day
            if a == -1 and b >= 0 and capital > 0:
                currentPrice = currentPriceVec[b]
                if capital < z:
                    z = capital
                stockHolding[i][b] += z * (1 - transFeeRate) / currentPrice
                capital = capital - z
                realAction[i] = 1
            elif b == -1 and a >= 0 and stockHolding[i][a] > 0:
                currentPrice = currentPriceVec[a]
                sellStock = z / currentPrice
                if stockHolding[i][a] < sellStock:
                    sellStock = stockHolding[i][a]
                getCash = sellStock * currentPrice * (1 - transFeeRate)
                capital = capital + getCash
                stockHolding[i][a] -= sellStock
                realAction[i] = -1
            elif a >= 0 and b >= 0 and stockHolding[i][a] > 0:
                currentPriceSell = currentPriceVec[a]
                currentPriceBuy = currentPriceVec[b]
                sellStock = z / currentPriceSell
                if stockHolding[i][a] < sellStock:
                    sellStock = stockHolding[i][a]
                getCash = sellStock * currentPriceSell * (1 - transFeeRate)
                stockHolding[i][a] -= sellStock
                stockHolding[i][b] += getCash * (1 - transFeeRate
                    ) / currentPriceBuy
                realAction[i] = 2
            else:
                print(actionVec, stockHolding[i])
                assert False
        else:
            print(actionVec)
            assert False
    total = capital
    print(capital, stockHolding[-1])
    for stock in range(stockCount):
        currentPriceVec = priceMat[actionMat[-1][0]]
        total += stockHolding[-1][stock] * currentPriceVec[stock] * (1 -
            transFeeRate)
    print(total)
    returnRate = (total - capitalOrig) / capitalOrig
    return returnRate


<mask token>

<mask token>


def computeReturnRate(priceMat, transFeeRate, actionMat):
    capital = 1000
    capitalOrig = capital
    stockCount = len(priceMat[0])
    suggestedAction = actionMat
    actionCount = len(suggestedAction)
    stockHolding = np.zeros((actionCount, stockCount))
    realAction = np.zeros((actionCount, 1))
    preDay = 0
    for i in range(actionCount):
        actionVec = actionMat[i]
        day = actionVec[0]
        a = actionVec[1]
        b = actionVec[2]
        z = actionVec[3]
        currentPriceVec = priceMat[day]
        if day >= preDay and day >= 0 and z > 0:
            if i > 0:
                stockHolding[i] = stockHolding[i - 1]
                preDay = day
            if a == -1 and b >= 0 and capital > 0:
                currentPrice = currentPriceVec[b]
                if capital < z:
                    z = capital
                stockHolding[i][b] += z * (1 - transFeeRate) / currentPrice
                capital = capital - z
                realAction[i] = 1
            elif b == -1 and a >= 0 and stockHolding[i][a] > 0:
                currentPrice = currentPriceVec[a]
                sellStock = z / currentPrice
                if stockHolding[i][a] < sellStock:
                    sellStock = stockHolding[i][a]
                getCash = sellStock * currentPrice * (1 - transFeeRate)
                capital = capital + getCash
                stockHolding[i][a] -= sellStock
                realAction[i] = -1
            elif a >= 0 and b >= 0 and stockHolding[i][a] > 0:
                currentPriceSell = currentPriceVec[a]
                currentPriceBuy = currentPriceVec[b]
                sellStock = z / currentPriceSell
                if stockHolding[i][a] < sellStock:
                    sellStock = stockHolding[i][a]
                getCash = sellStock * currentPriceSell * (1 - transFeeRate)
                stockHolding[i][a] -= sellStock
                stockHolding[i][b] += getCash * (1 - transFeeRate
                    ) / currentPriceBuy
                realAction[i] = 2
            else:
                print(actionVec, stockHolding[i])
                assert False
        else:
            print(actionVec)
            assert False
    total = capital
    print(capital, stockHolding[-1])
    for stock in range(stockCount):
        currentPriceVec = priceMat[actionMat[-1][0]]
        total += stockHolding[-1][stock] * currentPriceVec[stock] * (1 -
            transFeeRate)
    print(total)
    returnRate = (total - capitalOrig) / capitalOrig
    return returnRate


if __name__ == '__main__':
    file = sys.argv[1]
    df = pd.read_csv(file, delimiter=' ')
    transFeeRate = float(sys.argv[2])
    priceMat = df.values
    actionMat = myOptimAction(priceMat, transFeeRate)
    for action in actionMat:
        print(action)
    returnRate = computeReturnRate(priceMat, transFeeRate, actionMat)
    print('returnRate : ' + str(returnRate))

import sys
import numpy as np
import pandas as pd
from student_test import myOptimAction


def computeReturnRate(priceMat, transFeeRate, actionMat):
    capital = 1000
    capitalOrig = capital
    stockCount = len(priceMat[0])
    suggestedAction = actionMat
    actionCount = len(suggestedAction)
    stockHolding = np.zeros((actionCount, stockCount))
    realAction = np.zeros((actionCount, 1))
    preDay = 0
    for i in range(actionCount):
        actionVec = actionMat[i]
        day = actionVec[0]
        a = actionVec[1]
        b = actionVec[2]
        z = actionVec[3]
        currentPriceVec = priceMat[day]
        if day >= preDay and day >= 0 and z > 0:
            if i > 0:
                stockHolding[i] = stockHolding[i - 1]
                preDay = day
            if a == -1 and b >= 0 and capital > 0:
                currentPrice = currentPriceVec[b]
                if capital < z:
                    z = capital
                stockHolding[i][b] += z * (1 - transFeeRate) / currentPrice
                capital = capital - z
                realAction[i] = 1
            elif b == -1 and a >= 0 and stockHolding[i][a] > 0:
                currentPrice = currentPriceVec[a]
                sellStock = z / currentPrice
                if stockHolding[i][a] < sellStock:
                    sellStock = stockHolding[i][a]
                getCash = sellStock * currentPrice * (1 - transFeeRate)
                capital = capital + getCash
                stockHolding[i][a] -= sellStock
                realAction[i] = -1
            elif a >= 0 and b >= 0 and stockHolding[i][a] > 0:
                currentPriceSell = currentPriceVec[a]
                currentPriceBuy = currentPriceVec[b]
                sellStock = z / currentPriceSell
                if stockHolding[i][a] < sellStock:
                    sellStock = stockHolding[i][a]
                getCash = sellStock * currentPriceSell * (1 - transFeeRate)
                stockHolding[i][a] -= sellStock
                stockHolding[i][b] += getCash * (1 - transFeeRate
                    ) / currentPriceBuy
                realAction[i] = 2
            else:
                print(actionVec, stockHolding[i])
                assert False
        else:
            print(actionVec)
            assert False
    total = capital
    print(capital, stockHolding[-1])
    for stock in range(stockCount):
        currentPriceVec = priceMat[actionMat[-1][0]]
        total += stockHolding[-1][stock] * currentPriceVec[stock] * (1 -
            transFeeRate)
    print(total)
    returnRate = (total - capitalOrig) / capitalOrig
    return returnRate


if __name__ == '__main__':
    file = sys.argv[1]
    df = pd.read_csv(file, delimiter=' ')
    transFeeRate = float(sys.argv[2])
    priceMat = df.values
    actionMat = myOptimAction(priceMat, transFeeRate)
    for action in actionMat:
        print(action)
    returnRate = computeReturnRate(priceMat, transFeeRate, actionMat)
    print('returnRate : ' + str(returnRate))

import sys
import numpy as np
import pandas as pd
from student_test import myOptimAction

# Compute return rate over a given price Matrix & action Matrix
def computeReturnRate(priceMat, transFeeRate, actionMat):
    capital = 1000    # Initial available capital
    capitalOrig = capital     # original capital
    stockCount = len(priceMat[0])   # stack size
    suggestedAction = actionMat    # Mat of suggested actions
    actionCount = len(suggestedAction)
    
    stockHolding = np.zeros((actionCount,stockCount))   # Mat of stock holdings
    realAction = np.zeros((actionCount,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). 
    preDay = 0  # previous action day
    
    # Run through each action, should order by day
    for i in range(actionCount):
        actionVec = actionMat[ i ]
        day = actionVec[0] # The index of day
        a = actionVec[1] # The index of "from" stock
        b = actionVec[2] # The index of "to" stock
        z = actionVec[3] # The equivalent cash for such transaction.
        currentPriceVec = priceMat[day]  # current priceVec
        
        # check action day and equivalent cash
        if day >= preDay and day >= 0 and z > 0 :
            # get real action by suggested action
            if i > 0:
                stockHolding[i] = stockHolding[i-1]  # The stock holding from the previous action day
                preDay = day  # previous action day
            
            if a == -1 and b >= 0 and capital > 0 :  # Suggested action is "buy"
                currentPrice = currentPriceVec[b]  # The current price of stock
                if capital < z :  # "buy" allonly if you don't have enough capital
                    z = capital
                stockHolding[i][b] += z*(1-transFeeRate) / currentPrice # Buy stock using cash
                capital = capital - z  # Cash
                realAction[i] = 1
            elif b == -1 and a >= 0 and stockHolding[i][a] > 0 :  # Suggested action is "sell"
                currentPrice = currentPriceVec[a]  # The current price of stock
                sellStock = z / currentPrice
                if stockHolding[i][a] < sellStock :  # "sell" all only if you don't have enough stock holding
                    sellStock = stockHolding[i][a]
                getCash = sellStock * currentPrice*(1-transFeeRate)  # Sell stock to have cash
                capital = capital + getCash  # get cash from sell stock
                stockHolding[i][a] -= sellStock  # Stocking holding
                realAction[i] = -1
            elif a >= 0 and b >= 0 and stockHolding[i][a] > 0 :  # Suggested action is "buy" and "sell"
                currentPriceSell = currentPriceVec[a]  # The current price of sell stock
                currentPriceBuy = currentPriceVec[b]  # The current price of buy stock
                sellStock = z / currentPriceSell
                if stockHolding[i][a] < sellStock :  # "sell" all only if you don't have enough stock holding
                    sellStock = stockHolding[i][a]
                getCash = sellStock * currentPriceSell*(1-transFeeRate)  # Sell stock to have cash
                stockHolding[i][a] -= sellStock  # Stocking holding
                stockHolding[i][b] += getCash*(1-transFeeRate) / currentPriceBuy # Buy stock using cash
                realAction[i] = 2
            else:
                print( actionVec , stockHolding[i] )
                assert False
        else:
            print( actionVec )
            assert False
            
    # calculate total cash you get at last day
    total = capital
    print( capital, stockHolding[-1] )
    for stock in range(stockCount) :
        currentPriceVec = priceMat[ actionMat[-1][0] ]
        total += stockHolding[-1][stock] * currentPriceVec[stock]*(1-transFeeRate)  # Total asset, including stock holding and cash 
    print( total )
    returnRate=(total-capitalOrig)/capitalOrig  # Return rate of this run
    return returnRate
    
if __name__ == "__main__":
    file = sys.argv[1]    # input file
    df = pd.read_csv(file, delimiter=' ')
    transFeeRate= float(sys.argv[2])    # Rate for transaction fee
    priceMat = df.values    # Get price as the m×n matrix which holds n stocks' price over m days
    actionMat = myOptimAction(priceMat, transFeeRate)   # Obtain the suggested action
    # print( np.array(actionMat) )
    for action in actionMat :
        print( action )
    returnRate = computeReturnRate(priceMat, transFeeRate, actionMat)  # Compute return rate
    print( 'returnRate : ' + str(returnRate) )