Advertisement
Guest User

Untitled

a guest
Mar 31st, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.90 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.math.RoundingMode;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Calendar;
  7.  
  8. import javax.swing.JButton;
  9. import javax.swing.JComboBox;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.JSlider;
  14. import javax.swing.JTextArea;
  15. import javax.swing.JTextField;
  16. import javax.swing.event.ChangeEvent;
  17. import javax.swing.event.ChangeListener;
  18.  
  19. public class TestYahooFinance extends JFrame implements ActionListener,
  20. ChangeListener
  21. {
  22. private JButton bRunSimulation, bExit, bChart;
  23. private JLabel lInvestmentAmount, lSectorSelect, lEntitiesNumber,
  24. lStrdDays;
  25. private JSlider slGetHowManyDaysStrdv, slNumberOfEntities;
  26. private JTextField tGetInvestmentAmount;
  27. private JComboBox cIndexchoice;
  28. private JTextArea taResultArea;
  29. private int HowManyDaysStrdv, NumberOfEntities;
  30. private int investmentAmount;
  31.  
  32. public TestYahooFinance()
  33. {
  34. // creating main window
  35. setSize(700, 700);
  36. setTitle("Day-trading simulation");
  37.  
  38. bChart = new JButton("Create chart");
  39. bChart.setBounds(20, 430, 150, 50);
  40. bChart.setEnabled(false);
  41. add(bChart);
  42.  
  43. // creating buttons
  44. bRunSimulation = new JButton("Run Simulation");
  45. setLayout(null);
  46. bRunSimulation.setBounds(20, 500, 150, 50);
  47. // adding buttons to the main window
  48. add(bRunSimulation);
  49. bRunSimulation.addActionListener(this);
  50.  
  51. bExit = new JButton("Exit");
  52. bExit.setBounds(20, 570, 150, 50);
  53. add(bExit);
  54. bExit.addActionListener(this);
  55.  
  56. lSectorSelect = new JLabel("Select sector for simulation:");
  57. lSectorSelect.setBounds(20, 20, 190, 25);
  58. add(lSectorSelect);
  59.  
  60. cIndexchoice = new JComboBox();
  61. cIndexchoice.setBounds(20, 50, 100, 20);
  62. cIndexchoice.addItem("XLU");
  63. cIndexchoice.addItem("XLK");
  64. cIndexchoice.addItem("XLB");
  65. cIndexchoice.addItem("XLP");
  66. cIndexchoice.addItem("XLY");
  67. cIndexchoice.addItem("XLI");
  68. cIndexchoice.addItem("XLV");
  69. cIndexchoice.addItem("XLF");
  70. cIndexchoice.addItem("XLE");
  71. add(cIndexchoice);
  72. cIndexchoice.addActionListener(this);
  73.  
  74. lStrdDays = new JLabel("Days of Standard Deviation: 2");
  75. lStrdDays.setBounds(20, 80, 300, 20);
  76. add(lStrdDays);
  77.  
  78. slGetHowManyDaysStrdv = new JSlider(2, 30, 2);
  79. slGetHowManyDaysStrdv.setBounds(10, 100, 180, 45);
  80. slGetHowManyDaysStrdv
  81. .setToolTipText("Enter number days of Standard Deviation");
  82. slGetHowManyDaysStrdv.setMajorTickSpacing(5);
  83. slGetHowManyDaysStrdv.setMinorTickSpacing(1);
  84. slGetHowManyDaysStrdv.setPaintLabels(true);
  85. slGetHowManyDaysStrdv.setPaintTicks(true);
  86. slGetHowManyDaysStrdv.addChangeListener(this);
  87. add(slGetHowManyDaysStrdv);
  88.  
  89. lEntitiesNumber = new JLabel("Number of entities: 1");
  90. lEntitiesNumber.setBounds(20, 145, 200, 35);
  91. add(lEntitiesNumber);
  92.  
  93. slNumberOfEntities = new JSlider(1, 20, 1);
  94. slNumberOfEntities.setBounds(10, 180, 180, 45);
  95. slNumberOfEntities.setToolTipText("Set number of entities");
  96. slNumberOfEntities.setMajorTickSpacing(5);
  97. slNumberOfEntities.setMinorTickSpacing(1);
  98. slNumberOfEntities.setPaintLabels(true);
  99. slNumberOfEntities.setPaintTicks(true);
  100. slNumberOfEntities.addChangeListener(this);
  101. add(slNumberOfEntities);
  102.  
  103. tGetInvestmentAmount = new JTextField();
  104. tGetInvestmentAmount.setBounds(20, 260, 100, 25);
  105. tGetInvestmentAmount.setToolTipText("Enter amount of investments");
  106. add(tGetInvestmentAmount);
  107.  
  108. lInvestmentAmount = new JLabel("Amount of investment:");
  109. lInvestmentAmount.setBounds(20, 235, 300, 20);
  110. add(lInvestmentAmount);
  111.  
  112. taResultArea = new JTextArea();
  113. taResultArea.setEditable(false);
  114. JScrollPane scrollPane = new JScrollPane(taResultArea);
  115. scrollPane.setBounds(300, 15, 350, 600);
  116. add(scrollPane);
  117. }
  118.  
  119. public static void main(String[] args)
  120. {
  121. // tworzenie nowego obiektu
  122. TestYahooFinance simulation = new TestYahooFinance();
  123. // jak nacisniemy X to sie ramka wylacza a nie ukrywa
  124. simulation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  125. // setVisible ramka sie pojawia
  126. simulation.setVisible(true);
  127.  
  128. }
  129.  
  130. public void actionPerformed(ActionEvent e)
  131. {
  132.  
  133. if (e.getSource() == bRunSimulation)
  134. {
  135.  
  136. long t1 = System.currentTimeMillis();
  137. HowManyDaysStrdv = slGetHowManyDaysStrdv.getValue();
  138. investmentAmount = Integer.parseInt(tGetInvestmentAmount.getText());
  139. NumberOfEntities = slGetHowManyDaysStrdv.getValue();
  140.  
  141. // Start Date
  142. Calendar startDate = Calendar.getInstance();
  143. // Set startDate
  144. startDate.set(Calendar.YEAR, 2013);
  145. startDate.set(Calendar.MONTH, 0); // Month Jan = 0, Feb = 1...Dec
  146. // =// 11
  147. startDate.set(Calendar.DATE, 1);
  148.  
  149. // End Date
  150. Calendar endDate = Calendar.getInstance();
  151. // Set endtDate
  152. endDate.set(Calendar.YEAR, 2014);
  153. endDate.set(Calendar.MONTH, 11);
  154. endDate.set(Calendar.DATE, 15);
  155.  
  156. // geting sector from combobox and change it to string
  157. String sectorSelected = cIndexchoice.getSelectedItem().toString();
  158.  
  159. // sector (xlu.[0]) + entities belong to the sector XLU
  160. String[] XLU = { "XLU", "DUK", "SO", "D", "NEE", "EXC", "AEP",
  161. "PCG", "SRE", "PPL", "ED", "PEG", "FE", "EIX", "XEL", "NU",
  162. "ETR", "DTE", "CNP", "WEC", "OKE", "NI", "AEE", "NRG",
  163. "AES", "CMS", "SCG", "PNW", "POM", "GAS", "TEG", "TE" };
  164.  
  165. String[] XLK = { "XLK", "AAPL", "GOOG", "MSFT", "VZ", "IBM", "T",
  166. "ORCL", "QCOM", "CSCO", "FB", "V", "INTC", "MA", "EBAY",
  167. "EMC", "ACN", "TXN", "ADP", "YHOO", "CRM", "ADBE", "CTSH",
  168. "GLW", "MU", "INTU", "AMAT" };
  169.  
  170. String[] XLB = { "XLB", "DD", "DOW", "MON", "LYB", "PX", "FCX",
  171. "ECL", "PPG", "APD", "IP", "SHW", "MOS", "NUE", "CF",
  172. "EMN", "AA", "SIAL", "FMC", "VMC", "BLL", "IFF", "ARG",
  173. "MWV", "SEE", "OI", "AVY", "BMS", "ATI", "X", "CLF" };
  174.  
  175. String[] XLP = { "XLP", "PG", "PM", "CVS", "MO", "WAG", "CL",
  176. "MDLZ", "COST", "KMB", "GIS", "KRFT", "ADM", "KR", "SYY",
  177. "WFM", "STZ", "LO", "EL", "RAI", "HSY", "K", "MJN", "BEAM",
  178. "CAG", "TSN", "CCE", "CLX", "DPS", "SJM", "SWY", "TAP",
  179. "MNST", "MKC", "CPB", "HRL", "AVP" };
  180.  
  181. String[] XLY = { "XLY", "AMZN", "CMCSA", "DIS", "HD", "MCD",
  182. "PCLN", "FOXA", "TWX", "F", "NKE", "SBUX", "LOW", "TJX",
  183. "TWC", "DTV", "TGT", "CBS", "VIAB", "YUM", "JCI", "NFLX",
  184. "M", "VFC", "DISCA", "OMC", "WYNN", "AZO", "DLPH", "CMG",
  185. "KORS", "CCL", "DG", "ORLY", "ROST", "HOG", "HOT", "BBBY",
  186. "BWA", "LB" };
  187.  
  188. String[] XLE = { "XLE", "XOM", "CVX", "SLB", "OXY", "EOG", "COP",
  189. "PXD", "HAL", "APC", "PSX", "VLO", "NOV", "APA", "BHI",
  190. "SE", "NBL", "COG", "HES", "MPC", "DVN", "MRO", "RRC",
  191. "TSO", "FTI", "CAM", "KMI", "CHK", "EQT", "CNX", "RIG" };
  192.  
  193. String[] XLF = { "XLF", "WFC", "JPM", "C", "AXP", "USB", "GS",
  194. "MET", "SPG", "PNC", "MS", "COF", "PRU", "BK", "BLK",
  195. "ACE", "AMT", "SCHW", "TRV", "AFL", "STT", "DFS", "BBT",
  196. "MMC", "ALL", "AON", "CCI", "PSA", "CME", "ICE", "CB",
  197. "MHFI", "AMP" };
  198.  
  199. String[] XLI = { "XLI", "GE", "UTX", "UNP", "BA", "MMM", "HON",
  200. "UPS", "CAT", "DHR", "EMR", "LMT", "CMI", "FDX", "ETN",
  201. "GD", "DE", "PCP", "RTN", "ITW", "CSX", "NSC", "DAL",
  202. "NOC", "PCAR", "PH", "ROK", "GWW", "TYC", "WM", "LUV",
  203. "FLR", "DOV", "PNR", "IR" };
  204.  
  205. String[] XLV = { "XLV", "JNJ", "PFE", "MRK", "GILD", "AMGN", "BMY",
  206. "ABBV", "BIIB", "UNH", "ESRX", "CELG", "MDT", "LLY", "ABT",
  207. "TMO", "MCK", "ACT", "AGN", "BAX", "ALXN", "COV", "WLP",
  208. "AET", "REGN", "CAH", "SYK", "BDX", "FRX", "PRGO", "CI",
  209. "MYL", "STJ", "A", "HUM" };
  210.  
  211. // creating new arrays based on stocks in index selected by user
  212. // (comobobox)
  213. DataGeting[] listEntities = new DataGeting[XLK.length];
  214. for (int i = 0; i < XLK.length; i++)
  215. {
  216. ConstructorURL spolka = new ConstructorURL(
  217. startDate, endDate, XLK[i]);
  218. DataGeting new1 = new DataGeting(
  219. spolka.constructURL(), HowManyDaysStrdv, i);
  220. listEntities[i] = new1;
  221. }
  222.  
  223. // creating new arrays with pseudo ID and standard deviation for
  224. // definite day (day 0 = end date)
  225. // rows = days
  226. // columns = stocks
  227. StockStandardDeviation[][] listStrd = new StockStandardDeviation[listEntities[0]
  228. .getStandardDeviationList().size()][XLK.length - 1];
  229.  
  230. // filling arrays with ID's and standard deviation's stock
  231. for (int i = 0; i < listEntities[0].getStandardDeviationList()
  232. .size(); i++)
  233. {
  234. for (int j = 0; j < XLK.length - 1; j++)
  235. {
  236. StockStandardDeviation newStock = new StockStandardDeviation(
  237. j + 1, listEntities[j + 1].getStandardDeviation(i));
  238. listStrd[i][j] = newStock;
  239. }
  240. }
  241.  
  242. // sorting every day (day 0 = end date) from stock with the lowest
  243. // standard deviation to the highest
  244. Sorter sortMachine = new Sorter();
  245. for (int j = 0; j < listStrd[0].length; j++)
  246. {
  247. for (int i = 0; i < listStrd.length; i++)
  248. {
  249. Arrays.sort(listStrd[i], sortMachine);
  250. }
  251. }
  252.  
  253. // creating list of result from opening short position every day on
  254. // index ETF
  255. // number of shares is calculating using the invest amount/2
  256. // (because the second half
  257. // of amount will be invest in stock belongs to index) / adjustment
  258. // close price
  259. ArrayList<Double> indexResult = new ArrayList<Double>();
  260. for (int i = 0; i < listStrd.length; i++)
  261. {
  262. int numberOfShares = (int) (Math.round((investmentAmount / 2)
  263. / listEntities[0].getAdjClosePrice(i + 1)));
  264. double result = (listEntities[0].getOpenPrice(i) - listEntities[0]
  265. .getClosePrice(i)) * numberOfShares;
  266. indexResult.add(result);
  267. }
  268.  
  269. // same as the above but creating list for stocks in xlu index
  270. // with the smallest standard deviation
  271. // param j(5 in this case) is a number of stocks to buy, so
  272. // simulation buy 5 stocks
  273. // with the lowest standard deviation
  274. ArrayList<Double> stocksResult = new ArrayList<Double>();
  275. for (int i = 0; i < listStrd.length; i++)
  276. {
  277. double result = 0;
  278. for (int j = 0; j < NumberOfEntities; j++)
  279. {
  280. int numberOfShares = (int) ((Math
  281. .round((investmentAmount / 2)) / NumberOfEntities / listEntities[listStrd[i][j]
  282. .getID()].getAdjClosePrice(i + 1)));
  283. result += ((listEntities[listStrd[i][j].getID()]
  284. .getClosePrice(i) - listEntities[listStrd[i][j]
  285. .getID()].getOpenPrice(i)) * numberOfShares);
  286.  
  287. }
  288. stocksResult.add(result);
  289. }
  290.  
  291. // showing results in AreaText
  292. taResultArea.append("Results of invested " + investmentAmount
  293. + "$ and Standard Deviation of: " + HowManyDaysStrdv
  294. + " Days are: \n");
  295. taResultArea.append("Date" + " Result \n");
  296.  
  297. // creating loop which sum the result from stocks and index and then
  298. // show the result in the textarea
  299. // and calculating cumulative result
  300. double cumulativeResult = 0;
  301. for (int i = 0; i < stocksResult.size(); i++)
  302. {
  303. taResultArea.append("Result for day "
  304. + listEntities[0].getDefineDate(i));
  305. taResultArea.append(String.format(" is: %.2f",
  306. indexResult.get(i) + stocksResult.get(i))
  307. + " $ \n");
  308. cumulativeResult += indexResult.get(i) + stocksResult.get(i);
  309. }
  310.  
  311. taResultArea.append(String.format("Cumulative result: %.2f ",
  312. cumulativeResult) + "$ \n");
  313. taResultArea.append(String.format("Average result per day: %.2f ",
  314. cumulativeResult / stocksResult.size()) + "$ \n");
  315. long t2 = System.currentTimeMillis();
  316. taResultArea.append("Time needed to created the simulation: "
  317. + String.valueOf((t2 - t1) / 1000.0) + "sec");
  318.  
  319. bChart.setEnabled(true);
  320.  
  321. }
  322. else if(e.getSource() == bChart){
  323. // here i want creat chart
  324. }
  325.  
  326. else if (e.getSource() == bExit)
  327. {
  328. dispose();
  329. }
  330.  
  331. }
  332.  
  333. @Override
  334. public void stateChanged(ChangeEvent arg0)
  335. {
  336.  
  337. lEntitiesNumber.setText("Number of entities: "
  338. + slNumberOfEntities.getValue());
  339. lStrdDays.setText("Days of Standard Deviation: "
  340. + slGetHowManyDaysStrdv.getValue());
  341. }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement