始値と安値 と 高値と終値からローソク足のSVG ファイルを作成するバッチファイル

バッチファイル

candle.bat chart.svg 60 80 30 40
@echo off
setlocal

:: 引数からファイル名と価格データを取得(なければ初期値)
set "output_file=%~1"
if "%output_file%"=="" set "output_file=Y:\candle.svg"

set "open=%~2"
if "%open%"=="" set "open=60"

set "high=%~3"
if "%high%"=="" set "high=120"

set "low=%~4"
if "%low%"=="" set "low=30"

set "close=%~5"
if "%close%"=="" set "close=40"

:: SVGサイズ設定
set "width=100"
set "height=100"
set "candle_width=10"
set /a x_center=%width% / 2
set /a candle_center=%x_center% + %candle_width% / 2

:: 色(赤=上昇、緑=下降)
set "color_up=#f44336"
set "color_down=#4caf50"
set "fill_color=%color_up%"
if %close% LSS %open% set "fill_color=%color_down%"

:: 自動スケール:価格範囲に余白を追加
set /a price_range=high - low
set /a margin=price_range / 20
set /a price_min=low - margin
set /a price_max=high + margin

:: 価格→Y座標変換(高いほど上)
set /a y_open=height - ((open - price_min) * height / (price_max - price_min))
set /a y_close=height - ((close - price_min) * height / (price_max - price_min))
set /a y_high=height - ((high - price_min) * height / (price_max - price_min))
set /a y_low=height - ((low - price_min) * height / (price_max - price_min))

:: 胴体の上下(常に top < bottom にする)
if %y_open% LSS %y_close% (
    set /a body_top=%y_open%
    set /a body_bottom=%y_close%
) else (
    set /a body_top=%y_close%
    set /a body_bottom=%y_open%
)

set /a body_height=%body_bottom% - %body_top%

:: SVG出力(ラベルなし)
(
echo ^<?xml version=^"1.0^" encoding=^"UTF-8^"?^>
echo ^<svg xmlns=^"http://www.w3.org/2000/svg^" width=^"%width%^" height=^"%height%^" viewBox=^"0 0 %width% %height%^"^>
echo   ^<line x1=^"%candle_center%^" y1=^"%y_high%^" x2=^"%candle_center%^" y2=^"%body_top%^" stroke=^"black^" stroke-width=^"2^" /^>
echo   ^<line x1=^"%candle_center%^" y1=^"%body_bottom%^" x2=^"%candle_center%^" y2=^"%y_low%^" stroke=^"black^" stroke-width=^"2^" /^>
echo   ^<rect x=^"%x_center%^" y=^"%body_top%^" width=^"%candle_width%^" height=^"%body_height%^" fill=^"%fill_color%^" stroke=^"black^" /^>
echo ^</svg^>
) > "%output_file%"

echo SVG file created: %output_file%
endlocal

bat,

Posted by eightban