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:
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)
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:
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)
- 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.
- It calculates the number of loss-making trades by subtracting the total profitable trades from the total number of trades.
- It calculates the percentage of profitable trades and percentage of loss trades.
- It calculates the net profit/loss by taking into account the profits and losses.
- It calculates the total brokerage.
- It calculates the profit after deducting brokerage.
- It checks if the net profit is positive or negative and prints the result accordingly.
- It prints the percentage of profitable and loss trades.
- It prints the total brokerage for the complete year.
- It prints the total profits and total losses.
- All the results are rounded off to one place of decimal using python's round() function.
- 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
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
Post a Comment