[R-SIG-Finance] Ques regarding price conversion to 32nds

Gandhi, Puneet - RSCH AMRS p.gandhi at baml.com
Thu Sep 2 13:44:30 CEST 2010


 I apologies for not being clear enough with my query. My requirement is to see Treasury prices not 32 fractions. Treasury Prices have '+' and other things.

Actually I have VBA code for it

Function TO_32(BondPrice As Variant) As String

    Dim handle As Double
    Dim x As Double
    Dim y As Double
    Dim r As Double
    Dim thirtyTwos As Integer
    Dim twoFiftySixths As Integer
    Dim eighths As Integer
    Dim ch As String
    Dim bp As Double
    
    Const tol = 256
    
    On Error GoTo errHandler
    
    If IsArray(BondPrice) Then
        bp = CDbl(BondPrice(1))
    Else
        bp = CDbl(BondPrice)
    End If
    
    handle = Int(bp)
    r = (bp - handle) * 256
    twoFiftySixths = Int(r + 0.5)
    thirtyTwos = Int(twoFiftySixths / 8)
    eighths = twoFiftySixths - 8 * thirtyTwos
    Select Case eighths
    Case 0
        ch = ""
    Case 4
        ch = "+"
    Case Else
        ch = CStr(eighths)
    End Select
    ' correct for big figure rounding issues
    If thirtyTwos = 32 Then
        handle = handle + 1
        thirtyTwos = 0
    End If
1
    TO_32 = CStr(handle) & "-" & Format(thirtyTwos, "0#") & ch
    Exit Function
    
errHandler:
    TO_32 = "#Bad Price"
    
End Function

Function FROM_32(s As Variant) As Variant

    Dim handleStr As String
    Dim remStr As String
    Dim fracStr As String
    Dim ch As String
    Dim i As Integer
    Dim slen As Integer
    Dim rlen As Integer
    Dim frac As Double
    Dim remain As Double
    Dim handle As Double
    
    On Error GoTo errHandler
    
    slen = Len(s)
    ch = ""
    For i = 1 To slen
        ch = mid(s, i, 1)
        If ch = "-" Then Exit For
    Next i
    If (Not ch = "-") Or (i > (slen - 2) Or (i < 2)) Then
        FROM_32 = "#Bad Input Format"
        Exit Function
    End If
    
    handleStr = mid(s, 1, i - 1)
    rlen = slen - i
    Select Case rlen
    Case 2
        remStr = mid(s, i + 1, slen - i)
        fracStr = "0"
    Case 3
        remStr = mid(s, i + 1, slen - i - 1)
        fracStr = Right(s, 1)
        If fracStr = "+" Then fracStr = "4"
    End Select
        
    handle = CDbl(handleStr)
    remain = CDbl(remStr)
    frac = CDbl(fracStr)
    
    FROM_32 = handle + remain / 32# + frac / 256#
    Exit Function
    
errHandler:
    FROM_32 = "#Bad Conversion"
    
End Function




Can you point me to a package/function on conversion of Decimal Prices to Bond prices whbich gives this '+' and everything

-----Original Message-----
From: Pierre Lapointe [mailto:pierrelap at gmail.com] 
Sent: Wednesday, September 01, 2010 6:46 PM
To: Gandhi, Puneet - RSCH AMRS
Cc: Joshua Ulrich; r-sig-finance at stat.math.ethz.ch
Subject: Re: [R-SIG-Finance] Ques regarding price conversion to 32nds

Thanks,

So here's the full function to convert fractions to decimals

#Fractions to decimals
x <- c("2 1/32","7 7/32","4 27/32")

dec.fun <-function(x){
my.split<-strsplit(x," ")
my.mat <-matrix(unlist(my.split),,2,byrow=TRUE)
my.eval <-function(y){
eval(parse(text=paste(y,collapse="+")))
}
apply(my.mat,1,my.eval)
}

dec.fun(x)

This will need work if you feed it data.frames or matrices. And it
does not work if there is no integer, only a fraction. But you get the
idea...

HTH

Pierre


On Wed, Sep 1, 2010 at 6:15 PM, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
>
> Here's the answer to the second part, using the results from Pierre's email:
>
> > eval(parse(text=paste(my.split[[1]],collapse="+")))
> [1] 2.03125
>
> Best,
> --
> Joshua Ulrich
> FOSS Trading: www.fosstrading.com
>
>
> On Wed, Sep 1, 2010 at 5:09 PM, Pierre Lapointe <pierrelap at gmail.com> wrote:
> > Here's the answer to the first part of your question:
> >
> > #Decimals to fractions
> > frac.fun <- function(x, den){
> > dec <- seq(0, den) / den
> > nams <- paste(seq(0, den), den, sep = "/")
> > sapply(x, function(y) nams[which.min(abs(y - dec))])
> > }
> >
> > frac.fun(c(0, 1, 0.8266667, .066666, 0.2666666,0.03125), 32)
> >
> > To convert from fraction to decimal, I would strsplit numbers in two parts,
> > one integer and one fraction.
> >
> > #Fractions to decimals
> > x <- c("2 1/32","3 7/32","4 27/32")
> > my.split<-strsplit(x," ")
> >
> > However, I do not know how to convert a "1/32" caracter string to numeric.
> >
> > Pierre
> >
> >
> > On Wed, Sep 1, 2010 at 5:22 PM, Gandhi, Puneet - RSCH AMRS <
> > p.gandhi at baml.com> wrote:
> >
> >>
> >>
> >>
> >> Hi,
> >>
> >> Can any one point me to a function which converts prices in decimals to
> >> 32nds and vice versa.
> >>
> >> Thanks
> >> Puneet
> >>
> >> ----------------------------------------------------------------------
> >> This message w/attachments (message) is intended solely ...{{dropped:7}}
> >>
> >> _______________________________________________
> >> R-SIG-Finance at stat.math.ethz.ch mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >> -- Subscriber-posting only. If you want to post, subscribe first.
> >> -- Also note that this is not the r-help list where general R questions
> >> should go.
> >>
> >
> >        [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-SIG-Finance at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > -- Subscriber-posting only. If you want to post, subscribe first.
> > -- Also note that this is not the r-help list where general R questions should go.
> >

----------------------------------------------------------------------
This message w/attachments (message) is intended solely for the use of the intended recipient(s) and may contain information that is privileged, confidential or proprietary. If you are not an intended recipient, please notify the sender, and then please delete and destroy all copies and attachments, and be advised that any review or dissemination of, or the taking of any action in reliance on, the information contained in or attached to this message is prohibited. 
Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Sender. Subject to applicable law, Sender may intercept, monitor, review and retain e-communications (EC) traveling through its networks/systems and may produce any such EC to regulators, law enforcement, in litigation and as required by law. 
The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or free of errors or viruses. 

References to "Sender" are references to any subsidiary of Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this EC may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: 
http://www.bankofamerica.com/emaildisclaimer. By messaging with Sender you consent to the foregoing.



More information about the R-SIG-Finance mailing list