[R-SIG-Finance] Blotter - Improvement for addTxn when symbol is missing

Wolfgang Wu wobwu22 at yahoo.de
Thu Jul 1 12:58:04 CEST 2010


A small addition to the addTxn function. So far you need to specify the contracts that you are using in a Portfolio object before doing any transaction. This might not be known in advance. Therefore I've added a more generic functionality to addTxn which adds a symbol to the Portfolio object if it doesn't exist yet.

The new function looks like this. Maybe you guys can have a look and update the repository if everything seems fine.

Thanks.


Txn <- function(Portfolio, Symbol, TxnDate, TxnQty, TxnPrice, ..., TxnFees=0, ConMult=NULL, verbose=TRUE)
{ # @author Peter Carl
    
    pname<-Portfolio
    Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
    
    if(is.null(ConMult) | !hasArg(ConMult)){
        tmp_instr<-try(getInstrument(Symbol))
        if(inherits(tmp_instr,"try-error") | !is.instrument(tmp_instr)){
            warning(paste("Instrument",Symbol," not found, using contract multiplier of 1"))
            ConMult<-1
        } else {
            ConMult<-tmp_instr$multiplier
        }  
    }
    
    #If there is no table for the symbol then create a new one
    if (is.null(Portfolio[[Symbol]])){    
        if (length(Portfolio) == 0) {
            initDate <- as.Date(TxnDate) - 1;
        } else {
            initDate <- as.Date(first(zoo::index(Portfolio[[1]]$posPL)));
        }
        Portfolio[[Symbol]]$txn <- initTxn(initDate = initDate, initPosQty = 0)
        Portfolio[[Symbol]]$posPL <-  initPosPL(initDate = initDate, initPosQty = 0, initConMult = ConMult)
        Portfolio[[Symbol]][[paste('posPL',attr(Portfolio, "currency"),sep='.')]] = Portfolio[[Symbol]]$posPL
    }        
    assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)
    
    # Outputs:
    # Portfolio: hands back the entire portfolio object with the additional
    # transaction in the correct slot: Portfolio[[Symbol]]$txn

    # FUNCTION
    # Compute transaction fees if a function was supplied
    txnfees <- ifelse( is.function(TxnFees), TxnFees(TxnQty, TxnPrice), TxnFees)
    # Calculate the value and average cost of the transaction
    TxnValue = calcTxnValue(TxnQty, TxnPrice, 0, ConMult) # Gross of Fees
    TxnAvgCost = calcTxnAvgCost(TxnValue, TxnQty, ConMult)

    # Calculate the change in position
    PrevPosQty = getPosQty(pname, Symbol, TxnDate)
    PosQty = PrevPosQty + TxnQty

    # Calculate the resulting position's average cost
    PrevPosAvgCost = getPosAvgCost(pname, Symbol, TxnDate)
    PosAvgCost = calcPosAvgCost(PrevPosQty, PrevPosAvgCost, TxnValue, PosQty, ConMult)

    # Calculate any realized profit or loss (net of fees) from the transaction
    GrossTxnRealizedPL = calcRealizedPL(TxnQty, TxnAvgCost, PrevPosAvgCost, PosQty, PrevPosQty, ConMult)
    NetTxnRealizedPL = GrossTxnRealizedPL + txnfees

    # Store the transaction and calculations
    NewTxn = xts(t(c(TxnQty, TxnPrice, TxnValue, TxnAvgCost, PosQty, PosAvgCost, GrossTxnRealizedPL, txnfees, NetTxnRealizedPL, ConMult)), order.by=as.POSIXct(TxnDate))
    #colnames(NewTxns) = c('Txn.Qty', 'Txn.Price', 'Txn.Value', 'Txn.Avg.Cost', 'Pos.Qty', 'Pos.Avg.Cost', 'Gross.Txn.Realized.PL', 'Txn.Fees', 'Net.Txn.Realized.PL', 'Con.Mult')
    Portfolio[[Symbol]]$txn<-rbind(Portfolio[[Symbol]]$txn, NewTxn)

    if(verbose)
        print(paste(TxnDate, Symbol, TxnQty, "@",TxnPrice, sep=" "))
        #print(Portfolio[[Symbol]]$txn)
    
    assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)
}


 
Regards,


Wolfgang Wu






More information about the R-SIG-Finance mailing list