Task
Write the optimal portfolio, the optimal trade, the existing portfolio, or all of these to a file.
Preparation
- an object that is the result of
trade.optimizer
- Portfolio Probe
You need an optimization object before you can write parts of it to a file (obviously).
You also need to have the Portfolio Probe package loaded into your R session:
require(PortfolioProbe)
If you don’t have Portfolio Probe, see “Demo or Buy”.
Doing the example
- the
opMinVar
andpriceVector
objects from “Passive, no benchmark (minimum variance)”
Doing it
These tasks are shown:
- write the trade to a comma-separated file
- write the optimal portfolio to a comma-separated file
- write the existing portfolio, the trade and the optimal portfolio to a comma-separated file
- write the trade to a tab-separated file
- write the trade transforming from lots to a comma-separated file
- write monetary values of the optimal portfolio to a comma-separated file
In all of these the deport
function is used. The only difference is the value of a few arguments. The value that is returned is the name of the file that is created (or overwritten).
Write the trade to a comma-separated file
> deport(opMinVar, 'opTrade', what="trade") [1] "opTrade.csv"
We merely need to tell the what
argument that we want the trade.
Write the optimal portfolio to a comma-separated file
> deport(opMinVar, 'opPort', what="optimal") [1] "opPort.csv"
We merely need to tell the what
argument that we want the optimal (portfolio).
Write everything to a comma-separated file
> deport(opMinVar, 'opAll', what="all") [1] "opAll.csv"
We merely need to tell the what
argument that we want everything. Actually in this case we didn’t need to give what
since "all"
is its default value.
Write the trade to a tab-separated file
> deport(opMinVar, 'opTrade', what="trade", to="txt") [1] "opTrade.txt"
The way to get a tab-separated file is to say: to="txt"
.
Write the trade transforming from lots to a comma-separated file
If the prices given to trade.optimizer
were for lots and you want the file to be in terms of shares, then you can use the multiplier
argument. There are two cases:
- all lots are the same size
- all lots are not the same size
all lots the same
If all the lots are the same size, then just give that number to multiplier
:
deport(opMinVar, 'opTradeSamelot', what="trade", mult=100)
all lots not the same
In this case we need a vector that holds the lot sizes for each asset. Let’s make one up. We’ll have all lot sizes equal to 100 except one that has only 1 share and one that has 50 shares.
> lotSizes <- rep(100, 350) > names(lotSizes) <- names(priceVector) > lotSizes["XA101"] <- 1 > lotSizes["XA105"] <- 50 > head(lotSizes) XA101 XA103 XA105 XA107 XA108 XA111 1 100 50 100 100 100
Now we merely give the vector of lot sizes as multiplier
:
deport(opMinVar, 'opTradeVarilot', what="trade", mult=lotSizes)
Write monetary values of the optimal portfolio to a comma-separated file
Writing the file in terms of money rathter than units requires giving a vector of prices to the multiplier
argument:
deport(opMinVar, 'opPortValue', what="optimal", mult=priceVector)
Explanation
Arguments
The to
argument takes either "csv"
(the default) or "txt"
. The first produces comma-separated files, the second produces tab-separated files.
The filename can include the file extension — but if given, it should match the value for to
.
Values you can give to what
are: "trade"
, "optimal"
, "existing"
, "all"
.
Files
We can see what the files look like because if filename=""
, then what would be written to a file is written to R instead.
> deport(opMinVar, '') assets,existing,trade,optimal XA101,1000,-1000, XA103,2000,-2000, XA105,3000,-341,2659 XA107,4000,-4000, XA108,5000,-5000, XA111,6000,-6000, XA113,7000,-7000, XA115,8000,-8000, XA120,9000,-9000, XA126,10000,-10000, XA280,,17920,17920 XA298,,2906,2906 XA643,,20192,20192 XA675,,7182,7182 XA709,,6485,6485 XA731,,4166,4166 XA778,,6840,6840 XA891,,5200,5200 XA966,,6247,6247 > deport(opMinVar, '', what="trade") XA101,-1000 XA103,-2000 XA105,-341 XA107,-4000 XA108,-5000 XA111,-6000 XA113,-7000 XA115,-8000 XA120,-9000 XA126,-10000 XA280,17920 XA298,2906 XA643,20192 XA675,7182 XA709,6485 XA731,4166 XA778,6840 XA891,5200 XA966,6247
Troubleshooting
- If the file to be written to already exists, it will be overwritten — you will lose the previous contents.
- The file extension — if not given — is affected by the value of the
to
argument. But the file extension never affects theto
argument.
Navigate
- Back to “Optimize Trades”
- Back to the top level of “Portfolio Probe Cookbook”