Skip to content

Commit

Permalink
Implemented ArnaudLegouxMAVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jul 12, 2021
1 parent 5844fba commit 62354a7
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 0 deletions.
113 changes: 113 additions & 0 deletions docs/HTML/ArnaudLegouxMAVisitor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<!--
Copyright (c) 2019-2022, Hossein Moein
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Hossein Moein and/or the DataFrame nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Hossein Moein BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<!DOCTYPE html>
<html>
<body>
<table align="center" border="1">

<tr bgcolor="lightblue">
<th>Signature</th> <th>Description</th> <th>Parameters</th>
</tr>
<tr bgcolor="lightgrey">
<td bgcolor="maroon"> <font color="white">
<PRE><B>#include &lt;DataFrame/DataFrameFinancialVisitors.h&gt;

template&lt;typename T, typename I = unsigned long&gt;
struct ArnaudLegouxMAVisitor;

// -------------------------------------

template&lt;typename T, typename I = unsigned long&gt;
using alma_v = ArnaudLegouxMAVisitor&lt;T, I&gt;;
</B></PRE></font>
</td>
<td>
This is a “single action visitor”, meaning it is passed the whole data vector in one call and you must use the single_act_visit() interface.<BR><BR>
This visitor calculates the rolling values of Arnaud Legoux Moving Average. It requires 1 input column.<BR>
The result is a vector of values with same number of items as the given column. The first roll_count items, in the result, will be NAN.<BR>
The ALMA is a technical analysis tool that aims to give investors and traders a more reliable trading signal by reducing the noise that can interfere with traditional moving averages. It eliminates the small fluctuations in an asset price to make the trend clearer. The ALMA reduces price lag and creates a smoother line than other moving averages. Rather than a straightforward moving average for a certain period, it applies a moving average twice – from left to right as well as right to left.<BR>
<I>
<PRE>
explicit
ArnaudLegouxMAVisitor(size_t roll_count = 10, T sigma = 6.0, T dist_offset = 0.85)

<B>roll_count</B>: Moving average period
<B>sigma</B>: The standard deviation applied to the combo line. It makes the combo line sharper.
<B>dis_offset</B>: Offset is the Gaussian applied to the combo line and it is 0.85 by default.
Setting offset at 1 makes it fully aligned to the current price just
like the exponential moving average.
</PRE>
</I>
</td>
<td width="12%">
<B>T</B>: Column data type<BR>
<B>I</B>: Index type
</td>
</tr>

</table>

<pre style='color:#000000;background:#ffffff;'><span style='color:#800000; font-weight:bold; '>static</span> <span style='color:#800000; font-weight:bold; '>void</span> test_ArnaudLegouxMAVisitor<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span> <span style='color:#800080; '>{</span>

<span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>cout</span> <span style='color:#808030; '>&lt;</span><span style='color:#808030; '>&lt;</span> <span style='color:#800000; '>"</span><span style='color:#0f69ff; '>\n</span><span style='color:#0000e6; '>Testing ArnaudLegouxMAVisitor{ } ...</span><span style='color:#800000; '>"</span> <span style='color:#808030; '>&lt;</span><span style='color:#808030; '>&lt;</span> <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>endl</span><span style='color:#800080; '>;</span>

<span style='color:#800000; font-weight:bold; '>typedef</span> StdDataFrame<span style='color:#800080; '>&lt;</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>string</span><span style='color:#800080; '>></span> StrDataFrame<span style='color:#800080; '>;</span>

StrDataFrame df<span style='color:#800080; '>;</span>

<span style='color:#800000; font-weight:bold; '>try</span> <span style='color:#800080; '>{</span>
df<span style='color:#808030; '>.</span>read<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>data/SHORT_IBM.csv</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> io_format<span style='color:#800080; '>::</span>csv2<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

alma_v<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#808030; '>,</span> <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>string</span><span style='color:#800080; '>></span> alma<span style='color:#800080; '>;</span>

df<span style='color:#808030; '>.</span>single_act_visit<span style='color:#800080; '>&lt;</span><span style='color:#800000; font-weight:bold; '>double</span><span style='color:#800080; '>></span><span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>IBM_Close</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> alma<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

assert<span style='color:#808030; '>(</span>alma<span style='color:#808030; '>.</span>get_result<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>.</span>size<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span> <span style='color:#808030; '>=</span><span style='color:#808030; '>=</span> <span style='color:#008c00; '>1721</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
assert<span style='color:#808030; '>(</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>isnan<span style='color:#808030; '>(</span>alma<span style='color:#808030; '>.</span>get_result<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>[</span><span style='color:#008c00; '>0</span><span style='color:#808030; '>]</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
assert<span style='color:#808030; '>(</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span>isnan<span style='color:#808030; '>(</span>alma<span style='color:#808030; '>.</span>get_result<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>[</span><span style='color:#008c00; '>9</span><span style='color:#808030; '>]</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
assert<span style='color:#808030; '>(</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>abs</span><span style='color:#808030; '>(</span>alma<span style='color:#808030; '>.</span>get_result<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>[</span><span style='color:#008c00; '>10</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>-</span> <span style='color:#008000; '>187.533</span><span style='color:#808030; '>)</span> <span style='color:#808030; '>&lt;</span> <span style='color:#008000; '>0.001</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
assert<span style='color:#808030; '>(</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>abs</span><span style='color:#808030; '>(</span>alma<span style='color:#808030; '>.</span>get_result<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>[</span><span style='color:#008c00; '>14</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>-</span> <span style='color:#008000; '>186.359</span><span style='color:#808030; '>)</span> <span style='color:#808030; '>&lt;</span> <span style='color:#008000; '>0.001</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
assert<span style='color:#808030; '>(</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>abs</span><span style='color:#808030; '>(</span>alma<span style='color:#808030; '>.</span>get_result<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>[</span><span style='color:#008c00; '>25</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>-</span> <span style='color:#008000; '>176.892</span><span style='color:#808030; '>)</span> <span style='color:#808030; '>&lt;</span> <span style='color:#008000; '>0.001</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
assert<span style='color:#808030; '>(</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>abs</span><span style='color:#808030; '>(</span>alma<span style='color:#808030; '>.</span>get_result<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>[</span><span style='color:#008c00; '>1720</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>-</span> <span style='color:#008000; '>117.841</span><span style='color:#808030; '>)</span> <span style='color:#808030; '>&lt;</span> <span style='color:#008000; '>0.001</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
assert<span style='color:#808030; '>(</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>abs</span><span style='color:#808030; '>(</span>alma<span style='color:#808030; '>.</span>get_result<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>[</span><span style='color:#008c00; '>1712</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>-</span> <span style='color:#008000; '>127.677</span><span style='color:#808030; '>)</span> <span style='color:#808030; '>&lt;</span> <span style='color:#008000; '>0.001</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
assert<span style='color:#808030; '>(</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>abs</span><span style='color:#808030; '>(</span>alma<span style='color:#808030; '>.</span>get_result<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>[</span><span style='color:#008c00; '>1707</span><span style='color:#808030; '>]</span> <span style='color:#808030; '>-</span> <span style='color:#008000; '>121.435</span><span style='color:#808030; '>)</span> <span style='color:#808030; '>&lt;</span> <span style='color:#008000; '>0.001</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
<span style='color:#800080; '>}</span>
<span style='color:#800000; font-weight:bold; '>catch</span> <span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>const</span> DataFrameError <span style='color:#808030; '>&amp;</span>ex<span style='color:#808030; '>)</span> <span style='color:#800080; '>{</span>
<span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>cout</span> <span style='color:#808030; '>&lt;</span><span style='color:#808030; '>&lt;</span> ex<span style='color:#808030; '>.</span>what<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span> <span style='color:#808030; '>&lt;</span><span style='color:#808030; '>&lt;</span> <span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>endl</span><span style='color:#800080; '>;</span>
<span style='color:#800080; '>}</span>
<span style='color:#800080; '>}</span>
</pre>
<!--Created using ToHtml.com on 2021-07-12 15:55:00 UTC -->

</body>
</html>

<!--
Local Variables:
mode:HTML
tab-width:4
c-basic-offset:4
End:
-->
4 changes: 4 additions & 0 deletions docs/HTML/DataFrame.html
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,10 @@ <H2><font color="blue">Table of Features (with Code Samples)</font></H2>
<th><font color="white">DataFrame Built-in Financial Visitors</font></th>
</tr>

<tr class="item" onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
<td title="Calculates Arnaud Legoux Moving Average">struct <a href="https://htmlpreview.github.io/?https://github.com/hosseinmoein/DataFrame/blob/master/docs/HTML/ArnaudLegouxMAVisitor.html">ArnaudLegouxMAVisitor</a>{ }</td>
</tr>

<tr class="item" onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
<td title="Calculates Average Directional Movement Index (ADX)">struct <a href="https://htmlpreview.github.io/?https://github.com/hosseinmoein/DataFrame/blob/master/docs/HTML/AvgDirMovIdxVisitor.html">AvgDirMovIdxVisitor</a>{ }</td>
</tr>
Expand Down
69 changes: 69 additions & 0 deletions include/DataFrame/DataFrameFinancialVisitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -3479,6 +3479,75 @@ struct CenterOfGravityVisitor {
template<typename T, typename I = unsigned long>
using cog_v = CenterOfGravityVisitor<T, I>;

// ----------------------------------------------------------------------------

// Arnaud Legoux Moving Average
//
template<typename T, typename I = unsigned long>
struct ArnaudLegouxMAVisitor {

DEFINE_VISIT_BASIC_TYPES_3

template <typename K, typename H>
inline void
operator() (const K &idx_begin,
const K &idx_end,
const H &column_begin,
const H &column_end) {

if (roll_count_ == 0) return;

GET_COL_SIZE
assert (roll_count_ > 1);
assert (col_s > roll_count_);

result_type result (col_s, std::numeric_limits<T>::quiet_NaN());

for (size_type i = roll_count_; i < col_s; ++i) {
value_type win_sum { 0 };

for (size_type j = 0; j < roll_count_; ++j)
win_sum += wtd_[j] * *(column_begin + (i - j));

result[i] = win_sum / cum_sum_;
}

result_.swap(result);
}

DEFINE_PRE_POST
DEFINE_RESULT

explicit
ArnaudLegouxMAVisitor(size_type r_count = 10,
value_type sigma = 6.0,
value_type dist_offset = 0.85)
: roll_count_(r_count),
m_(dist_offset * T(r_count - 1)),
s_(T(r_count) / sigma),
wtd_(r_count) {

for (size_type i = 0; i < roll_count_; ++i) {
wtd_[i] =
std::exp(T(-1) *
((T(i) - m_) * (T(i) - m_)) /
((T(2) * s_ * s_)));
cum_sum_ += wtd_[i];
}
}

private:

const size_type roll_count_;
const value_type m_;
const value_type s_;
value_type cum_sum_ { 0 };
std::vector<value_type> wtd_;
result_type result_ { };
};

template<typename T, typename I = unsigned long>
using alma_v = ArnaudLegouxMAVisitor<T, I>;

} // namespace hmdf

Expand Down
34 changes: 34 additions & 0 deletions test/dataframe_tester_2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3641,6 +3641,39 @@ static void test_CenterOfGravityVisitor() {
}
}

// -----------------------------------------------------------------------------

static void test_ArnaudLegouxMAVisitor() {

std::cout << "\nTesting ArnaudLegouxMAVisitor{ } ..." << std::endl;

typedef StdDataFrame<std::string> StrDataFrame;

StrDataFrame df;

try {
df.read("data/SHORT_IBM.csv", io_format::csv2);

alma_v<double, std::string> alma;

df.single_act_visit<double>("IBM_Close", alma);

assert(alma.get_result().size() == 1721);
assert(std::isnan(alma.get_result()[0]));
assert(std::isnan(alma.get_result()[9]));
assert(std::abs(alma.get_result()[10] - 187.533) < 0.001);
assert(std::abs(alma.get_result()[14] - 186.359) < 0.001);
assert(std::abs(alma.get_result()[25] - 176.892) < 0.001);
assert(std::abs(alma.get_result()[1720] - 117.841) < 0.001);
assert(std::abs(alma.get_result()[1712] - 127.677) < 0.001);
assert(std::abs(alma.get_result()[1707] - 121.435) < 0.001);
}
catch (const DataFrameError &ex) {
std::cout << ex.what() << std::endl;
}
}


// -----------------------------------------------------------------------------
int main(int argc, char *argv[]) {

Expand Down Expand Up @@ -3712,6 +3745,7 @@ int main(int argc, char *argv[]) {
test_HeikinAshiCndlVisitor();
test_FastFourierTransVisitor();
test_CenterOfGravityVisitor();
test_ArnaudLegouxMAVisitor();

return (0);
}
Expand Down
2 changes: 2 additions & 0 deletions test/dataframe_tester_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1638,3 +1638,5 @@ Testing FastFourierTransVisitor{ } ...
(8.88178e-16,-4.44089e-16) | (1,1) | (3,3) | (4,4) | (4,4) | (3,3) | (1,1) |

Testing CenterOfGravityVisitor{ } ...

Testing ArnaudLegouxMAVisitor{ } ...

0 comments on commit 62354a7

Please sign in to comment.