Exemple #1
0
    def test_figure(self):
        my_app = edifice.App(plotting.Figure(lambda x: None),
                             create_application=False)

        class MockQtApp(object):
            def exec_(self):
                pass

        my_app.app = MockQtApp()
        my_app.start()
Exemple #2
0
 def render(self):
     all_plots = app_state.subscribe(self, "all_plots").value
     return View(layout="column", style={"margin": 10})(
         ScrollView(layout="column")(*[
             add_divider(PlotDescriptor(plotname)) for plotname in all_plots
         ]),
         # Edifice comes with Font-Awesome icons for your convenience
         IconButton(name="plus", title="Add Plot", on_click=self.add_plot),
         # We create a lambda fuction so that the method doesn't compare equal to itself.
         # This forces re-renders everytime this entire component renders.
         plotting.Figure(lambda ax: self.plot(ax)),
     )
Exemple #3
0
 def render(self):
     return ed.View(layout="row")(
         ed.View(layout="column", style={
             "width": 720,
             "margin": 10
         })(
             ed.View(layout="row")(
                 ed.IconButton("pause" if self.is_playing else "play",
                               on_click=lambda e: self.set_state(
                                   is_playing=not self.is_playing)),
                 ed.Button(
                     "Reset",
                     on_click=lambda e: self.set_state(simulation_time=0)),
             ),
             plotting.Figure(lambda figure: self.plot(figure)),
             ed.View(layout="row", style={"margin": 10})(
                 ed.Label("Angular Frequency"),
                 ed.Slider(value=self.angular_frequency,
                           min_value=1,
                           max_value=10,
                           on_change=lambda value: self.set_state(
                               angular_frequency=value, simulation_time=0)),
                 ed.Label("Damping Factor"),
                 ed.Slider(value=self.damping,
                           min_value=-3,
                           max_value=0,
                           on_change=lambda value: self.set_state(
                               damping=value, simulation_time=0)),
             ),
             # We position the ball and the centroid using absolute positioning.
             # The label and ball offsets are different since we have to take into-account the size of the ball
             ed.View(layout="none",
                     style={
                         "width": 720,
                         "height": 10,
                         "margin-top": 40
                     })
             (
                 ed.Icon("bowling-ball",
                         size=20,
                         color=(255, 0, 0, 255),
                         style={
                             "left":
                             350 + 200 * self.calculate_harmonic_motion(
                                 self.simulation_time)
                         }),
                 ed.Label("|",
                          style={
                              "left": 356,
                              "font-size": 20,
                              "color": "blue"
                          }),
             )))
Exemple #4
0
    def render(self):
        values = ["option_price", "delta", "gamma", "theta", "vega",]
        days_to_maturity = None
        if self.expiry:
            days_to_maturity = int(days_till_expiration(self.expiry)) - 1
        expiry_loaded = self.ticker is not None and self.expiries != [] and not self.loading_expiries
        option_chain_loaded = (self.ticker is not None and self.option_chain is not None
                               and not self.loading_option_chain)

        if expiry_loaded and option_chain_loaded:
            option_chain = self.get_option_chain()
            implied_vol = option_chain[option_chain.strike == float(self.strike_price)].impliedVolatility.iloc[0] * 100
            if math.isnan(implied_vol):
                implied_vol = 1000.0
            args = [float(self.strike_price), -float(days_to_maturity),
                    float(self.last_close_price), 0.01 / 365, implied_vol]
            greeks = {
                "gamma": black_scholes.gamma(*args),
                "theta": black_scholes.theta(*args),
                "vega": black_scholes.vega(*args),
            }
            if self.option_type == "Call":
                greeks.update({
                    "delta": black_scholes.delta(*args),
                    "option_price": black_scholes.call_price(*args),
                })
            else:
                greeks.update({
                    "delta": black_scholes.delta(*args) - 1,
                    "option_price": black_scholes.put_price(*args),
                })

        return ed.View(style={"align": "top", "margin": 10})(
            ed.Label("Loading..." if self.loading_expiries or self.loading_option_chain else "",
                     style={"margin-bottom": 5}),
            ed.View(layout="row", style={"align": "left"})(
                ed.View(layout="row", style={"width": 200})(
                    ed.Label("Ticker"),
                    ed.TextInput(self.ticker, on_change=self.ticker_changed),
                    ed.Dropdown(str(self.option_type), options=["Call", "Put"],
                                on_select=lambda text: self.set_state(option_type=text)),
                ),
                expiry_loaded and ed.View(layout="row", style={"width": 170, "margin-left": 5})(
                    ed.Label("Expiry"),
                    ed.Dropdown(str(self.expiry), options=self.expiries, on_select=self.expiry_changed),
                ),
                option_chain_loaded and ed.View(layout="row", style={"width": 150, "margin-left": 5})(
                    ed.Label("Strike Price"),
                    ed.Dropdown(str(self.strike_price),
                            options=list(map(str, self.get_option_chain().strike)) if self.option_chain else [],
                            on_select=self.strike_changed),
                ),
            ),
            expiry_loaded and option_chain_loaded and ed.View(layout="row", style={"align": "left"})(
                ed.Label("X"),
                ed.Dropdown(selection=self.xaxis, options=["days_to_expiration", "stock_price", "implied_vol"],
                            on_select=lambda text: self.set_state(xaxis=text)),
                ed.Label("Y"),
                ed.Dropdown(selection=self.yaxis, options=values,
                            on_select=lambda text: self.set_state(yaxis=text)),
                self.xaxis != "stock_price" and ed.Label(f"Stock Price ({self.stock_price:.2f})"),
                self.xaxis != "stock_price" and ed.Slider(
                    self.stock_price, min_value=self.last_close_price / 3, max_value=self.last_close_price * 3 + 1,
                    on_change=lambda val: self.set_state(stock_price=val),
                ).set_key("stock_price_slider"),
                self.xaxis != "days_to_expiration" and ed.Label(f"Days to Maturity ({self.days_to_maturity:.1f})"),
                self.xaxis != "days_to_expiration" and ed.Slider(
                    self.days_to_maturity, min_value=days_to_maturity, max_value=-0.1,
                    on_change=lambda val: self.set_state(days_to_maturity=val),
                ).set_key("maturity_slider"),
                self.xaxis != "implied_vol" and ed.Label(f"Implied Vol ({self.implied_vol:.1f})"),
                self.xaxis != "implied_vol" and ed.Slider(
                    self.implied_vol, min_value=implied_vol/2, max_value=implied_vol*2 + 10,
                    on_change=lambda val: self.set_state(implied_vol=val),
                ).set_key("vol_slider"),
            ),
            expiry_loaded and option_chain_loaded and ed.View()(
                ed.Label("Option Greeks:", style={"font-size": 18}),
                ed.View(layout="row")(
                    ed.Label(f"<b>Days to maturity:</b> {days_to_maturity}"),
                    ed.Label(f"<b>Stock price:</b> ${self.last_close_price:.2f}\t"),
                    ed.Label(f"<b>Option price:</b> ${greeks['option_price']:.2f}\t\n"),
                    ed.Label(f"<b>Implied Vol:</b> {implied_vol:.0f}%\t"),
                ),
                ed.View(layout="row")(
                    ed.Label(f"<b>Delta:</b> {greeks['delta']:.2f}\t"),
                    ed.Label(f"<b>Gamma:</b> {greeks['gamma']:.3f}\t"),
                    ed.Label(f"<b>Theta:</b> {greeks['theta']:.3f}\t"),
                    ed.Label(f"<b>Vega:</b> {greeks['vega']:.2f}\t"),
                )
            ),
            expiry_loaded and option_chain_loaded and plotting.Figure(plot_fun=lambda ax: self.plot(ax)),
        )