Script conversion - TradeStation, MultiCharts, MetaTrader, NinjaTrader, Neuroshell, R...

When starting with Zorro, you often have used another trade platform or computing environments before, and would like to take over your familiar strategies, indicators, and algorithms. The examples below show how to convert the code of different platforms.

TradeStation™ and MultiCharts™

TradeStation was the first platform that supported automated trading. Its EasyLanguage™, also used by the MultiCharts platform, has a similar design philosophy as Zorro's lite-C. Although its syntax is a C/Pascal mix and requires some code modifications, conversion to C is relatively straightforward. EasyLanguage Vars are equivalent to lite-C data series. However, EasyLanguage makes no distinction between series and variables - f.i. myseries is the same as myseries[0]. In most cases, Vars is only a single variable (var), but sometimes it's a series (vars), as movAvgVal in the second example below. Easylanguage also stores the content of variables - if this is required, declare a static var in lite-C. Trigonometric functions expect angles in degrees, while in lite-C and most other languages angles are in radians. EasyLanguage has no native functions, but separate scripts can be called like functions. Aside from that, EasyLanguage strategies and lite-C strategies look very similar.

{ Easylanguage version }
{ Choppy Market Index Function by George Pruitt }
Inputs: periodLength(Numeric); Vars: num(0),denom(1);
if(periodLength <> 0) then
begin
denom = Highest(High,periodLength) – Lowest(Low,periodLength);
num = Close[periodLength-1] – Close;
ChoppyMarketIndex = 0.0;
if(denom <> 0) then ChoppyMarketIndex = AbsValue(num/demon)*100;
end;
// lite-C version
// Choppy Market Index Function by George Pruitt
var ChoppyMarketIndex(int periodLength) { if(periodLength != 0) {
var denom = HH(periodLength) – LL(periodLength);
var num = priceClose(periodLength-1) – priceClose();
if(denom != 0) return abs(num/denom)*100; } return 0;
}
{ Easylanguage version }
{ enter a trade when the RSI12 crosses over 75 or under 25 }
Inputs:  
    Price(Close),LengthLE(12),LengthSE(12),
    OverSold(25),OverBought(75),StoplossPips(200),ProfitTargetPips(200);
 
variables:  
    var0(0),var1(0);
 
{ get the RSI series }
var0 = RSI( Price, LengthLE );
var1 = RSI( Price, LengthSE );
 
{ if rsi crosses over buy level, exit short and enter long }
condition1 = Currentbar > 1 and var0 crosses over OverBought ;
if condition1 then                                                                    
    Buy( "RsiLE" ) next bar at market;
 
{ if rsi crosses below sell level, exit long and enter short }
condition2 = Currentbar > 1 and var1 crosses under OverSold ;
if condition2 then                                                                    
    Sell Short( "RsiSE" ) next bar at market;
 
{ set up stop / profit levels }
SetStoploss(StoplossPips);
SetProfitTarget(ProfitTargetPips);
// Zorro version
// enter a trade when the RSI12 crosses over 75 or under 25
function run()
{
// get the RSI series
  vars Close = series(priceClose());
  vars rsi12 = series(RSI(Close,12));
 
// set up stop / profit levels
  Stop = 200*PIP;
  TakeProfit = 200*PIP;
 
// if rsi crosses over buy level, exit short and enter long
  if(crossOver(rsi12,75))
    reverseLong(1);
// if rsi crosses below sell level, exit long and enter short
  if(crossUnder(rsi12,25))
    reverseShort(1);
}

MetaTrader4™ (MT4)

MT4 is the most popular platform for private traders and supported by most brokers. The MQL4™ script language of its "Expert Advisors" (EAs) is based on C, which would theoretically allow easy conversion to Zorro's lite-C. Unfortunately, MT4 has many issues that make "Expert Advisors" a lot more complex and difficult to handle than scripts of other platforms.
  The MQL4 main script runs at every tick; therefore the last price candle is normally incomplete and affects indicators in a different way than the other candles. Trades are not managed by the platform, but must be managed by code in the script. Series are not natively supported, but emulated with loops and functions. Indicators often produce different results in MT4 than in other platforms because the MT4 standard indicators do not use their standard algorithms, but special MT4 variants. Time zones and account parameters are not normalized, so EAs must be individually adapted to the broker. To complicate matters further, MT4 does not use common trade units such as lots and pips, but calculates with "standard lots" and "points" that need to be multiplied with account-dependent conversion factors. Most code in an EA is therefore not used for the trade algorithm, but for working around all those problems. This results in the long and complex 'spaghetti code' that is typical for EAs.
  For conversion, first remove the MQL4 specific code that is not needed in lite-C, such as trade management loops, broker dependent pip and trade size calculations, and array loops that emulate series. Then the rest can be converted by replacing the MQL4 indicators and trade commands by their lite-C equivalents. Note that the result can still differ due to the effects of incomplete candles and different indicator algorithms.

// MT4 version
// enter a trade when the RSI12 crosses over 75 or under 25
int start()
{
// get the previous and current RSI values
   double current_rsi = iRSI(Symbol(), Period(), 12, 
     PRICE_CLOSE, 1); // mind the '1' - candle '0' is incomplete!!
   double previous_rsi = iRSI(Symbol(), Period(), 12, PRICE_CLOSE, 2);
 
// set up stop / profit levels
   double stop = 200*Point;
   double takeprofit = 200*Point;

// correction for prices with 3, 5, or 6 digits
   int digits = MarketInfo(Symbol(), MODE_DIGITS);
if (digits == 5 || digits == 3) {
stop *= 10; takeprofit *= 10; } else if (digits == 6) {
stop *= 100; takeprofit *= 100; } // find the number of trades int num_long_trades = 0; int num_short_trades = 0; int magic_number = 12345;
// exit all trades in opposite direction for(int i = 0; i < OrdersTotal(); i++) { // use OrderSelect to get the info for each trade if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; // Trades not belonging to our EA are also found, so it's necessary to // compare the EA magic_number with the order's magic number if(magic_number != OrderMagicNumber()) continue; if(OrderType() == OP_BUY) { // if rsi crosses below sell level, exit long trades if((current_rsi < 25.0) && (previous_rsi >= 25.0)) OrderClose(OrderTicket(), OrderLots(), Bid, 3, Green); else // otherwise count the trades num_long_trades++; } if(OrderType() == OP_SELL) { // if rsi crosses over buy level, exit short trades if((current_rsi > 75.0) && (previous_rsi <= 75.0)) OrderClose(OrderTicket(), OrderLots(), Ask, 3, Green); else // otherwise count the trades num_short_trades++; } } // if rsi crosses over buy level, enter long if((current_rsi > 75.0) && (previous_rsi <= 75.0) && (num_long_trades == 0)) { OrderSend(Symbol(), OP_BUY, 1.0, Ask, 3, Ask-stop, Bid+takeprofit, "", magic_number, 0, Green); } // if rsi crosses below sell level, enter short if((current_rsi < 25.0) && (previous_rsi >= 25.0) && (num_short_trades == 0)) { OrderSend(Symbol(), OP_SELL, 1.0, Bid, 3, Bid+stop, Ask-takeprofit, "", magic_number, 0, Green); } return(0); }
// Zorro version
// enter a trade when the RSI12 crosses over 75 or under 25
function run()
{
// get the RSI series
  vars Close = series(priceClose());
  vars rsi12 = series(RSI(Close,12));
 
// set up stop / profit levels
  Stop = 200*PIP;
  TakeProfit = 200*PIP;
 
// if rsi crosses over buy level, exit short and enter long
  if(crossOver(rsi12,75))
    reverseLong(1);
// if rsi crosses below sell level, exit long and enter short
  if(crossUnder(rsi12,25))
    reverseShort(1);
}
Under Tips & Tricks you can find an example how to emulate the MQL4-style indicator parameters with Zorro.
 

NinjaTrader™

NinjaScript™ is based on C# and thus similar in syntax to Zorro's lite-C. NinjaScript also supports data series in the same way as lite-C, and its basic function list is very similar; this makes script migration rather easy. One major difference is that all NinjaTrader indicator functions return data series, while Zorro indicators return single values. Use the series function (f.i. series(indicator(..))) for making Zorro indicators also return series.

// NinjaTrader version
// Trade when a fast SMA crosses over a slow SMA
protected override void Initialize()
{ // Run OnBarUpdate on the close of each bar
CalculateOnBarClose = true; // Set stop loss and profit target at $5 and $10 SetStopLoss(CalculationMode.Ticks,5); SetProfitTarget(CalculationMode.Ticks,10); } protected override void OnBarUpdate()
{ // don't trade during the LookBack period if(CurrentBar < 20)
return; double Fast = 10; double Slow = 20; // Exit short and go long if 10 SMA crosses over 20 SMA
if(CrossAbove(SMA(Close,Fast),SMA(Close,Slow),1)) { ExitShort();
EnterLong(); } // Exit long and go short if 10 SMA crosses under 20 SMA
else if(CrossBelow(SMA(Close,Fast),SMA(Close,Slow),1)) { ExitLong();
EnterShort(); } }
// Zorro version
// Trade when a fast SMA crosses over a slow SMA
void run()
{ // Set stop loss and profit target at $5 and $10 Stop = 5; TakeProfit = 10; vars Close = series(priceClose()); vars SMAFast = series(SMA(Close,10)); vars SMASlow = series(SMA(Close,20)); // Exit short and go long if 10 SMA crosses over 20 SMA
if(CrossOver(SMAFast,SMASlow))
enterLong(); // Exit long and go short if 10 SMA crosses under 20 SMA
else if(CrossUnder(SMAFast,SMASlow)) enterShort(); }
 

Neuroshell Trader™ and other DLL based trading platforms

Neuroshell Trader™ is a platform specialized in employing artificial intelligence for automated trading. Neuroshell indicators are functions added through DLLs. They take an input array, an output array, the array size, and additional parameters. Many other trade platforms use similar DLL based indicators. Such indicators are often based on normal C, thus conversion to Zorro is very easy - especially when you don't have to convert it at all and can call the DLL function directly.
   When using an indicator made for a different platform, the array order convention must be take care of. Neuroshell stores time series in ascending order (contrary to most other platforms that store them in reverse order) and passes the end of the array, not its begin, to the indicator function. Neuroshell indicators normally return an output series instead of a single value. Below both methods of indicator conversion are shown.

// Neuroshell version - Entropy indicator
// published by ForeTrade Technologies (www.foretrade.com/entropy.htm)
#include "math.h"
#include "stdlib.h"

__declspec(dllexport) void Entropy (double *price, double *entropy, long int size, long int numbars)
{
  double *in, *out, P, G;
  long int i,j;
  double sumx = 0.0;
  double sumx2 = 0.0;
  double avgx = 0.0;
  double rmsx = 0.0;

  in=price;
  out=entropy;

  for (i=0; i<size; i++)
  {
    if (i < numbars+1) *out = 3.4e38;
    else 
    {
      sumx = sumx2 = avgx = rmsx = 0.0;
      for (j=0;j<numbars+1;j++)
      {
        sumx += log(*(in-j) / *(in-j-1)) ;
        sumx2 += log(*(in-j) / *(in-j-1)) * log(*(in-j) / *(in-j-1));
      }
      if (numbars==0) 
      {
        avgx = *in;
        rmsx = 0.0;
      }
      else 
      {
        avgx = sumx / numbars;
        rmsx = sqrt(sumx2/numbars);
      }
      P = ((avgx/rmsx)+1)/2.0;
      G = P * log(1+rmsx) + (1-P) * log(1-rmsx);
      *out=G;
    }
    in++; out++;
  }
}
// Zorro version - Entropy indicator
// Method 1 - calling the DLL function directly
// Copy the Entropy DLL into the Zorro folder
int __stdcall Entropy(double *price, double *entropy, long size, long numbars); // function prototype
API(Entropy,entropy) // use the Entropy function from entropy.dll

var EntropyZ(vars Data,int Period)
{
  Period = clamp(Period,1,LookBack-1); // prevent exceeding the Data size 
  double E; // single element "array" for receiving the output value 
  Entropy(
    Data+Period+1, // end of the array (element order does not matter here)
    &E, 1,         // pointer to the output "array" with size 1
    Period); 
  return E;
}
// Zorro version - Entropy indicator
// Method 2 - converting the DLL code to lite-C
var EntropyZ(vars Data,int Period)
{
  Period = clamp(Period,1,LookBack-1); // prevent exceeding the Data size 
  var sumx = 0., sumx2 = 0.;
  int j;
  for (j=0; j<Period; j++) {
    sumx += log(Data[j]/Data[j+1]);
    sumx2 += log(Data[j]/Data[j+1]) * log(Data[j]/Data[j+1]);
  }
  var avgx = sumx/Period;
  var rmsx = sqrt(sumx2/Period);
  var P = ((avgx/rmsx)+1)/2.;
  return P * log(1+rmsx) + (1-P) * log(1-rmsx);
}
 

R

R is an interactive script language and an open source environment for statistical computations and charting, developed in 1996 by New Zealand scientists Ross Ihaka and Robert Gentleman of Oakland University. It is meanwhile the global standard in statistical computations and has a large set of packages for all advanced fields of statistics, machine learning and data mining.

Although it is possible, it normally makes no sense to convert complex R calculations to lite-C scripts, especially when R 'packages' are used. An easier way is available through the R bridge for directly executing R functions in lite-C.

MatLab™

MatLab™ is a commercial computing environment and interactive programming language by MathWorks, Inc. Other than R, it is not specialized on data analysis and machine learning, but allows symbolic and numerical computing in all fields of mathematics. Its integrated compiler makes it relatively easy to convert a MatLab trading algorithm to a C/C++ DLL that can then be used with Zorro.

See also:

lite-C, Pointers, Structs, Functions, DLLs, C to lite-C, MT4 bridge, R bridge

► latest version online