Trading Systems Coding: The Coding Stage
  1. Trading Systems Coding: Introduction
  2. Trading Systems Coding: System Design
  3. Trading Systems Coding: The Coding Stage
  4. Trading Systems Coding: The Coding Process
  5. Trading Systems Coding: Testing, Troubleshooting and Optimizing
  6. Trading Systems Coding: Using Your System
  7. Trading Systems Coding: Conclusion

Trading Systems Coding: The Coding Stage


By Justin Kuepper
Contact Justin

Now that we have a design document in hand, we can look at how these rules are put into code that a computer can understand. In this section, we'll break down a section of code and look at it piece by piece. For this example, we will use MetaTrader's programming language MetaQuotes II to build a very simple moving average trading system.

Defines: MATrendPeriod(100);

Var:MaCurrent(0),MaPrior(0);

If Bars < 100 Then Exit;

If FreeMargin < 1000 then Exit;

maCurrent =iMA(MATrendPeriod,MODE_SMA,0);

maPrior =iMA(MATrendPeriod,MODE_SMA,1);

If maCurrent > maPrior then

{SetOrder(OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,RED); Exit;};


If maCurrent < maPrior then

{SetOrder(OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,RED); Exit;};

The If/Then Format
After taking a brief look at this code, you should recognize a few elements that we touched on in the design phase. For example, you should recognize the If/Then format that we used when constructing our design. To get a better understanding of this code, let's break it down and analyze each part:

Defines: MATrendPeriod(100);

Var: MaCurrent(0),MaPrior(0);


Here, we're simply defining the moving average that we are using by saying that we want it to be an average of the last 100 bars. The Defines function here lets us do that for any type of data we want. After that, we simply invent two variables (items which we create to hold data) MaCurrent(0) and MaPrior(0). These two variables will hold the data that we will set in a later step.

If Bars < 100 Then Exit;

If FreeMargin < 1000 then Exit;

Here we see the If/Then format that we used in the design phase put to work. These two statements tell the computer to exit if certain conditions aren't met. Let's translate these two commands into English:

If Bars < 100 Then Exit; à "If there are fewer than 100 bars (data points) on the chart, then exit the program without doing anything.''

If FreeMargin < 1000 then Exit; à "If my account has less than $1,000 in available funds, then exit the program without doing anything.''

You can translate any criteria you want into this format and put it at the beginning of your program in order to adapt to certain situations.

Buy and Sell Signals

maCurrent =iMA(MATrendPeriod,MODE_SMA,0);

maPrior =iMA(MATrendPeriod,MODE_SMA,1);

Now let's make use of the two variables we described above. Let's take these statements apart to see what they are doing:

maCurrent = à Here we are telling the computer to assign the following information to "maCurrent''.

iMA(MATrendPeriod,MODE_SMA,0); à Here we are using a simple statement, which uses the following basic format: Study(TimePeriod,Mode,Start).

Note that you can replace iMA with MACD, RSI or any other studies your trading system may be using. You can also replace the parameters to suit your own system.


If maCurrent > maPrior then

{SetOrder(OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,RED); Exit;};

Now we are getting somewhere! Here is the part of the code that tells the computer when to buy. Notice that we are making use again of the If/Then format that we used in the design document. Let's translate this to English to see what's happening:

If maCurrent > maPrior then { à "If the current Moving Average is greater than the prior Moving Average, then…''

SetOrder( à "Create an order entry to …''

OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,RED à "Buy my defined number of lots at the ask price plus my take profit point, and mark it as a red point on the chart.''

); à "End the order.''

Exit; à "Exit the trading strategy.''

}; à "End the If/Then statement.''




Note that the Take Profit Point is something that is defined by users when they add the trading system to their charts. Also notice that we are buying at the ask price and selling at the bid price - this is a key feature, especially when creating a system for stocks.

If maCurrent < maPrior then

{SetOrder(OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,RED); Exit;};

Finally, we have our code that tells the computer when to take a short position. Note that this statement is almost identical to the "buy'' statement, aside from the OP_SELL instead of OP_BUY as well as the usage of the bid price as opposed to the ask price.

Conclusion
And there you have it - the bare bones of what a trading system code looks like. Please note that the above code is not a complete trading system, as it does not include any commands to close open positions. Such additional aspects can be implemented using a format similar to the code we've shown.

In the next section of this tutorial, we will go into greater depth regarding the specific ways in which your trading system can be converted to code.

Trading Systems Coding: The Coding Process

  1. Trading Systems Coding: Introduction
  2. Trading Systems Coding: System Design
  3. Trading Systems Coding: The Coding Stage
  4. Trading Systems Coding: The Coding Process
  5. Trading Systems Coding: Testing, Troubleshooting and Optimizing
  6. Trading Systems Coding: Using Your System
  7. Trading Systems Coding: Conclusion
RELATED TERMS
  1. Standard Industrial Classification - SIC Code

    A standard series of four-digit codes created by the U.S. government ...
  2. Internal Revenue Code - IRC

    The comprehensive set of tax laws created by the Internat Revenue ...
  3. Message Authentication Code - MAC

    A security code that is typed in by the user of a computer to ...
  4. Validation Code

    A series of three or four numbers on the front or back of a credit ...
  5. Authorization Code

    A generic term that refers to a code or password that identifies ...
  6. American Code For Information Interchange - ASCII

    A type of code for data transmission. The ASCII translates all ...
RELATED FAQS
  1. How do quant traders build the relative strength index (RSI) into their algorithms?

    Learn how quantitative traders build the relative strength index (RSI) into their algorithms. Explore how automated trading ... Read Answer >>
  2. Do we need to pay tax on a property my ex-husband and I sold and split the profit? ...

    I didn't live on the property but I still owned it. When selling the property with my ex-husb... Read Answer >>
  3. What trading opportunities do Broadening Formation patterns give to traders?

    Profit from recognizing a broadening formation pattern on a chart that allows you to enter low-risk, high profit potential ... Read Answer >>
  4. Why can't I enter two sell orders on the same stock?

    To answer this question, let's look at a few different situations. You bought a stock for $10 but want to be able to protect ... Read Answer >>
  5. I don't understand how a stock has a trading price of 5.97, but when I buy it I have ...

    It might seem logical that the last traded price of a security is the price at which it would currently be trading, but this ... Read Answer >>
  6. What do the phrases "sell to open", "buy to close", "buy to open", and "sell to close" ...

    The confusing terminology mentioned in the question deals with entering and exiting option orders. In review, there are two ... Read Answer >>
Hot Definitions
  1. Labor Market

    The labor market refers to the supply and demand for labor, in which employees provide the supply and employers the demand. ...
  2. Demand Curve

    The demand curve is a graphical representation of the relationship between the price of a good or service and the quantity ...
  3. Goldilocks Economy

    An economy that is not so hot that it causes inflation, and not so cold that it causes a recession. This term is used to ...
  4. White Squire

    Very similar to a "white knight", but instead of purchasing a majority interest, the squire purchases a lesser interest in ...
  5. MACD Technical Indicator

    Moving Average Convergence Divergence (or MACD) is a trend-following momentum indicator that shows the relationship between ...
  6. Over-The-Counter - OTC

    Over-The-Counter (or OTC) is a security traded in some context other than on a formal exchange such as the NYSE, TSX, AMEX, ...
Trading Center