1

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

我尝试使用 Scipy 的“最小化”和“盆地跳跃”。当我运行脚本时,我似乎被困在当地的井中,结果几乎与我最初的猜测不同。

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

下面的示例代码

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())
4

1 回答 1

3

您没有提供很多细节,但我假设您考虑“完美的远见”收入最大化问题 - 即您一开始就知道价格将如何演变。

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

您需要添加一个限制条件,即您只能从有限数量的现金开始,并且您只能出售您拥有的股票(这并不完全正确,您现在可以在卖东西的地方“卖空”期望它的价值会下降(当你以后不得不再次购买时)。

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

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 ]

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

于 2019-10-31T20:45:13.823 回答