Python的基本交易策略
我正在尝试对基本交易策略进行编码出售获胜者并购买失败者。我已经列出了要在Python中编码的三个条件。我是财务分析的新手。基于以下问题,我决定采用这种方法来实现最终目标。
问题。
假设您在开始T0时拥有100美元,并将50%投资于第一股 第二股份为50%。现在,在时间T1(即一个星期后),可以有三种情况:
如果第一股的退货等于第二股票的回报,则不会更改您的组成 投资组合。
如果第一股的退货大于第二股股票的回报,则可以减少投资的金额 第二股第二股3%,并使用收益购买第一股股票。
如果第二股的退货大于第一股股票的回报,则可以减少投资的金额 第一股股票比例为3%,并使用收益购买第二股股票。 然后,您将T1移至T2并再次应用相同的规则。
以下是我的方法和实施。
从100美元开始,在第1周向两个股票投资50%
- ,以计算第1周投资后的股票收益
创建投资组合以股票记录
检查第一股票的退货是否等于第二股票的回报,如果是的,请留下投资组合组成
数据
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:
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.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.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.
start with 100 USD, invest 50% to both stock at week 1
calculate stock return after week 1 investment
create a portfolio to stock records of stocks
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论