标签: algorithmic-trading

如何启用TWS延迟市场数据?

这是我用来请求市场数据的脚本.

我还没有订阅数据馈送,所以我会自动返回延迟的市场数据,但显然我必须启用它,但无法找到这样做的地方.
这是脚本和我得到的错误,我只需要接收延迟数据,所以我可以测试我的算法.

from ib.opt import ibConnection, message
from ib.ext.Contract import Contract
from time import sleep

def fundamentalData_handler(msg):
    print(msg)

def error_handler(msg):
    print(msg)

tws = ibConnection(port=7496, clientId=100)
tws.register(error_handler, message.Error)
tws.register(fundamentalData_handler, message.fundamentalData)
tws.connect()

c = Contract()
c.m_symbol = 'AAPL'
c.m_secType = 'STK'
c.m_exchange = "SMART"
c.m_currency = "USD"

print "on it"

tws.reqMktData(897,c,"",False)
sleep(50)

tws.disconnect()
Run Code Online (Sandbox Code Playgroud)

错误:

<error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:hfarm>
<error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:eufarm>
<error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:jfarm> …
Run Code Online (Sandbox Code Playgroud)

python algorithmic-trading interactive-brokers ibpy

7
推荐指数
1
解决办法
2756
查看次数

Python Finance:如何使用macd指标进行信号策略?

我试图了解库存数据并在python中实现它.在开始我在Python 库中使用MACD指标stockstats.

我想知道,如果我有一个特定股票的100个OHLC条目,我怎样才能使用MACD输出来产生信号,无论我应该买入还是卖出或持有?在图表中,可以想象,但在编程方面我如何得到这个想法?示例代码如下:

import pandas as pd
from stockstats import StockDataFrame as Sdf
from pandas_datareader import data, wb

data = pd.read_csv('data.csv')

stock = Sdf.retype(data)
print(stock.get('pdi'))
Run Code Online (Sandbox Code Playgroud)

它产生的输出如下:

0       0.000000e+00
1      -8.951923e-08
2       1.758777e-07
3      -3.844324e-08
4      -2.217396e-07
5      -3.893329e-07
6      -2.373225e-07
7      -5.082528e-07
8      -8.260595e-07
9      -1.099751e-06
10     -1.429675e-06
11     -1.211562e-06
12     -8.230303e-07
13     -5.163039e-07
14     -4.979626e-07
15     -4.777865e-07
16     -6.217018e-07
17     -1.145459e-06
18     -1.461550e-06
19     -1.744250e-06
20     -1.677791e-06
21     -1.820319e-06
22     -2.024092e-06
23     -1.958413e-06
24     -2.450087e-06 …
Run Code Online (Sandbox Code Playgroud)

python finance stocks algorithmic-trading

7
推荐指数
1
解决办法
7909
查看次数

使用图表构建交易平台 - 对Python GUI库的建议

我正在构建一个小程序来从市场中检索数据并实时绘制图表.虽然交易决策将在很大程度上自动化,但图表会不断更新,以便有人可以跟踪决策的采取方式,并在必要时进行人工干预.

什么是一个很好的GUI库任务(对于Python).以下是考虑因素 -

编程语言:Python(您认为我应该使用其他东西吗?甚至可以使用不同的语言进行GUI和后端操作?!!).
操作系统:最好是跨平台的,但如果它必须是平台特定的,那么它就是Linux.
速度 + 学习曲线:虽然时间(低延迟)不是一个关键问题,我更喜欢易于使用和快速学习的东西,但程序必须感觉到响应,我不想为了便于编码而交易速度超过一定点.我想这是你的经历可以帮助我的部分.

我曾经强烈考虑过WxPython,但有些评论说它设计得不好(因为它不适合Python!)

因此,任务的复杂性和元考虑已经摆在你面前.请帮忙/建议.

PS:虽然我们在这里,如果有人可以评论一个合适的图表库,那就太好了.

python user-interface wxpython trading algorithmic-trading

6
推荐指数
0
解决办法
4519
查看次数

在数组中查找异常值,列表

我有数组形式的销售统计数据,以计算该数据的标准偏差或平均值.

stats = [100, 98, 102, 100, 108, 23, 120] 
Run Code Online (Sandbox Code Playgroud)

让说+ -20%差异是正常情况,23显然是一个特例.

什么是最好的算法(在任何语言,伪或任何原则)找到这个不寻常的价值?

python algorithm algorithmic-trading

6
推荐指数
1
解决办法
589
查看次数

技术分析 - 抛物线停止和反向指示器

我正在努力建立一个创建SAR系列的课程.但似乎我不太了解这些步骤.我不确定计算的初始值.

这是我的第一次尝试:

public class SAR : IndicatorCalculatorBase
{
    public override List<Ohlc> OhlcList { get; set; }
    public double AccelerationFactor = 0.02;
    public double MaximumAccelerationFactor = 0.2;

    public override IIndicatorSerie Calculate()
    {
        SingleDoubleSerie sarSerie = new SingleDoubleSerie();

        bool trendUp = false, trendChanged = false;
        double instantFactor = AccelerationFactor;

        for (int i = 1; i < OhlcList.Count; i++)
        {
            if (OhlcList[i].Low > OhlcList[i - 1].Low) // uptrend
            {   
                if (!trendUp)
                {
                    trendUp = true;
                    trendChanged = true;
                    instantFactor = AccelerationFactor;
                }
                else …
Run Code Online (Sandbox Code Playgroud)

c# r trading indicator algorithmic-trading

6
推荐指数
1
解决办法
2175
查看次数

IBPY 获取正确的历史成交量数据

我正在尝试从 IBPY 获取历史数据。我明白了,但是音量太低了,根本没用。我想知道如何获得正确的历史成交量估计。

我正在执行以下代码:

from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
from time import sleep, strftime

def historical_data_handler(msg):
    print(msg)

connection = Connection.create(port=7496, clientId=999)
connection.register(historical_data_handler, message.historicalData)
connection.connect()

req = Contract()
req.m_secType = "STK"
req.m_symbol = "TSLA"
req.m_currency = "USD"
req.m_exchange = "AMEX"
endtime = strftime('%Y%m%d %H:%M:%S')
connection.reqHistoricalData(1,req,endtime,"1 D","1 hour","TRADES",1,1)

sleep(5)
connection.disconnect()
Run Code Online (Sandbox Code Playgroud)

这是输出:

<historicalData reqId=1, date=20181123  16:30:00, open=333.21, high=333.33, low=331.04, close=332.92, volume=22, count=21, WAP=332.233, hasGaps=False>
<historicalData reqId=1, date=20181123  16:30:00, open=333.21, high=333.33, low=331.04, close=332.92, volume=22, count=21, WAP=332.233, …
Run Code Online (Sandbox Code Playgroud)

algorithmic-trading interactive-brokers ibpy

6
推荐指数
1
解决办法
2353
查看次数

ValueError:传递值的长度为 7,索引暗示 0

我正在尝试使用 ccxt 从 bitmex 获取 1 分钟的开盘价、最高价、最低价、收盘价和成交量值。一切似乎都很好,但我不知道如何解决这个错误。我知道索引是 7,因为我进入数据帧的 OHLC 列中有 7 个值。我不知道为什么它暗示有 0。非常感谢这让我一整天都头疼:(

# noinspection PyUnresolvedReferences
from datetime import datetime
# noinspection PyUnresolvedReferences
import time
# noinspection PyUnresolvedReferences
import ccxt
# noinspection PyUnresolvedReferences
import numpy as np
import pandas as pd
# noinspection PyUnresolvedReferences
from IPython.display import display, clear_output


OHLCVcolumns = ['date', 'timestamp', 'open', 'high', 'low', 'close', 'volume']

dfOHLCV = pd.DataFrame(index=[], columns=OHLCVcolumns)

bitmex = ccxt.bitmex()


def fetch_current(x):
    while True:
        if datetime.now().second == x:
            break
        time.sleep(0.5)


def fetch_mex():
    listOHLCV = bitmex.fetch_ohlcv('BTC/USD', …
Run Code Online (Sandbox Code Playgroud)

python algorithmic-trading python-3.x ccxt

6
推荐指数
1
解决办法
2万
查看次数

如何判断 3 条移动平均线何时在 4 根蜡烛或更少的时间内交叉

我一直在使用 2 条移动平均线交叉,这是非常直接的。我想在混合中添加三分之一,我试图找出在 4 根蜡烛或更少的时间内检查是否发生这种情况。

对于两个移动平均线,我使用了以下内容:

  // if shift5MA > shift0MA 
   if (shift5MAArray[1] > shift0MAArray[1]) {
      
      //if shift5MA < shift0MA VALID SELL
      if (shift5MAArray[2] < shift0MAArray[2]) {
         signal = "sell";
      }
      }
   
   if (shift5MAArray[1] < shift0MAArray[1]) {
      
      //if shift5MA > shift0MA
      if (shift5MAArray[2] > shift0MAArray[2]) {
         signal = "buy";
      }
      }

Run Code Online (Sandbox Code Playgroud)

我如何检查 3 条移动平均线何时在 4 根蜡烛或更少的时间内相互交叉,如图像中的三个交叉:

在此处输入图片说明

algorithmic-trading metatrader5 mql5

6
推荐指数
1
解决办法
153
查看次数

如何在 Python 中实现 RSI Divergence

我想知道是否有任何涵盖RSI-Divergence(快速和慢速之间的区别)的Python 库RSI或有关如何在 Python 中实现其算法的任何指导。

已经提出的问题:以编程方式检测 RSI 分歧。答案之一建议使用 Python 版本的quantconnect 论坛,但它没有涵盖任何内容。

我无法找到它的数学公式,但我能够在 pine-script 中找到RSI-Divergence,如下所示,但我无法将其转换为 Python,因为它无法pine-script使用tradingview进行调试。

study(title="RSI Divergence", shorttitle="RSI Divergence")
src_fast = close, len_fast = input(5, minval=1, title="Length Fast RSI")
src_slow = close, len_slow = input(14,minval=1, title="Length Slow RSI")
up_fast = rma(max(change(src_fast), 0), len_fast)
down_fast = rma(-min(change(src_fast), 0), len_fast)
rsi_fast = down_fast == 0 ? 100 : up_fast == 0 ? 0 : 100 - (100 …
Run Code Online (Sandbox Code Playgroud)

python algorithm algorithmic-trading pine-script

6
推荐指数
1
解决办法
4557
查看次数

Trail_price、trail_offset 在 pinescript 中到底是如何工作的?

感谢大家迄今为止的帮助。到目前为止,我已经在 pinescript 中编写了许多不同的策略,并且我已经阅读了 pinescript 手册和许多谷歌文章,但我仍然对追踪止损在 pinescript 中如何工作感到困惑。

例如,对于strategy.exit,我有一个trail_price,它标记要激活的追踪止损条目。然而,我所有的回溯测试都表明追踪止损位于该特定蜡烛线的最高点,即使 Trail_offset 尚未被触及。仅仅是因为交易视图回溯测试假设在一根蜡烛线中达到了最大利润,即使后续蜡烛线继续朝着您的目标方向发展?

例如,这是我的strategy.exit 的示例。Strategy.exit("long_TP", "long", Trail_price = 入场价格 + ATR, Trail_offset = ATR, stop= 入场价格 - ATR). 我注意到,只要在该特定交易收盘前获利,我将赚取 2 倍到 3 倍的 Trail_offset(在本例中基于 ATR,即,如果 ATR 为 50 点,我将赚取 100 甚至 150 点)。蜡烛吧。任何后续的蜡烛线,即使做多,即使没有达到 Trail_offset 止损,也不会被纳入计算(即,即使我的 ATR 是 50 点,当蜡烛线收盘时我可能会赚取 70 点,即使随后的蜡烛继续做多)。

我的假设是否不正确(即我的代码),或者这只是回溯测试的限制,因为程序无法知道蜡烛条内部发生了什么,只能知道最高价、最低价、开盘价和收盘价?然而,我确实对此感到好奇,因为有时即使蜡烛线处于最低点,也没有达到 Trail_offset,因此理论上利润应该继续累积,而不是在蜡烛线收盘后止损。

编辑:我添加了一些更多信息以进行澄清 - 这是一个带有一些解释的示例代码:

If condition == true
long = strategy.position_size[0] > strategy.position_size[1]  //go long if there is order entry
entry_price_long = valuewhen(long, open, 0) //entry price is the opening price, …
Run Code Online (Sandbox Code Playgroud)

trading algorithmic-trading forex back-testing pine-script

6
推荐指数
1
解决办法
6149
查看次数