If I wanted to create a schedule function that did not use time as it's decision of when to execute the code, how would I do that?
I want the schedule function to execute based on parameters I set previously, not associated with time.
My sloppy code looks like this now... having syntax errors, no updated backtest...
context.security = sid(8554)
""" The problem is this schedule function situation gets called on hours, minutes, market open, market close etc... but does not execute when the conditions of the trader function are met... it executes when the hour happens, when the minute happens, when market open or market close happens etc... it needs to execute when the event occurs, in this case the crossover, the "x", formed by the mavg_1 and mavg_2 signifying a change in the market going up or down or sideways. """
"""Also I want to be able to only execute the trader function when I do not have a position, in other words I do not want to take a position if I already have a position in the underlying security, in this case SPY. The following code is an attempt to describe that situation. """
# position = context.portfolio.positions
# if position > abs(2000000):
# return True
#do go to the trader function below
price_hist_2 = data.history(context.security, 'price', 9, '1m') mavg_1 = price_hist_2.mean() price_hist = data.history(context.security, 'price', 18, '1m') mavg_2 = price_hist.mean()
"""Here I need to calculate the changing slope of mavg_1 and mavg_2. When the slope of mavg_1 begins to become negative after the slope has been positive, a downward moving market should be coming, and the "x". When the slope of mavg_1 becomes positive after being negative, an upward moving market should be coming, and the "x". The shift in slope of the mavg_1 being positive then negative then positive then negative and so on is where you capture your profit, you're making profit in the change in location of the "x". The crossover of the moving averages indicates the long or short market... When the lines intersect, you trade. When the lines don't intersect but are still moving with coresponding slopes indicative of the direction you are positioned for, the code executes on a small percentage of profit, continuously, to capute profit. """
slope_1 = changing slope of mavg_1 slope_2 = changing slope of mavg_2 cost_basis = context.portfolio.positions[context.security].cost_basis current_price = context.portfolio.positions[context.security].last_sale_price account_value = context.portfolio.portfolio_value if mavg_2 < mavg_1 and : order_percent(context.security, .10) log.info("Long") if mavg_2 > mavg_1: order_percent(context.security, -.10) log.info("Short")
"""Here is me trying to execute the small percentage change of profit by exiting all positions at a .0625 % gain"""
elif current_price >= cost_basis * 1.0625 or current_price <= cost_basis * .9375: order_percent(context.security, 0.0) log.info("exiting")
If you look at the transaction details in the backtest you will see it's only trading one time a day, 2 minutes after market open, rather than through out the day whenever the crossover of the moving averages occurs.
I guess my main question is, how do you program a "Schedule Function" without using time to determine when it executes, instead of using time it would use any other indicator.
|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|