Chart Geany Technical Analysis Software for stocks,
                mutual funds and forex

CGScript version 1.5 Developer's Guide

Revision 1


Table of Contents

Overview

Basic concepts

Tool-chain

Types
   Arrays
   Bar data
   Candle
  
Chart object
   Colors
   Error codes
   Event codes
   Font weight
   Strings
   Text adjustment
   Time frames
   Other

Macros

Constants  

Supported ISO C99 functions
    From stdio.h
    From string.h
    From stdlib.h
    From math.h  

CGScript functions
    Array functions
    Bar functions
    Chart functions
    Common functions
    Input variable functions
    Object functions
    String functions
    Technical analysis functions
     

Overview


CGScript is an ISO C99 dialect for developing Chart Geany modules. CGScript allows you to create custom technical indicators and objects as modules for Chart Geany version 5.0.0 or later. You may treat CGScript as a C99 module API, but with serious limitations that violate the standard in many cases.

Basic concepts


A Chart Geany module is the binary code of a compiled CGScript program.

CGScript programs must meet the following minimum requirements:
  1. Declare the module properties: MODID, MODAUTHOR, MODVERSION and MODTYPE.
  2. Must have an Init() function with the prototype: static int Init();
  3. Must have a Loop() function with the prototype: static int Loop();
  4. Both Init() and Loop() functions must return an error code.
  5. An Event() function is needed with the prototype: static void Event();
Chart Geany's integrated code editor will generate code, meeting all the above requirements when you begin a new program. You can edit the generated code template according to your needs.

There are a few things you need to keep in mind:
  • Better have some ANSI C89/ISO C99 coding experience before you begin.
  • Init() is the function to be executed first during module's execution and is executed only once.
  • Loop() is executed every time the chart receives a refresh signal.
  • FirstRun macro returns true when Loop() is executed for the first time.
  • When NeedsUpdate() function returns true, you need to update the value sets of the object and/or its children.
  • C preprocessor is not available for developers (but it is still used internally).
  • Dynamic memory (eg. malloc) and process/thread creation (eg. fork, pthread_create etc) is not available too.
  • Declare your global variables static.
  • Declare your functions static.
  • Non-static Array_t, String_t and ObjectHandler_t declared variables are auto destroyed when they go out of scope and default to NULL.
  • Debug mode enables the memory sanitizer. The sanitizer sends its output to Chart Geany's debug console. 
  • printf() sends its output to Chart Geany's debug console.
  • Most functions disable module's execution on failure and send a message to debug console.
  • The compiler treats all warnings as errors.
  • CGScript is in beta stage. Expect annoying bugs.

Tool-chain


Until version 1.3, under Linux and Mac OS X ChartGeany uses /usr/bin/cc to compile CGScript programs. Under Microsoft Windows the recommended compiler is MINGW GCC 4.8 or later. This compiler is included, in binary form, in the standard distribution package for Microsoft Windows. The binary comes from the UNMODIFIED source code of the compiler. GCC is free and open source software distributed under the terms of the GPL v3.

Since version 1.4, under Linux and Windows, ChartGeany embeds Fabrice Bellard's Tiny C Compiler and uses it as the default compiler to compile CGScript programs. For macOS, ChartGeany will use the cc program found in the path. This is usually /usr/bin/cc. Command line tools for macOS will be installed with the command: xcode-select --install.

Types

Chart object: Legal chart object types

typedef enum

{

LABEL_OBJECT,
/* Label object */
TEXT_OBJECT,
/* Text object */
VLINE_OBJECT,
/* Vertical line object */
HLINE_OBJECT,
/* Horizontal line object */
LINE_OBJECT,
/* Trend line object */
FIBO_OBJECT
/* Fibonacci object */
SUBCHART_OBJECT,
/* Subchart object */
VBARS_OBJECT,
/* Vertical bars object */
CURVE_OBJECT,
/* Curve object */
DOT_OBJECT,
/* Dot object */
CONTAINER_OBJECT
/* Container object */
} Object_t; /* Object type */


Text adjustment: Legal text adjustment methods

typedef enum

{

TEXT_VADJUST_NORMAL,
/* Text vertical adjustment normal */
TEXT_VADJUST_CENTER,
/* Text vertical adjustment center */
TEXT_VADJUST_ABOVE,
/* Text vertical adjustment above */
TEXT_VADJUST_BELOW,
/* Text vertical adjustment below */
TEXT_HADJUST_NORMAL,
/* Text horizontal adjustment normal */
TEXT_HADJUST_CENTER,
/* Text horizontal adjustment center */
TEXT_HADJUST_LEFT,
/* Text horizontal adjustment left */
TEXT_HADJUST_RIGHT
/* Text horizontal adjustment right */
} TextAdjustment_t; /* Text adjustment */


Time frames: Legal time frames

typedef enum
{

TF_DAY,
/* Day time frame (daily chart) */
TF_WEEK,
/* Week time frame (weekly chart) */
TF_MONTH,
/* Month time frame (monthly chart) */
TF_YEAR
/* Year time frame (yearly chart) */
} TimeFrame_t /* Time frame */


Candle: Legal candle types

typedef enum
{
CTYPE_CANDLE,
/* Standard candle */
CTYPE_HEIKINASHI
/* Japanese heikin-ashi candle */
} Candle_t; /* Candle types */


Price: For open, close, high and low price

typedef enum
{
OPEN_PRICE,
/* Open price */
CLOSE_PRICE,
/* Close price */
HIGH_PRICE,
/* High price */
LOW_PRICE
/* Low price */
} Price_t;
/* Price types */


Error codes: Error codes returned by various CGScript functions

typedef enum

{

CGSERR_OK = -4999,
/* No error */   
CGSERR_INITIALIZATION_FAILED,
/* Initialization failed */
CGSERR_RUNTIME_ERROR,   
/* Run-time error */
CGSERR_UNINITIALIZED,   
/* Uninitialized module */   
CGSERR_ALREADY_INITIALIZED,
/* Module is initialized */   
CGSERR_NO_PARENT_CHART,
/* No parent */   
CGSERR_NO_MODULE,   
/* No module */
CGSERR_NO_ARRAY,
/* No array */
CGSERR_ARRAY_CREATION_AFTER_INIT,
/* Array creation after Init() execution */
CGSERR_ARRAY_ELEM_BIG,
/* Array element too big */
CGSERR_ARRAY_ELEM_ZERO,
/* Array element size is zero */
CGSERR_ARRAY_CREATION_FAILED,   
/* Array creation failed */
CGSERR_ARRAY_SUBSCRIPT,   
/* Array subscript out of range */
CGSERR_MEMORY,
/* Out of memory */
CGSERR_TIMEFRAME,   
/* Invalid time frame */
CGSERR_INVALID_ARGUMENT,
/* Invalid argument */
CGSERR_NEGATIVE_SHIFT,
/* Negative shift */
CGSERR_INVALID_MODTYPE,
/* Invalid module type */
CGSERR_INTERNAL_100,
/* internal error 100 */
CGSERR_INTERNAL_101,
/* internal error 101 */
CGSERR_FPE,
/* floating point exception */
CGSERR_SEGV,
/* segmentation fault */
CGSERR_ILL,
/* illegal instruction */
CGSERR_SIGNAL
/* signal not handled */
} ErrCode_t;
/* Error codes */


Event codes: Event codes returned in LastEvent global variable

typedef enum
{
EV_MOUSE_ON_BAR = 1,
/* mouse pointer changed bar */   
EV_INPUT_VAR
/* input variable changed */
} Event_t; /* Event types */


Bar data: Structure holding information about bars

typedef struct

{

double High;
/* High */
double Low;
/* Low */
double Open;
/* Open */
double Close;
/* Close */
double AdjClose;
/* Adjusted close */
double Volume;
/* Volume */
unsigned short Year;
/* Year */
unsigned short Month;
/* Month */
unsigned short Day;
/* Day */
char Date[16];   
/* Full date */
char Time[16];
/* Time */
char Id[256];
/* Bar id string */
} BarData_t;
/* Bar data */


Strings:

typedef void * String_t;


Arrays:

typedef void * Array_t;


Colors:

typedef unsigned int Color_t;

See: Color definitions


Font Weight:

typedef int FontWeight_t;

See: Constants for font weight

Other: Various types used in CGScript programs

typedef const char Property;

typedef void * ObjectHandler_t;

Macros


CGScript version:

#define CGSCRIPT_VERSION
0x00010005


TLS definition for module's global variables:

#define ModuleLocal static __thread


Various symbols used in text or label objects:

#define UPWARDS_ARROW
"↑"
#define DOWNWARDS_ARROW
"↓"
#define UPWARDS_WHITE_ARROW
"⇧"
#define DOWNWARDS_WHITE_ARROW
"⇩"
#define WHITE_UP_POINTING_INDEX
"☝"
#define WHITE_DOWN_POINTING_INDEX
"☟"


Color definitions

#define COLOR_WHITE
4294967295
#define COLOR_BLACK
4278190080
#define COLOR_RED
4294901760
#define COLOR_DARKRED
4289331200
#define COLOR_GREEN
4278255360
#define COLOR_DARKGREEN
4278233600
#define COLOR_BLUE
4278190335
#define COLOR_DARKBLUE
4278190208
#define COLOR_CYAN
4278255615
#define COLOR_DARKCYAN
4278222976
#define COLOR_MAGENTA
4294902015
#define COLOR_DARKMAGENTA
4286578816
#define COLOR_YELLOW
4294967040
#define COLOR_DARKYELLOW
4286611456
#define COLOR_GRAY
4288716964
#define COLOR_DARKGRAY
4286611584
#define COLOR_LIGHTGRAY
4290822336

Constants


Constants for font weight:

static const FontWeight_t

FONTWEIGHT_LIGHT =
25,
FONTWEIGHT_NORMAL =
50,
FONTWEIGHT_DEMIBOLD =
63,
FONTWEIGHT_BOLD =
75,
FONTWEIGHT_BLACK =
87;

Supported ISO C99 functions


From stdio.h:

int printf(const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);

Notice: printf prints formatted data to debug console, not to stdout

From string.h:

void *memset(void *s, int c, size_t n);
void *memmove(void *dest, const void *src, size_t n);
char *strncpy(char *dest, const char *src, size_t n);
char *strncat(char *dest, const char *src, size_t n);
int strncmp(const char *s1, const char *s2, size_t n);
size_t strlen(const char *s);


From stdlib.h:

int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
double atof(const char *nptr);

double strtod(const char *nptr, char **endptr);
float strtof(const char *nptr, char **endptr);
long double strtold(const char *nptr, char **endptr);

long int strtol(const char *nptr, char **endptr, int base);
long long int strtoll(const char *nptr, char **endptr, int base);

int abs(int j);
long int labs(long int j);
long long int llabs(long long int j);

void qsort(void *base, size_t nmemb, size_t size,
                  int (*compar)(const void *, const void *));

void *bsearch(const void *key, const void *base,
                     size_t nmemb, size_t size,
                     int (*compar)(const void *, const void *));


From math.h:

double sqrt(double x);
float sqrtf(float x);
long double sqrtl(long double x);

double cos(double x);
float cosf(float x);
long double cosl(long double x);

double sin(double x);
float sinf(float x);
long double sinl(long double x);

double tan(double x);
float tanf(float x);
long double tanl(long double x);

double atan(double x);
float atanf(float x);
long double atanl( long double x);

double log(double x);
float logf(float x);
long double logl(long double x);

double log10(double x);
float log10f(float x);
long double log10l(long double x);

double log2(double x);
float log2f(float x);
long double log2l(long double x);

double ldexp(double x, int exp);
float ldexpf(float x, int exp);
long double ldexpl(long double x, int exp);

double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);

double modf(double x, double *iptr);
float modff(float x, float *iptr);
long double modfl(long double x, long double *iptr);

double frexp(double x, int *exp);
float frexpf(float x, int *exp);
long double frexpl(long double x, int *exp);

double scalbln(double x, long int exp);
float scalblnf(float x, long int exp);
long double scalblnl(long double x, long int exp);

CGScript functions 


Array functions

CGScript supports its own array type along with the standard arrays of ISO C99. Usage of CGScript arrays is recommended for most applications. Non-static Array_t declared variables are auto destroyed when they go out of scope.

Synopsis

Array_t ArrayCreate
(int elemsize,
int maxdim);
int ArrayAppend
(Array_t arr,
void *elem);
void ArrayPut
(Array_t arr,
int idx,
void *elem);
int ArraySize
(Array_t arr);
void *ArrayGet
(Array_t arr,
int index);
void ArrayReset
(Array_t arr);

Details

ArrayCreate ()

Array_t
ArrayCreate
(int elemsize,
int maxdim);

Creates and initializes an array. On failure it disables the module's execution. This function cannot be present in Loop().

elemsize:
The maximum size in bytes of each element. Valid values are between 1 and 512.

maxdim:
The maximum expected dimension of the array. Valid values are between 1 and 65536.

Returns:
A new array of type Array_t.



ArrayAppend ()

int
ArrayAppend
(Array_t arr,
void *elem);

Appends an element to the array. On failure it disables the module's execution.

arr:
The array where the element is going to be appended.

elem:
The element to be appended. ArrayAppend will copy the contents of element into a new array element.

Returns:
The size of the array after the append operation.


Macros:

#define cArrayAppend(a,e)
ArrayAppend(a,(void *)&e)



ArrayPut ()

void
ArrayPut
(Array_t arr,
int index,
void *elem);

Overwrites the contents of an already existing array element. If the element does not exist, it does nothing. On failure it disables the module's execution.

arr:
The array that holds the element to be overwritten.

index:
The position of the element in the array.

elem:
The new contents of the element.

Returns:
Nothing.



Macros:

#define cArrayPut(a,i,e)
ArrayPut(a,i,(void *)&e)



ArraySize ()

int
ArraySize
(Array_t arr);

Returns the number of elements in the array. On failure it disables the module's execution.

arr:
The array.

Returns:
The total number of elements in the array.



ArrayGet ()

void *
ArrayGet
(Array_t arr,
int index);

Returns a pointer to the contents of an array element. On failure it disables the module's execution.

arr:
The array the element belongs

index:
The position of the element in the array.

Returns:
A pointer to the contents of the element. For negative index, the contents of the first element are returned. For index greater than the size of the array, ArrayGet returns the contents of the last element. 



Macros:

#define sArrayGet(a,i)
(*(short *)ArrayGet(a,i))
#define usArrayGet(a,i)
(*(unsigned short *)ArrayGet(a,i))
#define iArrayGet(a,i)
(*(int *)ArrayGet(a,i))
#define uiArrayGet(a,i)
(*(unsigned int *)ArrayGet(a,i))
#define lArrayGet(a,i)
(*(long *)ArrayGet(a,i))
#define ulArrayGet(a,i)
(*(unsigned long *)ArrayGet(a,i))
#define llArrayGet(a,i)
(*(long long *)ArrayGet(a,i))
#define ullArrayGet(a,i)
(*(unsigned long long *)ArrayGet(a,i))
#define fArrayGet(a,i)
(*(float *)ArrayGet(a,i))
#define dArrayGet(a,i)
(*(double *)ArrayGet(a,i))
#define ldArrayGet(a,i)
(*(long double *)ArrayGet(a,i))



ArrayReset ()

void
ArrayReset
(Array_t arr);

Clears the contents of an array and resets its size to zero. On failure it disables the module's execution.

arr:
The array to reset.

Returns:
Nothing.




Bar functions

Information about bars.

Synopsis

BarData_t Bar
(TimeFrame_t tf, int shift);
BarData_t BarHA
(TimeFrame_t tf, int shift);
int NBars
(TimeFrame_t tf);
int NVisibleBars
(void);
int NewestVisibleBar
(void);
int OldestVisibleBar
(void);

Details

Bar ()

BarData_t Bar (TimeFrame_t tf,
int shift);

Returns a bar data structure for time frame tf. Shift is an index, relative to the most recent bar. The most recent bar can be accessed with shift = 0. On failure it disables the module's execution.

tf:
The time frame the bar belongs to.

shift:

An index relative to the most recent bar of time frame tf. Valid values between 0 and NBars (tf). A value below 0, returns a bar data structure containing only nulls. A value above NBars (tf) is truncated to NBars (tf).

Returns:
A bar data structure.


Macros:

#define Open(s)
Bar(TF_CURRENT,s).Open
#define High(s)
Bar(TF_CURRENT,s).High
#define Low(s)
Bar(TF_CURRENT,s).Low
#define Close(s)
Bar(TF_CURRENT,s).Close
#define Volume(s)
Bar(TF_CURRENT,s).Volume
#define Year(s)
Bar(TF_CURRENT,s).Year
#define Month(s)
Bar(TF_CURRENT,s).Month
#define Day(s)
Bar(TF_CURRENT,s).Day
#define Date(s)
Bar(TF_CURRENT,s).Date
#define Time(s)   
Bar(TF_CURRENT,s).Time
#define Id(s)
((const char *) Bar(TF_CURRENT,s).Id)



BarHA ()

BarData_t BarHA (TimeFrame_t tf,
 int shift);

Same as Bar () but for Heikin-Ashi candles.

tf:
The time frame the bar belongs to.

shift:

An index relative to the most recent bar of time frame tf. Valid values between 0 and NBars (tf). A value below 0, returns a bar data structure containing only nulls. A value above NBars (tf) is truncated to NBars (tf).

Returns:
A bar data structure.


Macros:

#define OpenHA(s)
BarHA(TF_CURRENT,s).Open
#define HighHA(s)
BarHA(TF_CURRENT,s).High
#define LowHA(s)
BarHA(TF_CURRENT,s).Low
#define CloseHA(s)
BarHA(TF_CURRENT,s).Close
#define Volume(s)
Bar(TF_CURRENT,s).Volume
#define Year(s)
Bar(TF_CURRENT,s).Year
#define Month(s)
Bar(TF_CURRENT,s).Month
#define Day(s)
Bar(TF_CURRENT,s).Day
#define Date(s)
Bar(TF_CURRENT,s).Date
#define Time(s)   
Bar(TF_CURRENT,s).Time
#define IdHA(s)
((const char *) BarHA(TF_CURRENT,s).Id)



NBars ()

int
NBars (TimeFrame_t tf);

Returns the total number of bars for time frame tf.

tf:
The time frame.

Returns:
The total number of bars for time frame.




NVisibleBars ()

int
NVisibleBars (void);

Returns the number of visible bars on chart. Time frame is always the current.

Arguments:
None.

Returns:
The number of visible bars on chart.




NewestVisibleBar ()

int
NewestVisibleBar (void);

Returns the shift value of the newest visible bar. Time frame is always the current.

Arguments:
None.

Returns:
The shift value of the newest visible bar.




OldestVisibleBar ()

int
OldestVisibleBar (void);

Returns the shift value of the oldest visible bar. Time frame is always the current.

Arguments:
None.

Returns:
The shift value of the oldest visible bar.



Chart functions

Information about and manipulation of charts.

Synopsis

Color_t ChartBackColor
(void);
int ChartCurrentBar
(void);
TimeFrame_t ChartCurrentTF
(void);
Color_t ChartForeColor
(void);
int ChartHeight
(void);
int ChartWidth
(void);

Details

ChartBackColor ()

Color_t
ChartBackColor (void);

Returns the background color of the chart.

Arguments:
None.

Returns:
The background color of the chart.




ChartCurrentBar ()

int
ChartCurrentBar (void);

Returns the shift of the bar under the mouse pointer, or -1 if none.

Arguments:
None.

Returns:
The shift of the bar under the mouse pointer, or -1 if none.




ChartCurrentTF ()

TimeFrame_t ChartCurrentTF (void);

Returns the current time frame.

Arguments:
None.

Returns:
The current time frame of the chart.


Macros:
#define TF_CURRENT ChartCurrentTF ()



ChartForeColor ()

Color_t
ChartForeColor (void);

Returns the foreground color of the chart.

Arguments:
None.

Returns:
The foreground color of the chart.




ChartHeight ()

int
ChartHeight (void);

Returns the height of the chart in pixels.

Arguments:
None.

Returns:
The height of the chart in pixels.




ChartWidth ()

int
ChartWidth (void);

Returns the width of the chart in pixels.

Arguments:
None.

Returns:

The height of the chart in pixels.



Common functions

Commonly used functions.

Synopsis

String_t GetParentTitle
(void);
String_t GetModuleName (void);
int GetFractionalDigits
(double num);

Details

GetParentTitle ()

String_t
GetParentTitle (void);

Returns the title of the chart the module is attached on. On failure it disables the module's execution.

Arguments:
None.

Returns:
A string containing the title of the parent chart.




GetModuleName ()

String_t
GetModuleName (void);

Returns the module's name. On failure it disables the module's execution.

Arguments:
None.

Returns:
A string containing the current module's name.




GetFractionalDigits ()

int
GetFractionalDigits (double num);

Returns the number of significant digits after the decimal point. In Chart Geany 5 this is up to two for numbers greater than 1.0 and up to four for number between 0.0 and 1.0.

Arguments:
A double precision number.

Returns:
An integer containing the number of significant digits after the decimal point.



Input variable functions

This set of functions gives access to the object's properties dialog. An input variable appears in the object's properties dialog where the user can change its value. In conjunction with Event() and EV_INPUT_VAR signal, gives to the module the ability to interact with the user. Input variables must be of integer, double or Color_t type. Input variable functions must appear in Init(). The object type must be SUBCHART_OBJECT or CONTAINER_OBJECT, otherwise input variables are ignored.

Synopsis

void InputIntegerVariable
(const char *title,
int value,
int min,
int max,
int *var,
bool show);
void InputDoubleVariable
(const char *title,
double value,
double min,
double max,
double *var,
bool show);
void InputColorVariable
(const char *title,
Color_t value,
Color_t *var,
bool show);   

Details

InputIntegerVariable ()

void
InputIntegerVariable (const char *title,
int value,
int min,
int max,
int *var,
bool show);

Adds an integer property in the object's dialog box.

title:
Variable's title as it appears in the dialog.

value:
The default value.

min:
The minimum valid value.

max:
The maximum valid value.

var:
A pointer to the module's variable that keeps input variable's value.

show:
Set false to hide the variable from dialog, true otherwise.

Returns:
Nothing.

Since version 1.1


InputDoubleVariable

void
InputDoubleVariable (const char *title,
double value,
double min,
double max,
double *var,
bool show);


Adds a double precision property in the object's dialog box.

title:
Variable's title as it appears in the dialog.

value:
The default value.

min:
The minimum valid value.

max:
The maximum valid value.

var:
A pointer to the module's variable that keeps input variable's value.

show:
Set false to hide the variable from dialog, true otherwise.

Returns:
Nothing.

Since version 1.1


InputColorVariable

void
InputColorVariable (const char *title,
Color_t value,
Color_t *var,
bool show);   

Adds a color dialog property in the object's dialog box.

title:
Variable's title as it appears in the dialog.

value:
The default value.

var:
A pointer to the module's variable that keeps input variable's value.

show:
Set false to hide the variable from dialog, true otherwise.

Returns:
Nothing.

Since version 1.1

Object functions

This set of functions provides access to the attributes of a Chart Geany object. Drawing objects and technical indicators share the same attributes. If an attribute is not applicable for a specific type of object, it's simply ignored.

Synopsis

ObjectHandler_t AddCurve (void);
ObjectHandler_t AddHorizontalLine (void);
ObjectHandler_t AddLabel (void);
ObjectHandler_t AddText
(void);
ObjectHandler_t AddVerticalBars
(void);
ObjectHandler_t AddVerticalLine (const char *id);
void AttachText
(ObjectHandler_t handler,
const char *id,
double y);
Color_t GetColor
(ObjectHandler_t handler);
int GetPeriod
(ObjectHandler_t handler);
void SetColor
(ObjectHandler_t handler,
Color_t color);
void SetColorRGB
(ObjectHandler_t handler,
 int red,
 int green,
 int blue);
void SetFontSize
(ObjectHandler_t handler,
int fontsize);
void SetFontWeight
(ObjectHandler_t handler,
FontWeight_t fontweight);
void SetHAdjustment
(ObjectHandler_t handler,
TextAdjustment_t hadjustment);
void SetObjectType
(Object_t type);
void SetPeriod
(ObjectHandler_t handler,
int period);
void SetRefresh
(ObjectHandler_t handler);
void SetSubChartRange
(double min,
double max);
void SetSymbol
(ObjectHandler_t handler,
const char *symbol);
void SetText
(ObjectHandler_t handler,
String_t text);
void SetThickness
(ObjectHandler_t handler,
int thickness);
void SetVAdjustment
(ObjectHandler_t handler,
 TextAdjustment_t vadjustment);
void SetXY
(ObjectHandler_t handler,
int x,
int y);
void SetY
(ObjectHandler_t handler,
double y)
void ValueSet
(ObjectHandler_t handler,
double value,
int shift);

Details

AddCurve ()

ObjectHandler_t AddCurve
(void);

Adds a curve object as a child of the current object. Y axis values must be set using ValueSet(). It returns an object handler to the created object. On failure it disables the module's execution

Arguments:
None.

Returns:
An object handler to the created object.




AddHorizontalLine ()

ObjectHandler_t AddHorizontalLine (void);

Adds a horizontal line object as a child the current object. Line's position on Y axis can be set with SetY(). It returns an object handler to the created object. On failure it disables the module's execution.

Arguments:
None.

Returns:
An object handler to the created object.




AddLabel ()

ObjectHandler_t AddLabel (void);

Adds a label object as a child of the current object. It returns an object handler to the created object. On failure it disables the module's execution. Labels cannot be added on sub charts.

Arguments:
None.

Returns:
An object handler to the created object.




AddText ()

ObjectHandler_t AddText (void);

Adds a trailing text object as a child of the current object, attached on the  bar with shift equal to 0, at close price level. It returns an object handler to the created object. On failure it disables the module's execution.Trailing text objects cannot be added on sub charts.

Arguments:
None.

Returns:
An object handler to the created object.




AddVerticalBars ()

ObjectHandler_t AddVerticalBars (void);

Adds vertical bars object as a child of the current object. The current object must be a sub chart, otherwise it fails and disables the module's execution. It returns an object handler to the created object.

Arguments:
None.

Returns:
An object handler to the created object.




AddVerticalLine ()

ObjectHandler_t AddVerticalLine (const char *id);

Adds a vertical line object as a child of the current object. Line's position is determined by id. It returns an object handler to the created object. On failure it disables the module's execution.


Arguments:
id as it is returned by Id(s).

Returns:
An object handler to the created object.


Since version 1.1


AttachText ()

void
AttachText (ObjectHandler_t handler,
const char *id,
double y);

Adds a trailing text object as a child of handler. The object's X coordinate is determined by argument id as it is returned by Id(s). The Y coordinate (aka price) is determined by argument y. 

handler:
The handler of the parent object or NULL if the parent is the current object.

id:
The id of the bar the object is attached on.

y:

The Y coordinate, means the price.
Returns:
Nothing




GetColor ()

Color_t GetColor (ObjectHandler_t handler);

Returns the color of handler. If handler is NULL, it returns the color of the current object.

handler:
A child object handler or NULL for the current object.

Returns: The color of handler.


Since version 1.2

GetPeriod ()

int
GetPeriod (ObjectHandler_t handler);

Returns the period of handler. If handler is NULL, it returns the period of the current object.

handler:
A child object handler or NULL for the current object.

Returns:
The period of handler.




SetColor ()

void
SetColor
(ObjectHandler_t handler,
Color_t color);

Set's the color of handler. If handler is NULL, sets the color of the current object.

handler:
A child object handler or NULL for the current object.

color:
A color definition.

Returns:
Nothing.




SetColorRGB ()

void
SetColorRGB
(ObjectHandler_t handler,
 int red,
 int green,
 int blue);

Set's the color of the handler using red, green and blue. If handler is NULL, sets the color of the current object.

handler:
A child object handler or NULL for the current object.

red:
Code for red.

green:
Code for green.

blue:
Code for blue.

Returns:
Nothing.




SetFontSize ()

void
SetFontSize (ObjectHandler_t handler,
int fontsize);

Set's the font size of handler. If handler is NULL, sets the font size of the current object. Minimum font size is 7 and maximum is 24. Below or above the limits, font size is adjusted accordingly.

handler:
A child object handler or NULL for the current object.

fontsize:
The font size.

Returns:
Nothing.




SetFontWeight ()

void
SetFontWeight (ObjectHandler_t handler,
FontWeight_t fontweight);

Set's the font weight of handler. If handler is NULL, sets the font weight of the current object.

handler:
A child object handler or NULL for the current object.

fontweight:
The font weight.

Returns:
Nothing.




SetHAdjustment ()

void
SetHAdjustment (ObjectHandler_t handler,
TextAdjustment_t hadjustment);

Sets the horizontal adjustment of handler. If handler is NULL, sets the horizontal adjustment of the current object. Handler must point to a label or text object.

handler:
A child object handler or NULL for the current object.

hadjustment:
The horizontal adjustment attribute.

Returns:
Nothing.




SetObjectType ()

void
SetObjectType (Object_t type);

Sets the object type of the current object. It must appear in Init().

type:
The type of the current object.

Returns:
Nothing.




SetPeriod ()

void
SetPeriod (ObjectHandler_t handler,
int period);

Sets the period of handler. If handler is NULL, sets the period of the current object.

handler:
A child object handler or NULL for the current object.

period:
The period.

Returns:
Nothing.




SetRefresh ()

void
SetRefresh (ObjectHandler_t handler);

Imposes NeedsUpdate() to return true when is called the next time.

handler:
A child object handler or NULL for the current object.

Returns:
Nothing.




SetSubChartRange ()

void
SetSubChartRange (double min,
double max);

Sets the minimum and maximum value of the Y axis of a sub chart object. Default values are 0 and 100. If minimum or  maximum values cannot be determined in advance, DBL_MIN and DBL_MAX must be used.

min:
Minimum value of Y, or DBL_MIN. Default is 0.

max:

Maximum value of Y, or DBL_MAX. Default is 100.
Returns:
Nothing.




SetSymbol ()

void
SetSymbol (ObjectHandler_t handler,
const char *symbol);

Set's the symbol as content of a text or label object. If handler is NULL, it refers to the current object.

handler:
A child object handler or NULL for the current object.

symbol:
The symbol.

Returns:
Nothing.




SetText ()

void
SetText (ObjectHandler_t handler,
String_t text);

Set's the text as content of a text or label object. if handler is NULL, it refers to the current object.

handler:
A child object handler or NULL for the current object.

text:
The text.

Returns:
Nothing.




SetThickness ()

void
SetThickness (ObjectHandler_t handler,
int thickness);

Determines how thick a curve, dot or bar object will be. Minimum is 1 and maximum is 4. Values outside the limits will be adjusted within the limits. Default is 1. If handler is NULL, it refers to the current object.

handler:
A child object handler or NULL for the current object.

thickness:
The width of the object.

Returns:
Nothing.




SetVAdjustment ()

void
SetVAdjustment (ObjectHandler_t handler,
TextAdjustment_t vadjustment);

Sets the vertical adjustment of handler. If handler is NULL, sets the vertical adjustment of the current object. Handler must point to a label or text object.

handler:
A child object handler or NULL for the current object.

vadjustment:
The vertical adjustment attribute.

Returns:
Nothing.




SetXY ()

void
SetXY (ObjectHandler_t handler,
int x,
int y);

Sets the X and Y coordinates of a text or label object. If handler is NULL, sets the coordinates of the current object.

handler:
A child object handler or NULL for the current object.

x:
The X coordinate.

y:

The Y coordinate.
Returns:
Nothing.




SetY ()

void
SetY (ObjectHandler_t handler,
int y);


Sets the Y coordinate of a text or label object. SetPrice() and SetLevel() are synonyms of this function. If handler is NULL, sets the Y coordinate of the current object.

handler:
A child object handler or NULL for the current object.

y:

The Y coordinate.
Returns:
Nothing.




ValueSet ()

void
ValueSet
(ObjectHandler_t handler,
double value,
int shift);

In CGScript, each object has a value set. The value set is actually an array of double with dimension equal to the number of bars of the current time frame. This array is used for the Y values of bar, dot and curve objects. ValueSet() set's the value of the element indicated by shift, for the object pointed by handler. A NULL value for handler has the meaning of the current object.

handler:
A child object handler or NULL for the current object.

value:
The value of element indicated by shift.

shift:

An index relative to the most recent bar of current time frame. Valid values between 0 and NBars (TF_CURRENT) - 1. A value outside the limits is ignored.

Returns:
Nothing.



String functions

CGScript supports its own string type along with the standard ISO C99 null terminated string. Usage of CGScript strings is recommended for most applications.

Synopsis

String_t StrCpy
(String_t dst,
String_t src);
String_t Cstr2Str
(String_t dst,
const char *src);
String_t StrCat
(String_t dst,
String_t src);
char *Str2NCstr
(char *cstr,
String_t str,
size_t n);
size_t StrLen
(String_t str);
int StrCmp
(String_t str1,
String_t str2);
int StrCstrCmp
(String_t str1,
const char *cstr2);
const char *Str2Cstr
(String_t str);
String_t StrInit
(const char *cstr);

Details

StrCpy ()

String_t
StrCpy
(String_t dst,
String_t src);

Copies src to dst and returns dst. On failure, it returns NULL.

dst:
The destination string.

src:
The source string.

Returns:
dst or NULL on failure.




Cstr2Str ()

String_t
Cstr2Str (String_t dst,
const char *src);

Copies src to dst and returns dst. On failure, it returns NULL.

dst:
The destination string.

src:
The source null terminated string.

Returns:
dst or NULL on failure.




StrCat ()

String_t
StrCat (String_t dst,
String_t src);

Appends src to dst and returns dst. On failure, it returns NULL.

dst:
The destination string.

src:
The source string.

Returns:
dst or NULL on failure.




Str2NCstr ()

char * Str2NCstr (char *cstr,
String_t str,
size_t n);

Copies at most n bytes from str to cstr. It returns a pointer to cstr.

cstr:
The destination string.

src:
The source string.

n:
The maximum number of bytes to be copied.

Returns:
A pointer to null terminated c string.




StrLen ()

size_t
StrLen (String_t str);

Calculates the length of a string.

str:
A string.

Returns:
The number of bytes in the string str.




StrCmp ()

int
StrCmp (String_t str1,
String_t str2);

Compares two strings. It returns an integer less than, equal to, or greater than zero if str1 is found, respectively, to be less than, to match, or be greater than str2.

str1:
The first string of the comparison.

str2:
The second string of the comparison.

Returns:
if (str1 < str2), it returns a value less than zero.
if (str1 > str2), it returns a value greater than zero.
if (str1 == str2), it returns zero.




StrCstrCmp ()

int
StrCstrCmp (String_t str1,
const char *cstr2);

Compares a string with a null terminated c string. It returns an integer less than, equal to, or greater than zero if str1 is found, respectively, to be less than, to match, or be greater than cstr2.

str1:
The first string of the comparison.

cstr2:
The second string of the comparison.

Returns:
if (str1 < cstr2), it returns a value less than zero.
if (str1 > cstr2), it returns a value greater than zero.
if (str1 == cstr2), it returns zero.




Str2Cstr ()

const char * Str2Cstr (String_t str);

Converts a string to a null terminated c string.


str:
The string to be converted.

Returns:
A pointer to a null terminated string.




StrInit ()

String_t StrInit (const char *cstr);

Initializes a string with the contents of a c string. On failure, it returns NULL.

cstr:
The null terminated c string to be used as initial content for string.

Returns:
A string, or NULL on failure.



Technical analysis functions

There are two families of technical analysis functions. The 'f' family, where the input comes from current object's value set and the 'g' family where the input is an array of double. All technical analysis functions return an array of double.

Synopsis

Array_t fADX
(TimeFrame_t tf,
int period);
Average directional movement index
Array_t fATR
(TimeFrame_t tf,
int period);
Average true range
Array_t fAROONUP
(TimeFrame_t tf,
int period);
Aroon up
Array_t fAROONDOWN
(TimeFrame_t tf,
int period);
Aroon down
Array_t fBBANDSUPPER
(TimeFrame_t tf,
int period,
Price_t appliedprice);
Bollinger bands upper
Array_t fBBANDSLOWER
(TimeFrame_t tf,
int period,
Price_t appliedprice);
Bollinger bands lower
Array_t fBBANDSMIDDLE
(TimeFrame_t tf,
int period,
Price_t appliedprice);
Bollinger bands middle
Array_t fCCI
(TimeFrame_t tf,
int period);
Commodity channel index
Array_t fDMX
(TimeFrame_t tf,
int period);
Directional movement index
Array_t fEMA
(TimeFrame_t tf,
int period,
Price_t appliedprice);
Exponential moving average
Array_t fSTOCHSLOWK
(TimeFrame_t tf,
int period);
Slow stochastic %K
Array_t fSTOCHSLOWD
(TimeFrame_t tf,
int period);
Slow stochastic %D
Array_t fSTOCHFASTK
(TimeFrame_t tf,
int period);
Fast stochastic %K
Array_t fSTOCHFASTD
(TimeFrame_t tf,
int period);
Fast stochastic %D
Array_t fMACD
(TimeFrame_t tf,
int period,
Price_t appliedprice);
Moving average convergence/divergence
Array_t fMACDSIGNAL
(TimeFrame_t tf,
int period,
Price_t appliedprice);
Moving average convergence/divergence signal
Array_t fMACDHIST
(TimeFrame_t tf,
int period,
Price_t appliedprice);
Moving average convergence/divergence histogram
Array_t fMFI
(TimeFrame_t tf,
int period);
Money flow index
Array_t fMOMENTUM
(TimeFrame_t tf,
int period,
Price_t appliedprice);
Momentum
Array_t fPSAR
(TimeFrame_t tf,
int period);
Parabolic SAR
Array_t fROC
(TimeFrame_t tf,
int period,
Price_t appliedprice);
Rate of change
Array_t fRSI
(TimeFrame_t tf,
int period,
Price_t appliedprice);
Relative strength index
Array_t fSMA
(TimeFrame_t tf,
int period,
Price_t appliedprice);
Simple moving average
Array_t fSTDDEV
(TimeFrame_t tf,
int period,
Price_t appliedprice);
Standard deviation
Array_t fWILLR
(TimeFrame_t tf,
int period);
Williams %R
Array_t fLINEARREG (TimeFrame_t tf,
int period,
Price_t appliedprice);
Linear regression
Array_t fLINEARREG_INTERCEPT (TimeFrame_t tf,
int period,
Price_t appliedprice);
Linear regression intercept
Array_t fLINEARREG_SLOPE (TimeFrame_t tf,
int period,
Price_t appliedprice);
Linear regression slope
Array_t gEMA
(int period,
Array_t data)
Exponential moving average
Array_t gSMA (int period,
Array_t data)
Simple moving average
Array_t gSTDDEV (int period,
Array_t data)
Standard deviation
Array_t gLINEARREG (int period,
Array_t data)
Linear regression
Array_t gLINEARREG_INTERCEPT (int period,
Array_t data)
Linear regression intercept
Array_t gLINEARREG_SLOPE (int period,
Array_t data)
Linear regression slope