Hello,

I would like to create a rolling window of theil-sen estimator median slope values for use in entry triggers but I am still not too familiar with python / coding in general so I've run into some difficulties.

Is there a way to create a rolling window of tse (medslope) values that can be accessed in a similar fashion to talib.EMA values?

Ideally, I would like to be able to access tse (medslope) values the same way I do with ema values:

If tse[-1] > tse[-2]:

#do something like setting weight to 0.95

Attached is some code I've written to show where I am at so far:

I only know how to generate and access the current bar's TSE medslope value using the scipy theilslopes function.

I appreciate any and all help.

Clone Algorithm

4

Loading...

There was an error loading this backtest.

Backtest from
to
with
initial capital

Cumulative performance:

Algorithm
Benchmark

Custom data:

Total Returns

--

Alpha

--

Beta

--

Sharpe

--

Sortino

--

Max Drawdown

--

Benchmark Returns

--

Volatility

--

Returns | 1 Month | 3 Month | 6 Month | 12 Month |

Alpha | 1 Month | 3 Month | 6 Month | 12 Month |

Beta | 1 Month | 3 Month | 6 Month | 12 Month |

Sharpe | 1 Month | 3 Month | 6 Month | 12 Month |

Sortino | 1 Month | 3 Month | 6 Month | 12 Month |

Volatility | 1 Month | 3 Month | 6 Month | 12 Month |

Max Drawdown | 1 Month | 3 Month | 6 Month | 12 Month |

from quantopian.algorithm import attach_pipeline, pipeline_output from quantopian.pipeline import Pipeline from quantopian.pipeline.data.builtin import USEquityPricing from quantopian.pipeline.factors import AverageDollarVolume from quantopian.pipeline.filters.morningstar import Q1500US import numpy as np import pandas as pd import talib from scipy import stats def initialize(context):#------------------------------------------------------------------------ context.eject_switch = 0 context.spylong = sid(38533) #context.spyshort = sid(38532) total_minutes = (6 * 60) + 15 for i in range(1,total_minutes): if i % 5 == 0: schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(minutes=i), True) schedule_function(abandon_ship, date_rules.every_day(), time_rules.market_close(minutes=10), True) schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close()) def my_assign_weights(context, data):#----------------------------------------------------------- weight = 0 closes_1m = data.history(context.spylong,'close',390,'1m') fast_ema_1m = talib.EMA(closes_1m.dropna(),timeperiod = 8) tse_input = closes_1m.dropna()[-10:-1] tse = stats.mstats.theilslopes(tse_input,np.arange(len(tse_input)))[-4] if fast_ema_1m[-1] > fast_ema_1m[-2]: weight = 0.95 #What I would like to be able to do: #if the current tse value > the tse value for the previous bar: #weight = 0.95 return weight def target_stop_exit(context,data):#---------------------------------------------------------------- security = context.spylong maximum_profit = 0.001 maximum_loss = 0.001 verdict = "" if context.portfolio.positions[security].amount > 0: amount = context.portfolio.positions[security].amount price = data.current(security,'price') current_value = amount * price paid_value = context.portfolio.positions[security].amount * context.portfolio.positions[security].cost_basis if current_value >= paid_value * (1 + maximum_profit): verdict = "target" if current_value <= paid_value * (1 - maximum_loss): verdict = "stop" return verdict def cancel(context,data):#----------------------------------------------------------------------- oo = get_open_orders() for sec in oo: for order in oo[sec]: cancel_order(order.id) def my_rebalance(context,data):#----------------------------------------------------------------- weight = my_assign_weights(context,data) if context.portfolio.positions[context.spylong].amount == 0 and len(get_open_orders(context.spylong)) == 0: order_target_percent(context.spylong,weight) if weight > 0: print "Opening Position" if context.portfolio.positions > 0 and len(get_open_orders(context.spylong)) == 0: if target_stop_exit(context,data) == "target": #cancel(context,data) order_target_percent(context.spylong,0.0) print "Exiting at target" elif target_stop_exit(context,data) == "stop": #cancel(context,data) order_target_percent(context.spylong,0.0) print "Exiting at stop" #print weight def my_record_vars(context, data):#-------------------------------------------------------------- pass def before_trading_start(context, data):#-------------------------------------------------------- context.eject_switch = 0 def abandon_ship(context, data):#------------------------------------------------------------- cancel(context,data) print "Abandoning Ship!" order_target_percent(context.spylong,0.0) def handle_data(context,data):#------------------------------------------------------------------ pass