1090. Highest Price in Supply Chain (25)解题报告

本文介绍了一种使用深度优先搜索算法遍历图结构的方法,并通过计算从根节点到每个节点的最深深度来调整商品价格。算法首先初始化节点状态并进行遍历,最后根据遍历深度和价格增长率计算出最终价格。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用一个int类型的数组来表示这个位置的元素是否被访问和该元素的深度。根结点深度为0。用一个栈来记录该元素到根的路径。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <cmath>
using namespace std;

int count(int arr[], int num, int &p);

int main(int argc, char** argv) {
	int num, *arr, i, cnt, p;
	double price, rate, finalprice;
	p = 0;
	scanf("%d %lf %lf", &num, &price, &rate);
	arr = new int[num];
	for(i = 0; i < num; i++){
		scanf("%d", &arr[i]);
	}
	cnt = count(arr, num, p);
	finalprice = price * pow(1.0 + rate / 100, cnt);
	printf("%.2lf %d\n", finalprice, p);
	delete[] arr;
	return 0;
}

int count(int arr[], int num, int &p){
	int i, *visit, index, deep, max = -1;
	stack<int> s;
	visit = new int[num];
	for(i = 0; i < num; i++){
		visit[i] = -1;
	}
	
	for(i = 0; i < num; i++){
		if(visit[i] == -1){
			index = i;
			while(arr[index] != -1 && visit[index] == -1){
				s.push(index);	
				index = arr[index];
			}
			if(arr[index] == -1){
				deep = 0;
				s.push(index);
			}
			else{
				deep = visit[index] + 1;
			}
			while(!s.empty()){
				index = s.top();
				s.pop();
				visit[index] = deep++;
			}
			if(deep && deep - 1 > max){
				p = 1;
				max = deep - 1;
			}
			else if(deep && deep - 1 == max){
				p++;
			}
			else if(!deep && deep > max){
				max = deep;
				p = 1;
			}
		}
	}
	delete[] visit;
	return max;
}

<think>根据用户提供的代码片段,止损条件包含三个部分:浮动止盈触发、回撤止损(0.1%)、动态止损(入场价-2×ATR)。但问题在于,当条件满足时,还需要额外的过滤条件(RSI<25且ATR14>3且ATR10>3)才会执行平仓。这可能导致止损条件被满足但未触发交易。 用户希望修改代码,以解决止损延迟的问题(如前例中回撤0.7%才卖出)。根据之前的分析,主要问题在于过滤条件过于严格。 修改建议: 1. 将硬止损(动态止损)设为无条件执行,避免因过滤条件导致无法止损。 2. 对于回撤止损和浮动止盈,可以保留过滤条件,但应放宽条件(如调整RSI阈值或取消ATR限制)。 3. 或者,将过滤条件改为预警条件,不直接作为执行平仓的必要条件。 具体修改方案如下: 原始代码: ```python if (g.float_profit_mode and (current_price < g.trail_stop)) or drop_ratio >= 0.001 or (current_price < g.stop_loss_price): if rsi1[-1] < 25 and (ATR14[-1] > 3 and ATR10[-1] > 3): order_target(IM_current_month, 0, side=g.long_short) log.info('最高级价格:' + str(g.highest_high)) log.info('卖出价格:' + str(current_price)) g.flag = True ``` 修改后的代码(方案1:分离止损条件): ```python # 情况1:硬止损无条件执行 if current_price < g.stop_loss_price: order_target(IM_current_month, 0, side=g.long_short) log.info(f'动态止损触发,卖出价格:{current_price}') g.flag = True # 情况2:回撤止损或浮动止盈,带过滤条件(放宽条件) elif (g.float_profit_mode and current_price < g.trail_stop) or drop_ratio >= 0.001: if rsi1[-1] < 40: # 放宽RSI条件,取消ATR限制 order_target(IM_current_month, 0, side=g.long_short) log.info(f'最高级价格:{g.highest_high}, 卖出价格:{current_price}') g.flag = True ``` 修改后的代码(方案2:取消过滤条件,仅保留RSI作为预警): ```python # 所有止损条件触发后,仅用RSI作为辅助判断(非必须) if (g.float_profit_mode and current_price < g.trail_stop) or drop_ratio >= 0.001 or (current_price < g.stop_loss_price): # 将RSI条件改为预警日志,不阻止交易 if rsi1[-1] < 25: log.warning("RSI处于超卖区,谨慎止损") # 无条件执行平仓 order_target(IM_current_month, 0, side=g.long_short) log.info(f'卖出触发,最高价:{g.highest_high}, 卖出价:{current_price}') g.flag = True ``` 方案选择: - 若想确保止损立即执行,选方案2(无条件执行)。 - 若想保留部分过滤但避免延迟,选方案1(分离止损条件,硬止损无条件,其他放宽)。 另外,注意回撤止损阈值由0.3%改为0.1%(drop_ratio>=0.001),这是更敏感的设置,需确认是否符合策略意图。 其他优化: - 可增加日志输出当前RSI和ATR值,便于复盘。 - 考虑将过滤条件参数设为全局变量,便于回测优化。 示例修改(采用方案1): ```python # 硬止损无条件执行 if current_price < g.stop_loss_price: order_target(IM_current_month, 0, side=g.long_short) log.info(f'动态止损触发,卖出价格:{current_price}') g.flag = True # 回撤止损或浮动止盈(带放宽的过滤条件) elif (g.float_profit_mode and current_price < g.trail_stop) or drop_ratio >= 0.001: # 放宽条件:RSI<40即可,且不再要求ATR>3 if rsi1[-1] < 40: order_target(IM_current_month, 0, side=g.long_short) log.info(f'回撤/止盈触发,最高价:{g.highest_high}, 卖出价:{current_price}') g.flag = True else: log.info(f'回撤/止盈条件满足,但RSI({rsi1[-1]})未<40,未平仓') ``` 这样修改后,硬止损将立即执行,而回撤止损和浮动止盈在RSI<40时执行(避免在强势回调时过早平仓)。同时,去除了ATR波动率限制,因为ATR条件在止损时可能不必要。 注意:如果策略中ATR过滤是为了避免低波动市场中的假信号,可以考虑其他方式(如整体市场波动率过滤开仓而非平仓),以免影响止损。</think>### 策略代码优化方案 针对您提供的代码,核心问题在于**止损条件被RSI和ATR过滤条件阻挡**。以下是优化建议(修改部分用`# <<<`标记): ```python if not g.flag: if current_price > g.highest_high: g.highest_high = current_price if g.entry_price and current_price - g.entry_price > 3000: g.float_profit_mode = True g.trail_stop = current_price - 1000 drop_ratio = (g.highest_high - current_price) / g.highest_high # ============== 核心修改部分 ============== # # 条件1:硬止损无条件执行 if current_price < g.stop_loss_price: order_target(IM_current_month, 0, side=g.long_short) log.info('动态止损触发 | 卖出价格:' + str(current_price)) g.flag = True # 条件2:回撤止损与浮动止盈(带优化过滤) elif (g.float_profit_mode and current_price < g.trail_stop) or drop_ratio >= 0.001: # 优化过滤条件:放宽RSI限制 + 取消ATR绝对值限制 if rsi1[-1] < 40: # <<< RSI阈值从25放宽到40 order_target(IM_current_month, 0, side=g.long_short) log.info('回撤止盈触发 | 最高价:' + str(g.highest_high)) log.info('卖出价格:' + str(current_price)) g.flag = True # ======================================== # ``` ### 关键优化说明 1. **分离止损条件**: - 将`硬止损 (current_price < g.stop_loss_price)`设为**无条件执行** - 避免因过滤条件导致本金保护失效 2. **放宽过滤阈值**: - `RSI阈值从25放宽到40`:0.1%小幅回撤时RSI很少<25 - 取消`ATR>3`限制:波动率过滤更适合开仓而非止损 3. **增加止损优先级**: ```mermaid graph TD A[触发条件] --> B{硬止损触发?} B -->|是| C[立即平仓] B -->|否| D{回撤/止盈触发?} D -->|是| E[RSI<40?] E -->|是| F[平仓] E -->|否| G[继续持仓] ``` 4. **日志增强**: - 添加`动态止损触发`/`回撤止盈触发`标识 - 保留最高价和卖出价记录便于复盘 ### 备选增强方案 若需更精细控制,可添加波动率自适应过滤: ```python # 替代原RSI/ATR过滤部分 atr_ratio = ATR14[-1] / ATR14.mean() # 当前ATR与均值的比率 if rsi1[-1] < max(35, 50 - atr_ratio*10): # 波动越大要求越宽松 ``` 此修改确保: 1. 硬止损绝对优先执行 2. 回撤止损在RSI<40时可靠触发 3. 避免0.7%超额回撤
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值