Guest User

Untitled

a guest
Mar 15th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.util.Arrays;
  4. import java.util.Calendar;
  5.  
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9. import javax.swing.JScrollPane;
  10. import javax.swing.JTextArea;
  11. import javax.swing.JTextField;
  12.  
  13. public class TestYahooFinance extends JFrame implements ActionListener
  14. {
  15. private JButton bRunSimulation, bExit;
  16. private JLabel lShowResults;
  17. private JTextField tGetHowManyDaysStrdv;
  18. private JTextField tGetInvestmentAmount;
  19. private int HowManyDaysStrdv;
  20. private int investmentAmount;
  21. private JTextArea taResultArea;
  22.  
  23. public TestYahooFinance()
  24. {
  25. // tworzenie ramki o rozmiarach i nazwie
  26. setSize(600, 700);
  27. setTitle("Day-trading simulation");
  28.  
  29. // tworzymy przyciski o nazwie w nawiasie
  30. bRunSimulation = new JButton("Run Simulation");
  31. setLayout(null);
  32. bRunSimulation.setBounds(20, 500, 150, 50);
  33. // dodajemy do ramki przycisk
  34. add(bRunSimulation);
  35. // "sluchacz" odbiera sygnal/ruch z przycisku this oznacza cala ramka
  36. bRunSimulation.addActionListener(this);
  37.  
  38. bExit = new JButton("Exit");
  39. bExit.setBounds(20, 570, 150, 50);
  40. add(bExit);
  41. bExit.addActionListener(this);
  42.  
  43. lShowResults = new JLabel("Days of Standard Deviation:");
  44. lShowResults.setBounds(20, 20, 300, 20);
  45. add(lShowResults);
  46.  
  47. tGetHowManyDaysStrdv = new JTextField();
  48. tGetHowManyDaysStrdv.setBounds(20, 50, 100, 25);
  49. tGetHowManyDaysStrdv.setToolTipText("Enter number days of Standard Deviation");
  50. add(tGetHowManyDaysStrdv);
  51.  
  52.  
  53.  
  54. JTextArea taResultArea = new JTextArea();
  55. // taResultArea.setEditable(false);
  56. JScrollPane scrollPane = new JScrollPane(taResultArea);
  57. scrollPane.setBounds(200, 15, 350, 600);
  58. add(scrollPane);
  59. }
  60.  
  61. public static void main(String[] args)
  62. {
  63. // tworzenie nowego obiektu
  64. TestYahooFinance simulation = new TestYahooFinance();
  65. // jak nacisniemy X to sie ramka wylacza a nie ukrywa
  66. simulation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67. // setVisible ramka sie pojawia
  68. simulation.setVisible(true);
  69.  
  70. }
  71.  
  72. public void actionPerformed(ActionEvent e)
  73. {
  74.  
  75. if (e.getSource() == bRunSimulation)
  76. {
  77.  
  78. HowManyDaysStrdv = Integer.parseInt(tGetHowManyDaysStrdv.getText());
  79. // Start Date
  80. Calendar startDate = Calendar.getInstance();
  81. // Set startDate
  82. startDate.set(Calendar.YEAR, 2013);
  83. startDate.set(Calendar.MONTH, 0); // Month Jan = 0, Feb = 1...Dec =// 11
  84. startDate.set(Calendar.DATE, 1);
  85.  
  86. // End Date
  87. Calendar endDate = Calendar.getInstance();
  88. // Set endtDate
  89. endDate.set(Calendar.YEAR, 2014);
  90. endDate.set(Calendar.MONTH, 2);
  91. endDate.set(Calendar.DATE, 15);
  92.  
  93. // sector + entities belong to the sector XLU
  94. String[] xlu = { "XLU", "DUK", "SO", "D", "NEE", "EXC", "AEP",
  95. "PCG", "SRE", "PPL", "ED", "PEG", "FE", "EIX", "XEL", "NU",
  96. "ETR", "DTE", "CNP", "WEC", "OKE", "NI", "AEE", "NRG",
  97. "AES", "CMS", "SCG", "PNW", "POM", "GAS", "TEG", "TE" };
  98.  
  99. // creating new arrays based on stocks in XLU arrays above
  100. YahooFinanceHttp[] listEntities = new YahooFinanceHttp[xlu.length];
  101. for (int i = 0; i < xlu.length; i++)
  102. {
  103. YahooFinanceURLConstruct spolka = new YahooFinanceURLConstruct(
  104. startDate, endDate, xlu[i]);
  105. YahooFinanceHttp new1 = new YahooFinanceHttp(
  106. spolka.constructURL(), HowManyDaysStrdv, i);
  107. listEntities[i] = new1;
  108. }
  109.  
  110. // creating new arrays with pseudo ID and standard deviation for
  111. // definite day (day 0 = end date)
  112. StockStandardDeviation[][] listStrd = new StockStandardDeviation[listEntities[0]
  113. .getSize() - listEntities[0].getHowManyDays()][xlu.length - 1];
  114.  
  115. // filling arrays with ID's and standard deviation's stock
  116. for (int i = 0; i < listEntities[0].getSize()
  117. - listEntities[0].getHowManyDays() - 1; i++)
  118. {
  119. for (int j = 0; j < xlu.length - 1; j++)
  120. {
  121. StockStandardDeviation newStock = new StockStandardDeviation(
  122. j + 1, listEntities[j + 1].getStandardDeviation(i),
  123. listEntities[j + 1].getDefineDate(i));
  124. listStrd[i][j] = newStock;
  125. }
  126. }
  127.  
  128. // sorting every day (day 0 = end date) from stock with the lowest
  129. // standard deviation to the highest
  130. Sorter sortMachine = new Sorter();
  131. for (int j = 0; j < listStrd[0].length; j++)
  132. {
  133. for (int i = 0; i < listStrd.length; i++)
  134. {
  135. Arrays.sort(listStrd[i], sortMachine);
  136. }
  137. }
  138.  
  139. // for (int i = 0; i < 25; i++)
  140. // {
  141. // System.out.println(listStrd[i][i + 5]);
  142. // }
  143.  
  144.  
  145. // Tutaj bedzie petla ktora bedzie zliczac wyniki nastepnie chcialem je wyswietlac w textArea ale poki co
  146. // nawet jednej linii nie potrafie wyswietlic
  147. // dla Jlabel dziala poprawnie
  148. double result = ((listEntities[listStrd[0][0].getID()]
  149. .getClosePrice(0)) - (listEntities[listStrd[0][0].getID()]
  150. .getOpenPrice(0))) * 1000;
  151.  
  152. lShowResults.setText(String.format("Result: %.2f", result) + "$");
  153. taResultArea.setText(String.format("Result: %.2f", result) + "$");
  154.  
  155. long t2 = System.currentTimeMillis();
  156.  
  157. }
  158. else if (e.getSource() == bExit)
  159. {
  160. dispose();
  161. }
  162.  
  163.  
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment