File size: 16,579 Bytes
6510661 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
\documentclass[11pt]{article}
\usepackage[margin=0.5in]{geometry}
% Core packages
\usepackage{amsmath,amssymb,mathrsfs,bm,dsfont,bbold,euscript}
\usepackage{tikz-cd}
\usepackage{multicol}
% Paragraphs
\setlength{\parindent}{0pt}
\setlength{\parskip}{1\baselineskip}
\title{Simulated Trades}
\author{algorembrant}
\date{\today}
\begin{document}
\maketitle
\begin{abstract}
I formalized how to get the best simulated trades at historical data with the use of mix lookback and lookahead. Although it sound dumb for the first time, the purpose of having a $time$ with rich information such as
$\mathcal{V}_{\mathfrak{t}} =
(
\mathfrak{E}_{\mathfrak{t}}, \Omega_{\mathfrak{t}}, \mathcal{T}_{\mathfrak{t}}, \mathcal{R}_{\mathfrak{t}}, \mathcal{L}_{\mathfrak{t}}, \mathcal{Q}_\mathfrak{t}, \mathcal{Y}_{\mathfrak{t}}
)$ as another form of dataset, is benificial for field such as shaping the Reward function in Reinforcement Learning. The following contents will not discuss anything about RL, but a pure mathematital formalization of the said best trade at times $\mathfrak{t}$.
\end{abstract}
\newpage
\section{Information-rich Time}
\begin{align}
\mathfrak{E}_{\mathfrak{t}} &= \text{simulated entry at historical time } \mathfrak{t} \\
O &= open \\
C &= close \\
H &= high \\
L &= low \\
P &\in \{O,C,H,L\}, \quad \text{raw prices} \\
\mathfrak{O}_{\mathfrak{t}} &= (O_{\mathfrak{t}} - \mathfrak{E}_{\mathfrak{t}}), \quad \text{deviated open price at historical time } \mathfrak{t} \\
\mathfrak{C}_{\mathfrak{t}} &= (C_{\mathfrak{t}} - \mathfrak{E}_{\mathfrak{t}}), \quad \text{deviated close price at historical time } \mathfrak{t} \\
\mathfrak{H}_{\mathfrak{t}} &= (H_{\mathfrak{t}} - \mathfrak{E}_{\mathfrak{t}}), \quad \text{deviated high price at historical time } \mathfrak{t} \\
\mathfrak{L}_{\mathfrak{t}} &= (L_{\mathfrak{t}} - \mathfrak{E}_{\mathfrak{t}}), \quad \text{deviated low price at historical time } \mathfrak{t} \\
\mathfrak{P} &\in \{\mathfrak{O},\mathfrak{C},\mathfrak{H},\mathfrak{L}\}, \quad \text{deviated prices} \\
mfe &= \text{max favorable excursion} \\
mae &= \text{max adverse excursion} \\
s_{\mathfrak{t}} &= \text{spread of that historical time, say we have 0.260}
\end{align}
\begin{align}
t &= \text{current time}\\
t-n &= \text{where } n \text{ refers to number of lookback} \\
\dot{t} &= \text{is } t \ge \dot{t} \ge (t-n) \\
\mathfrak{t} &= \text{time where \textit{simulated entry} occured, where } \mathfrak{t} \in \{t-n\} \\
\mathfrak{t} + \mathfrak{n} &= \text{where } \mathfrak{n} \text{ refers to the lookahead or holding period}\\
\dot{\mathfrak{t}} &= \text{is } \mathfrak{t} \le \dot{\mathfrak{t}} \le (\mathfrak{t} + \mathfrak{n})\\
\mathbb{t} &= \text{time before } \mathfrak{M} \ge -50\% \text{, where } \mathbb{t} \in \{\max((\mathfrak{t} + \mathfrak{n}) - 1)\} \\
\mathbb{t} - \mathbb{n} &= \text{where } \mathbb{n} \text{ also refers to number of lookback}\\
\dot{\mathbb{t}} &= \text{is } \mathbb{t} \ge \dot{\mathbb{t}} \ge (\mathbb{t}-\mathbb{n})\\
\end{align}
\begin{align}
\mathfrak{X}_{\dot{\mathfrak{t}}} &=
\begin{cases}
\text{if buy}, \quad \underbrace{\max_{\mathfrak{t} \le \dot{\mathfrak{t}} \le (\mathfrak{t}+\mathfrak{n})} \mathfrak{H}_{\dot{\mathfrak{t}}}}_{mfe} \\
\text{if buy}, \quad \underbrace{\min_{\mathfrak{t} \le \dot{\mathfrak{t}} \le (\mathfrak{t}+\mathfrak{n})} \mathfrak{L}_{\dot{\mathfrak{t}}}}_{mae} \\
\text{if sell}, \quad \underbrace{\max_{\mathfrak{t} \le \dot{\mathfrak{t}} \le (\mathfrak{t}+\mathfrak{n})} \mathfrak{H}_{\dot{\mathfrak{t}}}}_{mae} \\
\text{if sell}, \quad \underbrace{\min_{\mathfrak{t} \le \dot{\mathfrak{t}} \le (\mathfrak{t}+\mathfrak{n})} \mathfrak{L}_{\dot{\mathfrak{t}}}}_{mfe}
\end{cases},
\quad \text{\textcolor{red}{running extreme}}
\end{align}
And we formalize our imaginary stoploss as:
\begin{align}
\omega &= \frac{1}{\mathfrak{T}}\sum_{\mathfrak{t}=0}^{\mathfrak{T}-1}
\begin{cases}
\text{if buy}, \quad \max\left({\mathfrak{O}}_{-\mathfrak{t}} - {\mathfrak{L}}_{-\mathfrak{t}},|{\mathfrak{O}}_{-\mathfrak{t}}-{\mathfrak{C}}_{{-\mathfrak{t}}-1}|,|{\mathfrak{L}}_{-\mathfrak{t}}-{\mathfrak{C}}_{{-\mathfrak{t}}-1}|\right)\\
\text{if sell}, \quad \max\left(|{\mathfrak{O}}_{-\mathfrak{t}} - {\mathfrak{H}}_{-\mathfrak{t}}|,|{\mathfrak{O}}_{-\mathfrak{t}}-{\mathfrak{C}}_{{-\mathfrak{t}}-1}|,|{\mathfrak{H}}_{-\mathfrak{t}}-{\mathfrak{C}}_{{-\mathfrak{t}}-1}|\right)
\end{cases}, \quad \text{\textcolor{red}{avg. imaginary $mae$}}
\\
\Omega_{\mathfrak{t}} &=
\begin{cases}
\text{if buy}, \quad \mathfrak{E}_{\mathfrak{t}} -(\omega_{{\mathfrak{t}}-1} \cdot s_{\mathfrak{t}} \cdot m) \\
\text{if sell}, \quad \mathfrak{E}_{\mathfrak{t}} +(\omega_{{\mathfrak{t}}-1} \cdot s_{\mathfrak{t}} \cdot m)
\end{cases},\quad \text{\textcolor{red}{projected deviated stoploss} and $m$ (10x) as multiplier (the entry is $\mathfrak{E}_{\mathfrak{t}}$)}
\end{align}
That ensures that the stoploss is always greater than the spread multiplier, $\Omega_{\mathfrak{t}} > (s_{\mathfrak{t}} \cdot m)$
Therefore, our run stop creterion is defined as
\begin{align}
\mathfrak{R}_{\dot{\mathfrak{t}}} &=
\begin{cases}
\text{if buy}, \quad \left(\left(\underbrace{\max_{\mathfrak{t} \le \dot{\mathfrak{t}} \le (\mathfrak{t}+\mathfrak{n})} \mathfrak{H}_{\dot{\mathfrak{t}}}}_{mfe} + \Omega_{\mathfrak{t}} \right) \cdot d \right)+ \Omega_{\mathfrak{t}} \\
\text{if sell}, \quad \left(\left(\underbrace{\min_{\mathfrak{t} \le \dot{\mathfrak{t}} \le (\mathfrak{t}+\mathfrak{n})} \mathfrak{L}_{\dot{\mathfrak{t}}}}_{mfe} - \Omega_{\mathfrak{t}} \right) \cdot d \right)+ \Omega_{\mathfrak{t}}
\end{cases}, \quad
\text{\textcolor{red}{run stop creterion}, where $d$ is divider (say 0.50)}
\end{align}
And the time where price crosses the opposite bound of run stop creterion is defined as:
\begin{align}
\mathfrak{C}_{\mathbb{t}} &=
\begin{cases}
\text{if buy}, \quad \mathfrak{L}_{\max(\dot{\mathfrak{t}})} \ge \mathfrak{R}_{\dot{\mathfrak{t}}} \\
\text{if sell}, \quad \mathfrak{H}_{\max(\dot{\mathfrak{t}})} \le \mathfrak{R}_{\dot{\mathfrak{t}}}
\end{cases}, \quad \text{\textcolor{red}{crossover price}, where $\mathfrak{C}$ is the deviated close price of time $\mathbb{t}$}
\end{align}
That will get the price at max timestamp indexing before the price touches the lowerbound (if buyside) or upperbound (if sellside) of $\mathfrak{R}_{\dot{\mathfrak{t}}}$
And to get the max imaginary takeprofit, we formalize it as:
\begin{align}
\mathcal{T}_{\dot{\mathbb{t}}} &=
\begin{cases}
\text{if buy}, \quad \displaystyle \max_{\mathbb{t} \ge \dot{\mathbb{t}} \ge \mathfrak{t}}(\mathfrak{H}_{\dot{\mathbb{t}}}) - (s_{\mathfrak{t}} \cdot \mathfrak{m})\\
\text{if sell}, \quad \displaystyle \min_{\mathbb{t} \ge \dot{\mathbb{t}} \ge \mathfrak{t}}(\mathfrak{L}_{\dot{\mathbb{t}}}) + (s_{\mathfrak{t}} \cdot \mathfrak{m})
\end{cases}, \quad \text{where $\mathfrak{m} (2x)$ as multiplier, and $\mathfrak{t} \in \{\mathbb{t}-\mathbb{n}\}$}, \quad \text{\textcolor{red}{max simulated takeprofit}} \\
\mathcal{R}_{\dot{\mathbb{t}}} &=
\begin{cases}
\text{if buy,} \quad \mathfrak{C}_{\mathbb{t}}^{buy} \\
\text{if sell,} \quad \mathfrak{C}_{\mathbb{t}}^{sell}
\end{cases}, \quad \text{\textcolor{red}{emergency return}} \\
\mathcal{L}_{\dot{\mathbb{t}}} &=
\begin{cases}
\text{if buy}, \quad \displaystyle \min_{\mathbb{t} \ge \dot{\mathbb{t}} \ge \mathfrak{t}}(\mathfrak{L}_{\dot{\mathbb{t}}}) \\
\text{if sell}, \quad \displaystyle \max_{\mathbb{t} \ge \dot{\mathbb{t}} \ge \mathfrak{t}}(\mathfrak{H}_{\dot{\mathbb{t}}})
\end{cases}, \quad \text{where $\mathfrak{t} \in \{\mathbb{t}-\mathbb{n}\}$}, \quad \text{\textcolor{red}{max simulated adverse-loss }}
\end{align}
That finds the max high (if buyside) or min low (if sell side) in between from $\mathfrak{P}_{\mathbb{t}}$ to $\mathfrak{E}_{\mathfrak{t}}$
And quality is defined as;
\begin{align}
\mathcal{Q}_{\mathfrak{t}} &=
\begin{cases}
\text{if buy}, \quad \displaystyle \underbrace{\min_{\mathbb{t} \ge \dot{\mathbb{t}} \ge \mathfrak{t}}(\mathfrak{L}_{\dot{\mathbb{t}}})}_{\mathcal{L}_{\dot{\mathfrak{t}}}} \cdot \frac{1}{\underbrace{\mathfrak{E}_{\mathfrak{t}} -(\omega_{{\mathfrak{t}}-1} \cdot s_{\mathfrak{t}} \cdot m)}_{\Omega_{\mathfrak{t}}}} \\
\text{if sell}, \quad \displaystyle \underbrace{\max_{\mathbb{t} \ge \dot{\mathbb{t}} \ge \mathfrak{t}}(\mathfrak{H}_{\dot{\mathbb{t}}})}_{\mathcal{L}_{\dot{\mathfrak{t}}}} \cdot \frac{1}{\underbrace{\mathfrak{E}_{\mathfrak{t}} +(\omega_{{\mathfrak{t}}-1} \cdot s_{\mathfrak{t}} \cdot m)}_{\Omega_\mathfrak{t}}}
\end{cases}, \quad
\text{\textcolor{red}{quality of adverse}}
\end{align}
where if $\mathcal{Q}_{\mathfrak{t}}$ is closer or equal to zero, then the better, $\mathcal{Q}_{\mathfrak{t}} \succeq 0$.
And we formalize our simulated risk-reward ratio as:
\begin{align}
\mathcal{Y}_{\mathfrak{t}} &=
\begin{cases}
\text{if buy,} \quad \displaystyle \left|\frac{\mathcal{R}_{\mathfrak{t}}^{buy}}{\Omega_{\mathfrak{t}}^{buy}} \right| \\
\text{if sell,} \quad \displaystyle \left|\frac{\mathcal{R}_{\mathfrak{t}}^{sell}}{\Omega_{\mathfrak{t}}^{sell}} \right|
\end{cases}, \quad \text{\textcolor{red}{simulated risk-reward ratio}}
\end{align}
Therefore we can now project the value of that entry at spcific time $\mathfrak{E}_{\mathfrak{t}}$, formalized as:
\begin{align}
\mathcal{V}_{\mathfrak{t}} &=
(
\mathfrak{E}_{\mathfrak{t}}, \Omega_{\mathfrak{t}}, \mathcal{T}_{\mathfrak{t}}, \mathcal{R}_{\mathfrak{t}}, \mathcal{L}_{\mathfrak{t}}, \mathcal{Q}_\mathfrak{t}, \mathcal{Y}_{\mathfrak{t}}
), \text{\textcolor{red}{values of simulated trade at $\mathfrak{t}$} where $\mathcal{T}_{\mathfrak{t}}=\mathcal{T}_{\dot{\mathbb{t}}}, \mathcal{R}_{\mathfrak{t}}=\mathcal{R}_{\dot{\mathbb{t}}}, \mathcal{L}_{\mathfrak{t}}=\mathcal{L}_{\dot{\mathbb{t}}} $}
\end{align}
That equation above defines a perfect trade (when enter at that time $\mathfrak{E}_{\mathfrak{t}}$) because all values are already showed at the time of entry. Before you argue that this is a product of mix loockback and lookahead, there is no problem since we are trying to make a another form of data here (the perfect trade).
The catch is that if $\mathcal{Y}_{\mathfrak{t}} \ge 1$ then $\mathcal{V}_{\mathfrak{t}}$ is valid, otherwise it's invalid and must not be included in the dataset. Therefore, of we follow this catch, then a valid $\mathcal{V}_{\mathfrak{t}}$ is always ideal and have positive expectancy.
Now lets proceed to polishing.
Since $\mathfrak{E}_{\mathfrak{t}}$ is also equavalent to 0 as deviated price. Therefore our two possible trades at time $\mathfrak{t}$ is defined as:
\begin{align}
\mathcal{V}_{\mathfrak{t}}^{buy} &=
\left(
\mathfrak{E}_{\mathfrak{t}}^{buy}, \Omega_{\mathfrak{t}}^{buy}, \mathcal{T}_\mathfrak{t}^{buy}, \mathcal{R}_{\mathfrak{t}}^{buy}, \mathcal{L}_{\mathfrak{t}}^{buy}, \mathcal{Q}_\mathfrak{t}^{buy}, \mathcal{Y}_\mathfrak{t}^{buy}
\right) \\
\mathcal{V}_{\mathfrak{t}}^{sell} &=
\left(
\mathfrak{E}_{\mathfrak{t}}^{sell}, \Omega_{\mathfrak{t}}^{sell}, \mathcal{T}_\mathfrak{t}^{sell}, \mathcal{R}_{\mathfrak{t}}^{sell}, \mathcal{L}_{\mathfrak{t}}^{sell}, \mathcal{Q}_\mathfrak{t}^{sell}, \mathcal{Y}_\mathfrak{t}^{sell}
\right)
\end{align}
First, we need to make all variables of $\mathcal{V}_{\mathfrak{t}}^{sell}$ into into reverse becuase the favorable side is the opposite of its value (i.e. the loss is located at positive and the return is located at the negative, if we follow the formula discussed from the very beginning), therefore we formalize that as
\begin{align}
\mathcal{V}_{\mathfrak{t}}^{{sell},{r}} &=
\text{Rev}
(
\mathfrak{E}_{\mathfrak{t}}, \Omega_{\mathfrak{t}}, \mathcal{T}_{\mathfrak{t}}, \mathcal{R}_{\mathfrak{t}}, \mathcal{L}_{\mathfrak{t}}, \mathcal{Q}_\mathfrak{t}, \mathcal{Y}_{\mathfrak{t}}
) =
(
-\mathfrak{E}_{\mathfrak{t}}, -\Omega_{\mathfrak{t}}, -\mathcal{T}_{\mathfrak{t}}, -\mathcal{R}_{\mathfrak{t}}, -\mathcal{L}_{\mathfrak{t}}, -\mathcal{Q}_\mathfrak{t}, -\mathcal{Y}_{\mathfrak{t}}
), \quad \text{where $r$ is notation for $reverse$}
\end{align}
then let $v$ be the invididual variables of $\mathcal{V}$, and we formalize our points calculator as:
\begin{align}
\text{Point}\left(v_{\mathfrak{t}}^{buy}, v_{\mathfrak{t}}^{sell}\right) &=
\begin{cases}
(1,1) & \text{if } \mathfrak{E}_{\mathfrak{t}}^{buy} \gtreqless \mathfrak{E}_{\mathfrak{t}}^{{sell},r} \\
(1,1) & \text{if } \Omega_{\mathfrak{t}}^{buy} \gtreqless \Omega_{\mathfrak{t}}^{{sell},r} \\
(1,0) & \text{if } \mathcal{T}_{\mathfrak{t}}^{buy} > \mathcal{T}_{\mathfrak{t}}^{{sell},r} \\
(0,1) & \text{if } \mathcal{T}_{\mathfrak{t}}^{buy} < \mathcal{T}_{\mathfrak{t}}^{{sell},r} \\
(1,0) & \text{if } \mathcal{R}_{\mathfrak{t}}^{buy} > \mathcal{R}_{\mathfrak{t}}^{{sell},r} \\
(0,1) & \text{if } \mathcal{R}_{\mathfrak{t}}^{buy} < \mathcal{R}_{\mathfrak{t}}^{{sell},r} \\
(0,1) & \text{if } \mathcal{L}_{\mathfrak{t}}^{buy} > \mathcal{L}_{\mathfrak{t}}^{{sell},r} \\
(1,0) & \text{if } \mathcal{L}_{\mathfrak{t}}^{buy} < \mathcal{L}_{\mathfrak{t}}^{{sell},r} \\
(0,1) & \text{if } \mathcal{Q}_\mathfrak{t}^{buy} > \mathcal{Q}_\mathfrak{t}^{{sell},r} \\
(1,0) & \text{if } \mathcal{Q}_\mathfrak{t}^{buy} < \mathcal{Q}_\mathfrak{t}^{{sell},r} \\
(1,0) & \text{if } \mathcal{Y}_\mathfrak{t}^{buy} > \mathcal{Y}_\mathfrak{t}^{{sell},r} \\
(0,1) & \text{if } \mathcal{Y}_\mathfrak{t}^{buy} < \mathcal{Y}_\mathfrak{t}^{{sell},r} \\
\end{cases}
\end{align}
Therefore our scoring is formalized as:
\begin{align}
\text{Favor} \left(\mathcal{V}_{\mathfrak{t}}^{buy},\mathcal{V}_{\mathfrak{t}}^{sell}\right) &=
\begin{cases}
(1,0) & \text{if } \displaystyle\sum_{\text{all } v} \text{Point}\left(v_{\mathfrak{t}}^{buy}\right) > \sum_{\text{all } v} \text{Point}\left(v_{\mathfrak{t}}^{sell}\right) \\
(0,1) & \text{if } \displaystyle\sum_{\text{all } v} \text{Point}\left(v_{\mathfrak{t}}^{buy}\right) < \sum_{\text{all } v} \text{Point}\left(v_{\mathfrak{t}}^{sell}\right)
\end{cases}
\end{align}
That equation above will tell us whether if it is best to buy or sell at that time $\mathfrak{t}$
Say we want to know wheter it is best to buy or sell at times $\mathfrak{t}_1, \mathfrak{t}_2, \mathfrak{t}_3, \mathfrak{t}_4, \mathfrak{t}_5$, and we calculate each $\text{Favor} \left(\mathcal{V}_{\mathfrak{t}}^{buy},\mathcal{V}_{\mathfrak{t}}^{sell}\right)$ at that time, then we get something like; as example:
\begin{align}
\mathcal{V}_{\mathfrak{t}_1}^{sell}, \mathcal{V}_{\mathfrak{t}_2}^{sell}, \mathcal{V}_{\mathfrak{t}_3}^{buy}, \mathcal{V}_{\mathfrak{t}_4}^{sell}, \mathcal{V}_{\mathfrak{t}_5}^{buy}
\end{align}
where each time contains rich information of
\begin{align}
\mathcal{V}_{\mathfrak{t}} &=
(
\mathfrak{E}_{\mathfrak{t}}, \Omega_{\mathfrak{t}}, \mathcal{T}_{\mathfrak{t}}, \mathcal{R}_{\mathfrak{t}}, \mathcal{L}_{\mathfrak{t}}, \mathcal{Q}_\mathfrak{t}, \mathcal{Y}_{\mathfrak{t}}
),
\end{align}
Moving on, since we stated "\textit{if $\mathcal{Y}_{\mathfrak{t}} \ge 1$ then $\mathcal{V}_{\mathfrak{t}}$ is valid, otherwise it's invalid and must not be included in the dataset}" that mean we will be having three possible outcome for each $\mathfrak{t}$, which is defined as:
\begin{align}
T^{valid}_{\mathfrak{t}} &\in \{\mathcal{V}_{\mathfrak{t}}^{buy},\mathcal{V}_{\mathfrak{t}}^{sell}\}, \quad \text{where $T^{valid}$ means there is a Trade on that time} \\
T^{null}_{\mathfrak{t}} &\notin \{\mathcal{V}_{\mathfrak{t}}^{buy},\mathcal{V}_{\mathfrak{t}}^{sell}\}, \quad \text{where $T^{null}$ there is no Trade on that time}
\end{align}
Therefore we get something like this:
\begin{align}
T^{valid}_{\mathfrak{t}_1},T^{valid}_{\mathfrak{t}_2},T^{valid}_{\mathfrak{t}_3},T^{valid}_{\mathfrak{t}_4},T^{valid}_{\mathfrak{t}_5},T^{null}_{\mathfrak{t}_6},T^{valid}_{\mathfrak{t}_7},T^{null}_{\mathfrak{t}_8},T^{null}_{\mathfrak{t}_9},T^{valid}_{\mathfrak{t}_{10}}
\end{align}
\end{document}
|