交易品种情绪 SymbolSentiment 接口和 SymbolSentimentUpdatedEventArgs 类允许您通过 API 访问实时市场情绪数据。 对于任何给定的交易品种:
Symbol.Sentiment.BuyPercentage 提供买入活动的总体百分比,而 Symbol.Sentiment.SellPercentage 返回卖出活动的总体百分比。 SymbolSentimentUpdatedEventArgs 是情绪更新事件的数据载体。 此类允许您访问相应交易品种的更新情绪数据。 警告
交易品种情绪 API 对象仅在 .NET 6 算法中受支持,不适用于 Cloud 或 cTrader CLI。
提示
将市场情绪数据整合到自动交易策略中,以增强决策能力,提高盈利交易的可能性,并加强风险管理。
交易品种情绪 API 对象可用于在算法中执行以下操作:
功能或操作 示例 情绪驱动交易 跟随情绪偏向交易 逆极端情绪交易 结合情绪与蜡烛图 情绪背离时退出 自适应风险配置 过滤噪音交易 根据情绪调整交易规模 根据市场活动调节策略 根据情绪设置止损 监控和分析 创建基于情绪的提醒 运行情绪热图策略 通过情绪发现突破 验证指标信号
注意
由于情绪数据仅在实时可用,因此无法用于回测或优化,因为没有历史或存储数据。
情绪驱动交易 跟随情绪偏向交易 您可以编程 cBot 在进入交易前确认趋势偏向,或仅在情绪与方向匹配时交易。 例如,当至少 70% 的情绪看涨时,cBot 执行买入;当 70% 看跌时,执行卖出。
protected override void OnBar ()
{
if ( Symbol . Sentiment . BuyPercentage > 70 )
{
ExecuteMarketOrder ( TradeType . Buy , "EURUSD" , 10000 );
}
else if ( Symbol . Sentiment . SellPercentage > 70 )
{
ExecuteMarketOrder ( TradeType . Sell , "EURUSD" , 10000 );
}
}
逆极端情绪交易 考虑一个逆强烈情绪共识进行交易的 cBot。 算法中的反转方法假设群众在极端情况下通常是错误的,或者认为当很高比例的交易者站在一边时,市场可能准备转向。 例如,如果超过 80% 的交易者在买入,cBot 执行卖出,预期会出现反转。 当大多数人在卖出时,同样的逻辑也适用。
1
2
3
4
5
6
7
8
9
10
11
12
13 protected override void OnBar ()
{
if ( Symbol . Sentiment . BuyPercentage > 80 )
{
// Everyone is buying. Consider selling
ExecuteMarketOrder ( TradeType . Sell , "EURUSD" , 10000 );
}
else if ( Symbol . Sentiment . SellPercentage > 80 )
{
// Everyone is selling. Consider buying
ExecuteMarketOrder ( TradeType . Buy , "EURUSD" , 10000 );
}
}
结合情绪与蜡烛图 您可以配置 cBot 仅在市场情绪和蜡烛图方向都一致时采取行动。 如果出现看涨蜡烛,且情绪超过 60% 看涨,cBot 买入。 如果蜡烛图看跌且情绪一致,cBot 卖出。 这种方法有助于您过滤掉弱信号并避免错误入场。
1
2
3
4
5
6
7
8
9
10
11
12
13
14 protected override void OnBar ()
{
bool bullishCandle = Bars . LastBar . Close > Bars . LastBar . Open ;
if ( bullishCandle && Symbol . Sentiment . BuyPercentage > 60 )
{
ExecuteMarketOrder ( TradeType . Buy , "EURUSD" , 10000 );
}
bool bearishCandle = Bars . LastBar . Close < Bars . LastBar . Open ;
if ( bearishCandle && Symbol . Sentiment . SellPercentage > 60 )
{
ExecuteMarketOrder ( TradeType . Sell , "EURUSD" , 10000 );
}
}
基于情绪分歧退出交易 cBot 可以帮助您在情绪不再支持您的头寸时退出交易。 例如,如果您持有买入头寸,而情绪转为超过 60% 看跌,cBot 就会平仓。 当情绪转为看涨时,它也会对卖出交易执行相同操作。
protected override void OnBar ()
{
foreach ( var position in Positions )
{
if ( position . TradeType == TradeType . Buy && Symbol . Sentiment . SellPercentage > 60 )
ClosePosition ( position );
if ( position . TradeType == TradeType . Sell && Symbol . Sentiment . BuyPercentage > 60 )
ClosePosition ( position );
}
}
自适应风险配置 过滤掉噪音交易 您可以开发一个 cBot,在没有明确情绪偏向时避免交易。 例如,如果情绪几乎平分且差异小于 10%,cBot 就会跳过该交易。 这种设置可以过滤掉噪音,保护您免受犹豫不决的市场影响。
1
2
3
4
5
6
7
8
9
10
11
12 protected override void OnBar ()
{
var diff = Math . Abs ( Symbol . Sentiment . BuyPercentage - Symbol . Sentiment . SellPercentage );
if ( diff < 10 )
{
// Sentiment is too neutral. Avoid trading
return ;
}
// Continue strategy logic here
}
根据情绪调整交易规模 您可以设置 cBot 根据情绪倾向的强度来调整头寸规模。 例如,如果看涨情绪大幅超过看跌情绪,cBot 就会增加交易量。 如果情绪接近中性,cBot 就会减小交易规模。
1
2
3
4
5
6
7
8
9
10
11
12
13
14 protected override void OnBar ()
{
double sentimentStrength = Math . Abs ( Symbol . Sentiment . BuyPercentage - Symbol . Sentiment . SellPercentage );
long volume = ( long )( 10000 * ( sentimentStrength / 100.0 )); // Adjust volume dynamically
if ( Symbol . Sentiment . BuyPercentage > Symbol . Sentiment . SellPercentage )
{
ExecuteMarketOrder ( TradeType . Buy , "EURUSD" , 10000 );
}
else
{
ExecuteMarketOrder ( TradeType . Sell , "EURUSD" , 10000 );
}
}
根据市场活动调节策略 您的策略可以设置为在情绪显示市场拥挤迹象时暂停交易。 例如,如果超过 90% 的交易者都在买入或卖出,cBot 就会停止交易,以避免在市场可能即将反转时建立头寸。
protected override void OnBar ()
{
if ( Symbol . Sentiment . BuyPercentage > 90 || Symbol . Sentiment . SellPercentage > 90 )
{
Print ( "Extreme sentiment detected. Halting trading temporarily." );
return ;
}
// Continue normal trading logic
}
根据情绪设置止损 您可以指示 cBot 测量情绪强度并做出适当的止损设置。 例如,当看涨情绪超过 80% 时,cBot 使用更紧的 20 点止损。 如果情绪不太确定,cBot 会设置更宽的 50 点止损,以留出更多余地。
protected override void OnBar ()
{
double stopLossPips = Symbol . Sentiment . BuyPercentage > 80 ? 20 : 50 ;
if ( Symbol . Sentiment . BuyPercentage > Symbol . Sentiment . SellPercentage )
{
ExecuteMarketOrder ( TradeType . Buy , "EURUSD" , 10000 , "buy" , stopLossPips , null );
}
}
监控和分析 创建基于情绪的警报 考虑使用一个实时监控情绪并在达到极端水平时发出警报的 cBot。 例如,如果买入情绪超过 90%,cBot 会记录或打印看涨警报。 如果卖出情绪超过相同阈值,它会通知您极端看跌情况。
// This method handles the SymbolSentiment_Updated event
// It is triggered whenever the symbol sentiment data is updated
private void SymbolSentiment_Updated ( SymbolSentimentUpdatedEventArgs obj )
{
if ( obj . Sentiment . BuyPercentage > 90 )
Print ( "Extreme bullish sentiment detected!" );
if ( obj . Sentiment . SellPercentage > 90 )
Print ( "Extreme bearish sentiment detected!" );
}
运行情绪热图策略 考虑使用一个 cBot 对多个资产进行情绪扫描并识别最佳交易机会。 例如,cBot 遍历所有可用交易品种并打印买入和卖出百分比,然后您可以专注于那些具有最极端看涨或看跌情绪的交易品种。
protected override void OnStart ()
{
var symbols = Symbols . GetSymbols ();
foreach ( var sym in symbols )
{
Print ( $"{sym.Name} - Buy %: {sym.Sentiment.BuyPercentage}, Sell %: {sym.Sentiment.SellPercentage}" );
}
}
通过情绪发现突破 cBot 可以被编程为检测情绪的快速变化,作为潜在突破或市场波动加剧的信号。 例如,如果买入情绪跳升或下降超过 15%,cBot 会记录这一飙升,作为波动行情的早期指示。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 // Field to store the previous buy percentage value
private double previousBuy ;
protected override void OnStart ()
{
previousBuy = Symbol . Sentiment . BuyPercentage ;
// Subscribe to the sentiment updated event
// This will trigger the SymbolSentiment_Updated method whenever sentiment data is updated
Symbol . Sentiment . Updated += SymbolSentiment_Updated ;
}
// obj is an instance of SymbolSentimentUpdatedEventArgs, which carries the updated sentiment data
private void SymbolSentiment_Updated ( SymbolSentimentUpdatedEventArgs obj )
{
double change = Math . Abs ( obj . Sentiment . BuyPercentage - previousBuy );
if ( change > 15 )
Print ( "Spike in sentiment detected. Breakout or heightened volatility coming soon" );
previousBuy = obj . Sentiment . BuyPercentage ;
}
验证指标信号 您可以通过编程 cBot 只在指标和情绪条件都符合时采取行动,来增强您的策略,而不是仅仅依赖指标。 例如,当 RSI 显示超卖且情绪超过 70% 看涨时,cBot 买入。 如果 RSI 显示超买且情绪超过 70% 看跌,cBot 卖出。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 private RelativeStrengthIndex rsi ;
protected override void OnStart ()
{
rsi = Indicators . RelativeStrengthIndex ( Bars . ClosePrices , 14 );
}
protected override void OnBar ()
{
if ( rsi . Result . LastValue < 30 && Symbol . Sentiment . BuyPercentage > 70 )
{
ExecuteMarketOrder ( TradeType . Buy , "EURUSD" , 10000 );
}
if ( rsi . Result . LastValue > 70 && Symbol . Sentiment . SellPercentage > 70 )
{
ExecuteMarketOrder ( TradeType . Sell , "EURUSD" , 10000 );
}
}
上一页 文件操作 下一页 自定义品种