📄 dollarcostaveraging.java
字号:
/* WARANTY NOTICE AND COPYRIGHTThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.Copyright (C) Michael J. Meyermatmjm@mindspring.comspyqqqdia@yahoo.com*//* * DollarCostAveraging.java * * Created on March 13, 2002, 12:35 PM */package Examples.Trading;import Statistics.*;import Market.*;import TradingStrategies.*;import Triggers.*;import java.lang.Math;/** <p>Console program allocating a constant volatility asset and examining * the dollar cost averaging strategy 100 shares are bought at time * t=0 and at equal time intervals thereafter regardless of current price. * Computes the mean and standard deviation of:</p> * * <ul> * <li>the gains from trading</li> * <li>the maximum amount borrowed</li> * <li>the maximum drawdown</li> * <li>the total invested at time T</li> * <li>the annualized return on investment (compounded continuously)</li> * <li>the number of trades</li> * </ul> * * <p>All except the last quantity are discounted amounts and all are evaluated * at the time horizon T. * Parameters (time to expiration, volatility,...) fixed in source code. * No user interaction. 10,000 paths of the underlying asset are simulated.</p> * * @author Michael J. Meyer * */public class DollarCostAveraging{ public static void main(String[] args) { int T=50, nPaths=10000, nSignChange=5, // irrelevant but needed for asset constructor nBuys=5; // number of buys made before the horizon double S_0=50, mu=-0.24, sigma=0.31, q=0.05, r=0.07, dt=0.02, fixed_trc=0.0, // fixed transaction costs prop_trc=0.0; // proportional transaction costs ConstantVolatilityAsset asset=new ConstantVolatilityAsset(T,dt,nSignChange,S_0,r,q,mu,sigma); TradingStrategy dollarCostAverage=new StrategyDollarCostAverage (fixed_trc,prop_trc,asset,nBuys); /* the statistics associated with this strategy: * mean and variance of five quantities: * the gains from trading, maximum borrowings, * maximum drawdown, amount invested at time T, * return on investment. * * Recall: vector of means is stats[0][.] * vector of standard deviations is stats[1][.] */ double[][] stats=dollarCostAverage.tradeStatistics(). meanAndStandardDeviation(nPaths); // report the results: String message; message= "Asset: S(0)="+S_0+", mu="+mu+", sigma="+sigma+"\n"+ "Investor buys 100 shares at regular intervals. The intended number\n"+ "of buys is "+nBuys+", the actual number can vary slightly fom this.\n"+ "Initial investment: "+dollarCostAverage.initialInvestment()+"\n"+ "Results evaluated at the horizon T="+T*dt+" years.\n\n"; System.out.println(message); double mean, stdv; //gains from trading: mean=stats[0][0]; stdv=stats[1][0]; mean=1.0*Math.round(10000*mean)/10000; //cut down to 5 decimals stdv=1.0*Math.round(10000*stdv)/10000; //cut down to 5 decimals message= "Gains from trading:\n"+ "Mean="+mean+", standard deviation="+stdv+"\n\n"; System.out.println(message); // maximum amount borrowed: mean=stats[0][1]; stdv=stats[1][1]; mean=1.0*Math.round(10000*mean)/10000; //cut down to 5 decimals stdv=1.0*Math.round(10000*stdv)/10000; //cut down to 5 decimals message= "Maximum amount borrowed:\n"+ "Mean="+mean+", standard deviation="+stdv+"\n\n"; System.out.println(message); //maximum drawdown: mean=stats[0][2]; stdv=stats[1][2]; mean=1.0*Math.round(10000*mean)/10000; //cut down to 5 decimals stdv=1.0*Math.round(10000*stdv)/10000; //cut down to 5 decimals message= "Maximum drawdown:\n"+ "Mean="+mean+", standard deviation="+stdv+"\n\n"; System.out.println(message); //total invested at time T: mean=stats[0][3]; stdv=stats[1][3]; mean=1.0*Math.round(10000*mean)/10000; //cut down to 5 decimals stdv=1.0*Math.round(10000*stdv)/10000; //cut down to 5 decimals message= "Total invested at time T:\n"+ "Mean="+mean+", standard deviation="+stdv+"\n\n"; System.out.println(message); //return on investment: mean=stats[0][4]; stdv=stats[1][4]; mean=1.0*Math.round(10000*mean)/10000; //cut down to 5 decimals stdv=1.0*Math.round(10000*stdv)/10000; //cut down to 5 decimals message= "Return on investment:\n"+ "Mean="+mean+", Standard deviation="+stdv+"\n"+ "Benchmark: asset drift mu="+mu+"\n\n"; System.out.println(message); //number of trades: mean=stats[0][5]; stdv=stats[1][5]; mean=1.0*Math.round(10000*mean)/10000; //cut down to 5 decimals stdv=1.0*Math.round(10000*stdv)/10000; //cut down to 5 decimals message= "Number of trades:\n"+ "Mean="+mean+", Standard deviation="+stdv+"\n\n"; System.out.println(message); }//end main }//end BuyAndHold
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -