Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. #!/bin/python3
  2.  
  3. import sys
  4. import os
  5.  
  6. buying_history = [];
  7. buying_history_pointer = 0;
  8.  
  9.  
  10. def getFromTable(shares):
  11. print("-----------------------------")
  12. print("Asked for",shares,"shares")
  13.  
  14. wanted = shares
  15. temp_total = 0.0;
  16. global buying_history_pointer;
  17.  
  18. while(wanted > 0):
  19. print("Wanted", wanted, "more")
  20. print("Available ",buying_history)
  21.  
  22. if wanted < buying_history[buying_history_pointer][0]:
  23. temp_total = temp_total + wanted * buying_history[buying_history_pointer][1]
  24. buying_history[buying_history_pointer][0] = buying_history[buying_history_pointer][0] - wanted
  25. print("Finished with",buying_history)
  26. wanted = 0
  27.  
  28. elif wanted >= buying_history[buying_history_pointer][0]:
  29. temp_total = buying_history[buying_history_pointer][0] * buying_history[buying_history_pointer][1]
  30. wanted = wanted - buying_history[buying_history_pointer][0];
  31. buying_history[buying_history_pointer][0] = 0
  32. buying_history_pointer+=1
  33.  
  34. return temp_total;
  35.  
  36. # Complete the function below.
  37. def calculateTax(trades):
  38. processed_trades = [];
  39. total_tax = float(0);
  40.  
  41. print(trades)
  42. # Create a 2D list
  43. for i in range(0, len(trades)):
  44. processed_trades.append(trades[i].split(","))
  45.  
  46. # Create a history of buying
  47. for i in range(0, len(processed_trades)):
  48. #Whenever there is a buy order
  49. if processed_trades[i][2] == "B":
  50. temp = [int(processed_trades[i][3]),float(processed_trades[i][4])]
  51. buying_history.append(temp)
  52.  
  53.  
  54. for i in range(0,len(processed_trades)):
  55. #Check if is buy or sell
  56. if processed_trades[i][2] == "B":
  57. print("Buy order")
  58. else:
  59. print("Sell order")
  60. cost_price = float(getFromTable(int(processed_trades[i][3])))
  61. selling_price = float(processed_trades[i][3]) * float(processed_trades[i][4])
  62.  
  63. margin = selling_price - cost_price
  64.  
  65. print("C.P",cost_price)
  66. print("S.P", selling_price)
  67. print("Margin", margin)
  68.  
  69. if margin < 0:
  70. #Loss
  71. print("Loss")
  72. else:
  73. print("Profit")
  74. #Calculate the tax
  75. tax = (25/100) * margin
  76. total_tax= total_tax + tax
  77.  
  78.  
  79. print("Total tax so far :",total_tax)
  80.  
  81. return 0;
  82.  
  83.  
  84.  
  85. if __name__ == "__main__":
  86.  
  87. trades = []
  88.  
  89. trades.append("2015-01-03,AAPL,B,50,80.0")
  90. trades.append("2015-01-05,AAPL,B,60,100.0")
  91. trades.append("2015-02-05,AAPL,S,70,130.0")
  92. trades.append("2015-02-08,AAPL,S,10,90.0")
  93. trades.append("2015-03-10,AAPL,S,80,120.0")
  94. trades.append("2015-03-12,AAPL,B,10,70.0")
  95. trades.append("2015-04-08,AAPL,B,70,160.0")
  96.  
  97.  
  98. res = calculateTax(trades);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement