# Public and for release, CBEC v3.2.06 Build 4255+ [16th November, 2025] # # Copyright(c) Ivyware Pty Ltd 2018-25 (all rights reserved) # MELBOURNE, VICTORIA, AUSTRALIA, 3000 # # This file is provided as-is by Ivyware Pty Ltd. No claims are made # as to fitness for any particular purpose. No warranties of any kind # are expressed or implied. The recipient agrees to determine # applicability of information provided. # # Ivyware hereby grants the right to freely use the information # supplied in this file for the creation of Expert Advisor products # supporting the Chartboard Application, and to make copies of this # file in any form for internal or external distribution as long as # this notice remains attached. # # No waranty or suitability for purpose is implied. # # Simple Python Advisor script that uses MACD, RSI and ADX indicators # NOTES: Download latest version from # https://www.ivyware.com.au/PythonScripts/Sample[MACD]-[RSI]-[ADX].pya # : Download latest version of the Chartboard Extension Classes # (PythonCBEC.pyw) upon which this script is based from # https://www.ivyware.com.au/PythonScripts/PythonCBEC.pyw # : Script can be run over any OHLCvs type chart stack, necessary # charts, indicators and overlays that are absent will be auto # generated, calculated and displayed whilst this script is # active. Such will be cleaned up upon completion, or new script # activation, returning OHLCvs chart stack to its original state. # Refer [Ribbon Bar >> Advisors Tab >> {Cancel}] button # : OHLCvs chart scripts can be run over chart stacks generated and # activated through the Scanning results window. # : Simply drag this script from either "Windows File Explorer" or # "Python Scripts Explorer" and drop over any OHLCvs type chart stack. # It then runs automatically and becomes the default [Ribbon Bar # >> Advisors Tab >> {Run Local}] script. # : Based upon Chartboard Extension Classes (CBEC) shipped with # Chartboard product # : Requirement is for phython 3.8 to be installed. # : Can be enhanced as circumstances dictate. However, it should be # be renamed given each successive Chartboard update over-writes this # file. # ***: Script under development and subject to change without notice*** # import sys sys.path.insert(0, 'C:\\Program Files\\Chartboard\\PythonScripts') # CBEC directory from PythonCBEC import * import ctypes # An included library with Python install. from datetime import datetime from PYCB import PYCB_return from PYCB import PYCB_error # # Environment variables # NOTES: List of current environment variables suitable for debuuging import os print ( 'Python environment') for param in os.environ.keys(): print ( "%20s %s" % (param,os.environ[param]) ) # # STEP 1 - Establish Root from which all other objects are descendants # NOTES: Effectively the Chartboard application itself. This script # is coded to run on CViewOHLCvs type views only. # : The parent window 'CView' refers to collection of tabbed views # in the centre of the application, 'this' refers to current # Chartboard view with focus from which script has been activated. oCRoot = CRoot(); if oCRoot.CWndType('CView','this') != 'CStackOHLCvs': raise PYCB_error ( "Script only supports CStackOHLCvs views, not " + oCRoot.CWndType('CView','this') ) # # STEP 2 - Establish CView # NOTES: CView is the parent of all the tabbed views. Effectively # the View tab under which this python script is running and # identified by the 'this' tag # : CViews can be of many types (CViewOHLCvs, CViewPFigure, CScanOHLCvs etc) # : Confirm 'CViewOHLCvs' type view, script only supports such oCView = oCRoot.CViewFactory('this'); if not oCView.IsCViewOHLCvs(): raise PYCB_error ( "Script only supports CViewOHLCvs type CView's, not " + oCView.sType ) #oCView.SetPUnits(PUNITS_Month) # Don't do this, keeps reverting to month # # STEP 3 - Establish the CViewOHLCvs object # NOTES: Must be a OHLCvs type View tab for this script to work. Fails for # all other view types. Script may only access single Chart tab. # : Chart Period Units controlled by the current Ribbon Bar >> Chart Group # >> Chart Period Units tab selection. Alternatively this value may be # over-ridden via PUnits_Year, Quarter, Month, Week or Day definition # from PythonCBEC.pyw # : Calculations are performed and values persist concurrently for all # Period Units. Hence, state for alternative periods can be compared. oCViewOHLCvs = oCRoot.CViewFactory('this'); if not oCViewOHLCvs.IsCViewOHLCvs(): raise PYCB_error ( "Script only supports OHLCvs views, not " + oCViewOHLCvs.sCViewType ) oCViewOHLCvs.PaintEoD(1) # Activates visual updates # # STEP 4 - Access the CStackOHLCvs object within oCViewOHLCvs # NOTES: The CViewOHLCvs tab can add synthetic charts and overlays as # required via the Prerequisites method called within this script. # : Chart reference order is respected. # : Some of the Prerequisites are for demonstration and cosmetic # purposes only. Firstly the chart stack is defined as a list of # prerequisites. # : Stack type MUST identify as 'CStackOHLCvs' oCStackOHLCvs = oCViewOHLCvs.CStackFactory() if not oCStackOHLCvs.IsOHLCvs(): raise PYCB_error ( "Script only supports OHLCvs stacks, not " + oCStackOHLCvs.sStackType ) print ( 'CStackOHLCvs Period Units:' + oCStackOHLCvs.sPUnits) print ( 'CStackOHLCvs Stock Code:' + oCStackOHLCvs.StockCode()) print ( oCStackOHLCvs ) print ( 'CStackOHLCvs Operative Time=' + str(oCStackOHLCvs.DATE()) ) print ( 'CStackOHLCvs Period Units=' + str(oCStackOHLCvs.nPUnits) ) # # STEP 5 - Configuration of Charts and Overlays within stack # NOTES: Chart decorations that may or may not be referenced by advisor logic # : Secondly the Prerequisite overlays are defined for each chart. # : Prerequisites method ensures Chart and Overlay existence within # the CView tab. If absent they are created, calculated and displayed. # Then discarded upon script displacement or cancellation. Refer # RibbonBar >> Advisors tab >> {Cancel} button for further details. # # STEP 5.1 - MACD Chart test # NOTES: Logically MACD chart needs to exist within the chart stack oCStackOHLCvs.Prerequisites('MACD') if oCStackOHLCvs.ChartExists('MACD'): print ('Testing MACD chart components') oChartMACD = oCStackOHLCvs.ChartFactory('MACD') # # Testing DSeriesMACD print ('Testing MACD DSeries components') oDSeriesMACD = oChartMACD.DSeriesFactory('MACD') print('EMA1='+str(oDSeriesMACD.iEMA1periods)) print('EMA2='+str(oDSeriesMACD.iEMA2periods)) print('Signal='+str(oDSeriesMACD.iSignalperiods)) print('Value-MACD='+str(oDSeriesMACD.GetValue_d("MACD",0,0))) print('Value-Signal='+str(oDSeriesMACD.GetValue_d("MACDsignal",0,0))) # # Testing DragTag upon chart oDrawTag = DrawTag(oChartMACD,oDSeriesMACD) oDrawTag.DoDrawTag(PUNITS_Week,'Buy','Buy tag message') # # STEP 5.2 - RSI Chart test # NOTES: Logically RSI chart needs to exist within the chart stack oCStackOHLCvs.Prerequisites('RSI') if oCStackOHLCvs.ChartExists('RSI'): print ('Testing RSI chart components') oChartRSI = oCStackOHLCvs.ChartFactory('RSI') # # Testing DSeriesRSI oDSeriesRSI = oChartRSI.DSeriesFactory('RSI') print('RSIperiods='+str(oDSeriesRSI.iRSIperiods)) print('OBought='+str(oDSeriesRSI.iOBought)) print('OSold='+str(oDSeriesRSI.iOSold)) print('Value-RSI='+str(oDSeriesRSI.GetValue_d("RSI",0,0))) # # Step 5.3 - ADX Chart test # NOTES: Logically ADX chart needs to exist within the chart stack oCStackOHLCvs.Prerequisites('ADX') if oCStackOHLCvs.ChartExists('ADX'): print ('Testing ADX chart components') oChartADX = oCStackOHLCvs.ChartFactory('ADX') # # Testing DSeriesADX oDSeriesADX = oChartADX.DSeriesFactory('ADX') print('ADXperiods='+str(oDSeriesADX.iADXperiods)) print('Value-ADX='+str(oDSeriesADX.GetValue_d("ADX",0,0))) print('Value-DIplus='+str(oDSeriesADX.GetValue_d("DIplus",0,0))) print('Value-DIminus='+str(oDSeriesADX.GetValue_d("DIminus",0,0))) ################################################# # Process DSeries bars for weekly and daily period units # NOTES: DSeries capable of looking upwards by not downwards # with respect to period units oCStackOHLCvs.PYCB_ShadeBarClear(0); oCStackOHLCvs.PYCB_ShadeBarMask(0,(1< 20.0 and dDIplus is not None and dDIplus > 20.0 and dRSI is not None and dRSI > 40.0 and dMACDsignal_P1 is not None and dMACD_P1 is not None and dMACDsignal_P1 < dMACD_P1 and dMACDsignal is not None and dMACD is not None and dMACDsignal < dMACD ): oCStackOHLCvs.PYCB_ShadeBarUpdate(PUNITS_Week,0,SBTYPE_Bullish,11) # # Daily buy signals # NOTES: Evaluates buy shade bars based on daily period data # : Step(PUNITS_Day,1) iterates over all DSets by one day if oCStackOHLCvs.ChartExists('ADX') and oCStackOHLCvs.ChartExists('RSI') and oCStackOHLCvs.ChartExists('MACD') : oCStackOHLCvs.Rewind() # All DSets rewind to start while oCStackOHLCvs.Step(PUNITS_Day,1): dADX = oDSeriesADX.GetValue_d('ADX',PUNITS_Day,0) dDIplus = oDSeriesADX.GetValue_d('DIplus',PUNITS_Day,0) dRSI = oDSeriesRSI.GetValue_d('RSI',PUNITS_Day,0) dMACDsignal = oDSeriesMACD.GetValue_d('MACDsignal',PUNITS_Day,0) dMACDsignal_P1 = oDSeriesMACD.GetValue_d('MACDsignal',PUNITS_Day,1) dMACD = oDSeriesMACD.GetValue_d('MACD',PUNITS_Day,0) dMACD_P1 = oDSeriesMACD.GetValue_d('MACD',PUNITS_Day,1) if ( dADX is not None and dADX > 20.0 and dDIplus is not None and dDIplus > 20.0 and dRSI is not None and dRSI > 40.0 and dMACDsignal_P1 is not None and dMACD_P1 is not None and dMACDsignal_P1 < dMACD_P1 and dMACDsignal is not None and dMACD is not None and dMACDsignal < dMACD ): oCStackOHLCvs.PYCB_ShadeBarUpdate(PUNITS_Day,0,SBTYPE_Bullish,11) #ADX(14) > 20 AND #DIP(14) > 20 AND RSI(CLOSE, 14) > 40 AND #REF(MACDSignal(13, 26, 9, SIMPLE), 1) < REF(MACD(13, 26, 9, SIMPLE), 1) #AND MACDSignal(13, 26, 9, SIMPLE) < MACD(13, 26, 9, SIMPLE)