Back to Community
How do I implement "Wait" part?

I wanted to implement something as under.


if MAV(5)>MAV(14)
wait for 5 days.
if again MAV(5)>MAV(14)
Order Buy.


Now I am not sure how to get this "wait" part implemented
any pointer will be appreceated.

7 responses

You could create variable inside initialize() that can be used to track days since the trigger. (Note: This is only pseudo code to help get the idea across.)

# Number of days since trigger  
context.timer = 0  

Then schedule() a function to run once a day with this logic:

if MAV(5)>MAV(14):  
    # Add one day to timer  
    context.timer += 1  
    if context.timer >= 5:  
        order_target_percent(stock, 1)  
        # Reset timer  
        context.timer = 0  
else: # MAV(5) < MAV(14):  
    # Reset counter  
    context.timer = 0  

This code assumes that you want to reset the timer if the moving averages change positions.

Hi Yagnesh,

There are two API methods you should check out that will make this easier to implement: history and schedule_function.

History allows you to grab a trailing window of historical prices.

Schedule_function allows you to schedule a regularly occurring function, such as trading every 5 days, or once per week or month.

In the sample algo attached I've set the algo to invest 100% of its portfolio in the SPY when the 5 day moving average is greater than the 14 day moving average, both 'today' AND 5 days ago, otherwise it goes to all cash. I did that by adding an offset term that looks back 5 days to check the 5 vs. 14 day moving average comparison in the past.

This sample also shows you how to set slippage and commissions costs, and I've run it in minutely mode so that it would be compatible with deploying to paper or live trading.

If you're just getting started with these methods learning a bit about using the pandas library in python will be useful as well.

Hope this is helpful - best,
Jess

Clone Algorithm
9
Loading...
Backtest from to with initial capital
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
# Backtest ID: 557b00db27c3e810e32f2035
There was a runtime error.
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

I have implemented "wait part" without using the schedule() function.
Somehow I need to get used to schedule() function

I was wondering what is difference between
implementing the algo using schedule() function and
implementing algo in handle(data) space.

Clone Algorithm
0
Loading...
Backtest from to with initial capital
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
# Backtest ID: 558167460602ae1199024c95
There was a runtime error.

@Yagnesh

schedule_function() is a more robust way to execute trading logic exactly when you want too.handle_data() is called whenever a market event occurs for any of your algorithm's specified securities. By using schedule_function() you don't have to implement any sort of datetime or counter logic, it allows you to focus more on writing a good algorithm and strategy than worry about the logistics of it. For a more detailed idea of the differences between handle_data() and schedule_function() and how they relate to when orders are executed see this post, specifically look at Alisa's answers.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

This is not fair ;) . . .
The Q - IDE encourages a newbie to implement algo logic under
handle data() .

Now I realize that I was using a not so good approach.
Any way better late than never.

By all means use handle_data(), but it's important to know the mechanics behind handle_data so it is actually implementing your exact algorithm.

Tangent: it is important to recognize the difference between the algorithm and the implementation. I try to view the algorithm as the strategy, the solid foundation for which any sort of actions are taken; furthermore we like our algorithms to be complete and correct. The implementation on the other hand should be correct and honestly reflect the intent of the algorithm (else how do you know you have a good strategy?).

Thanks James for your reply.

I conclude that if you want your algo to be working in minute mode during live trading
use handle_data() to implement for other frequencies schedule_function() is the only way out. .

I wish there was a "schedule" parameter for handle_data() which could make the algo writen in
handle_data() be triggered in a day / hour / minute / weekly / monthly modes in live trading
scenerio. . .