README

Currency Trader is an example that demonstrates how to employ Ajax techniques with Ajax-unaware JavaServer Faces components using NetBeans Visual Web and Dynamic Faces technology. The example employs both the Ajax Zone and Ajax Transaction components. The example comprises one rendered page and is entirely Ajax-based; no conventional page submissions occur.

The Currency Trader example simulates real-time foreign exchange trading. The Exchange Rates table lists various currency pairs (such as EUR/USD) along with their sell and buy prices. The prices denote the exchange rate to sell or buy one unit of the primary currency, the first of the two currencies in the pair. For example, a sell price of 1.2752 for EUR/USD denotes that a trader receives 1.2752 US dollars (USD) when selling one Euro (EUR). Similarly, a buy price of 1.2755 for EUR/USD denotes that a trader pays 1.2755 US dollars (USD) when buying one Euro (EUR). As another example, a sell price of 112.31 for USD/JPY denotes that a trader receives 112.31 Japanese Yen (JPY) when selling one US dollar (USD). Similarly, a buy price of 112.35 for USD/JPY denotes that a trader pays 112.35 Japanese Yen (JPY) when buying one US dollar (USD).

Ajax requests poll the server every few seconds to retrieve the latest exchange rates and update the web page Document Object Model (DOM). When a sell or buy price changes, up or down arrows display to give a visual sign of the change.

There are two types of currency trades: Buy and Sell. In a Buy trade, the trader buys some amount of the primary currency and later sells that amount (or less) of the primary currency. The idea is to buy low and sell high in order to make a profit. For example, a trader might buy 100,000 Euros (paying, say, 125,000 US dollars) and later sell that same amount of Euros. If the trader receives, say, 126,000 US dollars from the sale, the trader profits 1,000 US dollars.

Similarly, in a Sell trade, the trader initially sells some amount of the primary currency and later buys back that amount (or less) of the primary currency. Again, the idea is to buy back the primary currency at a lower rate in order to make a profit. For example, a trader might sell 100,000 Euros (receiving, say, 125,000 US dollars) and later buy back that same amount of Euros. If the trader pays, say, 120,000 US dollars in the buyback, the trader profits 5,000 US dollars.

When the trader initiates the first exchange (buying the primary currency in the case of a Buy trade or selling the primary currency in the case of a Sell trade), it is known as opening a position. When the trader initiates the second exchange (selling the primary currency in the case of a Buy trade or buying back the primary currency in the case of a Sell trade), it is known as closing a position. Once a position is closed, the trader sustains a profit or loss from the trade.

To open a position in the Currency Trader example, use the dropdowns in the panel labeled Open a Position to choose a currency pair, the type of trade (Sell or Buy), and an amount. Then click the Open button. The position appears in the Open Positions table below. The position displays the open price, which is the relevant price (the sell price in the case of a Sell trade or the buy price in the case of a Buy trade) at the time the position was opened. The position also displays the current price, which is the relevant price (the buy price in the case of a Sell trade or the sell price in the case of a Buy trade) the trader would sustain if the trader were to close the position at the present time. You will notice that as exchange rates are updated in the Exchange Rates table, the corresponding figures are also updated in the Current Price column of the Open Positions table. Again, up or down arrows display to give a visual sign of the change.

Each open position also displays a floating profit figure. This is the profit (or loss) the trader would sustain if the trader were to close the position at the present time. Since the floating profit is calculated based on the current price, when exchange rates are updated in the Exchange Rates table and the Current Price column of the Open Positions table, the corresponding figures in the Floating Profit column are also updated. Again, up or down arrows display to give a visual sign of the change. The floating profit is also calculated based on the amount selected in the Close Amount dropdown, which is the amount of the primary currency the trader intends to sell or buy back in order to close the trade. When the trader changes the selection in the Close Amount dropdown, an Ajax request is sent to the server to recalculate the floating profit and update the web page Document Object Model (DOM). When the trader clicks the Close button, a closed position is created. The open position is either removed from the Open Positions table or the figure in the Amount column is adjusted by subtracting the figure in the Close Amount dropdown. At the same time, a new position appears in the Closed Positions table, and the Total Profit figure at the top of the page is updated. Again, all updates to the page are achieved via Ajax requests leveraging Dynamic Faces technology. No conventional page submissions occur.

For information on installing and running the example, see the accompanying online technical article "Installing the Currency Trader Sample Application."