我正在编写一个小型技术分析库,其中包含TA-lib中不可用的项目。我从在cTrader上找到的示例开始,并将其与TradingView版本中的代码进行了匹配。
这是TradingView 的Pine Script代码:
len = input(9, minval=1, title="Length")
high_ = highest(hl2, len)
low_ = lowest(hl2, len)
round_(val) => val > .99 ? .999 : val < -.99 ? -.999 : val
value = 0.0
value := round_(.66 * ((hl2 - low_) / max(high_ - low_, .001) - .5) + .67 * nz(value[1]))
fish1 = 0.0
fish1 := .5 * log((1 + value) / max(1 - value, .001)) + .5 * nz(fish1[1])
fish2 = fish1[1]
Run Code Online (Sandbox Code Playgroud)
这是 …
我有一个时间序列数据。生成数据
date_rng = pd.date_range('2019-01-01', freq='s', periods=400)
df = pd.DataFrame(np.random.lognormal(.005, .5,size=(len(date_rng), 3)),
columns=['data1', 'data2', 'data3'],
index= date_rng)
s = df['data1']
Run Code Online (Sandbox Code Playgroud)
我想创建一条连接局部最大值和局部最小值的锯齿形线,它满足在 y 轴上|highest - lowest value|每条锯齿形线必须超过前一个距离的百分比(比如 20%)的条件之字形线,以及预先设定的值 k(比如 1.2)
我可以使用以下代码找到局部极值:
# Find peaks(max).
peak_indexes = signal.argrelextrema(s.values, np.greater)
peak_indexes = peak_indexes[0]
# Find valleys(min).
valley_indexes = signal.argrelextrema(s.values, np.less)
valley_indexes = valley_indexes[0]
# Merge peaks and valleys data points using pandas.
df_peaks = pd.DataFrame({'date': s.index[peak_indexes], 'zigzag_y': s[peak_indexes]})
df_valleys = pd.DataFrame({'date': s.index[valley_indexes], 'zigzag_y': s[valley_indexes]})
df_peaks_valleys = pd.concat([df_peaks, df_valleys], axis=0, ignore_index=True, sort=True)
# …Run Code Online (Sandbox Code Playgroud) 我是python和pandas的新手,主要是学习它以使我的编程技能多样化以及python作为通用程序语言的优势.在这个程序中,我使用它从雅虎获取历史数据,并使用talib中的函数进行一些技术分析
import pandas_datareader.data as web
import datetime
import talib as ta
start = datetime.datetime.strptime('12/1/2015', '%m/%d/%Y')
end = datetime.datetime.strptime('2/20/2016', '%m/%d/%Y')
f = web.DataReader('GOOG', 'yahoo', start, end)
print 'Closing Prices'
print f['Close'].describe()
print f.Close
print ta.RSI(f.Close,2)
print ta.SMA(f.Close,2)
print ta.SMA(f.Volume,4)
print ta.ATR
print ta.ATR(f.High,f.Low,f.Close,3)
Run Code Online (Sandbox Code Playgroud)
上面的代码工作,print f.Close但它显示此错误
print ta.RSI(f.Close,2)
TypeError: Argument 'real' has incorrect type (expected numpy.ndarray, got Series)
Run Code Online (Sandbox Code Playgroud)
我使用R及其库进行库存技术分析,它有一个内置的库Quantmod,可以使技术分析更容易,代码更少.
library(quantmod)
symbol=getSymbols(AAPL)
SMA=SMA(Cl(Symbol),2)
Run Code Online (Sandbox Code Playgroud)
是否有类似的Python可用的库?
我在 pip 安装 ta-lib 时遇到以下错误。\n我使用了命令:
\n!pip install ta-lib\nRun Code Online (Sandbox Code Playgroud)\n请为我提供解决方案。
\n Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\nCollecting ta-lib\n Using cached TA-Lib-0.4.25.tar.gz (271 kB)\n Installing build dependencies ... done\n Getting requirements to build wheel ... done\n Installing backend dependencies ... done\n Preparing metadata (pyproject.toml) ... done\nRequirement already satisfied: numpy in /usr/local/lib/python3.8/dist-packages (from ta-lib) (1.21.6)\nBuilding wheels for collected packages: ta-lib\n error: subprocess-exited-with-error\n \n \xc3\x97 Building wheel for ta-lib (pyproject.toml) did not run successfully.\n \xe2\x94\x82 exit code: 1\n \xe2\x95\xb0\xe2\x94\x80> See above for output.\n …Run Code Online (Sandbox Code Playgroud) python artificial-intelligence algorithmic-trading technical-indicator
我正在尝试使用PHP 交易函数(可用作PECL扩展)来计算各种证券的移动平均收敛/差异(MACD).但是,返回的值似乎与我的计算不符.
考虑以下一系列股票的收盘价:
$close = array (
0 => 459.99,
1 => 448.85,
2 => 446.06,
3 => 450.81,
4 => 442.8,
5 => 448.97,
6 => 444.57,
7 => 441.4,
8 => 430.47,
9 => 420.05,
10 => 431.14,
11 => 425.66,
12 => 430.58,
13 => 431.72,
14 => 437.87,
15 => 428.43,
16 => 428.35,
17 => 432.5,
18 => 443.66,
19 => 455.72,
20 => 454.49,
21 => 452.08,
22 => 452.73,
23 => 461.91, …Run Code Online (Sandbox Code Playgroud) 此处定义的兴登堡预兆指标为:
股市指数每日新高 52 周高点和新低 52 周低点的数量超过阈值(通常为 2.2%)。
对我来说,这意味着,我们每天滚动并回顾 52 周或 252 个业务/交易日,然后计算高点(或低点)的数量,最后计算该回报或 pct_change,即新高(或低点)的比率)他们想要监控,例如高于 2.2%
import pandas as pd
import numpy as np
import yfinance as yf
# download the S&P500
df = yf.download('^GSPC')
# compute the "highs" and "lows"
df['Highs'] = df['Close'].rolling(252).apply(lambda x: x.cummax().diff().
apply(lambda x: np.where(x > 0, 1, 0)).sum()).pct_change()
df['Lows'] = df['Close'].rolling(252).apply(lambda x: x.cummin().diff().
apply(lambda x: np.where(x < 0, 1, 0)).sum()).pct_change()
Run Code Online (Sandbox Code Playgroud)
我们的理解是一样的吗?有更好的方法吗?
我正在定义一个函数Heiken Ashi,它是技术分析中流行的图表类型之一.我正在使用Pandas编写一个函数,但没有遇到任何困难.这就是Heiken Ashi [HA]的样子 -
Heikin-Ashi Candle Calculations
HA_Close = (Open + High + Low + Close) / 4
HA_Open = (previous HA_Open + previous HA_Close) / 2
HA_Low = minimum of Low, HA_Open, and HA_Close
HA_High = maximum of High, HA_Open, and HA_Close
Heikin-Ashi Calculations on First Run
HA_Close = (Open + High + Low + Close) / 4
HA_Open = (Open + Close) / 2
HA_Low = Low
HA_High = High
Run Code Online (Sandbox Code Playgroud)
在使用for循环和纯python的各种网站上有很多东西,但我认为Pandas也可以做得很好.这是我的进步 -
def HA(df):
df['HA_Close']=(df['Open']+ df['High']+ df['Low']+ …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 pandas 在 python 中为 SuperTrend 指标编写以下算法。
BASIC UPPERBAND = (HIGH + LOW) / 2 + Multiplier * ATR
BASIC LOWERBAND = (HIGH + LOW) / 2 - Multiplier * ATR
FINAL UPPERBAND = IF( (Current BASICUPPERBAND < Previous FINAL UPPERBAND) or (Previous Close > Previous FINAL UPPERBAND))
THEN (Current BASIC UPPERBAND) ELSE Previous FINALUPPERBAND)
FINAL LOWERBAND = IF( (Current BASIC LOWERBAND > Previous FINAL LOWERBAND) or (Previous Close < Previous FINAL LOWERBAND))
THEN (Current BASIC LOWERBAND) ELSE Previous FINAL …Run Code Online (Sandbox Code Playgroud) 我无法获得平滑的 RSI。下图来自 freestockcharts.com。计算使用此代码。
public static double CalculateRsi(IEnumerable<double> closePrices)
{
var prices = closePrices as double[] ?? closePrices.ToArray();
double sumGain = 0;
double sumLoss = 0;
for (int i = 1; i < prices.Length; i++)
{
var difference = prices[i] - prices[i - 1];
if (difference >= 0)
{
sumGain += difference;
}
else
{
sumLoss -= difference;
}
}
if (sumGain == 0) return 0;
if (Math.Abs(sumLoss) < Tolerance) return 100;
var relativeStrength = sumGain / sumLoss;
return 100.0 - …Run Code Online (Sandbox Code Playgroud) 我计算简单的移动平均线:
def sma(data_frame, length=15):
# TODO: Be sure about default values of length.
smas = data_frame.Close.rolling(window=length, center=False).mean()
return smas
Run Code Online (Sandbox Code Playgroud)
使用滚动函数可以计算加权移动平均值吗?正如我在文档中看到的那样,我认为我必须传递win_type参数.但我不确定我必须选择哪一个.
这是加权移动平均线的定义.
提前致谢,
python weighted-average moving-average pandas technical-indicator
简单移动平均线的计算是否将当前价格包含在平均值中?例如,如果价格为 {1, 2, 3, 4, 5},则 3 日 SMA 看起来像 {-, -, 2, 3, 4} 还是 {-, -, -, 2, 3}?
我正在尝试将平均真实范围列添加到包含历史股票数据的数据框中。
到目前为止,我使用的代码是:
def add_atr_to_dataframe (dataframe):
dataframe['ATR1'] = abs (dataframe['High'] - dataframe['Low'])
dataframe['ATR2'] = abs (dataframe['High'] - dataframe['Close'].shift())
dataframe['ATR3'] = abs (dataframe['Low'] - dataframe['Close'].shift())
dataframe['TrueRange'] = max (dataframe['ATR1'], dataframe['ATR2'], dataframe['ATR3'])
return dataframe
Run Code Online (Sandbox Code Playgroud)
包含max函数的最后一行给出了错误:
def add_atr_to_dataframe (dataframe):
dataframe['ATR1'] = abs (dataframe['High'] - dataframe['Low'])
dataframe['ATR2'] = abs (dataframe['High'] - dataframe['Close'].shift())
dataframe['ATR3'] = abs (dataframe['Low'] - dataframe['Close'].shift())
dataframe['TrueRange'] = max (dataframe['ATR1'], dataframe['ATR2'], dataframe['ATR3'])
return dataframe
Run Code Online (Sandbox Code Playgroud)
我已经搜索了几天,试图学习如何解决该错误,或者以更好的方式进行代码处理,等等,但没有发现任何可以帮助我的方法。
在以下方面的任何帮助将不胜感激:
如何解决错误
如何以更好的方式进行编码-我并不是说我必须以这种方式进行编码,并且可能会有更好的方式来进行编码。
提前谢谢。