A new trading algorithm with financial applications

: The gravity equation is a useful tool for trading, but also for financial services as recently found. This paper tries to adapt modern theories of gravity equation for these services to a novel theory on trading, for both bilateral and multilateral trade, and supply and demand sides, finding an explicit expression of demand and supply of trade. This paper also includes an explanation of some Internet-based services by considering less transport costs between both countries. The proposed trading algorithm is key for trading development and for evaluating international trade, but also for finance, taking into account the midpoint between the proposed supply and demand of financial transactions, instead of the midpoint between bid and ask prices. This achieves a good fit for international trade and an alpha for financial trading.


Introduction
Introduced in international economics by Isard (1954), the gravity model of international trade was initially empirical, but not theoretically founded. In fact, some authors suggest some doubts about the current theoretical foundations of this specification (Deardorff, 1998). Apart from theoretical discussions, the gravity model in commerce has empirically been an accurate predictor of bilateral trade for years. Furthermore, this model can establish possible anomalies in the international trade.
While, since Anderson (1979), numerous authors have theoretically derived the gravity equation from different models (Evenett and Keller, 2002), some authors considered the gravity equation had not theoretical justification (Deardorff, 1998). Therefore, it is not far from reality to assert there is not yet a conclusive and integrative theoretical foundation for the gravity equation in international trade.
While in a farer field, López-Laborda and Peña (2018) develop a mechanism to allocate the value added of financial services among transactions in order to levy VAT, and they discover the empirical evidence of a gravity equation for the "pure" interest (the interest free of risk and charges), which follows a gravity equation similar to a simple equation described by Feenstra (2015) for bilateral trade. They use data from developed countries, finding the bond rate of the Treasury of these countries (which is close to the pure interest) follows a relationship equal to the product of two, the interest rate of deposits, the interest rate of loans and divided by the sum of both rates, with a low interval of error.
They also provide an economic theory, considering a ratio (the marginal productivity of the financial services) that is the same for deposits and loans. It is multiplied by, first, the interest receipts equalizing to the difference between interest receipts and the pure interest, and second, this ratio is multiplied by the interest payments, product that is equal to the difference between the pure interest and the interest payments. From this simple equation system, they obtain the gravity equation for the pure interest. This theory is followed in this paper, in which the financial basis is extended to a commerce basis, by the consideration of financial services as inter-temporal trading of consumption (Loewenstein and Thaler, 1989). Finally, the analysis comes back to finance in order to construct a useful indicator for algorithmic trading on bid-ask services. Treleaven et al. (2013) and Huang et al. (2019) provide a good literature review on the topic of algorithmic trading. The term refers to trading using algorithms to systematize the trade cycle. Data sources for testing the success of the algorithm are several, such as financial and economic data, financial news (Hafezi et al., 2015 andFeuerriegel andPrendinger, 2016), real time and historical data. Trading algorithms can be theory-driven or empirical and there are also several types of them. Among others, some ones optimize by choosing the portfolio with the highest rate of return, as the Black-Litterman algorithm.
Other algorithms to highlight are market-neutral and risk-neutral, profiting from relative under or over pricings between the analyzed instruments, being unaltered by wide market moves or where expected return is the only concern, being neutral to risk, respectively. Finally, two additional main strategies were developed (Dunis et al., 2003 andKaufman, 2005), the first considers to capture the momentum according to the trend, performing capital gains after large increases and capital losses after large decreases on the prices of the financial services. The second strategy, mean reversion, consider whether prices and returns are higher or lower than the average, or where there is divergence in two historically correlated financial products and convergence is assumed.
Far from being solved, financial applications of the gravity equation could be further more used in finance than only for value added taxation of financial services, even when the mobile-ratio developed by López-Laborda and Peña (2018) assigns the value added for each financial transaction. This can be compared with the "pure" value or interest without risk and charges, in order to obtain the best investment among others.
This paper aims to provide a theoretical explanation for the gravity equation on trade, based on banking intermediation literature, and returns to financial transactions in order to establish financial applications by adapting robustness-checked trading indicators proposed in this paper to financial investment on bid-ask products. So, the paper seeks to extend to international trade the current empirical evidence in financial services and its correspondent theory, in order to see the analogies for potential applications to algorithmic trading in finance or accounting analysis. The first step for this study is considering financial services as intertemporal trade of consumption.
The paper is divided as follows. Next to this section, Section 2 provides the theoretical framework for a theory on bilateral trade and multilateral trade based on the gravity equation. Section 3 provides the evidence for that theory, with a proposal of trading indicator based on the previous theory willing to be useful for finance. While Section 4 discusses the results and exposes the additional applications for finance, finally, Section 5 concludes.

Analogies between spatial and intertemporal trading theories
Some other remarkable theories of trade commerce are the Krugman's (1983) theory, or the Linder hypothesis. This last hypothesis suggests that the trading commerce depends on the different preferences of the agents from the countries of origin, and emphasizes the relationship with differentiated products. On the other hand, Feenstra et al. (2001) compares differentiated and homogeneous products, suggesting an inverse relationship between these two kinds of goods.
The proposed theory will be compared in some aspects with these two groups of theories. As explained in the introduction, the proposal extends to trading commerce the theory proposed by López-Laborda and Peña (2018), in which the value added of financial services equals to the sum of the margin applied to interest payments ( IP   ) and interest receipts ( IR   ): where the ratio that reflects the value added per unit and the marginal productivity of financial services is  , IP are the interest payments, IR are the interest receipts by the bank and  is the pure interest.
This interest performs a gravity equation jointly with interest receipts and payments, as follows: As explained in the previous section, this expression is very similar to the simple equation formulated in Feenstra (2015) for trading commerce, considering the total income of the trading partners instead of interest payments and receipts and the total exports among them instead of the pure interest.
In the first way, there are two ways for obtaining the same results for bilateral trade in a theoretical point of view, which are the demand side, or the preferences side according to the Linder hypothesis; and the supply side, related to Feenstra et al. (2001). Both sides lead to the gravity equation for trade.
In financial services, gravity equation could be considered related to '  , the pure interest without the maturity costs related to time (T) of the financial services, as proposed: which leads to a pure interest free of the temporal variables as maturity, , as well as, in trade commerce the exports could also be expressed as independent from the transport costs, and therefore, from the distance to the other country. So first, the demand side and total trade volumes are expressed without including transport costs, ' ' X M  , in contrast to including them, X M  , exports and imports respectively, related to a trading parameter  is between 0 and 1.
where  equals to zero if there are less transport costs due to Internet-based services and it is positive between zero and one otherwise, where D are the transport costs related to the distance, B the minimum benefits of commerce and C are the maximum costs of commerce, while '  is the marginal productivity of trading. The above Equation (4) expresses the surplus of the importer, calculated as the difference between the maximum costs of the exporters and the trade volume, and the below equation reflects the surplus of the exporter, obtained by the difference between the trade and the minimum benefits. So, the trade volume net from transport costs equals to: It is worth highlighting that some trading of Internet-based services, as some specific financial services, would be included in the 0   case, which is reflected in (5). Nonetheless, the following proposal is given after incorporating trading costs usually outside of the final price of the product, in a supply basis related to Feenstra et al. (2001): where A Y is the income or total value added of country A and B Y the income of country B, in which ''  are the benefits of countries A and B for trading by the supply side and per unit of income. So, the equivalence between the supply and demand side would be: which is equivalent to the aforementioned Feenstra et al. (2001) equation and where TF are the trading flows X + M. A parallelism could be made between gravity equations without charges in trade and in finance: while in finance the "pure interest" does not include fees but neither maturity costs related time, it is the same in trade but with "pure trade" and transport costs related distance. Bilateral trade has nexus with multilateral trade, as will be seen in the discussion section, but now the section is focused in multilateral trade, where the relationship between supply and demand can be considered in the following way: where  is the marginal productivity of multilateral trading, which reflects the trading supply per unit,  represents the demand of trading, and l TF and m TF are the trading flows, i.e., the highest and the lowest amount between exports and imports, respectively.
The expressions of  and  are: As shown above, the marginal productivity of trading will show the supply of spatial trading, while the gravity equation will reflect the demand side. The first one is the value added of trading divided by the total trading volume. These two variables will serve to construct the indicators for trading algorithms of the next section, so they are going to be harmonized to the same units, and only considering exports and imports, it leads to: These are the proposals of trading supply (TS, the value added of trading) and of trading demand (TD, the trade without charges such as transport costs), now, the indicator "KT" is proposed, which indicates the midpoint between supply and demand.
This indicator will be useful for the trading algorithm for finance, taking into account that will be implemented for bid and ask instead of quantities (exports and imports, i.e., trading amounts), and the prices behave inversely than quantities.

A proposal of trading algorithm
Traditionally, trading development indicators have been focused on the "supply" side, i.e., net exports, or at least, trading volume, openness or coverage. Furthermore, gravity equation has only been considered as an indicator of bilateral trading depending from national income, but not of multilateral trading related to exports and imports. In this line and up to our knowledge, literature of international economics has never considered trading demand as defined in this paper, a gravity equation of exports and imports. So, currently there are indicators of trading development without considering the trading demand reflected on Equation (10).
This paper proposes two indicators, one of trading development, and another more restrictive, of "promising trading future", both defined as follows. The first one allocates the countries with the highest "KT", which is the midpoint between trading demand and supply and would be related to the trading equilibrium, without taking into account whether it is stable or unstable. Development matters in this case: the higher developed, the higher interest in that country. This can be expressed as: which takes the value 1 if the KTi of country i is higher than the average of the countries and 0 otherwise. This is the Extrinsic Competitive Indicator (ECI) because it is an extrinsic indicator of competitive advantage of a country that points and encourages investing in there without being too risky. This constitute the proposal of algorithm. The second indicator reflects the potential of a country in trading, measured as the difference between trading supply (TS) and trading demand (TD) as defined at (10), becoming a more restrictive one: It takes the value "1" if the trading supply indicator of Equation (10) is higher or equal than the trading demand of the same equation, and takes "0" in another case. This is denominated Intrinsic Competitive Indicator, since it is formed by the intrinsic trading properties from the own country. This indicator reflects countries where seems to be an opportunity for investing in there because of the current lack of demand that can rise in the next years, because, as the Say law suggests, supply generates its own demand in the near future. Taking this as starting point, the end of the section is going to check the predictability of the algorithm on trade for applying it to finance in the next section, concretely to bid-ask prices. In addition, an example of application of the second indicator, the ICI one, is provided. For this exercise, the full data sample of the World Bank for Imports and Exports of commercial services, dollars in current prices, is used for the 218 countries and the period 1960-2018, as Table 1 shows. The TSi, TDi and KTi indicators are taken by applying the average of the indicator to the full time sample for each country. The main results of the study are collected in Table 2. It can be seen that the algorithm, the ECI indicator of trading development, could be considered a good mixture of measures of trading demand and supply since it includes countries reflecting high level of trade (Malaysia, Thailand, China) with others with high level of economic development (Norway, New Zealand, Belgium). Thus, this indicator may be a good test for investing or not in that country without too risk. Within brackets it is given the datum obtained from the TS, TD, K and Ki indicators, in thousands of millions. On the other hand, countries that are currently not so developed in trading terms as the others such as Dominican Republic, Macao and Maldives could be considered a good investment based on the proposed criteria due to their promising future, but without current corroboration in terms of per capita income, for instance, or even in trading volume or net exports.

Discussion and financial applications
This paper has provided another theory explanatory of gravity commerce, considering the demand and supply side. This model attempts to provide further light on the topic of the gravity equation, relating the gravity equation found in financial services by López-Laborda and Peña (2018) with the well-known trading gravity equation. The model is also useful for explaining trading with less transport costs, as trading with Internet-based services.
The theory developed in this paper considers bilateral and multilateral trade. In the bilateral trade, the supply side provides the traditional gravity equation is provided, while in the demand side the surplus of the exporter and of the importer are included, by introducing the differences between the trade volume and the maximum/minimum costs/benefits. Regarding the multilateral trade, a novel theory inspired in the underlying mechanisms of financial services has been developed, in which the supply and demand of trading can be explicitly obtained relating with exports and imports.
A consensus between the bilateral and multilateral trade can be exposed in this section, and an example with two countries is developed, where each country only produces for trading, each one exports/imports everything produced in there, respectively for countries A and B, so it is the only producer country. The illustration of the nexus between bilateral and multilateral trade starts with the following gravity equation of bilateral trade that is equal to the "pure trade" with transport costs, following Equation (7) but considering the absolute value of the income for each country and given  the trading parameter, it leads to: Trading is the only value added in this case. The next expression is constructed taken into account formula (9) (14) can be expressed as: , which given (9) leads to: Thus, the trading parameter has to be equal to the value added per unit, so the bilateral trading parameter associated to trading has to be equal to the value added per unit of multilateral trading in order to keep the equivalence and above joint analysis of bilateral-multilateral trading.
Another issue dealt in this section is the financial application of the algorithm (i.e., the ECI indicator) to bid-ask spread services and transactions. There are many trading strategies, as the recent proposal of Cartea et al. (2014). Before exposing the financial application, some basic concepts have to be clear. First, it is worth mentioning bid and ask refer to the prices for immediate purchase or an immediate sale, respectively, of some financial products. Its spread measures the weight of the transaction cost or market liquidity. The value added per unit, or marginal productivity is related to the here denominated as the "quoted spread", following the next relationship: Another usually employed variable is the midpoint, the arithmetic average between bid and ask. This variable is used to know about the suitability of purchasing or selling, related to whether the trading price is below or above that midpoint (Lee and Ready, 1991).
Taking these facts into account, the trading algorithm proposed in this paper for buying or selling financial transactions with bid and ask spreads is: (or trading price, if known) (or trading price, if known) (or trading price, if known) As quantities are negative correlated with prices (due to the negative elasticity of prices related to the product), then the option of investing according to the trade volumes is now the selling option with bid-ask prices. The example of this paper illustrates cases when trading prices are not known yet, but it is also useful by considering the trading price instead of the KTi indicator, where KT is the average among the KTi indicators of the total of financial products with similar characteristics to the target, where the KTi indicator is the midpoint between the proposed supply and demand instead of the ask or bid price, letting KTi to: Table 3 illustrates the example used in this section: similar bid-ask operations, where the value added (spread, TS), pure trade or gravity (TD) and the midpoint between both as KTi are reflected, also the average KT is showed (0.149). The two last columns are formulated according to the trading algorithm of formula (18).  Table 4 follows with the same example but considering that the reference is the usual one, i.e., the midprice. Going on with the hypothesis of the lack of a trading price yet, the investor only invests on or buy those products with a midprice below the average (0.185).
According to this, an investor guided by the proposal would see a potential purchase on product b while a usual trader would not. This could generate a higher alpha (higher returns from other traders), on the former investor, as Table 5 sums up: Taking into account the previously proven efficiency of the proposed algorithm in spatial trading, Tables 1-3 show a potential case in which the algorithm captures an investment opportunity that other indicators do not cover. Thus, this algorithm can be supported by the fact that it is a good predictor of opportunities in trading, and after the adaption to finance, it could be also a good predictor for returns on bid-ask transactions.
Regarding other algorithmic strategies, the proposal could be compared with two main strategies (Dunis et al., 2003 andKaufman, 2005), being farer from the momentum strategy where capital gains are performed after large increases and capital losses after large decreases on the prices of the financial services. The mean reversion strategy is nearer to the proposed algorithm, where the specific indicator of a security or financial product is compared with the average of the whole portfolio, as well as the mean reversion studies whether prices and returns are higher or lower than the average. The above-mentioned "usual behavior" of the numerical example is based on a kind of mean reversion where investors decide to invest whether there is a negative difference between the mid-price of a product and the average of the mid-price of the full portfolio, instead of the indicator as the proposal.
The proposed algorithm finds a case of buying that the usual behavior does not meet. Concretely, in a product with a value of 0.25 for ask and of 0.15 for bid, the indicator, as the average between "gravity" and value added (VA, spread), discovers a K-value (0.144) that is lower than the K-average (0.149), while, according to the usual behavior, the mid-price of the product (0.2) is higher than the average mid-price (0.185). So, applying these two different strategies, the proposed algorithm provides, in some cases, a laxer way of finding investment opportunities, as the above example illustrates. Focusing on the international trade exercise, the country with the highest ICI is Macao (TS = 7.023 > TD = 5.591), and with the highest ECI is the USA (K = 139.7), and the lowest among the highlighted is New Zealand (5.5).
The main assumptions of the proposal are that finance is supposed to follow a gravity equation, finance behaves in a similar way as intertemporal trade and that international predictions can be easily extended to finance, finally it is assumed that the difference between the portfolio average and the indicator can lead to extraordinary earnings as an alpha. One of the limitations and possible further extensions of the work is that the proposed algorithm could be also assessed by other sources apart from macroeconomic variables, such as historical portfolio data.
Another financial application in addition to the proposed trading-algorithm would be considering both indicators of (10), (11) for assets and liabilities, or even for sales and purchases, instead of exports and imports, taking into account topics of accounting and quantitative analysis of enterprises.

Conclusions and further research
A new theory of bilateral and multilateral trade commerce has been developed according the recent findings of López-Laborda and Peña (2018) in financial services and applied to trading. The bilateral theory considers both the supply and demand side of commerce, taking into account the surplus of exporters and importers on trade, as well as the transport cost, finding a similarity between the maturity of financial services and the costs of transport of trading. Even trading with Internetbased services is considered in the models, assuming no distance between countries.
In the multilateral trade, an explicit expression of supply and demand for trade provided by the exports and imports is provided. These last equations, which provide a gravity equation as supply of trading, could be also useful as indicators for other fields, as accounting by considering assets and liabilities, fiscal policy, by considering the potential and real tax collection limited by the tax evasion, or even for the cost-benefit analysis.
The two theories of multilateral and bilateral trade are joint in a single hypothetical case of two countries where their only economic activity is trading, showing a condition corroborating these two theories at the same time, when the parameter associated to bilateral trading equals to the value added per unit of trading in multilateral trade.
Derived from these theories, two indicators of global trading are proposed, one indicative of competitive advantage regarding others and another that reflects intrinsic competitive advantage, taking some proposed indicators of trading demand and supply. These indicators are checked with a full sample of the World Bank with 218 countries and a period between 1961 and 2018. Results with the extrinsic indicator show countries with high level of trading development, both due to economic (demand) and net exports or value added (supply) factors. On the other side, the intrinsic indicator shows countries with potential increase of development, Macao, Dominican Republic and Maldives, where their trading value added (supply) is greater than their economic size (demand).
The first of the two indicators, regarding competitive advantage relating other countries (ECI), is used to be applied to finance in bid-ask financial transactions. The essence of the proposed trading algorithm is taking into account the midpoint between indicators of supply and demand instead of the bid and ask prices. This is illustrated with an example where an opportunity of making higher alpha is provided for an investor guided with the proposal, an opportunity that other indicators, currently more common than this, do not cover.
Further research would include the analysis of the mentioned above extensions for accounting, or even for cost-benefit analysis, by considering benefits and costs instead of exports and imports.