我试图围绕特朗普的推文数据来修改交易策略。目前的策略是根据新信息采取行动,在不清算旧交易的情况下推出新的交易--允许杠杆无限增长。代码中的某些内容需要进行调整,这样策略才能在没有杠杆的情况下进行交易,但我不确定其中的哪一部分。有人能帮帮我吗?另外,我如何比较彼此的策略呢?
test_dataset2=pd.DataFrame({'Predict':model.predict(test_dataset'text'),‘LongOnly’:test_dataset‘’toclose‘})
test_dataset2.index=test_dataset['created_at']
test_dataset2.sort_index(inplace=True)
test_dataset2['Strategy']=0
for i in range(0,len(test_dataset2)):
if (test_dataset2.iloc[i,0]=='Up'):
test_dataset2.iloc[i,2]=test_dataset2.iloc[i,1]
else:
test_dataset2.iloc[i,2]=-test_dataset2.iloc[i,1]
((1 + test_dataset2[['LongOnly','Strategy']]).cumprod() - 1).plot()
plt.xlabel('Date')
plt.ylabel('Cumulative Return')
plt.show()
import pickle
with open("test_dataset2.pickle", "wb") as f1:
pickle.dump(test_dataset2, f1)F1.关闭()
发布于 2022-03-03 00:23:17
如果您使用API提交订单,请使用调用来管理您的杠杆作用。例如:
import tradeapi as api
condition_met_tweet = True
trades_taken = 0
while True:
if condition_met_tweet == True and trades_taken < 20:
# Sumbit order
api.submit_order(
{
"symbol": "SPY",
"side": "long",
"notional_value": float(api.get_account_balance())/20 # 5% balance
}
)
# Add to trade counter
trades_taken += 1这将阻止该计划进行超过20次交易。
优点
缺点
API不知道您的restriction.
因此,最好使用API来计算可用的购买力。
import tradeapi as api
import math
condition_met_tweet = True
while True:
if condition_met_tweet == True:
# Calculate quantity
account_bal = float(api.get_account_balance())
asset_price = float(api.get_last_trade('SPY'))
quantity = math.floor((account_bal/20)/asset_price)
account_bp = float(api.get_account_buying_power())
if quantity == 0 or (quantity*asset_price) > account_bp:
continue # don't submit an order, re-loop
# Sumbit order
api.submit_order(
{
"symbol": "SPY",
"side": "long",
"quantity": quantity
}
)
# Add to trade counter
trades_taken += 1这将计算当您的条目被触发时的理想数量,如果您的理想数量(四舍五入)为0,则跳过订单输入部分。如果你“负担不起”下一笔交易,那么它也会这样做,因为你没有足够的购买力去执行它。
只要您能够从API中实时调用以下值,此系统就可以工作:
balance
https://stackoverflow.com/questions/68610444
复制相似问题