【发布时间】:2022-01-12 06:04:06
【问题描述】:
我有股票市场数据,我想让代码购买股票(买入)并等到它被卖出(卖出)并在卖出后再次购买股票。以下代码仅针对初始买卖运行。如果我想对整个数据集执行此操作,而不是在第一次买入和第一次卖出时中断。
i=0
j=0
while (i<1) and (j <len(data)):
if data['RSI'][j]>=70:
print(j,data['RSI'][j])
print("BUY")
k=j
i+=1
j+=1
while (i==1) and ((j>k) and (j<len(data))):
if data['RSI'][j]<=30:
print(j,data['RSI'][j])
print("Sell")
i+=1
j+=1
以上代码以表格形式输出(便于理解):
| index | RSI | tag |
|---|---|---|
| 0 | 100.0 | BUY |
| 7 | 25.38 | SELL |
预期输出:
| index | RSI | tag |
|---|---|---|
| 0 | 100.0 | BUY |
| 7 | 25.38 | SELL |
| 10 | 80.24 | BUY |
| 20 | 40.20 | SELL |
| 25 | 81.24 | BUY |
| 30 | 41.20 | SELL |
整个数据集都是这样的
【问题讨论】:
标签: python python-3.x pandas while-loop finance