openTime - open trade time in milliseconds; openPrice - opening share price in d
ID: 3822662 • Letter: O
Question
openTime - open trade time in milliseconds;
openPrice - opening share price in dollars;
closePrice - closing share price in dollars;
volume - number of traded shares.
Write a program that shall use NumPy and Pyplot to read and analyze the data in the file. The program shall automatically produce the following outputs:
A scatter plot that shows the cumulative account balance as a function of time.
At each trade, the balance changes by (closePrice-openPrice)*volume.
The X axis of the plot shall be labeled in days, assuming that the first trade took place at midnight of day 0.
Both axes shall be properly labeled.
The plot shall have a proper title and a colorbar.
The color of a point shall represent the balance change associated with the point.
The points shall be connected with a line. (Consider combining plot() and scatter().)
The plot shall have both vertical and horizontal grid lines.
A histogram showing the distribution of gains/losses for all trades.
The histogram shall have no fewer than 25 bins.
Both axes of of the histogram shall be properly labeled.
The histogram shall have a proper title.
A pie chart that shows the fraction of trades with positive, negative, and zero values (three wedges).
The wedges of the pie shall be properly labeled.
The chart shall have a proper title.
The program shall save the outputs as PNG files with the resolution of 200dpi. The program shall not display the images. The program shall not use any loops or list comprehensions.
This is NOT a group project.
Submit the program and the three output files. Do not submit the original data file.
trades.zip
for python
Explanation / Answer
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
#Load data
arr = np.loadtxt('trades.txt', comments="#", delimiter=None, skiprows=0, dtype=float)
arr = np.array(arr, dtype=float)
#Calculate balance changes and cumulative changes
balanceChanges = (arr[:,2]-arr[:,1])*arr[:,3]
cumBalanceChanges = balanceChanges.cumsum()
#Scat plot
def scatterPlot():
plt.figure(1)
colors = balanceChanges
plt.scatter(range(len(arr)), cumBalanceChanges, c=colors, cmap=plt.cm.autumn)
plt.plot(range(0,len(arr)), cumBalanceChanges)
plt.title('Forex Account Balance')
plt.xlabel('Day')
plt.ylabel('Balance($)')
plt.colorbar()
plt.savefig("scatter.png", dpi=200)
plt.close()
#Hist plot
def histPlot():
plt.figure(2)
plt.hist(balanceChanges, bins=30)
plt.title('Gain/loss distribution')
plt.xlabel('Gain/loss')
plt.ylabel('Number of gain/loss')
plt.savefig("histogram.png", dpi=200)
plt.close()
#Pie plot
def piePlot():
plt.figure(3)
fraction = ['Positive','Negative','Zeros']
positive = len(np.where(balanceChanges>0)[0])
negative = len(np.where(balanceChanges<0)[0])
zeros = len(np.where(balanceChanges==0)[0])
numData = [positive, negative, zeros]
plt.title("Trade results")
plt.pie(numData, labels=fraction, shadow=True, autopct='%1.1f%%')
plt.savefig("pie.png", dpi=200)
plt.close()
#Call Plot
scatterPlot()
piePlot()
histPlot()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.