Stock Trend Following Model in RapidMiner

Recently I discovered in my old archives a stock trend following model I started in RapidMiner. I never finished it back then but now I cleaned it up. I’m releasing it to the world as beta (use at your own risk) and will probably have some bugs” in it. The trend following model is based on Blackstar Funds, LLC paper Does Trend Following Work on Stocks?

The process is not production ready and there are some clean up things I need to do. I need to write a function that will cleanly output the ATR stops so I can chart them like in the paper.

Of course, the ultimate goal is to loop over a daily list of 52 week new highs and spit out recommendations, but that’s for another post and another process altogether.

Please note, you’ll need the Finance/Economics and Series extensions. You can get them at the RapidMiner marketplace.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<process version="7.1.001">
<context>
<input/>
<output/>
<macros>
<macro>
<key>period</key>
<value>25</value>
</macro>
<macro>
<key>symbol</key>
<value>PAAS</value>
</macro>
<macro>
<key>start_date</key>
<value>2014-01-01</value>
</macro>
<macro>
<key>end_date</key>
<value>2016-05-20</value>
</macro>
<macro>
<key>shares</key>
<value>10</value>
</macro>
</macros>
</context>
<operator activated="true" class="process" compatibility="7.1.001" expanded="true" name="Process">
<process expanded="true">
<operator activated="true" class="quantx1:yahoo_historical_data_extractor" compatibility="1.0.006" expanded="true" height="82" name="Yahoo Historical Stock Data" width="90" x="45" y="34">
<parameter key="I agree to abide by Yahoo's Terms &amp; Conditions on financial data usage" value="true"/>
<parameter key="Quick Stock Ticker Data" value="true"/>
<parameter key="Stock Ticker" value="%{symbol}"/>
<parameter key="select_fields" value="VOLUME|OPEN|DAY_LOW|DAY_HIGH|CLOSE|ADJUSTED_CLOSE"/>
<parameter key="date_format" value="yyyy-MM-dd"/>
<parameter key="date_start" value="%{start_date}"/>
<parameter key="date_end" value="%{end_date}"/>
<parameter key="Cache Data in Memory" value="true"/>
</operator>
<operator activated="true" class="rename" compatibility="7.1.001" expanded="true" height="82" name="Rename" width="90" x="45" y="187">
<parameter key="old_name" value="%{symbol}_CLOSE"/>
<parameter key="new_name" value="Close"/>
<list key="rename_additional_attributes">
<parameter key="%{symbol}_DAY_HIGH" value="High"/>
<parameter key="%{symbol}_DAY_LOW" value="Low"/>
<parameter key="%{symbol}_OPEN" value="Open"/>
<parameter key="%{symbol}_VOLUME" value="Volume"/>
</list>
</operator>
<operator activated="true" class="date_to_numerical" compatibility="7.1.001" expanded="true" height="82" name="Date to Numerical" width="90" x="179" y="187">
<parameter key="attribute_name" value="Date"/>
<parameter key="time_unit" value="day"/>
<parameter key="day_relative_to" value="week"/>
<parameter key="keep_old_attribute" value="true"/>
</operator>
<operator activated="true" class="filter_examples" compatibility="7.1.001" expanded="true" height="103" name="Filter Examples" width="90" x="313" y="187">
<list key="filters_list">
<parameter key="filters_entry_key" value="Date_day.eq.6"/>
</list>
</operator>
<operator activated="true" class="series:lag_series" compatibility="5.3.000" expanded="true" height="82" name="Lag Series" width="90" x="313" y="34">
<list key="attributes">
<parameter key="Close" value="1"/>
</list>
</operator>
<operator activated="true" class="generate_attributes" compatibility="7.1.001" expanded="true" height="82" name="Generate Attributes" width="90" x="447" y="34">
<list key="function_descriptions">
<parameter key="High_Low" value="High-Low"/>
<parameter key="Abs_High" value="abs(High-[Close-1])"/>
<parameter key="Abs_Low" value="abs(Low-[Close-1])"/>
<parameter key="TR" value="max(High_Low,Abs_High,Abs_Low)"/>
</list>
</operator>
<operator activated="true" class="series:moving_average" compatibility="5.3.000" expanded="true" height="82" name="Moving Average" width="90" x="581" y="34">
<parameter key="attribute_name" value="TR"/>
<parameter key="window_width" value="%{period}"/>
</operator>
<operator activated="true" class="generate_attributes" compatibility="7.1.001" expanded="true" height="82" name="Generate Attributes (2)" width="90" x="715" y="34">
<list key="function_descriptions">
<parameter key="Stop" value="Low-[average(TR)]"/>
</list>
</operator>
<operator activated="true" class="series:lag_series" compatibility="5.3.000" expanded="true" height="82" name="Lag Series (2)" width="90" x="849" y="34">
<list key="attributes">
<parameter key="Stop" value="1"/>
</list>
</operator>
<operator activated="true" class="generate_attributes" compatibility="7.1.001" expanded="true" height="82" name="Generate Attributes (3)" width="90" x="983" y="34">
<list key="function_descriptions">
<parameter key="Trend" value="if([Stop-1]&lt;Stop,&quot;UP&quot;,&quot;DOWN&quot;)"/>
</list>
</operator>
<operator activated="true" class="filter_example_range" compatibility="7.1.001" expanded="true" height="82" name="Filter Example Range" width="90" x="1117" y="34">
<parameter key="first_example" value="1"/>
<parameter key="last_example" value="%{period}"/>
<parameter key="invert_filter" value="true"/>
</operator>
<operator activated="true" class="series:lag_series" compatibility="5.3.000" expanded="true" height="82" name="Lag Series (3)" width="90" x="1251" y="34">
<list key="attributes">
<parameter key="Trend" value="1"/>
</list>
</operator>
<operator activated="true" class="generate_attributes" compatibility="7.1.001" expanded="true" height="82" name="Generate Attributes (4)" width="90" x="1251" y="238">
<list key="function_descriptions">
<parameter key="Rolling Cash" value="if((Trend==&quot;UP&quot; &amp;&amp; [Trend-1]==&quot;UP&quot;), ((Close-[Close-1])*100), ((Close-[Close-1])*100))"/>
<parameter key="Signal" value="if((Trend==&quot;UP&quot;) &amp;&amp; ([Trend-1]==&quot;UP&quot;) &amp;&amp; ([Close-1]&lt;Close),&quot;Buy&quot;,&quot;Sell&quot;)"/>
</list>
</operator>
<operator activated="true" class="multiply" compatibility="7.1.001" expanded="true" height="103" name="Multiply" width="90" x="1385" y="238"/>
<operator activated="true" class="generate_attributes" compatibility="7.1.001" expanded="true" height="82" name="Generate Attributes (5)" width="90" x="1519" y="238">
<list key="function_descriptions">
<parameter key="Price to Stop" value="(Close-Stop)*eval(%{shares})"/>
<parameter key="Entry Cost" value="(Close)*eval(%{shares})"/>
<parameter key="Risk Pct" value="[Price to Stop] / [Entry Cost]"/>
<parameter key="Modified Signal" value="if(([Risk Pct]&lt;0.1) &amp;&amp; (Trend==&quot;UP&quot;) &amp;&amp; ([Trend-1]==&quot;UP&quot;),&quot;BUY&quot;,&quot;SELL&quot;)"/>
</list>
</operator>
<operator activated="true" class="aggregate" compatibility="7.1.001" expanded="true" height="82" name="Aggregate" width="90" x="1519" y="34">
<list key="aggregation_attributes">
<parameter key="Rolling Cash" value="sum"/>
</list>
<parameter key="group_by_attributes" value="Trend"/>
</operator>
<operator activated="true" class="select_attributes" compatibility="7.1.001" expanded="true" height="82" name="Select Attributes" width="90" x="1653" y="340">
<parameter key="attribute_filter_type" value="subset"/>
<parameter key="attributes" value="Close|Date|Risk Pct|Signal|Stop|Trend|average(TR)|Modified Signal"/>
</operator>
<connect from_op="Yahoo Historical Stock Data" from_port="example set" to_op="Rename" to_port="example set input"/>
<connect from_op="Rename" from_port="example set output" to_op="Date to Numerical" to_port="example set input"/>
<connect from_op="Date to Numerical" from_port="example set output" to_op="Filter Examples" to_port="example set input"/>
<connect from_op="Filter Examples" from_port="example set output" to_op="Lag Series" to_port="example set input"/>
<connect from_op="Lag Series" from_port="example set output" to_op="Generate Attributes" to_port="example set input"/>
<connect from_op="Generate Attributes" from_port="example set output" to_op="Moving Average" to_port="example set input"/>
<connect from_op="Moving Average" from_port="example set output" to_op="Generate Attributes (2)" to_port="example set input"/>
<connect from_op="Generate Attributes (2)" from_port="example set output" to_op="Lag Series (2)" to_port="example set input"/>
<connect from_op="Lag Series (2)" from_port="example set output" to_op="Generate Attributes (3)" to_port="example set input"/>
<connect from_op="Generate Attributes (3)" from_port="example set output" to_op="Filter Example Range" to_port="example set input"/>
<connect from_op="Filter Example Range" from_port="example set output" to_op="Lag Series (3)" to_port="example set input"/>
<connect from_op="Lag Series (3)" from_port="example set output" to_op="Generate Attributes (4)" to_port="example set input"/>
<connect from_op="Generate Attributes (4)" from_port="example set output" to_op="Multiply" to_port="input"/>
<connect from_op="Multiply" from_port="output 1" to_op="Aggregate" to_port="example set input"/>
<connect from_op="Multiply" from_port="output 2" to_op="Generate Attributes (5)" to_port="example set input"/>
<connect from_op="Generate Attributes (5)" from_port="example set output" to_op="Select Attributes" to_port="example set input"/>
<connect from_op="Aggregate" from_port="example set output" to_port="result 1"/>
<connect from_op="Select Attributes" from_port="example set output" to_port="result 2"/>
<portSpacing port="source_input 1" spacing="0"/>
<portSpacing port="sink_result 1" spacing="0"/>
<portSpacing port="sink_result 2" spacing="0"/>
<portSpacing port="sink_result 3" spacing="0"/>
</process>
  </operator>
</process>


Stocks tutorials RapidMiner


Previous post
This Week's S&P500 Historical Volatility Prediction The $SPX’s 5 day Historical Volatility (HV) on May 20, 2016 was: 0.121. The model is predicting that this value will INCREASE by next Friday. The
Next post
This Week's S&P500 Historical Volatility Prediction The $SPX’s 5 day Historical Volatility (HV) on May 27, 2016 was: 0.099. The model is predicting that this value will DECREASE by next Friday. The