GO Markets,让交易更进一步
智慧交易,从选择值得信赖的全球券商开始。低点差、快速成交、零入金手续费、功能强大的交易平台,以及屡获殊荣的客户支持,让您的交易更进一步


全球交易者共同的选择
自 2006 年起,GO Markets 已帮助全球数十万交易者实现他们的投资目标。凭借严格监管、以客户为本的服务,以及屡获殊荣的教育资源,我们始终是交易者值得信赖的合作伙伴。


















































GO Markets
让交易更进一步
探索上千种交易机会,享受专业机构水准的交易工具、流畅稳定的交易体验,以及屡获殊荣的客户支持。开户流程简单快捷,让您轻松开启交易之旅。
%20(1).jpg)
在上一篇文章中,我们已经系统介绍了 Pine Script 中的 for loop,并通过实例说明了它在固定次数遍历、区间计算等场景下的常见用法。不过,在实际策略和指标开发中,并不是所有问题都能提前确定循环次数。有些逻辑需要在“条件满足之前不断执行”,这时 while loop 就显得尤为重要。
while loop 是 Pine Script 中另一种核心循环结构,它基于条件判断来决定是否继续执行代码,而不是依赖预先设定的次数。这使它在动态计算、逐步逼近目标值、状态驱动型逻辑等场景中具有更高的灵活性。当然,也正因为这种灵活性,while loop 在使用时需要格外注意边界条件,否则可能导致脚本超时或编译错误。
本文将在 for loop 的基础上,详细介绍 Pine Script 中 while loop 的语法结构、执行机制及其与 for loop 的关键区别,并结合实际示例,帮助你理解在什么情况下应该选择 while loop,以及如何安全、高效地使用它。
while 循环语句用于创建一个由条件控制的循环,它通过一个条件表达式来控制其本地代码块的执行。只要指定的条件保持为真,循环就会持续迭代。
Pine Script 使用以下语法来定义一个 while 循环:
[variables = | :=] while condition
statements | continue | break
return_expression
其中,循环头部中的 condition 可以是字面量、变量、表达式,或返回 bool(布尔) 值的函数调用。
while 循环的头部会在每一次迭代之前对条件进行求值。因此,当脚本在某次迭代中修改了该条件时,循环头部会在下一次迭代时反映这些变化。
根据循环头部中指定的条件,while 循环的行为可以与 for 循环类似,例如持续迭代直到某个计数变量达到指定的上限。
由于 while 循环 的执行依赖于其条件始终为真,而该条件在某一次特定迭代中可能不会发生变化,因此在循环开始之前,预期的迭代次数往往是无法准确确定的。因此,while 循环通常在无法事先确定精确循环边界的场景中非常有用。
下面的脚本用于跟踪当图表的收盘价突破用户指定长度和通道宽度的 Keltner 通道时的情况。当价格突破当前 K 线的通道范围时,脚本会绘制一个方框,用来高亮显示此前所有连续收盘价仍位于该价格区间内的 K 线。该脚本使用 while 循环来分析历史 K 线的价格,并逐步调整每一个新方框的左边界,直到绘制的区域覆盖当前区间内最新的一组连续 K 线。

下面逐行解析代码:
1. 指定脚本版本。
2. 定义一个指标脚本,指标名称为while loop, 简短名称为window,第三个参数 true:表示指标绘制在主图(价格图)上。
3. 创建一个整数输入参数。默认值:20,名称:Channel length,允许范围:1 到 4999。用于后续计算 EMA 和 ATR 的周期长度。
4. 创建一个浮点数输入参数。默认值:2.0,最小值:0。用于控制通道宽度(ATR 的倍数)。
5. 使用 ta.ema() 计算指数移动平均线。输入价格:close(收盘价),周期:lengthInput,结果存入变量 ma。
6. 计算 ATR 并乘以倍数:ta.atr(lengthInput) 计算 平均真实波幅(ATR)。再乘以用户设定的宽度倍数。用于构建价格通道的上下边界。
7. 计算通道下轨:通道下轨 = EMA − ATR × 倍数。
8. 计算通道上轨。
9. 判断价格是否突破通道:当满足以下任一条件时为 true:收盘价低于通道下轨或收盘价高于通道上轨。
10. 检测“首次突破”:当前 K 线价格在通道外,上一根 K 线价格不在通道外。这表示:刚刚发生突破(避免重复画框)。
11. 创建价格窗口 Box:创建一个新的 box 对象:
左边界:bar_index
右边界:bar_index(初始宽度为 0)
上边界:channelHigh
下边界:channelLow
border_width = 2:边框宽度
bgcolor:半透明灰色背景
这个方框将用来标记“价格在通道内的历史区间”。
12. 初始化计数器:定义一个整数变量 i,用于回溯历史 K 线(close[i] 表示第 i 根之前的收盘价)。
13. while 循环:向左扩展方框:只要 第 i 根之前的收盘价仍在当前通道范围内:
close[i] >= channelLow
close[i] <= channelHigh
循环继续执行。
14. 调整方框左边界:将方框的左边界向左移动到:当前 K 线索引 – i,实现“逐根向左扩展方框”。
15. 增加回溯步数:每次循环 i 加 1
16. 绘制通道下轨。
17. 在图表上绘制通道上轨线。
K线上结果呈现如下:

本文通过一个完整的 Pine Script 示例,系统地讲解了 while 循环在 TradingView 指标中的实际应用。脚本以 EMA 与 ATR 构建 Keltner 通道为基础,当价格首次突破通道时创建一个价格窗口方框,并利用 while 循环向左逐根回溯历史 K 线,只要收盘价仍处于当前通道范围内,就不断扩展方框的左边界。相比 for 循环,while 循环不依赖预先确定的迭代次数,更适合用于回溯区间长度不确定的场景。通过这一示例,可以清楚地看到 while 循环在处理“连续条件判断”和“动态边界”问题时的优势,有助于读者在编写更灵活、逻辑更清晰的 Pine Script 脚本时,合理选择和运用循环结构。

FX markets face a data-heavy period in the coming days, led by US inflation releases and late-week flash purchasing managers’ indexes (PMIs).
Regional data and central bank expectations in Japan, Europe, and Australia may influence cross-currency moves, particularly if outcomes differ from expectations.
Quick facts:
- US Personal Income and Outlays is a key inflation release this week, closely watched by policymakers.
- Flash PMIs across the US, Eurozone, Germany, and the UK offer a timely read on growth momentum.
- Australian data, including labour market indicators, remains important for AUD sensitivity and Reserve Bank of Australia (RBA) expectations.
- FX markets can be sensitive when data outcomes differ from expectations.
USDJPY
What to watch
US attention centres on inflation and activity data, particularly the Personal Income and Outlays report and the PCE price index, alongside late-week flash manufacturing and services PMIs.
These releases are closely followed by markets for their potential influence on rate expectations and USD sensitivity.
On the JPY side, Bank of Japan (BoJ) developments remain relevant, although US data has often been a key driver of recent moves.
Key releases and events
- Fri 23 Jan (US): US Personal Income and Outlays (including PCE inflation)
- Fri 23 Jan (US): Manufacturing and services PMI
Technical snapshot
USDJPY continues to trade above its rising 200-day moving average, with recent daily candles showing greater overlap and smaller ranges over recent weeks.
- Price has remained above the long-term average since late September, with higher swing lows still visible.
- Momentum appears to have moderated since early January, consistent with slowing follow-through rather than reversal.
- Daily ranges have narrowed compared with the October to November advance, again suggesting short-term consolidation.
EURUSD
What to watch
Eurozone flash PMIs and Germany producer price index (PPI) data provide insights into regional growth momentum and whether inflation pressures are building.
While these releases may influence immediate EUR sentiment, EURUSD continues to trade in the broader context of US data outcomes and global risk conditions.
Key releases and events
- Thu 22 Jan: Germany Producer Price Index (PPI)
- Fri 23 Jan: Eurozone / Germany flash PMIs (manufacturing and services)
Technical snapshot
EURUSD is trading above its rising 200-day moving average (daily chart), although price action since July suggests the market has become more range-bound rather than directional, following the advances in the first half of 2025.
- The broader upward structure has been in place since the beginning of 2025, although progress higher has stalled over recent months.
- Momentum readings have drifted toward neutral since late November, consistent with balanced conditions.
- Average daily range has continued to compress since July, consistent with a flattening of the trend.
GBPAUD
What to watch
Australian labour market data remains central for AUD sensitivity and RBA expectations. UK CPI is also due this week, which may contribute to cross volatility, particularly if it shifts expectations around the UK rates outlook.
Late-week PMI releases can also influence short-term direction, especially where they add to or challenge the current growth narrative.
Key releases and events
- Wed 21 Jan: UK CPI
- Thu 22 Jan: Australia Labour Force, Australia (December 2025)
- Fri 23 Jan: UK flash PMIs (manufacturing and services)
Technical snapshot
- GBPAUD continues to trade below its long-term moving average, with price action remaining in a downside direction since late November.
- The long-term average flattened through September and has turned lower since October, with the price remaining below and showing recent signs of a greater gap between the price and the moving average.
- Momentum has remained below neutral over recent months, with any retracements to the upside showing limited follow-through.
- Daily ranges have narrowed compared with earlier swings, suggesting a consistent but controlled drop in price rather than impulsive movement.
Bottom line
With multiple data releases due across key regions, FX markets may remain sensitive to outcomes that differ from expectations.
Existing technical conditions suggest that reactions may vary by pair, with some markets consolidating while others could retain recent directional characteristics.

US and European market attention this week is centred on the US Personal Income and Outlays report (which includes the PCE price index), late-week flash PMI releases, and a continued ramp-up in the US earnings season.
Alongside key data, geopolitical developments, including renewed discussion around Greenland and tariff threats, remain part of the broader risk backdrop.
Quick facts:
- US PCE inflation: Closely watched by policymakers as an important inflation measure (released within the Personal Income and Outlays report).
- Flash PMIs: US, Eurozone, Germany, and the UK are due late week, offering a read on growth momentum.
- US earnings: Large-cap and index-heavy companies shaping sentiment at elevated index levels.
- Geopolitical headlines: Greenland and proposed tariff measures add a layer of uncertainty to broader risk sentiment.
- Equity indices: Trading at elevated levels, which may increase sensitivity to data and earnings surprises.
United States
What to watch
US markets reopen after the Juneteenth holiday, with the US data calendar featuring the PCE price index and core PCE measures. Outcomes that differ from expectations can influence interest-rate expectations and near-term risk sentiment.
Later in the week, flash PMIs offer a more current snapshot of activity across manufacturing and services. US earnings remain a key driver of sentiment, and with indices at elevated levels, valuation and guidance narratives may be tested as results are released.
Key releases and events
- Thu 22 Jan (US): BEA GDP release — Q3 2025 (Updated Estimate)
- Thu 22 Jan (US): BEA Personal Income and Outlays (Oct & Nov 2025) — includes PCE price index and core PCE
- Fri 23 Jan (US): S&P Global flash PMIs (manufacturing and services)
- Throughout the week: US earnings season continues
How markets may respond
- Equities: Indices have been trading at elevated levels. As of 10:30am AEDT, 20 January 2026, the S&P 500 was within ~50 points of its record high.
- USD: PCE results that differ from expectations can contribute to volatility in FX and USD-linked assets, while PMI data can influence shorter-term momentum.
- Earnings: In a market trading at elevated levels, earnings results and forward guidance can generate volatility even without large headline misses. Forward guidance and margin commentary are likely to be closely watched.
UK and eurozone
What to watch
In the UK, CPI and labour market data can influence rate expectations and perceptions of growth momentum. In Germany, producer price data offers insight into pipeline inflation pressures. Flash PMIs across the Eurozone, Germany, and the UK complete the week’s calendar and may influence near-term growth assessments.
Key releases and events
Eurozone and Germany
- Thu 22 Jan: Germany PPI
- Fri 23 Jan: Eurozone flash manufacturing PMI (with services PMI)
- Fri 23 Jan: Germany flash manufacturing PMI
United Kingdom
- Wed 21 Jan: UK CPI
- Thu 22 Jan: UK labour market report
- Fri 23 Jan: UK flash manufacturing PMI (with services PMI)
How markets may respond
- DAX: The German index has been trading at elevated levels. PMI and PPI outcomes may influence cyclical sectors, notably industrials and exporters.
- FTSE 100 and GBP: UK CPI and labour market data can affect rate expectations and GBP sensitivity, while PMI outcomes may influence sector-level performance within the index.
- EUR: Euro moves may reflect PMI momentum and inflation signals, though direction can still be heavily influenced by US outcomes and global risk sentiment.
Geopolitics
Reporting has focused on renewed discussion around Greenland and associated tariff threats. Reporting also outlines tariff rates and potential escalation timelines, though details and implementation remain subject to change, and the situation is fluid.
Market reaction has been limited so far. If rhetoric escalates, markets could see intermittent volatility across equities, commodities, and FX. safe-haven moves (including in gold) are possible, though reactions can be uneven and may reverse.
US and Europe calendar summary
- Wed 21 Jan: UK CPI
- Thu 22 Jan (US) / Fri 23 Jan(AEDT):
- US GDP (Q3 2025 updated estimate)
- US Personal Income and Outlays (Oct/Nov, includes PCE)
- UK labour market report
- Fri 23 Jan: Flash PMIs (US, Eurozone, Germany, UK)
Bottom line
- The Personal Income and Outlays report (including PCE inflation measures) is one of the key US macro events this week and may influence rate expectations if outcomes differ materially from expectations.
- With equity indices trading at elevated levels, markets may be more sensitive to negative surprises and guidance downgrades than to confirmatory data.
- European releases — particularly UK CPI and the flash PMIs — remain important locally but may still trade in the context of US outcomes and broader risk sentiment.
- Geopolitical developments around Greenland and tariffs remain a secondary but persistent source of uncertainty.