C# CALCOLO DEL PnL

CODICE: 

using System;

using System.Windows.Forms;


namespace CalcoloPNL

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

            button1.Click += button1_Click;

        }


        private void button1_Click(object sender, EventArgs e)

        {

            if (double.TryParse(textBox1.Text, out double prezzoAcquisto) &&

                double.TryParse(textBox2.Text, out double prezzoVendita) &&

                int.TryParse(textBox3.Text, out int quantita))

            {

                double pnl = 0;


                if (quantita > 0)

                {

                    pnl = (prezzoVendita - prezzoAcquisto) * quantita; // Calcolo per posizione long

                }

                else if (quantita < 0)

                {

                    pnl = (prezzoAcquisto - prezzoVendita) * Math.Abs(quantita); // Calcolo per posizione short

                }


                label1.Text = $"PnL Totale: {pnl:F2}";

            }

            else

            {

                MessageBox.Show("Inserimento non valido. Inserisci numeri validi per Prezzo di Acquisto, Prezzo di Vendita e Quantità.", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }

    }

}


OUTPUT:


Post più popolari