📄 doubleornothing.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*//* * DoubleOrNothing.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 strategy which averages down as follows: 100 shares are bought at time * t=0. Each time the asset price declines 5% from the level of the last buy * the position (number of shares) is increased by a factor f=2. * Computes the means and standard deviations of:</p> * * <ul> * <li>the gains from trading</li> * <li>the maximum amount borrowed</li> * <li>the maximum drawdown</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> * * <p><b>Warning:</b>if f is large enough this can wrest positive returns even * from an asset drifting lower (bear market). In this case however the * position can grow very large. Do this only if you have very deep pockets! * </p> * * @author Michael J. Meyer * */public class DoubleOrNothing{ public static void main(String[] args) { int T=50, nPaths=10, nSignChange=5, // irrelevant but needed for asset constructor triggerPercentage=5; 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 f=2; // factor by which nmber of shares is increased // on each trade ConstantVolatilityAsset asset=new ConstantVolatilityAsset(T,dt,nSignChange,S_0,r,q,mu,sigma); /* // the buy and hold strategy (100 shares, no trades) Trigger tradeTrigger=new TriggerAtPercentDecline(asset,triggerPercentage); TradingStrategy averageDown=new TradingStrategy(fixed_trc,prop_trc,asset,tradeTrigger){ // double the position if trade is triggered public double newWeight(int t) { if(t==0)return 100; else return 2*get_currentWeight(); } }; // end buyAndHold */ TradingStrategy doubleOrNothing=new StrategyDoubleOrNothing (fixed_trc,prop_trc,asset,triggerPercentage,f); /* 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=doubleOrNothing.tradeStatistics(). meanAndStandardDeviation(nPaths); // report the results: String message; message= "Asset: S(0)="+S_0+", mu="+mu+", sigma="+sigma+"\n"+ "Investor buys 100 shares and doubles the position whenever\n"+ "the asset price declines "+triggerPercentage+" percent from the\n"+ "price of the last buy. Evaluation at the horizon T="+T*dt+" years.\n"+ "Initial investment: "+doubleOrNothing.initialInvestment()+"\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); //return on investment 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= "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][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= "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 + -