Journal of Mathematics and Statistics

Original Research Paper Application of Bi-Directional Grid Constrained Stochastic Processes to Algorithmic Trading

Aldo Taranto and Shahjahan Khan

School of Sciences, University of Southern Queensland, Australia

Article history Abstract: Bi-directional Grid Constrained (BGC) Stochastic Processes Received: 23-11-2020 (BGCSP) become more constrained the further they drift away from the Revised: 01-03-2021 origin or time axis are examined here. As they drift further away from the Accepted: 05-03-2021 time axis, then the greater the likelihood of stopping, as if by two hidden

reflective barriers. The theory of BGCSP is applied to a trading environment in Corresponding Author: Aldo Taranto which long and trading is available. The stochastic differential equation of School of Sciences, University the Grid Trading Problem (GTP) is proposed, proved and its solution is of Southern Queensland, simulated to derive new findings that can lead to further research in this area Australia and the reduction of in management. Email: [email protected] Keyword: Grid Trading, Random Walks, Probability of Ruin, Stochastic Differential Equation, Bi-Directional Grids, Trending Grids, Mean Reversion Grids

Introduction To visualize the impact of the BGC function Ψ(Xt,t) in (1.1), 1000 Itô diffusions were simulated both with Bi-directional Grid Constrained (BGC) Stochastic and without BGC, with zero drift coefficient µ and unit Processes (BGCSP) are described as Itô diffusions in diffusion coefficient σ, as shown in Fig. 1. which the further they drift from the origin or time axis, The zero drift in (a) is constrained in (b) the more it then the more they will be reflected back to the origin. deviates from the origin, causing the hidden reflective Definition 1.1. (SDE of BGC Stochastic Process) upper barrier and hidden reflective lower barrier to emerge, together with horizontal bands to form due to For a complete filtered probability space (Ω,,{}t0, the discretization effect of BGC. ℙ) and a BGC function Ψ(x): ℝ  ℝ, x ∈ ℝ, then the BGCSP have applications by solving problems corresponding BGC Itô diffusion is defined as follows: involving the constraining of stochastic processes within two reflective barriers (often times hidden and not predefined) and that an event occurs when the barriers are  dX fXtdtgXtdW t,  t , t  sgn Xt t ,  Xt t , , (1.1) hit. In the context of mathematical, quantitative (quant) and BGC computational finance and algorithmic trading, we define an application of BGCSP by the following definition. where, sgn[x] is defined in the usual sense as: Definition 1.2. [Bi-Directional] Grid Trading (BGT)

BGT is the simultaneous placement of a Long and  1 ,x  0 Short trade at every grid width g level at, above and  sgnxx  0 , 0, below the initial price rate R0 and the corresponding  1 ,x 0 taking profit of each trade at the nearest Take Profit level (also of width g) without any predetermined stop losses. This definition is illustrated in Fig. 2. and f(Xt,t), g(Xt,t) and Ψ(Xt,t) are convex functions. As the BGCSP evolves over time, it will collect (i.e., The drift function f(x): ℝ  ℝ and the diffusion function close) many winning trades and also hold on to some losing g(x): ℝ  ℝ, x ∈ ℝ, in the limit, they approach the typical trades, which should become profitable over time. It is constant expressions for the drift and diffusion coefficients: remarkable how such a simple trading strategy can generate profits most of the time due to frequent periods of low

volatility (i.e., diffusion σ). However, when a strong trend limf x  ,lim g x . (1.2) xx  emerges, this strategy accumulates large losses and so the

© 2021 Aldo Taranto and Shahjahan Khan. This open access article is distributed under a Creative Commons Attribution (CC-BY) 4.0 license.

Aldo Taranto and Shahjahan Khan / Journal of Mathematics and Statistics 2021, Volume 17: 22.29 DOI: 10.3844/jmssp.2021.22.29 trades need to be closed down before they exceed the of magnitude, then the trading account can become ruined, account’s current capital or balance level. When the losing inducing a stopping time. We call this the Grid Trading trades grow too far in terms of either total count or in terms Problem (GTP).

80

60 20

40 10 20

0

X

X 0 -20

-40 -10

-60 -20 -80

0 200 400 600 800 1000 0 200 400 600 800 1000 T T (a) (b)

Fig. 1: Itô Diffusions with and without BGC

R R

T T 1 2 3 4 5 6

T = 6, W = 6, L = 21, P = -15

Fig. 2: Illustration of BGCSP in trading R = Rate, T = Time, W = Winning trades, L = Losing trades, P = Profit, E = Equity. (a) Horizontal blue lines represent when Long Trades occur and horizontal red lines represent when Short trades occur. The arrows represent the movement of the rate Rt over time t. (b) Dotted lines depict trades in profit and closed at their nearest Take Profit (TP). Solid lines depict trades that are held in loss until they reach their TP, closed down when loss becomes ‘too large’ or finally if an account is ruined

23 Aldo Taranto and Shahjahan Khan / Journal of Mathematics and Statistics 2021, Volume 17: 22.29 DOI: 10.3844/jmssp.2021.22.29

Literature Review nEE0,, 00 nEEvvE1,, BGC stochastic processes are relatively new 100 nEEvvEv2,23, (Taranto and Khan, 2020a-d). To the best of the authors’ 200 (3.1) knowledge, there is no additional formal academic nn 1 ntEEvtv, definition of BGC stochastic processes nor grid trading t 0 2 Wt available within all the references on the subject matter Lt (Mitchell, 2019; DuPloy, 2008; 2010; Harris, 1998; King, 2010; 2015; Markets, 2017; Work, 2018). These where, n is the grid level reached by the price Rt at secondary sources are not rigorous journal papers but time t. However, the markets do not trend indefinitely instead informal blog posts or software user manuals. and so Lt in (3.1) needs to be replaced with a Even if there were any academic worthey results found stochastic process. In a continuous time stochastic on grid trading, there is a general reluctance for traders framework t ∈ ℝ+, (3.1) becomes: to publish any trading innovation that will help other traders and potentially errode their own trading edge. dEt v Despite this, grid trading can be expressed vt dt  n t n t 1, (3.2) Et 2 academically as a discrete form of the Dynamic Mean-

Variance Hedging and Mean-Variance Potfolio where, E = E at t = 0 as an initial condition and adopting Optimization problem (Schweizer, 2010; Biagini et al., t 0 the simplest of 1-Dimensional Itô Diffusion processes: 2000; Thomson, 2005). There are many reasons why a firm would undertake a , ranging from minimizing 1 the of one of its client’s trades by trading in ntdtdW  tt, (3.3) the opposite direction, through to minimizing the loss on g a wrong trade by correcting the new trade’s direction whilst keeping the old trade still open until a more noting that now we highlight that n is a function of t where: opportune time (Stulz, 2013). In the case of grid trading, it can be considered as a form of hedging of multiple  µt is The drift (or direction) over time positions simultaneously over time, for the generation of  σt is The diffusion (or volatility) over time, which trading profits whilst minimizing the total portfolio loss. are random and assumed independent of µt over time  Wt is A Wiener Process (or Brownian motion) as dWdt with Methodology ttt (0,1)

Derivation of Continuous Grid SDE We note that (3.2) is essentially a non-standard Geometric Brownian Motion (GBM). The reason why Theorem 3.1 we have not expressed it as a Arithmetic Brownian For a Bi-Directional grid trading constrained Itô Motion (ABM) is that we require the equity Itô diffusion process with a given grid width g, value ν per grid width, to be modelled as products of random factors and not drift (direction) µt and variance (risk) σt, then the change sums of random terms. GBM involves independently and in equity E over time t is: identically distributed ratios between successive factors. dW Furthermore, we require t 0, t as trading dEvvg   E  tt 2.tggdtdW22 t 22ttt  Eggt 22 systems seek to exponentially compound E over time and an Et = 0 equates to ruin or bankruptcy. In fact, since Proof our drift and diffusion terms are non-constant over time, then our non-standard GBM is actually a form of a more In the discrete time framework t ∈  of Fig. 2, one generalised Itô Processes. Finally, we note that (3.2) does can see that the equity Et at any time t is comprised of the not appear at first glance to be a GBM as it does not exhibit initial equity E0, plus the sum of all the winning trades an explicit dWt term, even though it is implied due to (3.3). Wt, minus the sum of any losing trades Lt. We can Substituting (3.3) into (3.2) expands to: elaborate how the progression can evolve over time, in dE v 2 the worst case scenario of a strongly trending market, as t vt dt  n t  n t      shown in Fig. 2b. Et 2 We can now derive the general formula for E , 2 (3.4) t v dt  dW    dt  dW  where ν is the value per grid width, g >0 is the grid vt dt t t t    t t t  . 2 gg width, giving:    

24 Aldo Taranto and Shahjahan Khan / Journal of Mathematics and Statistics 2021, Volume 17: 22.29 DOI: 10.3844/jmssp.2021.22.29

vvg   which completes the proof.  2tggdtdW22 t 22ttt  22gg (3.5) Results and Discussion  12dtdW  t , Profitable Path Analysis

v 22 vg t where, 1 2tg tt  g and 2 , Undertaking a sensitivity analysis of the parameters 2g 2  2g 2 µt, σt, ν and g as shown in Fig. 3, one finds that the completing the proof. model for Et is more sensitive to µt and σt than it is to ν It is worthwhile noting at this stage, setting aside the and g, noting that µt, σt, ν, g ∈ ℝ. constants ν and g, that since Γ1(t,µt,σt) and Γ2(σt), then We also note that most of the simulations resulted in (3.5) is not a standard simple linear SDE and that there is a positive profit in Et due to the impact of grid trading on some convolution of  2 within the deterministic t the imput Rt, one such typical scenario plotted in Fig. 5 component dt with the σt within the random component using the MT4 trading platform which supports the dWt. This means that we would expect to see some theoretical model, in the first half where Et grows almost relatively complex interactions from the underlying linearly. Specifically, the values ν > 1, g > 1 results in distribution samples. For example, negative σt values most simulations producing positive Et. Hence, we 2 becoming positive due to  t , skewing the results choose ν = 1 = g, simplifying (3.6) to: 2 towards Et  0 due to the negative sign before  t , t 5  EEtWexp. tt 2 (4.1) which supports to a certain extent why Et has a ttt 0  tendency to almost surely approach 0 over time 2282 (subject to certain drift and diffusion conditions set out in the results and discussion sections). Plotting (4.1) in Fig. 3 shows the general theoretical nature of grid trading’s potential if it is stopped early Solution of Continuous Grid SDE enough and restarted to minimize the risk of the losing trades. We see that the greatest E value not only occurs Theorem 3.2 t when Rt is range bound (having low volatility or For a Bi-Directional grid trading constrained Itô diffusion σt), but also when Rt is trending with relatively process with a given grid width g, value ν per grid width, small drift µt and relatively high diffusion σt values. drift (direction) µ and variance (risk) σ , then the equity t t Ruin Path Analysis and Stopping Times E over time t has the solution: Having presented scenarios that show that grid  v 2 trading can be very profitable, it is now beneficial to EEtggtt0 exp 2   2g present scenarios that show that grid trading can also (3.6) lead to ruin. As the trades are accumulated, one will v 44 v   2 tWt tt begin to collect profitable trades as the Balance grows 42g  linearly, whilst the Equity dips down, highlighting the existence of losing trades that are carried and not closed. Proof Ruin occurs when an investor’s account Equity Et at time Recall that (3.5) is a GBM whose well known t, which is the difference between the Balance Bt and (Oksendal, 1995) general solution is of the form: Profit Pt at time t (Et = Bt-Pt) is reduced to zero or if their equity is too low (close to zero) to prevent any new 2 trades to be placed due to brokerage rules.  t St S0 exp t  t  t W t . (3.7) 2 We know that the grid loss accumulation process that  grows via the triangular number series, grows faster with

We are now in a position to solve the bi-directional smaller and smaller values of the grid width g. A sensitivity analysis was undertaken for g ∈ (0,1) and is shown in Fig. grid trading SDE (3.5) by substituting Γ1 and Γ2. Making use of a change of variable s, substituting the 4, showing the transition from ruin to profitability, highlighting the importance of having g sufficiently large. expressions for Γ1 and Γ2, we know that the solution of a standard GBM is: This risk of ruin occurs in grid trading systems in the long term if and when it becomes ‘too grid-locked’ with t t t too many losing trades. To break a grid-lock, the 1 2 dln  Ess  1   2 ds   2 dW 0  02  0 underlying Itô diffusion Rt needs to have range bound movement for an extended period of time so that the vvvv 4    E  Eexp   tg22  g  t  t W , winning trade total can be greater than the losing trade t0 2  t t t 2gg 4 2 total. If this doesn’t occur, such as during strong trends

25 Aldo Taranto and Shahjahan Khan / Journal of Mathematics and Statistics 2021, Volume 17: 22.29 DOI: 10.3844/jmssp.2021.22.29 with low volatility, then the Itô diffusion’s equity will stopping time, as shown in the second half of Fig. 5. This eventually become ‘ruined’, which we relate to a scenario in MT4 also supports the theoretical model.

200

300 100

= 2 =

t  50

= 1.5, 20

200 t

µ 10

Time (t)

with

t

Time (t)

R 5 100 2

1 0 3.0e+10 2.0e+10 1.0e+10 0.0e+00

4 3 2 1 0 -1 50 simulations of

).

c

( Equity (E_t) Price rate (R_t)

40,000

40 35,000

, 30,000

values

20 t  25,000 Log (E_t) 0 20,000

Price(R_t) rate -20 ). Densities from 15,000

b ( -40 10 20 30 40 50 60 70 80 90 100

1 3 5 7 9 11 13 15 17 19 P Diffusion

20,000 30

10,000

values, 0

20 t

µ

-10,000

10 -20,000

-30,000 Log (E_t) Price(R_t) rate 0 -40,000 (a). Densities from -50,000

-10

0

0.5 0.7 0.9 1.1 1.3 1.6 1.7 1.9 2.1 2.3 2.5

10 20 30 40 50 60 70 80 90 100 g Drift

Fig. 3: Sensitivity analysis of various values of µt, v and g the stochastic model for Et is relatively insensitive to v and g is more influenced by the drift µt and the diffusion t and specifically their interrelationship. Most simulations resulted in exponential growth of Et, noting that Rt = f(µt, t) whilst Et = f(Rt, v, g) for some function f.

26 Aldo Taranto and Shahjahan Khan / Journal of Mathematics and Statistics 2021, Volume 17: 22.29 DOI: 10.3844/jmssp.2021.22.29

200 200

100 100

50 50

20 20

10 10

Time (t)Time (t)Time

5 5

2 2 (d). g = 0.9

(d'). g = 0.9 1 1 1e+32 1e+24 1e+16 1e+08 1e+00 4e+36 3e+36 2e+36 1e+36 0e+00

Equity (E_t) In (E_t)

200 200

100 100

50 50

20 20

10 10

Time (t)Time (t)Time 5 5

2 2

(c). g = 0.8,

(c'). (c'). g = 0.8, 1 1 1e+19 1e+14 1e+06 1e+04 1e+01 8e+20 6e+20 4e+20 2e+20 0e+00

Equity (E_t) In (E_t)

= 2.

200 200

= 1.5,

100 µ 100

50 50

20 20

10 10

Time (t)Time (t)Time

(b). (b). g = 0.7, 5 5 (b'). g = 0.7,

2 2

1 1 1e+07 1e+00 1e+07 1e-14 1e-21 6e+09 4e+09 2e+09 0e+00

Equity (E_t) In (E_t)

, 200

, 200

6

6 100 100

50 50

). g = 0.

). g = 0.

a'

a

(

(

20 20

10 10

Time (t)Time (t)Time 5 5

2 2

1 1 1e-04 1e-19 1e-34 1e-49 1e-64 1000000 600000 200000 0

Equity (E_t) In (E_t)

Fig. 4: The transition from ruin to profit (a) to (d) increase g from 0.6 to 0.9 showing the main states are displayed. They show that the simulations become increasingly profitable as the grid width g is increased. (a') to (b') are the corresponding figures for (a) to (b) respectively with the natural logarithm applied. We note that the most profitable simulations (highest pealos) are unstable and lead to ruin. Nevertheless, as g is increased, ruin occurs later and later in time

27 Aldo Taranto and Shahjahan Khan / Journal of Mathematics and Statistics 2021, Volume 17: 22.29 DOI: 10.3844/jmssp.2021.22.29

Balance/Equity/Every tick (the most precise method based on all available least timeframes to generate each tick)/90.00% 16980

15848

14716

13584

12452

11320

10188

9056

7924

6792

5660

4528

3396

2264

1132

0

0 23 43 64 84 105 125 146 166 187 207 228 248 268 289 309 330 350 371 391 412 432 453 473 493 514 534 555 575 596 616 637 657 678 698 718 739 759 780 800 821

Fig. 5: Sample negative growth path of a grid trader in MT4 blue line = balance, green line = equity = balance + open profit if the system that is in profit ans is not closed down early enough (such as halg way over the time above), then there will be numerous losing trades accumulated that will lead to ruin (unless the favour able conditions arlse that detailed in Fig. 4.)

Conclusion ETH Zürich and A/Prof. Ron Addie of University of Southern Queensland for their invaluable advice on A SDE was proposed as the novel theorem of Bi- refining this paper. We would also like to thank the Directional Grid Constrained Trading stochastic independent referees of this journal for their processes and its solution was provided as the proof. endorsements and suggestios. From this theoretical model, a number of important properties of grid trading were uncovered through Monte Funding Information Carlo simulation of the SDE and accompanying sensitivity analysis. It was shown that the grid width g The first author was supported by an Australian and the profit P per grid width have a relatively minor Government Research Training Program (RTP) Scholarship. impact on the equity over time Et and that the drift µt and diffusion σt have the most impact. This research has Author’s Contributions shown that it is the interrelationship between µt and σt of Aldo Taranto: Conceptualization, methodology, the underlying price rate Rt that determines whether Et is profitable at any point in time t. It has also been shown software, investigation, writing-original, draft, writing- that whilst strong trends either up or down are the enemy review and editing, formal analysis and visualization. Shahjahan Khan: Validation and supervision. of Bi-Directional grid trading strategies, so long as σt is relatively large, then there will be sufficient counter-trend fluctuations that better ensure that the system can grow in Ethics Et, albeit not eliminating the risk of ruin. This research also This article is original and contains unpublished paves the way for future work on the stochastic material. The corresponding author confirms that all of optimization of these SDEs. This forms a rich framework to the other authors have read and approved the manuscript further study such stochastic processes in their own right, and that there are no ethical issues involves. but can also lead to applications in quantitative finance, funds management, investment analysis and banking risk References management. This paper will be leveraged in future research as we focus on deeper mathematical and statistical Biagini, F., Guasoni, P., & Pratelli, M. (2000). properties and the potential benefits of grid trading. Mean‐variance hedging for stochastic volatility models. , 10(2), 109-123. Acknowledgement https://www.fm.mathematik.uni- muenchen.de/download/publications/biagini_gua_pr We would like to thank Prof. Martin Schweizer of a_2000_matin.pdf

28 Aldo Taranto and Shahjahan Khan / Journal of Mathematics and Statistics 2021, Volume 17: 22.29 DOI: 10.3844/jmssp.2021.22.29

DuPloy, A. (2008). The expert4x, no stop, hedged, grid Stulz, R. (2013). How companies can use hedging to trading system and the hedged, multi-currency, create shareholder value. Journal of Applied forex trading system. Corporate Finance, 25(4), 21-29. https://c.mql5.com/forextsd/forum/54/grid1.pdf. Taranto, A., & Khan, S. (2020a). Gambler’s ruin DuPloy, A. (2010). Expertgrid expert advisor user’s problem and bi-directional grid constrained trading guide. and investment strategies. Harris, M. (1998). Grid hedging currency trading. and Financial Innovations, 17(3), 54-66. https://www.forexfactory.com/attachment.php/1240 https://eprints.usq.edu.au/39320/ 107?attachmentid=1240107&d=1374712669. Taranto, A., & Khan, S. (2020b). Bi-directional grid King, J. (2010). ForexGridMaster v3. 01 Manual. absorption barrier constrained stochastic processes https://mafiadoc.com/queue/forexgridmaster-v301- with applications in finance and investment. Risk manual-forexgridmastercom- Governance & Control: Financial Markets & forex_59c762301723ddf980311864.html Institutions, 10(3), 20-33. King, J. (2015). ForexGridMaster 5.1 https://eprints.usq.edu.au/39449/ Advanced Manual. February 5. DOI: Taranto, A., & Khan, S. (2020c). and 10.3844/jmssp.2020.182.197 Drawup of Bi-Directional Grid Constrained Markets, A. (2017). Forex Grid trading strategy Stochastic Processes. Journal of Mathematics and explained. Statistics, 16(1), 182-197. https://admiralmarkets.com/education/articles/forex- https://eprints.usq.edu.au/39564/ strategy/forex-grid-trading-strategy-explained Taranto, A., & Khan, S. (2020d). Bounds of Bi- Mitchell, C. (2019). Grid trading. Directional Grid Constrained Stochastic Processes https://www.investopedia.com/terms/g/grid- in Mathemetical Finance & Algorithmic Trading. trading.asp Organizer–Karshi State University, 3. Oksendal, B. (1995). Stochastic Differential Equations: Thomson, R. J. (2005). The pricing of liabilities in an An Introduction with Applications. New York: incomplete market using dynamic mean–variance Springer. hedging. Insurance: Mathematics and Economics, https://en.wikipedia.org/wiki/%C3%98howtodothen 36(3), 441-455. orwegianOwitha/ https://www.sciencedirect.com/science/article/abs Schweizer, M. (2010). Mean-variance hedging and /pii/S0167668705000430 mean-variance portfolio selection. Encyclopedia of Work, F. S. (2018). Grid Trading Strategy. Quantitative Finance. https:/www.valutrades.com/en/blog/how-to-trade- https://people.math.ethz.ch/~mschweiz/Files/MVHP currency-using-a-grid-strategy S-eqf.pdf

29