Trading Systems Coding: The Coding Stage

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.
Read more »

National museum displays 30-year collection in HCMC



A stone yoni, considered to be the largest in Vietnam, at the exhibition
The National Museum of Vietnamese History–Ho Chi Minh City launched an exhibition Sunday of some 300 of nearly 30,000 items collected since its establishment.

Running until the end of this year, the exhibition highlights various articles ranging from Vietnam’s largest stone yoni dating from the 12-13th century, to two Bertin vases made in the mid 19th century by France’s National Manufacture of Sèvres.

Antiques belonging to Vietnamese royal families and Chinese pottery dating from the 18th century are also on display.

Director Tran Thi Thuy Phuong said the items were gathered from different sources including collectors and police and customs officers who confiscated them in smuggling operations.

Local and foreign benefactors, including famous antique collector Vuong Hong Sen, have also contributed to the collection, Phuong added.

Titled Typical Antiques: 30 Years’ Collection, the exhibition marks the center’s celebration of its 30th anniversary. The museum is located at No.2 Nguyen Binh Khiem Street, District 1.

Reported by Giao Huong

Read more »

Stocks recover on the back of international rally


Stocks rebounded Wednesday on the Ho Chi Minh Stock Exchange as the recovery on international stock markets boosted investor confidence.

The VN-Index, Vietnam’s main stock index, moved up sharply soon after the opening bell on heavy buying.

But the uptick ran out of steam in the second session which saw heavy trading with 18.7 million shares changing hands. The benchmark index eventually closed 8.82 points, or 2.73 percent, up at 331.62.

Trading volume was high, with 23.3 million shares traded, compared with Monday’s 14.7 million and Tuesday’s 18.2 million. Of the index’s members, 132 advanced and 21 declined.

Nguyen Hai Son, a broker at FPT Securities Co. in HCMC said sentiment was the key driver. “Investors were buoyant after gains on stock markets around the world,” he said.

But it is hard to make a prediction now since the market moves rely mainly on investor confidence, which is driven by the international markets, he said.

Asian stocks soared for a second straight day Wednesday after a strong rally on Wall Street as hopes mounted for further interest rate cuts to shore up the global economy.

Japan's Nikkei stock index jumped more than 7 percent in early trade, regaining the key 8,000-points level, after the Dow Jones had soared almost 11 percent in New York overnight.

Shares jumped almost 8 percent in Seoul and 4.5 percent in Sydney. Singapore saw a gain of 3.8 percent.

In Canada, the Toronto Stock Exchange rebounded 7.2 percent, nearly erasing Tuesday's 8.14 percent plunge, the worst single-session drop in 21 years.

In the wake of huge gains in Asia, European exchanges rebounded. London's FTSE 100 index rose 1.92 percent and the Paris CAC 40 gained 1.55 percent.

The Frankfurt DAX, again powered by big gains for Volkswagen, soared 11.28 percent to 4,823.45.

In Latin America's biggest stock market, Brazil's Ivobespa index soared 13.42 percent, while Argentina's Merval jumped 6.58 percent.

Stocks’ performances

Saigon Thuong Tin Commercial Joint Stock Bank, the exchange’s only listed traded lender, remained the most active stock by volume, with 2.4 million shares changing hands.

The HCMC-based lender edged up VND900, or 4.74 percent, to close at VND19,900.

Vitaly Joint Stock Co., Hanoi P&T Construction & Installation Joint stock Co., Petrolimex Joint Stock Tanker Co. and furniture maker Savimex Corp. were the top four movers Wednesday, all rising by the daily maximum limit.

Lam Dong Mineral and Building Material Joint Stock Co. and Lu Gia Mechanical Electric Joint Stock Co. were the two biggest losers, both plummeting 4.98 percent.

Foreigners have been net sellers since early this month, notching up net sales of VND71.5 billion (US$4.3 million).

Reported by Hoang Uy (With input from AFP)

Read more »