📄 returnshistogram.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*//* * ReturnsHistogram.java * * Created on March 26, 2002, 5:57 PM */package Examples.Trading;import hep.aida.*;import Statistics.*;import Market.*;import TradingStrategies.*;import Triggers.*;import java.lang.Math;import jas.hist.*;import jas.hep.*;import javax.swing.*;/** <p>We compute (smoothed, normalized) histograms of the returns of the * following two trading strategies:</p> * *<ul> * <li>average down</li> * <li>dollar cost averaging</li> * </ul> * * <p>defined in the package <code>TradingStrategies</code>. The average down * strategy buys on each 7 percent price decline from the level of the last buy. * Dollar cost averaging makes 5 buys.</p> * * <p>The volatility is fixed at sigma=0.4, fixed transaction costs at 10, * proportional transaction costs at 0.25. Initial number of shares bought * 100. Time horizon T is one year. * 10,000 paths of the underlying asset are simulated.</p> * * <p>The return on investment is computed as a return on the maximum invested * during the life of the strategy. The gains from trading are then viewed as * interest paid on this maximum. Financing through "borrowing" from existing * funds, no interest paid on borrowings.</p> * * <p>Console program, no user interaction, all parameters fixed in source * code.</p> * * @author Michael J. Meyer * */public class ReturnsHistogram { public static void main(String[] argv) throws java.io.FileNotFoundException, java.io.IOException { int T=50, nPaths=40000, nBins=100, nSignChange=5, // irrelevant but needed for asset constructor triggerPercentage=7, // triggers average down trade nBuys=5; // number of buys, dollar cost averaging double S_0=50, mu=0.3, sigma=0.4, q=0.0, r=0.05, dt=0.02, fixed_trc=10, // fixed transaction costs prop_trc=0.25; // proportional transaction costs String title, xAxisLabel, filename; int ftype=Graphics.Flag.EPS; ConstantVolatilityAsset asset=new ConstantVolatilityAsset(T,dt,nSignChange,S_0,r,q,mu,sigma); // AVERAGE DOWN TradingStrategy averageDown=new StrategyAverageDown (fixed_trc,prop_trc,asset,triggerPercentage); RandomVariable returnsAverageDown=averageDown.returnsFromTrading(); // let the user know what's going on String message= "Computing trading gains histogram, average down: "+nPaths+" paths"; System.out.println(message); // show the histogram title="Returns: averaging down"; xAxisLabel="returns from trading"; filename="returns_avgdwn.eps"; returnsAverageDown.displayHistogram (nPaths,nBins,true,title,xAxisLabel,filename,ftype); System.out.println("Now writing Gri data file"); filename="returns_avgdwn.dat"; returnsAverageDown.basicHistogram(nPaths,nBins,0.001,true). writeGriDataFile(filename); // DOLLAR COST AVERAGE TradingStrategy dollarCostAverage=new StrategyDollarCostAverage (fixed_trc,prop_trc,asset,nBuys); RandomVariable returnsDollarCostAverage=dollarCostAverage.returnsFromTrading(); // let the user know what's going on message= "Computing trading gains histogram, dollar cost averaging: " +nPaths+" paths"; System.out.println(message); // show the histogram title="Returns: dollar cost averaging"; xAxisLabel="returns from trading"; filename="returns_dcavg.eps"; returnsDollarCostAverage.displayHistogram (nPaths,nBins,true,title,xAxisLabel,filename,ftype); System.out.println("Now writing Gri data file"); filename="returns_dcavg.dat"; returnsDollarCostAverage.basicHistogram(nPaths,nBins,0.001,true). writeGriDataFile(filename); } // end main} // end histogram
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -