Skip to Content
掌握金融模式识别
book

掌握金融模式识别

by Sofien Kaabar
May 2025
Intermediate to advanced
290 pages
4h 9m
Chinese
O'Reilly Media, Inc.
Content preview from 掌握金融模式识别

第 2 章 算法思维与函数 算法思维和功能

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

算法是计算机在特定条件下应用的一套 。你通常会使用算法来解决一个特定的问题,或者只是遵循任务的重复序列。您还可以使用算法通过扫描您设定的条件来寻找模式。

本书的主要目的是向你展示如何扫描、寻找和评估蜡烛图形态和策略。与手动执行任务相比,使用算法的主要好处如下:

速度
与人类相比,算法的运行速度极快。一个简单的算法可以在几秒钟内扫描数十万条数据,而人类完成同样的任务可能需要数周甚至数月的时间。
纪律
算法遵循一套明确的规则,不会因为感情或情绪而偶尔忽略规则。此外,算法不会陷入主观解释的陷阱。这对评估过程非常重要,因为你需要客观、清晰的衡量标准来判断你的交易系统。
误差百分比
如果代码中没有错误,算法一般不会出错。人类由于注意力不集中和疲劳,可能会犯很多错误。
备注

交易系统由多种算法组成 ,如交易和风险管理算法。稳健的交易系统依赖于基于规则的清晰而稳定的算法,以提供可靠的衡量标准。

本章分为四节。首先是基本函数,处理我在全书中使用的基本数据操作。然后,介绍如何对形态和策略的信号进行编码。然后是在价格图表上将信号可视化,这样你就能获得美观且可解释的表示。最后,你将学习关键性能评估指标以及如何对其进行编码。请务必掌握本章的概念,因为它们对本书的其他内容至关重要;它们不仅有助于检测形态,还有助于创建策略。

原始函数的编码

原始函数是 的一个自定义函数小集合,可以帮助您更好地操作数据数组。原始函数的想法最初只是我早年练习编码的一个练习,但随着时间的推移,它们变成了我在研究中经常使用的常规代码片段。

分析和回溯测试是重复性工作,需要某些函数在任何地方都能工作。让我们从最基本的原始函数开始(请记住,您将在本书中始终使用numpy )。

为数组添加列的函数

有时,您需要 添加列,以填充指标或信号。例如,假设您有一个由四列组成的 OHLC 数组。您可以使用一个函数来添加额外的列,您可以用它来存储以下内容

  • 根据收盘价计算的指标,如简单移动平均线1
  • 使用预先确定的二进制值(如 1 表示买入信号,-1 表示卖出信号)代理买入和卖出信号
备注

数组中的行 代表时间步长。我在书中使用的是小时时间框架,这意味着每一行都包含每小时 OHLC 值和任何指标值或买卖代理。

因此,第一个基元函数 是add_column() ,如下面的代码片段所示:

def add_column(data, times):
    
    for i in range(1, times + 1):
    
        new = np.zeros((len(data), 1), dtype = float)
        
        data = np.append(data, new, axis = 1)

    return data

len(data) 所示,函数 循环的次数由变量times 选择,每次循环都会创建一个包含零值的新数组,其长度与原始数据相同。这个新数组由np.zeros() 预建的numpy 函数产生。

最后一步是使用np.append() 将新创建的数组粘贴到原来的四列 旁边,这样就得到了一个五列数组。

随着循环的继续,列数也会增加。参数axis 指定了行和列之间的二进制选择。当它等于 0 时,指的是行,当它等于 1 时,指的是列。请记住,变量times 是要添加的列数,这在调用函数时指定。让我们看一个例子来更清楚地说明问题。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Fondamenti di ingegneria del software

Fondamenti di ingegneria del software

Nathaniel Schutta, Dan Vega
The Meaning of Branded Objects

The Meaning of Branded Objects

Dr. Tom Guarriello, Mark Kingsley, Debbie Millman

Publisher Resources

ISBN: 9798341659056