Sergey Kosinsky Personal web site

 

Pyton bindings for Metastock.

 

Python is a pretty good scripting language that can be used to write complex trading strategies rapidly.

MsxPython is a Metastock extention dll (MSX dll) that works as a bridge between the python interpreter and Metastock charting application.

 

A well known RSI indicator in python language looks very simple and is clean to understand:

 

import msx

def msx_rsi(handle, bar_count):
    """Usage: pyRSI(Price[], Period)"""
	
    #get script argument values
    period = msx.get_argument(handle, 1)
    
    #initialize local variables
    gain = 0.0
    loss = 0.0

    #calculate first gain\loss 
    for i in range(1, period): 
		
        price_change = msx.get_argument(handle, 0, i) - msx.get_argument(handle, 0, i - 1)

        if price_change > 0:
            gain = gain + price_change
	                                
        if price_change < 0:
            loss = loss - price_change

    gain = gain / (period - 1)
    loss = loss / (period - 1) 
        
    #pass data back to MSX dll
    if loss == 0:
        rsi = 100
    else:
        rs = gain / loss
        rsi = 100 - 100 /(1 + rs)
     
    msx.set_result(handle, i, rsi)
             
    #do processing for every other bars    
    for i in range(period, bar_count):
        
        price_change = msx.get_argument(handle, 0, i) - msx.get_argument(handle, 0, i - 1)
        
        if price_change > 0:
            gain = (gain * (period - 1) + price_change) / period
        else:    
            gain = (gain * (period - 1)) / period
        
        if price_change < 0:   
            loss = (loss * (period - 1) - price_change) / period
        else:
            loss = (loss * (period - 1)) / period
            		
        if loss == 0:
            rsi = 100
        else:
            rs = gain / loss
            rsi = 100 - 100 / (1 + rs)

        #pass data back to MSX dll
        msx.set_result(handle, i, rsi)

    return 

msx_* reference

Function msx_*(handle, bar_count)

The function gets in 2 arguments. Handle is a security data handle, bar_count is a number of data bars of any data array type: either OHLCV price data or an argument of data array type or the output array. Those arguments are passed to MSX dll by Metastock.

The function should scroll through all bars and calculate output value for every bar. Any output values are sent back with msx.set_result() function.

Python doc string has special meaning. It defines function name and argument list to be exported to Metastock. "Usage: " should be in the front of a doc string. Every argument has a type modifier using the following convention: data array type ends with "[]" square brackets, string type includes argument name into quote characters and numeric type has no type modifier.

import msx

def msx_example(handle, bar_count):
    """Usage: Exampe(Price[], Period)"""

    for bar_index in range(bar_count):
        #do some processing for every bar
        open  = msx.get_open(handle, bar_index)
        close = msx.get_close(handle, bar_index)
        value = (open - close)/2 + close
        #pass data back to MSX dll
        msx.set_result(handle, bar_index, value)         
    return

 

If msx_* function needs price data or gets arguments from Metastock then it should call a msx.* access function.

 

Module msx

The built in module provides access to Metastock security price data and arguments passed to MSX dll by Metastock. The module has the following functions:

 

function
returned type
security price data access
msx.get_date(handle, bar_index) numeric
msx.get_time(handle, bar_index) numeric
msx.get_open(handle, bar_index) numeric
msx.get_high(handle, bar_index) numeric
msx.get_low(handle, bar_index) numeric
msx.get_close(handle, bar_index) numeric
msx.get_vol(handle, bar_index) numeric
msx.get_oi(handle, bar_index) numeric
script argument list access
msx.get_argument(handle, arg_index, bar_index) numeric
msx.get_argument(handle, arg_index) numeric/string
common security data access
msx.get_symbol(handle) string
msx.get_period(handle) string
msx.get_interval(handle) numeric
msx.get_start_time(handle) numeric
msx.get_end_timel(handle) numeric
output array access  
msx.set_result(handle, bar_index, value)  

 

 

- handle is a security data handle that was passed to msx_*() function;

- bar_index is an index to access the specified value in a data array. The index is zero based and should not exceed bar_count - 1 value;

- arg_index is a zero based position of an argument in the msx function argument list.

 

How to use

Installation guide.

Download and install MsxPython-v1.0-RELEASE-Setup.zip into Metastock home folder "C:\Program Files\Equis\MetaStock". Run Metastock and create a custom pyRSI indicator with the Metastock formula wizard:

 

{Usage: ExtFml( "MSXPython.pyRSI", Close[], Period)}
Period:=Input("Period", 1, 50, 14);
ExtFml( "MSXPython.pyRSI", C, Period); 
      

Drag and drop pyRSI indicator on a chart. Drag and drop Metastock's Relative Strange Index indicator on the chart . You will see a comparison chart of pyRSI and the buit-in RSI indicator.

Just a note. If you are not familiar with python language, you would rather not try to use the dll.

Experienced users can download and manually install advanced MSXPyhon.py script that contains implementation of pyJMA function based on WLD/C# code http://pastie.org/2484888.

Comparison pyJMA vs msxJMA:

 

Visit my feedback page to ask a question.

/ksr
Sergey R. Kosinsky,
СанктSaint-Petersburg, Russia.
Created: Feb 8, 2011

Last update : September 26, 2013

www.000webhost.com