Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
SMA Help needed

So I'm just trying to create SMA's but can't figure it out. can someone help? I just want to buy if SMA5 > SMA10.

Put any initialization logic here. The context object will be passed to

the other methods in your algorithm.

def initialize(context):
context.spy = sid(8554)
context.max_notional = 5000.0
context.min_notional = -0.0
set_long_only()

pass

Will be called on every trade event for the securities you specify.

def handle_data(context, data):

current_price = data[sid(8554)].price  
price = data[sid(8554)].price  
cash = context.portfolio.cash  
notional = context.portfolio.positions[sid(8554)].amount * price  

# Trying to create a sma  

SMA5 = history(bar_count=5, frequency='1m', field='price')  
log.info(SMA5.mean())  
SMA10 = history(bar_count=10, frequency='1m', field='price')  
log.info(SMA10.mean())  
SMA20 = history(bar_count=20, frequency='1m', field='price')  
log.info(SMA20.mean())  

# Implement your algorithm logic here.

# data[sid(X)] holds the trade event data for that security.  
# context.portfolio holds the current portfolio state.

# Place orders with the order(SID, amount) method.

# TODO: implement your own logic here.  
if (cash > current_price) and (SMA5 > SMA10):  
  order_value(sid(8554), 5000)  
23 responses

Hi Moe,

What's happening here is that you're trying to compare two DataFrames: SMA5 and SMA10 of unequal lengths.

So instead you can compare the mean of those by doing SMA5[context.stock].mean() and SMA10[context.stock].mean() like so

if SMA5[context.stock].mean() > SMA10[context.stock].mean():  
# order here  

Try that out and let me know if that works for you

Seong

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.

Hi Seong,
I have a little problem here, can you tell me why the backtester ignore the first day and add one more day after I specify the data range.
For example, if the range I specify is from 2014-02-19 to 2014-04-17. After the backtest completed, the Result Overview shows it's actually caculated from 2014-02-20 to 2014-04-18.

appreciated
Miles

Thanks Seong, I really appreciate your help! Moe

@Miles, can you share your backtest? I wasn't able to reproduce the behavior but this will show us what's going on.

If you run a full backtest, you can press the "Add Backtest" button in a new comment here. Thanks!

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.

@Alisa, the result here confuse me:
1. They both start at Feb 21.
2. The first Buy Signal appears at Feb 28, and the transaction complete at Feb 28, too. Shouldn't it complete at Feb 29?
I am a totally newbie :)

Hi Miles,

It looks like the first buy order gets submitted on February 27th, 2014 and so it takes about a day for the order to get filled on February 28, 2014.

The logs show this: 2014-02-27handle_data:53INFOBuying AAPL

Hope that clears up your confusion

Seong

So using the example above and the order gets filled on Feb 28, how can I get the purchase price and use it in the algo. I want to set a target sell price. Lets say the order is filled at $81 and I want to sell at ($81 + x). How can I do that?

Great question.

Gus Gordon provides a way to do that in this post (https://www.quantopian.com/posts/fill-price-of-market-orders) in the second to last reply where he logs the close prices when an order has been filled ( by keeping track of the order id )

Seong

You are right Seong. My logs shows the same thingļ¼š

2014-02-20handle_data:25INFOHolding 0 shares AAPL.
2014-02-21handle_data:25INFOHolding 0 shares AAPL.
2014-02-24handle_data:25INFOHolding 0 shares AAPL.
2014-02-25handle_data:25INFOHolding 0 shares AAPL.
2014-02-26handle_data:25INFOHolding 0 shares AAPL.
2014-02-27handle_data:16INFOBuying 132 shares AAPL
2014-02-27handle_data:25INFOHolding 0 shares AAPL.
2014-02-28handle_data:25INFOHolding 132 shares AAPL.
2014-03-03handle_data:25INFOHolding 132 shares AAPL.
2014-03-04handle_data:25INFOHolding 132 shares AAPL.
2014-03-05handle_data:25INFOHolding 132 shares AAPL.
2014-03-06handle_data:25INFOHolding 132 shares AAPL.
2014-03-07handle_data:22INFOSelling 132 shares AAPL
2014-03-07handle_data:25INFOHolding 132 shares AAPL.
2014-03-10handle_data:25INFOHolding 0 shares AAPL.

But check the Custom data graph I posted above, it shows at Feb 27 the stock_price line haven't cross up the avg5 line yet, how can it send a buy order at Feb 27 which is one day before the buy signal appears?
This is what I really don't understand...
ahhhhhh..............

Hi Miles,

It's okay! We can figure this out. So what I'm seeing right now is that the stock_price line actually has crossed the avg5 line on February 27th.

On 2/27/2014 the stock price is at 75.41 while the avg5 line is at 74.89.

Seong

OK Seong,
What I am seeing here is On Feb 27 the stock_price line is 73.9 while the avg5 line is 74.95.
Thanks a lot.

On February 27th?

https://copy.com/47W6UNmd1QYtGJxR

Do you mind posting a screenshot of the graph you're looking at? I've uploaded a shot of mine

Miles,

I think we're looking at different algorithms right now. I'm looking at the one you posted. Can you post the one you're looking at right now?

I actually looking at the one I posted, too. My screen shot picture is taken from this web page.

This one? https://copy.com/hKnj4g4AR8lj38iL

Exactly.
But why it shows different data in my screen?

Miles,

Can you post your full backtest again or invite me to a collaboration?

Seong,
Invited

Hi Miles,

In the algorithm you've invited me to collab on, I'm still seeing that on February 27th, the stock price is at 75.19 and the avg5 is at 74.89, so it makes sense that the buy signal was sent here.

Seong,
I've aware that the buy signal should appears at Feb 27, and it is.
But I just don't know why the two lines on my screen shows different from yours since we have share the same code.
http://app.yinxiang.com/l/ACW64KHrC7dCfpAu8Ivx9TOy4lky6PnBido/

Miles,

So it turns out both of us are right. What's happening is that because we're in different timezones, the backtester is reporting results according to the timezones that we're in. So currently, I'm in the US (EST) and you seem to be more in the Asian timezones. I've confirmed this by changing my timezone to South Korea, and I got the same exact screen as you here: http://app.yinxiang.com/l/ACW64KHrC7dCfpAu8Ivx9TOy4lky6PnBido/

I hope that clears up the confusion. This one was very tricky to solve

Seong

I Got it Seong.
My location is Canton, China.
Thank you very much :)