我可以使用一些帮助让我的代码正常工作.我试图根据收盘价高于MACD,布林带和慢速随机指标创建一个简单的头寸信号.我在第17行开始出错了.我不确定这是不是因为"Stock"是一个xts对象.我想在最后绘制输出图.谢谢!
#install.packages("quantmod")
library("quantmod")
#install.packages("FinancialInstrument")
library("FinancialInstrument")
#install.packages("PerformanceAnalytics")
library("PerformanceAnalytics")
#install.packages("TTR")
library("TTR")
#######################################
Stock <- get(getSymbols('CAT'))["2014::"]
# add the indicators
Stock$BBands <- BBands(HLC(Stock))
Stock$MACD <- MACD(HLC(Stock))
Stock$stochOSC <- stoch(Stock[,c("High","Low","Close")])
Stock$position <- ifelse(Cl(Stock) > Stock$BBands > Stock$MACD > Stock $stockOSC , 1 , -1)
Gains <- lag(Stock$position) * dailyReturn(Stock)
charts.PerformanceSummary(cbind(dailyReturn(Stock),Gains))
Run Code Online (Sandbox Code Playgroud) 是否可以通过专家顾问读取预建指标的变化(例如:其价值变化),当然 - 根据这些读取自动进行交易?
负责执行此操作的功能是什么?
我试图在谷歌上查找这个,但似乎我只能做跟踪对象创建或删除之类的事情......称为图表事件......也许我错过了什么?
看来我无法找出适合我的策略的正确脚本,如下所示:
\n在每日时间范围内效果更好,收到的信号更少,但更有效,脚本是这样的:
\n//@version=5\nstrategy("Estrategia Long Only")\n// Definir el indicador ADX\nlen = input(14, title="ADX Length")\nth = input(44, title="ADX Threshold")\nadx_val = ta.adx(high, low, close, len)\n// Definir el indicador RSI\nrsi_len = input(14, title="RSI Length")\nrsi_buy = input(20, title="RSI Buy Threshold")\nrsi_sell = input(35, title="RSI Sell Threshold")\nrsi_val = ta.rsi(close, rsi_len)\n// Generar se\xc3\xb1ales de compra\nadx_above_th = adx_val > th\nrsi_above_buy = rsi_val >= rsi_buy and rsi_val < rsi_sell\nbuy_signal = adx_above_th and rsi_above_buy\n// Entradas largas\nif buy_signal\n …Run Code Online (Sandbox Code Playgroud) algorithmic-trading tradingview-api pine-script pine-script-v5
我的For循环似乎是跳过它应该一步一步的步骤.
简化我的代码:
for(j in 1:5){
ventana <- spread_real[j + 1: 180 + j]
}
Run Code Online (Sandbox Code Playgroud)
它从矢量'spread_real'获取子集[j + 1:180 + j]并将其分配给变量'ventana'.
但是一旦我运行代码并且j等于5,那么ventana将获得子集spread_real [9:190]
任何帮助都会非常感激,因为我已经坚持了很长时间了.
鉴于此示例数据帧:
date;close;signal;positions
2017-01-02;27.90;0.0;0.0
2017-01-03;27.76;0.0;0.0
2017-01-04;28.65;1.0;1.0
2017-01-05;28.72;1.0;0.0
2017-01-06;28.00;1.0;0.0
2017-01-09;27.03;1.0;0.0 # <<<--- Note the price is -5% when compared to 28.65 (in 2017-01-04)
2017-01-10;28.26;1.0;0.0
2017-01-11;28.35;0.0;-1.0 # <<-- Sell
2017-01-12;29.12;0.0;0.0
2017-01-13;28.99;0.0;0.0
2017-01-16;28.50;1.0;1.0
2017-01-17;28.45;1.0;0.0
2017-01-18;29.06;1.0;0.0
2017-01-19;28.74;0.0;-1.0
2017-01-20;28.76;0.0;0.0
2017-01-23;29.50;0.0;0.0
2017-01-24;29.12;1.0;1.0
2017-01-25;29.87;1.0;0.0
2017-01-26;27.22;1.0;0.0 # <<<--- Note the price is -5% when compared to 29.12 (in 2017-01-24)
2017-01-27;29.76;1.0;0.0 # <<-- still holding the position...
Run Code Online (Sandbox Code Playgroud)
我想在价格低于 5% 时实施“止损”。在这种情况下,DataFrame 应如下所示:
date;close;signal;positions
2017-01-02;27.90;0.0;0.0
2017-01-03;27.76;0.0;0.0
2017-01-04;28.65;1.0;1.0 # <<-- Buy
2017-01-05;28.72;1.0;0.0
2017-01-06;28.00;1.0;0.0
2017-01-09;27.03;0.0;-1.0 # <<-- Sell with stop-loss
2017-01-10;28.26;0.0;0.0
2017-01-11;28.35;0.0;0.0
2017-01-12;29.12;0.0;0.0
2017-01-13;28.99;0.0;0.0 …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过 robin_stocks 从 Robinhood 导入数据。我能够导入该包,但是当我尝试使用 .login() 属性时,它会在 Jupyter Notebook 中返回此错误消息:error
模块“robin_stocks”没有属性“login”
我确信 robin_stocks 确实具有 Python 告诉我不存在的属性。我这里哪里出错了?
我对编程比较陌生,但通常我可以在 StackOverflow 或任何其他资源上很快找到问题的答案。不过,我似乎无法弄清楚这一点。
任何帮助将不胜感激。谢谢你!
r ×2
trading ×2
api ×1
attributes ×1
dataframe ×1
finance ×1
forex ×1
metatrader4 ×1
mql4 ×1
numpy ×1
pandas ×1
pine-script ×1
python ×1
quantmod ×1