Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:17:46+00:00 2026-06-07T05:17:46+00:00

I am writing a stock market trading game base on yahoo using php/mysql &

  • 0

I am writing a stock market trading game base on yahoo using php/mysql & it’s almost done. Last part I’m struggling is calculating the profit & loss

Lets say have these as transaction
Starting balance = $5000

> order_id stock_id  transaction qty_shares cost  user_id
> 1        1234      B           100        1000  1
> 2        1234      B           50         450   1
> 3        1234      S           80         640   1
> 4        1234      S           30         300   1

I’m lost trying to come up with an algorithm or a mysql query to calculate the profit & loss

Added after reading the 2 replies posted below
Additional Note :
Please take note that in this scenerio 150 shares was purchased & only 110 shares was sold & all was purchased & sold at different prices

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:17:48+00:00Added an answer on June 7, 2026 at 5:17 am
    SELECT SUM(qty_shares*cost) AS out, user_id, stock_id, order_id
    FROM table
    WHERE transaction = 'B'
    GROUP BY user_id, stock_id, order_id
    

    Then:

    SELECT SUM(qty_shares*cost) AS in, user_id, stock_id
    FROM table
    WHERE transaction = 'S'
    GROUP BY user_id, stock_id, order_id
    

    PHP:

    $Margin = ($query2->in - $query1->out);
    $Balance += $Margin;
    

    It could all be done on a single query, but if speed is more important then this could be faster than subqueries.

    EDIT:
    For average profit/loss per share:

    SELECT AVG(qty_shares/cost) AS price_per_share, transaction, user_id, stock_id
    FROM table
    GROUP BY transaction,user_id,stock_id
    

    This should return the average price per share for each transaction type, per stock type per user. so the transaction S row is average price sold for, and transaction B row is average price bought for. The difference between them is average profit or loss.

    SELECT 
    SUM(
    qty_shares *
        (
          SELECT AVG(A.qty_shares/A.cost) AS price_per_share
          FROM table AS A
          WHERE A.transaction = table.transaction
          AND   A.user_id = table.user_id
          AND   A.stock_id = table.stock_id
        )
    ), user_id, stock_id, order_id
    FROM table
    WHERE transaction = 'B'
    GROUP BY user_id, stock_id, order_id
    

    That query should return the number of shares sold per row multiplied by the current average price per share, giving you the average margin per transaction with the sold margin being negative. You may find the query slow though. I’d recommend using this information as a starting point and finding the best way for your application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a small application about stock monitoring for my company. Using mysql
when using Python's stock XML tools such as xml.dom.minidom for XML writing, a file
I am writing a stock market-related app, and would like to include an indicator
I am writing a stock market system that uses several threads to process the
I am writing a simple program to filter out stock quotes from a given
When writing C++ code I've learned that using the stack to store memory is
I am writing host code for a CUDA program, so I am stuck using
Writing htaccess that allows me to remove index.php from the URL can confuse search
I am writing an app in python using getk and I've come across a
I've gotten stuck writing some parallel c code using OpenMP for a concurrency course.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.