Trading System

Some of you may be wondering how to use the ESPL language to create a simple Trading System.  The following ESPL program opens an IBM daily chart and runs a Trading System based on the crossing of two Simple Moving Average lines.  The system will buy when the average lines cross up.  The system will sell when the average lines cross down.  The ResetTrades command is used to initialize the trading system with no commissions and specific trade arrows.  A FOR loop and the GetStudy command are used to loop through the bars and determine when the lines cross.  The Trade command is used to create each trade.  A TradeReport is printed in the Output window when the system is finished.  Arrows will mark each trade on the chart.  Read the on-line ESPL help documentation for details on each command.

Copy and Paste the following code to your Script Editor window to try it out.  Click the ESPL button in Ensign Windows to open the Script Editor window.  Paste the code into the left window.  Click the  RUN  button in the Script Editor window to run the Trading System on the IBM chart.  Change the symbol to something else to test a different market.  Substitute a different study (like Stochastics) to test the crossing of different kinds of lines.  The code below includes a comment (at the end of the line) to document each line.  Select  Chart | Trade Detail  from the Ensign Windows menu to view the individual trades generated by the system, or click the  Results  button in the Script Editor window.

var                                     {Start of Variable declarations}
  i,Handle: integer;                    {Two variables are declared as Integers}
begin                                   {Start of Main Programming code}
  Chart('IBM.D');                       {Open an IBM daily chart}
  Handle := AddStudy(eAve,5,20);        {Add Moving Averages to the chart}
  ResetTrades(0,0,1,3);                 {Initialize the Trading system}
  for i:= BarBegin to BarEnd do         {Loop through the bars}
  begin                                 {start of loop code}
    if GetStudy(Handle,5,i)=1 then Trade(eBuy+eIf+eReverse,i);    {look for buys}
    if GetStudy(Handle,5,i)=2 then Trade(eSell+eIf+eReverse,i);   {look for sells}
  end;                                  {end of loop code}
  Trade(eOut);                          {Close-out the last open trade}
  TradeReport(True);                    {Print a Trading System Summary}
end;                                    {End of program}

read more » Risk Disclosure