📄 gainsfromtrading.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*//* * GainsFromTrading.java * * Created on March 16, 2002, 12:11 PM */package Examples.Trading;import Statistics.*;import Market.*;import TradingStrategies.*;import Graphics.*;import java.lang.Math;/** <p>We compute and graph the following statistics associated with a trading * strategy as functions of the asset drift mu:</p> * *<ul> * <li>gains from trading</li> * <li>maximum amount invested</li> * <li>maximum drawdown</li> * <li>total invested at time T</li> * <li>annualized return on investment (compounded continuously)</li> * <li>the number of trades</li> * <li>total transaction costs</li> * </ul> * * <p>during the life of the strategy. These functions are valuated at drifts mu * evenly spaced between extreme values mu_min=-0.3 (bear merket) and mu_max=0.3 * (bull market). The graphs of the functions are charted for the following * trading strategies (define in the package <code>TradingStrategies</code>): * *<ul> * <li> Buy and hold.</li> * <li> Dollar cost averaging.</li> * <li> Averaging down.</li> * <li> Double or nothing (increase factor f=2).</li> *</ul> * * <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 average down and double or nothing strategies buy on each 7 percent * price decline from the level of the last buy. The double or nothing strategy * then doubles the number of shares held.</p> * * <p>The return on investment is computed under the assumption that the * investor has an infinite cash pile with which to sustain the strategy. * In particular no interest is paid on borrowings. The gains from trading * are then viewed as interest paid on the maximum amount sunk into the strategy * during its lifetime.</p> * * <p>By default the <code>doubleOrNothing</code> strategy is not shown. * Increase the variable nSeries to 4 (source code) if you want it.</p> * * <p>Console program, no user interaction, all parameters fixed in source * code.</p> * * @author Michael J. Meyer * */public class GainsFromTrading{ public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException { int T=50, // time steps to horizon nPaths=20000, // number of asset price paths simulated // per trading strategy nSignChange=5, nBuys=5, // number of buys for dollar cost average // strategy nPoints=20, // number of volatities mu at which // the functions are computed nSeries=4, // the series corresponding to our 4 // trading strategies, doubleOrNothing // series not shown (increase nSeries to 4 // if it is desired prcnt=7; // percentage decline by which triggers the // buys for average down and double or // nothing strategies double S_0=50, // initial asset price mu_min=-0.3, // drift range mu_max=+0.3, sigma=0.4, // volatility q=0.0, // dividend r=0.05, // risk free rate dt=0.02, // size of time step fixed_trc=10, // fixed transaction costs prop_trc=0.25, // proportional transa tion costs delta_mu=(mu_max-mu_min)/nPoints; System.out.println("mu between -0.3 and 0.3:\n"); /* arrays for function series: * * buy and hold = [0] * dollar cost average = [1] * average down = [2] * double or nothing = [3] */ String[] seriesNames={ "buy and hold", "dollar cost average", "average down", "double or nothing" }; String chart_title; double[][] series, mean_return_series=new double[nSeries][nPoints], mean_max_drawdown_series=new double[nSeries][nPoints], mean_max_borrowing_series=new double[nSeries][nPoints], mean_transaction_cost_series=new double[nSeries][nPoints], mean_ntrades_series=new double[nSeries][nPoints]; for(int i=0;i<nPoints;i++) { int N=nPoints-1; double mu_i=mu_min+i*(mu_max-mu_min)/N; System.out.println("mu_i="+mu_i); ConstantVolatilityAsset asset=new ConstantVolatilityAsset (T,dt,nSignChange,S_0,r,q,mu_i,sigma); // the trading strategies: TradingStrategy buyAndHold=new StrategyBuyAndHold(fixed_trc,prop_trc,asset), dollarCostAverage=new StrategyDollarCostAverage (fixed_trc,prop_trc,asset,nBuys), averageDown=new StrategyAverageDown (fixed_trc,prop_trc,asset,prcnt), doubleOrNothing=new StrategyDoubleOrNothing (fixed_trc,prop_trc,asset,prcnt,2.0); // the associated statistics double[] buyAndHold_stats= buyAndHold.tradeStatistics().expectation(nPaths), dollarCostAverage_stats= dollarCostAverage.tradeStatistics().expectation(nPaths), averageDown_stats= averageDown.tradeStatistics().expectation(nPaths), doubleOrNothing_stats= doubleOrNothing.tradeStatistics().expectation(nPaths); // store the results in the series // mean max_borrowings mean_max_borrowing_series[0][i]=buyAndHold_stats[1]; mean_max_borrowing_series[1][i]=dollarCostAverage_stats[1]; mean_max_borrowing_series[2][i]=averageDown_stats[1]; mean_max_borrowing_series[3][i]=doubleOrNothing_stats[1]; // mean drawdowns mean_max_drawdown_series[0][i]=buyAndHold_stats[2]; mean_max_drawdown_series[1][i]=dollarCostAverage_stats[2]; mean_max_drawdown_series[2][i]=averageDown_stats[2]; mean_max_drawdown_series[3][i]=doubleOrNothing_stats[2]; // mean returns mean_return_series[0][i]=buyAndHold_stats[3]; mean_return_series[1][i]=dollarCostAverage_stats[3]; mean_return_series[2][i]=averageDown_stats[3]; mean_return_series[3][i]=doubleOrNothing_stats[3]; // mean number of trades mean_ntrades_series[0][i]=buyAndHold_stats[4]; mean_ntrades_series[1][i]=dollarCostAverage_stats[4]; mean_ntrades_series[2][i]=averageDown_stats[4]; mean_ntrades_series[3][i]=doubleOrNothing_stats[4]; // mean transaction costs mean_transaction_cost_series[0][i]=buyAndHold_stats[5]; mean_transaction_cost_series[1][i]=dollarCostAverage_stats[5]; mean_transaction_cost_series[2][i]=averageDown_stats[5]; mean_transaction_cost_series[3][i]=doubleOrNothing_stats[5]; } // end for i // display the series in two JGraph charts: // where the charts will be saved as EPS files String filename; // RETURNS series=mean_return_series; chart_title="Mean_returns"; filename="Pictures/Graphs/GainsFromTrading/returns.dat"; drawGraph(chart_title,mu_min,mu_max,series,seriesNames,filename); // MAXIMUM AMOUNT INVESTED series=mean_max_borrowing_series; chart_title="Mean_maximum_borrowings"; filename="Pictures/Graphs/GainsFromTrading/borrow.dat"; drawGraph(chart_title,mu_min,mu_max,series,seriesNames,filename); // NUMBER OF TRDES series=mean_ntrades_series; chart_title="Mean_number_of_trades"; filename="Pictures/Graphs/GainsFromTrading/nTrades.dat"; drawGraph(chart_title,mu_min,mu_max,series,seriesNames,filename); // TRANSACTION COSTS series=mean_transaction_cost_series; chart_title="Mean_transaction_costs"; filename="Pictures/Graphs/GainsFromTrading/trc.dat"; drawGraph(chart_title,mu_min,mu_max,series,seriesNames,filename); // DRAWDOWNS series=mean_max_drawdown_series; chart_title="Mean_maximal_drawdown"; filename="Pictures/Graphs/GainsFromTrading/drawdowns.dat"; drawGraph(chart_title,mu_min,mu_max,series,seriesNames,filename); }//end main private static void drawGraph (String chartTitle, double xmin, double xmax, double[][] series, String[] seriesNames, String filename) throws java.io.FileNotFoundException, java.io.IOException { JGraph jGraph=new JGraph(xmin,xmax); jGraph.setTitle(chartTitle); for(int i=0;i<series.length;i++) jGraph.addSeries(series[i],seriesNames[i]); jGraph.show(); //jGraph.saveAsEPS(filename); jGraph.saveAsASCII(filename); } // end drawGraph }//end GainsFromTrading
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -