Saya sudah lama frustrasi dengan kurangnya solusi “out of the box” untuk merencanakan OHLC grafik lilin secara alami di Oktaf, solusi terdekat yang saya tahu adalah tinggi rendah fungsi plot dari paket keuangan ( yang belum mengimplementasikan fungsi lilin ) di Oktaf Sourceforge. Karena itu, saya memutuskan untuk menulis fungsi grafik candlestick saya sendiri, yang kodenya ditunjukkan di bawah ini.
Versi dasar pertama ini hanya menggambarkan grafik candlestick dengan warna biru untuk batang naik (penutupan lebih tinggi daripada pembukaan) atau warna merah untuk batang turun (penutupan lebih rendah daripada pembukaan).
## Copyright (C) 2017 dekalog
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see .
## -*- texinfo -*-
## @deftypefn {} {@var{retval} =} candles (@var{O}, @var{H}, @var{L}, @var{C})
##
## Takes O, H, L and C inputs and plots a candlestick chart, with blue bars for
## close up days and red bars for close down days.
##
## @seealso{}
## @end deftypefn
## Author: dekalog
## Created: 2017-11-16
function [retval] = candles ( open , high , low , close )
if ( nargin != 4 )
error ( "Not enough input arguments. Should be OHLC vectors." ) ;
endif
wicks = high .- low ;
body = close .- open ;
up_down = sign( body ) ;
hold on ;
% plot the wicks
x = ( 1 : length( close ) ) ; % the x-axis
idx = x ;
high_nan = nan( size( high ) ) ; high_nan( idx ) = high ; % highs
low_nan = nan( size( low ) ) ; low_nan( idx ) = low ; % lows
x = reshape( [ x ; x ; nan( size( x ) ) ] , [] , 1 ) ;
y = reshape( [ high_nan(:)' ; low_nan(:)' ; nan( 1 , length( high ) ) ] , [] , 1 ) ;
plot( x , y , "k" , 'linewidth' , 2 ) ; % plot black wicks
% plot the up bars
x = ( 1 : length( high ) ) ; % the x-axis
idx = ( up_down == 1 ) ; idx = find( idx ) ; % index by condition
high_nan = nan( size( high ) ) ; high_nan( idx ) = close( idx ) ; % index closes > opens
low_nan = nan( size( low ) ) ; low_nan( idx ) = open( idx ) ; % index opens < closes
x = reshape( [ x ; x ; nan( size( x ) ) ] , [] , 1 ) ;
y = reshape( [ high_nan(:)' ; low_nan(:)' ; nan( 1 , length( high ) ) ] , [] , 1 ) ;
plot( x , y , "b" , 'linewidth' , 20 ) ; % plot blue up bars
% plot the down bars
x = ( 1 : length( high ) ) ; % the x-axis
idx = ( up_down == -1 ) ; idx = find( idx ) ; % index by condition
high_nan = nan( size( high ) ) ; high_nan( idx ) = open( idx ) ; % index opens > closes
low_nan = nan( size( low ) ) ; low_nan( idx ) = close( idx ) ; % index closes < opens
x = reshape( [ x ; x ; nan( size( x ) ) ] , [] , 1 ) ;
y = reshape( [ high_nan(:)' ; low_nan(:)' ; nan( 1 , length( high ) ) ] , [] , 1 ) ;
plot( x , y , "r" , 'linewidth' , 20 ) ; % plot red down bars
hold off ;
endfunction
Versi kedua merupakan versi grafik kondisional yang mengambil vektor kondisi sebagai input beserta vektor OHLC dan grafik kandil dengan warna berbeda sesuai dengan kondisi dalam vektor kondisi (kondisi berupa bilangan bulat 1 hingga 3 inklusif).
## Copyright (C) 2017 dekalog
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see .
## -*- texinfo -*-
## @deftypefn {} {@var{retval} =} candles (@var{O}, @var{H}, @var{L}, @var{C}, @var{TC})
##
## Takes OHLC inputs and a TC vector and plots a candlestick chart, with up bars coloured
## cyan or blue and down bars magenta or red, dependent on value contained in the
## condition vector TC ( values == 1 or == 2 ).
##
## @seealso{}
## @end deftypefn
## Author: dekalog
## Created: 2017-11-16
function [retval] = candles_tc ( open , high , low , close , tc )
if ( nargin != 5 )
error ( "Not enough input arguments. Should be OHLC and a condition vector" ) ;
endif
if ( length( unique( tc ) ) > 3 )
error ( "Too many conditions in condition vector. Maximum of 3 conditions allowed." ) ;
endif
wicks = high .- low ;
body = close .- open ;
up_down = sign( body ) ;
up_colours = "cbg" ;
down_colours = "mrk" ;
candle_body_width = 20 ;
hold on ;
% plot the wicks
x = ( 1 : length( close ) ) ; % the x-axis
idx = x ;
high_nan = nan( size( high ) ) ; high_nan( idx ) = high ; % highs
low_nan = nan( size( low ) ) ; low_nan( idx ) = low ; % lows
x = reshape( [ x ; x ; nan( size( x ) ) ] , [] , 1 ) ;
y = reshape( [ high_nan(:)' ; low_nan(:)' ; nan( 1 , length( high ) ) ] , [] , 1 ) ;
plot( x , y , "k" , 'linewidth' , 2 ) ; % plot black wicks
for ii = 1 : length( unique( tc ) )
% plot the up bars for ii condition
x = ( 1 : length( high ) ) ; % the x-axis
idx = ( up_down == 1 ) .* ( tc == ii ) ; idx = find( idx ) ; % index by condition
high_nan = nan( size( high ) ) ; high_nan( idx ) = close( idx ) ; % index closes > opens
low_nan = nan( size( low ) ) ; low_nan( idx ) = open( idx ) ; % index opens < closes
x = reshape( [ x ; x ; nan( size( x ) ) ] , [] , 1 ) ;
y = reshape( [ high_nan(:)' ; low_nan(:)' ; nan( 1 , length( high ) ) ] , [] , 1 ) ;
plot( x , y , up_colours( ii ) , 'linewidth' , candle_body_width ) ;
% plot the down bars for ii condition
x = ( 1 : length( high ) ) ; % the x-axis
idx = ( up_down == -1 ) .* ( tc == ii ) ; idx = find( idx ) ; % index by condition
high_nan = nan( size( high ) ) ; high_nan( idx ) = open( idx ) ; % index opens > closes
low_nan = nan( size( low ) ) ; low_nan( idx ) = close( idx ) ; % index closes < opens
x = reshape( [ x ; x ; nan( size( x ) ) ] , [] , 1 ) ;
y = reshape( [ high_nan(:)' ; low_nan(:)' ; nan( 1 , length( high ) ) ] , [] , 1 ) ;
plot( x , y , down_colours( ii ) , 'linewidth' , candle_body_width ) ;
endfor
hold off ;
endfunction
Contoh plot versi kedua ini adalah
Ada dua kondisi yang diplot pada grafik 1 jam ini: batang naik cyan dan batang turun magenta adalah batang yang muncul dalam "sesi Asia," yaitu setelah pukul 17:00 waktu setempat Waktu New York dan sebelum pukul 07:00 waktu setempat London; dan batang naik biru dan batang turun merah adalah batang yang muncul dalam sesi London – New York yang tumpang tindih, yaitu antara pukul 07:00 waktu setempat London dan pukul 17:00 waktu setempat New York.
Garis hitam horizontal bukan bagian dari fungsi plot dasar tetapi ditambahkan kemudian dengan menggunakan fungsi "hold". Garis-garis ini adalah "Tokyo Channel", yaitu titik tertinggi dan terendah sesi Asia yang diperpanjang hingga sesi London – New York berikutnya.
Saya harap para pembaca yang menggunakan Octave akan menganggap fungsi plot ini berguna.