close

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...")

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 鑄劍為犁 的頭像
    鑄劍為犁

    Balance OP 交易

    鑄劍為犁 發表在 痞客邦 留言(0) 人氣()