Advertisement
javaprogrammer1996

updated Main.java

Sep 10th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. package application;
  2.    
  3.  
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6.  
  7. import cern.jet.random.Normal;
  8. import javafx.application.Application;
  9. import javafx.fxml.FXML;
  10. import javafx.fxml.FXMLLoader;
  11. import javafx.stage.Stage;
  12. import javafx.scene.Parent;
  13. import javafx.scene.Scene;
  14. import javafx.scene.control.Button;
  15. import javafx.scene.control.Label;
  16. import javafx.scene.control.RadioButton;
  17. import javafx.scene.control.TextArea;
  18. import javafx.scene.control.TextField;
  19. import javafx.event.EventHandler;
  20.  
  21.  
  22.  
  23. public class Main extends Application  {
  24.    
  25.     @FXML
  26.     private static RadioButton callBox1;
  27.    
  28.     @FXML
  29.     private static RadioButton putBox1;
  30.    
  31.     @FXML
  32.     private static TextField inputCSP;
  33.    
  34.     @FXML
  35.     private static TextField inputEP;
  36.    
  37.     @FXML
  38.     private static TextField inputRFIR;
  39.    
  40.     @FXML
  41.     private static TextField inputTUE;
  42.    
  43.     @FXML
  44.     private static TextField inputV;
  45.    
  46.     @FXML
  47.     private Button calculatebtn;
  48.    
  49.     @FXML
  50.     private static String output;
  51.  
  52.     @FXML
  53.     private static TextField outputarea;
  54.    
  55.     @Override
  56.     public void start(Stage primaryStage) throws Exception {
  57.        
  58.            
  59.     Parent root = FXMLLoader.load(getClass().getResource("/application/GUI.fxml"));
  60.     Scene scene = new Scene(root,640,400);
  61.     primaryStage.setResizable(false);
  62.     primaryStage.setScene(scene);
  63.     primaryStage.show();
  64.     primaryStage.setTitle("Black-Scholes Pricer (European Options)");
  65.         }
  66.  
  67.    
  68.    
  69.     public static void maths(String[] args) {
  70.        
  71.         System.out.println("hi");
  72.    
  73.         Normal a = new Normal(0,1, null);
  74.            
  75.         Double s= Double.parseDouble(inputCSP.getText());
  76.        
  77.         Double e = Double.parseDouble(inputEP.getText());
  78.  
  79.         Double risk = Double.parseDouble(inputRFIR.getText());
  80.         Double r2 = (risk/100);
  81.        
  82.         Double time = Double.parseDouble(inputTUE.getText());
  83.         Double t2 = (time/365);
  84.        
  85.         Double volatility = Double.parseDouble(inputV.getText());
  86.         Double v2 = (volatility/100);  
  87.        
  88.      // Calculation of d1
  89.         Double t = Math.log(s/e) + (r2 + 0.5*Math.pow(v2, 2))*t2;
  90.         Double b = v2*Math.sqrt(t2);
  91.         Double d1 = t/b;
  92.        
  93.         // Calculation of d2
  94.         double root = Math.pow(t2, 0.5);
  95.         double d2 = d1 - (v2*root);
  96.        
  97.         // Rounding d1 and d2 to 2 decimal places for use in normal distribution
  98.        
  99.         double roundedd1 = Math.round(d1*100)/100.0;
  100.         double roundedd2 = Math.round(d2 *100)/100.0;
  101.        
  102.         double cumulatived1 = a.cdf(roundedd1);
  103.         double cumulatived2 = a.cdf(roundedd2);
  104.        
  105.         double roundc1 = Math.round(cumulatived1*10000)/10000.0;
  106.         double roundc2 = Math.round(cumulatived2*10000)/10000.0;
  107.  
  108.         //Use of final formula
  109.        
  110.         double e2 = Math.E;
  111.         double value = (s*roundc1) - (((e/Math.pow(e2, r2*t2))*roundc2));
  112.        
  113.         double value2 = Math.round(value*1000)/1000.0;
  114.        
  115.         //calculating the put
  116.        
  117.         double value3 = (value2 + (e/Math.pow(e2, r2*t2)) - s);
  118.        
  119.         String valuecall = String.valueOf(value2);
  120.         String valueput = String.valueOf(value3);
  121.        
  122.         if(callBox1.isSelected()) output=valuecall;
  123.         if(putBox1.isSelected()) output=valueput;  
  124.        
  125.         outputarea.setText(output);
  126.         }
  127.    
  128.    
  129.  
  130.    
  131.    
  132.     public static void main(String[] args) {
  133.         launch(args);
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement