PowerShell绘制Candlestick异常求助:K线实体及标签格式疑问
关于Candlestick(K线)图表显示异常的问题
问题描述
Candlestick bodies是否被限制为零?
异常图表

当前使用代码
$csv= Import-Csv -Path "path\Collected.csv" | sort InstanceMarket $high = $csv.'Maximum Runup' $low = $csv.'Maximum Drawdown' $open = "0" $close = $csv.'Daily Profit/Loss' $vol = $csv.'Today Total Trades' [void]$chart1.Series.Add("high") $chart1.Series["high"].ChartType = 'candlestick' $chart1.Series["high"].BorderWidth = 5 $chart1.Series["high"].Color = 'Black' $csv| ForEach-Object { $chart1.Series["high"].Points.addxy($_.'InstanceMarket', $_.'Maximum Runup') } [void]$chart1.Series.Add("low") $chart1.Series["low"].ChartType = "candlestick" $chart1.Series["low"].BorderWidth = 5 $chart1.Series["low"].Color = 'Black' $csv| ForEach-Object { $chart1.Series["low"].Points.addxy($_.'InstanceMarket', $_.'Maximum Drawdown') } [void]$chart1.Series.Add("open") $chart1.Series["open"].ChartType = "candlestick" $chart1.Series["open"].BorderWidth = 5 $chart1.Series["open"].Color = 'orange' $csv| ForEach-Object { $chart1.Series["open"].Points.addy($open) } [void]$chart1.Series.Add("Closepnl") $chart1.Series["Closepnl"].ChartType = "candlestick" $chart1.Series["Closepnl"].BorderWidth = 10 $chart1.Series["Closepnl"].BorderDashStyle = 'Solid' $chart1.Series["Closepnl"].BorderColor = 'GreenYellow' #test $csv| ForEach-Object { $chart1.Series["Closepnl"].Points.addxy($_.'InstanceMarket', $_.'Daily Profit/Loss') } $points = $Chart1.Series["high"].Points ForEach ($series in $points) { #Work in progress for Currency format $series.LabelFormat = -f "{0:C0}" $series.Label = $series.YValues } ####### $points = $Chart1.Series["Closepnl"].Points ForEach ($series in $points) { if ($series.YValues -gt 0) { $Series.Color = [System.Drawing.Color]::FromArgb(0, 119, 178) } else { $Series.Color = [System.Drawing.Color]::FromArgb(138, 12, 19) } update-Chart $chart1 -XPoints $csv.'InstanceMarket' -YPoints $highmark, $lowmark, $openmark, $close -ChartType Candlestick
正式问询
我需要补充哪些设置才能让图表显示为正常的Candlestick?图中的绿黄色线条显示异常,横向宽度过大,仅灯芯显示正常,这是什么原因?同时我尝试设置标签格式,请问正确的格式语法是什么?恳请各位提供帮助,感谢您的时间。
解决方案
1. 显示正常K线的核心设置
你当前的核心错误是为高开低收四个值分别创建了独立的Series,但WinForms Chart的Candlestick类型要求单个Series包含所有四个值(Open、High、Low、Close),每个DataPoint需要同时传入这四个Y值,而非拆分到多个Series。
正确实现方式:
# 移除所有拆分的Series,只保留一个Candlestick类型的Series [void]$chart1.Series.Add("Candlestick") $chart1.Series["Candlestick"].ChartType = "Candlestick" $chart1.Series["Candlestick"].BorderWidth = 2 # 按需调整线条宽度 # 遍历CSV数据,为每个X轴项添加包含四个Y值的点 $csv | ForEach-Object { $openVal = 0 # 你的Open值固定为0 $highVal = $_.'Maximum Runup' $lowVal = $_.'Maximum Drawdown' $closeVal = $_.'Daily Profit/Loss' # 按顺序传入Open、High、Low、Close四个值 $chart1.Series["Candlestick"].Points.AddXY($_.'InstanceMarket', $openVal, $highVal, $lowVal, $closeVal) }
2. 绿黄色线条异常的原因
你为Close值单独创建了Candlestick Series,该Series仅包含Close一个Y值,缺少Open/High/Low的支撑,导致Chart无法正确渲染K线实体,只能显示异常的横向宽线条。按上述方法合并到单个Series后,该问题会自动解决。
另外你设置的BorderWidth = 10会放大线条宽度,合并后可根据需求调整该参数,避免实体过宽。
3. 标签格式的正确语法
你当前的$series.LabelFormat = -f "{0:C0}"语法错误,正确的格式设置方式有两种:
- 直接为
LabelFormat赋值格式字符串:
$series.LabelFormat = "{0:C0}"
- 手动格式化标签文本(可指定具体Y值,K线点的Y值索引:0=Open、1=High、2=Low、3=Close):
# 例如格式化Close值为货币格式 $series.Label = "{0:C0}" -f $series.YValues[3]
内容的提问来源于stack exchange,提问作者iceman
相关产品推荐
相关产品推荐

