Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
New to the community - Few very simple questions

All,

I am very new to the programming community, but would like to say that what you all are doing here is great!
I do come from the discretionary side of trading but am interested in the proprietary as well.
I was wondering if someone would be kind enough to show me a few very simple lines to get me started.

For example, lets say you wanted to go long 100 shares of Apple when the 20 day exponential moving average crossed the 50 day exponential moving average on a daily time frame given that the previous close was above the 200 day simple moving average.
Then, you sell your 100 shares of Apple when the 50 day exponential cross back over the 20 day exponential moving average.

I know this must seem extremely basic, but there doesn't seem to be a better place to ask the question.
Thank you all for the value you are adding to the trading community!

Regards,
Carter

7 responses

Hi Carter,

I'd suggest trying something a bit simpler, particularly if you are new to programming. Maybe see if you can write code to periodically re-balance a long-only portfolio of VTI & BND, at 70% % 30%, respectively?

See schedule_function & order_target_percent in the help docs., which should make the code fairly compact.

Grant

Hope this helps!

def initialize(context):  
    context.stock = symbol('AAPL')

# Will be called on every trade event for the securities you specify.  
def handle_data(context, data):  
    stock = context.stock  
    current_price = data[stock].price  
    twenty_day_ema = data[stock].mavg(20)  
    fifty_day_ema = data[stock].mavg(50)  
    if twenty_day_ema >= fifty_day_ema  
        order(stock, 100)  
    elif fifty_day_ema <= twenty_day_ema  
     #Below we are setting the target position from 100 to 0 thereby selling all shares  
        order_target(stock, 0)  

Try this.
May be it is more realistic for you.

@valdimir thanks for the algorithm but can you explain a little bit about your algorithm especially what is happenig with commission,slippage, order etc. I understood that you are using simple moving average crossover to buy and sell shares but what is that you changed which made this algorithm realistic?

You might find it useful to check out the tutorial series I have been releasing here: https://www.quantopian.com/posts/quantopian-tutorial-series

Thank you all very much. I will do some research and let you know how it goes.

Slippage and commissions can be modeled in the algorithm to better simulate real market conditions. You can customize the settings for your algo and securities:

https://www.quantopian.com/help#ide-slippage
https://www.quantopian.com/help#ide-commission

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.