Program to calculate net gain or loss for the complete year in swing stock trading after deducting brokerage and considering trades resulting in profits and losses


Input to chat GPT:

write a program to give net gain or loss over a span of trades in stocks. the program should ask the number of trades taken per week, the average stoploss in percentage, the average profit in percentage, the number of profitable trades, the number of trades which resulted in loss. the average position size, and any other parameters required. 

program can be tested at https://www.programiz.com/python-programming/online-compiler/

output:

<start of program>

def net_gain_loss(num_trades_per_week, avg_stoploss, avg_profit, num_profitable_trades, num_loss_trades, avg_position_size):

    # calculate the percentage of profitable trades

    profitable_percent = (num_profitable_trades / num_trades_per_week) * 100

    # calculate the percentage of loss trades

    loss_percent = (num_loss_trades / num_trades_per_week) * 100

    # calculate the net profit/loss

    net_profit = (num_profitable_trades * avg_profit * avg_position_size) - (num_loss_trades * avg_stoploss * avg_position_size)

    # check if net profit is positive or negative

    if net_profit > 0:

        print("Net gain: ", net_profit)

    else:

        print("Net loss: ", net_profit)

    # print the percentage of profitable and loss trades

    print("Profit percentage: ", profitable_percent)

    print("Loss percentage: ", loss_percent)


# example usage:

num_trades_per_week = 20

avg_stoploss = 0.05

avg_profit = 0.1

num_profitable_trades = 15

num_loss_trades = 5

avg_position_size = 1000

net_gain_loss(num_trades_per_week, avg_stoploss, avg_profit, num_profitable_trades, num_loss_trades, avg_position_size)

---
This program takes in six parameters: number of trades taken per week, the average stoploss in percentage, the average profit in percentage, the number of profitable trades, the number of trades which resulted in loss, and the average position size. It then calculates the net gain or loss, the percentage of profitable trades, and the percentage of loss trades. The results are printed out.

It's important to keep in mind that this program is a simple example and it is not taking into account any other parameters like trade fees, taxes, bid-ask spread, or other market conditions. It's also assuming that the stoploss and profit percentages are entered as decimal values (i.e. 5% = 0.05) and the position size is entered in the currency of the trade.
---

 Input 2 :

 write a program which takes in six parameters: number of trades taken per year , the average stoploss in percentage, the average profit in percentage, the number of profitable trades, the number of trades which resulted in loss, and the average position size, the percentage borkerage charged per trade. It then calculates the net gain or loss, the percentage of profitable trades, and the percentage of loss trades, the profit after deducting brokerage. The results are printed out. the total brokerage for the complete year is printed separately. the total profits and losses are printed seperately too.

output:

<start of program>

def net_gain_loss(num_trades_per_year, avg_stoploss, avg_profit, num_profitable_trades, num_loss_trades, avg_position_size, brokerage_percent):

    # calculate the percentage of profitable trades

    profitable_percent = (num_profitable_trades / num_trades_per_year) * 100

    # calculate the percentage of loss trades

    loss_percent = (num_loss_trades / num_trades_per_year) * 100

    # calculate the net profit/loss

    net_profit = (num_profitable_trades * avg_profit * avg_position_size) - (num_loss_trades * avg_stoploss * avg_position_size)

    # calculate the total brokerage

    total_brokerage = (num_trades_per_year * avg_position_size * brokerage_percent) / 100

    # calculate profit after deducting brokerage

    profit_after_brokerage = net_profit - total_brokerage

    # check if net profit is positive or negative

    if profit_after_brokerage > 0:

        print("Net gain: ", profit_after_brokerage)

    else:

        print("Net loss: ", profit_after_brokerage)

    # print the percentage of profitable and loss trades

    print("Profit percentage: ", profitable_percent)

    print("Loss percentage: ", loss_percent)

    #print the total brokerage

    print("Total brokerage: ", total_brokerage)

    #print the total profits and total losses

    print("Total profits: ", num_profitable_trades * avg_profit * avg_position_size)

    print("Total losses: ", num_loss_trades * avg_stoploss * avg_position_size)


# example usage:

num_trades_per_year = 250

avg_stoploss = 0.05

avg_profit = 0.1

num_profitable_trades = 175

num_loss_trades = 75

avg_position_size = 1000

brokerage_percent = 0.02

net_gain_loss(num_trades_per_year, avg_stoploss, avg_profit, num_profitable_trades, num_loss_trades, avg_position_size, brokerage_percent)

--
This program takes in seven parameters: number of trades taken per year, the average stoploss in percentage, the average profit in percentage, the number of profitable trades, the number of trades which resulted in loss, the average position size, and the percentage brokerage charged per trade.

It then calculates the net gain or loss, percentage of profitable trades, percentage of loss trades, total brokerage for the complete year, profit after deducting brokerage and the total profits and losses. The results are printed out.
----

input 3:
amend the program to let the user input each parameter

output:
<start of program>
def net_gain_loss():
    num_trades_per_year = int(input("Enter number of trades taken per year: "))
    avg_stoploss = float(input("Enter the average stoploss in percentage: "))
    avg_profit = float(input("Enter the average profit in percentage: "))
    num_profitable_trades = int(input("Enter the number of profitable trades: "))
    num_loss_trades = int(input("Enter the number of trades which resulted in loss: "))
    avg_position_size = int(input("Enter the average position size: "))
    brokerage_percent = float(input("Enter the percentage brokerage charged per trade: "))

    # calculate the percentage of profitable trades
    profitable_percent = (num_profitable_trades / num_trades_per_year) * 100
    # calculate the percentage of loss trades
    loss_percent = (num_loss_trades / num_trades_per_year) * 100
    # calculate the net profit/loss
    net_profit = (num_profitable_trades * avg_profit * avg_position_size) - (num_loss_trades * avg_stoploss * avg_position_size)
    # calculate the total brokerage
    total_brokerage = (num_trades_per_year * avg_position_size * brokerage_percent) / 100
    # calculate profit after deducting brokerage
    profit_after_brokerage = net_profit - total_brokerage
    # check if net profit is positive or negative
    if profit_after_brokerage > 0:
        print("Net gain: ", profit_after_brokerage)
    else:
        print("Net loss: ", profit_after_brokerage)
    # print the percentage of profitable and loss trades
    print("Profit percentage: ", profitable_percent)
    print("Loss percentage: ", loss_percent)
    #print the total brokerage
    print("Total brokerage: ", total_brokerage)
    #print the total profits and total losses
    print("Total profits: ", num_profitable_trades * avg_profit * avg_position_size)
    print("Total losses: ", num_loss_trades * avg_stoploss * avg_position_size)

# call the function to start the program
net_gain_loss()

input 4:
the program should calculate the number of loss making trades by substracting the total profitable trades from total number of trades. the system should calculate the net profit by taking into account profits for the year, loss for the year and adjusting brokerage. all output should be rounded off to one place of decimal.

output:

Sure, here is an updated version of the program that calculates the number of loss-making trades by subtracting the total profitable trades from the total number of trades, calculates the net profit by taking into account profits for the year, loss for the year and adjusting brokerage and rounded off to one place of decimal:

<start of program>
import math

def net_gain_loss():
    num_trades_per_year = int(input("Enter number of trades taken per year: "))
    avg_stoploss = float(input("Enter the average stoploss in percentage: "))
    avg_profit = float(input("Enter the average profit in percentage: "))
    num_profitable_trades = int(input("Enter the number of profitable trades: "))
    avg_position_size = int(input("Enter the average position size: "))
    brokerage_percent = float(input("Enter the percentage brokerage charged per trade: "))
    #calculate the number of loss making trades
    num_loss_trades = num_trades_per_year - num_profitable_trades
    # calculate the percentage of profitable trades
    profitable_percent = (num_profitable_trades / num_trades_per_year) * 100
    # calculate the percentage of loss trades
    loss_percent = (num_loss_trades / num_trades_per_year) * 100
    # calculate the net profit/loss
    net_profit = (num_profitable_trades * avg_profit * avg_position_size) - (num_loss_trades * avg_stoploss * avg_position_size)
    # calculate the total brokerage
    total_brokerage = (num_trades_per_year * avg_position_size * brokerage_percent) / 100
    # calculate profit after deducting brokerage
    profit_after_brokerage = round(net_profit - total_brokerage,1)
    # check if net profit is positive or negative
    if profit_after_brokerage > 0:
        print("Net gain: ", profit_after_brokerage)
    else:
        print("Net loss: ", profit_after_brokerage)
    # print the percentage of profitable and loss trades
    print("Profit percentage: ", round(profitable_percent,1))
    print("Loss percentage: ", round(loss_percent,1))
    #print the total brokerage
    print("Total brokerage: ", round(total_brokerage,1))
    #print the total profits and total losses
    print("Total profits: ", round(num_profitable_trades * avg_profit * avg_position_size,1))
    print("Total losses: ", round(num_loss_trades * avg_stoploss * avg_position_size,1))

# call the function to start the program
net_gain_loss()

<end of program>

This program uses the input() function to let the user input each parameter. It calculates the number of loss-making trades by subtracting the total profitable trades from the total number of trades. Then it calculates the net profit by taking into account profits for the year, loss for the year and adjusting brokerage and rounded off to one place of decimal using python's round() function. The results are printed out.

input 5:
enumerate the specifications for this program point wise.

output:
Sure, here are the specifications for the program point-wise:

  1. The program takes input from the user for the number of trades taken per year, the average stoploss in percentage, the average profit in percentage, the number of profitable trades, the average position size and the percentage brokerage charged per trade.
  2. It calculates the number of loss-making trades by subtracting the total profitable trades from the total number of trades.
  3. It calculates the percentage of profitable trades and percentage of loss trades.
  4. It calculates the net profit/loss by taking into account the profits and losses.
  5. It calculates the total brokerage.
  6. It calculates the profit after deducting brokerage.
  7. It checks if the net profit is positive or negative and prints the result accordingly.
  8. It prints the percentage of profitable and loss trades.
  9. It prints the total brokerage for the complete year.
  10. It prints the total profits and total losses.
  11. All the results are rounded off to one place of decimal using python's round() function.
  12. It's important to keep in mind that this program is a simple example and it is not taking into account any other parameters like trade fees, taxes, bid-ask spread, or other market conditions. It's also assuming that the stoploss and profit percentages are entered as decimal values
_____
Program was modified to remove some calculation mistakes and improve output remark text.

def net_gain_loss():
    num_trades_per_year = int(input("Enter number of trades taken per year: "))
    avg_stoploss = float(input("Enter the average stoploss in percentage: "))
    avg_profit = float(input("Enter the average profit in percentage: "))
    num_profitable_trades = int(input("Enter the number of profitable trades: "))
    avg_position_size = int(input("Enter the average position size: "))
    brokerage_percent = float(input("Enter the percentage brokerage charged per trade: "))
    #calculate the number of loss making trades
    num_loss_trades = num_trades_per_year - num_profitable_trades
    # calculate the percentage of profitable trades
    profitable_percent = (num_profitable_trades / num_trades_per_year) * 100
    # calculate the percentage of loss trades
    loss_percent = (num_loss_trades / num_trades_per_year) * 100
    # calculate the net profit/loss
    net_profit = (num_profitable_trades * avg_profit * 0.01 * avg_position_size) - (num_loss_trades * avg_stoploss * avg_position_size * 0.01)
    # calculate the total brokerage
    total_brokerage = (num_trades_per_year * avg_position_size * brokerage_percent) / 100
    # calculate profit after deducting brokerage
    profit_after_brokerage = round(net_profit - total_brokerage,1)
    # check if net profit is positive or negative
    if profit_after_brokerage > 0:
        print("Net gain: ", profit_after_brokerage)
    else:
        print("Net loss: ", profit_after_brokerage)
    # print the percentage of profitable and loss trades
    print("Percentage of profitable trades: ", round(profitable_percent,1))
    print("Percentage of losing trades: ", round(loss_percent,1))
    #print the total brokerage
    print("Total brokerage: ", round(total_brokerage,1))
    #print the total profits and total losses
    print("Total profit in trades: ", round(num_profitable_trades * avg_profit * 0.01 *  avg_position_size,1))
    print("Total loss in trades: ", round(num_loss_trades * avg_stoploss * 0.01 * avg_position_size,1))

# call the function to start the program
net_gain_loss()

Comments

Popular posts from this blog

Ranking of Airlines by Safety (Based on Accidents and Serious Snags, 2005–2025)

Points to clarify with your employer during interview to save you from stress and surprise later

100 stable and 100 unstable job roles for 2025–2030