如何最大化收入 - Python

How to maximize revenue - Python

我有一个由每小时股价组成的大 df。我希望找到最佳买入价和卖出价以最大化收益(收入 - 成本)。我不知道最大的 buy/sell 价格是多少,因此我最初的猜测是在黑暗中胡乱试探。

我尝试使用 Scipy 'minimize' 和 'basin hopping'。当我 运行 脚本时,我似乎陷入了局部井,结果几乎没有偏离我最初的猜测。

关于如何解决这个问题的任何想法?有没有更好的代码编写方法,或者更好的使用方法。

下面的示例代码

import pandas as pd
import numpy as np
import scipy.optimize as optimize

df = pd.DataFrame({
    'Time': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    'Price': [44, 100, 40, 110, 77, 109, 65, 93, 89, 49]})

# Create Empty Columns
df[['Qty', 'Buy', 'Sell', 'Cost', 'Rev']] = pd.DataFrame([[0.00, 0.00, 0.00, 0.00, 0.00]], index=df.index)


# Create Predicate to add fields
class Predicate:
    def __init__(self):
        self.prev_time = -1
        self.prev_qty = 0
        self.prev_buy = 0
        self.prev_sell = 0
        self.Qty = 0
        self.Buy = 0
        self.Sell = 0
        self.Cost = 0
        self.Rev = 0

    def __call__(self, x):
        if x.Time == self.prev_time:
            x.Qty = self.prev_qty
            x.Buy = self.prev_buy
            x.Sell = self.prev_sell
            x.Cost = x.Buy * x.Price
            x.Rev = x.Sell * x.Price
        else:
            x.Qty = self.prev_qty + self.prev_buy - self.prev_sell
            x.Buy = np.where(x.Price < buy_price, min(30 - x.Qty, 10), 0)
            x.Sell = np.where(x.Price > sell_price, min(x.Qty, 10), 0)
            x.Cost = x.Buy * x.Price
            x.Rev = x.Sell * x.Price
            self.prev_buy = x.Buy
            self.prev_qty = x.Qty
            self.prev_sell = x.Sell
            self.prev_time = x.Time
        return x


# Define function to minimize
def max_rev(params):
    global buy_price
    global sell_price
    buy_price, sell_price = params
    df2 = df.apply(Predicate(), axis=1)
    return -1 * (df2['Rev'].sum() - df2['Cost'].sum())


# Run optimization
initial_guess = [40, 90]
result = optimize.minimize(fun=max_rev, x0=initial_guess, method='BFGS')
# result = optimize.basinhopping(func=max_rev, x0=initial_guess, niter=1000, stepsize=10)
print(result.x)

# Run the final results
result.x = buy_price, sell_price
df = df.apply(Predicate(), axis=1)
print(df)
print(df['Rev'].sum() - df['Cost'].sum())

您没有提供很多细节,但我假设您考虑了 "perfect foresight" 收入最大化问题 - 即您一开始就知道价格将如何演变。

这个问题很容易解决,但据我所知,目前你的问题是不受约束的——你可以通过以低价购买无限数量的单位并出售它们来赚取任意大的收入高价。

您需要添加一个限制条件,即您只能以有限数量的现金开始,并且您只能出售您拥有的股票(严格来说这并不正确,可以 "short-sell"你现在卖东西的地方是期望它的价值会下降(当你以后必须再次购买它时)。

忽略卖空恶作剧,您可以将优化问题表述为线性规划,如下所示:

import pandas as pd
import numpy as np
from pulp import *

# Problem Data
df = pd.DataFrame({
    'Time': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    'Price': [44, 100, 40, 110, 77, 109, 65, 93, 89, 49]})

times = list(df.Time)
times_plus_1 = times + [times[-1] + 1]

# Instantiate maximisation problem
prob = LpProblem("numpy_constraints", LpMaximize)

# Create the problem vairables
# Cash in bank and stock-level at start of each interval
Cash = pulp.LpVariable.dicts("Cash", times_plus_1, cat='Continuous', lowBound=0)
Stock = pulp.LpVariable.dicts("Stock", times_plus_1, cat='Continuous', lowBound=0)

# Amount bought during interval
Buy = pulp.LpVariable.dicts("Buy", times, cat='Continuous')

# Add Objective to problem - cash at end of period modelled
prob += Cash[times_plus_1[-1]]

# Add constraints
# Start with a single dollar in the bank & no stock
prob += Cash[times[0]] == 1.0
prob += Stock[times[0]] == 0.0

# Cash & stock update rules
for t in times:
    prob += Cash[t+1] == Cash[t] - Buy[t]*df.Price[t]
    prob += Stock[t+1] == Stock[t] + Buy[t]

# Solve
prob.solve()

# Check when we bought when:
Buy_soln = np.array([Buy[t].varValue for t in times])
print("Buy_soln:")
print(Buy_soln)

Stock_soln = np.array([Stock[t].varValue for t in times_plus_1])
print("Stock_soln:")
print(Stock_soln)

Cash_soln = np.array([Cash[t].varValue for t in times_plus_1])
print("Cash_soln:")
print(Cash_soln)

结果如下:

Buy_soln:
[ 0.02272727 -0.02272727  0.05681818 -0.05681818  0.08116883 -0.08116883
  0.13611389 -0.13611389  0.          0.        ]
Stock_soln:
[0.         0.02272727 0.         0.05681818 0.         0.08116883
 0.         0.13611389 0.         0.         0.        ]
Cash_soln:
[ 1.         0.         2.2727273  0.         6.25       0.
  8.8474026  0.        12.658591  12.658591  12.658591 ]

不是特别有趣 - 正如预期的那样,使用所有可用现金来利用股价的任何上涨(低买高卖)。