import matplotlib.pyplot as plt
## use tuple to access data
inputList = [
['sell', 'call', 9900, 120, 1],
['sell', 'put', 9800, 120, 1]
]
fromValue = 9500
toValue = 10200
rangeValue = 50
dataList = []
## define paint function
def plotData(plt, data):
x = [p[0] for p in data]
y = [p[1] for p in data]
plt.plot(x, y, '-o')
## small function
def maxValueZero(num):
if (num >= 0):
return 0
else:
return num
## small function
def minValueZero(num):
if (num <= 0):
return 0
else:
return num
## main calculate profit and loss function
def calcProfit(n):
ret = 0
for i in range(0, inputList.__len__()):
if (inputList[i][0] == 'sell'):
if (inputList[i][1] == 'call'):
ret += (maxValueZero(inputList[i][2] - n) + inputList[i][3]) * inputList[i][4];
if (inputList[i][1] == 'put'):
ret += (maxValueZero(n - inputList[i][2]) + inputList[i][3]) * inputList[i][4];
if (inputList[i][0] == 'buy'):
if (inputList[i][1] == 'call'):
ret += (minValueZero(n - inputList[i][2]) - inputList[i][3]) * inputList[i][4];
if (inputList[i][1] == 'put'):
ret += (minValueZero(inputList[i][2] - n) - inputList[i][3]) * inputList[i][4];
return ret * 50
## assign each calculated values
for i in range(fromValue, toValue, rangeValue):
dataList.append([i, calcProfit(i)])
## display chart
plotData(plt, dataList)
plt.show()
print("Success...")