Python的基本交易策略

发布于 2025-01-28 10:27:48 字数 2353 浏览 5 评论 0原文

我正在尝试对基本交易策略进行编码出售获胜者并购买失败者。我已经列出了要在Python中编码的三个条件。我是财务分析的新手。基于以下问题,我决定采用这种方法来实现最终目标。

问题。

假设您在开始T0时拥有100美元,并将50%投资于第一股 第二股份为50%。现在,在时间T1(即一个星期后),可以有三种情况:

  1. 如果第一股的退货等于第二股票的回报,则不会更改您的组成 投资组合。

  2. 如果第一股的退货大于第二股股票的回报,则可以减少投资的金额 第二股第二股3%,并使用收益购买第一股股票。

  3. 如果第二股的退货大于第一股股票的回报,则可以减少投资的金额 第一股股票比例为3%,并使用收益购买第二股股票。 然后,您将T1移至T2并再次应用相同的规则。

以下是我的方法和实施。

  1. 从100美元开始,在第1周向两个股票投资50%

  2. ,以计算第1周投资后的股票收益

  3. 创建投资组合以股票记录

  4. 检查第一股票的退货是否等于第二股票的回报,如果是的,请留下投资组合组成

数据

import random
from datetime import datetime
import pandas as pd

pd.date_range(end = datetime.today(), periods = 10).to_pydatetime().tolist()
date = pd.date_range(start="2022-01-09",end="2022-01-18")
stock_a = random.choices(range(47, 50), k=10)
stock_b = random.choices(range(147, 150), k=10)
df = pd.DataFrame({'stockA': stock_a,'stockB': stock_b},  index=date)

打印(df.head()。to_dict())

{'index': {0: Timestamp('2022-01-09 00:00:00'),
  1: Timestamp('2022-01-10 00:00:00'),
  2: Timestamp('2022-01-11 00:00:00'),
  3: Timestamp('2022-01-12 00:00:00'),
  4: Timestamp('2022-01-13 00:00:00')},
 'stockA': {0: 48, 1: 49, 2: 48, 3: 49, 4: 48},
 'stockB': {0: 147, 1: 147, 2: 149, 3: 147, 4: 149}}



# Set the initial capital
initial_capital= float(100.0)
investment_pct = 0.5
stock_investment = initial_capital * investment_pct
#investment table. I invest 50% on both stock
portfolio = (df.iloc[:,1:]/df.iloc[:,1:].iloc[0]*50)
#add date
portfolio['Date'] = df.index

#Calculate Stock return 
portfolio['stockA_Return'] = portfolio['stockA'].pct_change()
portfolio['stockB_Return'] = portfolio['stockB'].pct_change()
#set index to data
portfolio.set_index('Date', inplace=True)

我的输出在投资50%后

             stockA      stockB     stockA_Return   stockB_Return
Date                
06-May-2022 50.000000   50.000000   0.000000    0.000000
29-Apr-2022 49.077804   48.696650   -0.018444   -0.026067
22-Apr-2022 55.024380   50.368044   0.121166    0.034323
15-Apr-2022 57.059572   49.763641   0.036987    -0.012000
08-Apr-2022 56.741573   50.584144   -0.005573   0.016488

,我只想在该公司中实现第二步以下问题,但我不知道该怎么做。任何建议或解决方案都将受到极大的赞赏。请这是我第一次问问题

I am trying to code a basic trading strategy call sell the winner and buy the loser. I have listed the three conditions I want to code in python. I am a newbie on financial analysis. Based on the below question, I decided to adopt this approach to achieve the end objective.

Question.

Suppose that you have 100 USD at the start t0 and invest 50% in the first stock
and 50% in the second stock. Now, at time t1 (i.e., one week later), there can be three cases:

  1. if the return of the first stock is equal to the return of the second stock, you do not change the composition of your
    portfolio.

  2. if the return of the first stock is larger than the return of the second stock, you reduce the amount invested in the
    second stock by 3% and use the proceeds to purchase the first stock.

  3. if the return of the second stock is larger than the return of the first stock, you reduce the amount invested in the
    first stock by 3% and use the proceeds to purchase the second stock.
    then you move from t1 to t2 and apply again the same rule.

Below is my approach and implementation.

  1. start with 100 USD, invest 50% to both stock at week 1

  2. calculate stock return after week 1 investment

  3. create a portfolio to stock records of stocks

  4. check if the return of 1st stock is equal to the return of second stocks, if so leave portfolio composition

Data

import random
from datetime import datetime
import pandas as pd

pd.date_range(end = datetime.today(), periods = 10).to_pydatetime().tolist()
date = pd.date_range(start="2022-01-09",end="2022-01-18")
stock_a = random.choices(range(47, 50), k=10)
stock_b = random.choices(range(147, 150), k=10)
df = pd.DataFrame({'stockA': stock_a,'stockB': stock_b},  index=date)

print(df.head().to_dict())

{'index': {0: Timestamp('2022-01-09 00:00:00'),
  1: Timestamp('2022-01-10 00:00:00'),
  2: Timestamp('2022-01-11 00:00:00'),
  3: Timestamp('2022-01-12 00:00:00'),
  4: Timestamp('2022-01-13 00:00:00')},
 'stockA': {0: 48, 1: 49, 2: 48, 3: 49, 4: 48},
 'stockB': {0: 147, 1: 147, 2: 149, 3: 147, 4: 149}}



# Set the initial capital
initial_capital= float(100.0)
investment_pct = 0.5
stock_investment = initial_capital * investment_pct
#investment table. I invest 50% on both stock
portfolio = (df.iloc[:,1:]/df.iloc[:,1:].iloc[0]*50)
#add date
portfolio['Date'] = df.index

#Calculate Stock return 
portfolio['stockA_Return'] = portfolio['stockA'].pct_change()
portfolio['stockB_Return'] = portfolio['stockB'].pct_change()
#set index to data
portfolio.set_index('Date', inplace=True)

My output after investing 50%

             stockA      stockB     stockA_Return   stockB_Return
Date                
06-May-2022 50.000000   50.000000   0.000000    0.000000
29-Apr-2022 49.077804   48.696650   -0.018444   -0.026067
22-Apr-2022 55.024380   50.368044   0.121166    0.034323
15-Apr-2022 57.059572   49.763641   0.036987    -0.012000
08-Apr-2022 56.741573   50.584144   -0.005573   0.016488

I just want to implement the second step in the below question but I can't figure out how to do that. any suggestion or solution is greatly appreciated. please this is my first time asking question on SO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文