Programming for Finance Part 3 - Back Testing Strategy



In this programming for Finance with Python, Zipline, and Quantopian, we cover finishing up the development of our basic simple moving average crossover strategy, and then we back test it. This illustrates the power of Quantopian well, since your only job is to create the logic for the strategy itself, and then the back-testing, and all sorts of advanced analysis are done automatically for you. Very cool. sample code: http://pythonprogramming.net http://hkinsley.com https://twitter.com/sentdex http://sentdex.com http://seaofbtc.com

Comments

  1. So here's a code that works for Quantoptian 2 (currently using minute data):
    """
    def initialize(context):
    context.security = symbol('SPY')


    def handle_data(context,data):
    print(data)
    MA_data_1 = data.history(context.security, 'price', 50, '1d')
    MA1 = MA_data_1.mean()
    MA_data_2 = data.history(context.security, 'price', 200, '1d')
    MA2 = MA_data_2.mean()

    current_price = data.current(context.security, "price")
    current_positions = context.portfolio.positions[symbol('SPY')].amount
    cash = context.portfolio.cash

    if (MA1 > MA2) and current_positions == 0:
    number_of_shares = int(cash/current_price)
    order(context.security, +number_of_shares)
    log.info("Buying shares")

    elif (MA2 < MA1) and current_positions != 0:
    order_target(context.security, 0)
    log.info("Selling shares")

    record(MA1 = MA1, MA2 = MA2, Price = current_price)
    """
  2. I have tried to use this but it comes up with errors?
  3. Why is my backtesting so slow?
    I have a LOT of transactions...not just 3
    I tried to replace the handle logic with the below comment but my backtesting is still slow, seemingly by the minute?
    Are your videos still current given the Quantopian update?

    Thanks!
  4. A lot of this is out of date now. This tutorial is using Quantopian 1, which is now defunct. Great tutorials all the same!
  5. i cant make any transaction in America, but i do trade in Thailand. So i am thinking if i can use Quantopian to analyze data from Thai stocks. Also, indicators that are not often used will usually not be in the options.
    Can we download indicators or data that are not included in Quantopian?
    This will help me alot. so thank you for those who takes time to read and answer my question.
  6. When I run the backtest I get this error message:

    TypeError: 'instancemethod' object has no attribute '__getitem__'
    ...
    USER ALGORITHM:14, in handle_dataGo to IDE
    MA1 = data.current[context.security].mavg(50)

    Not sure how to fix it? :/ I know that the program is updated from when you did the tutorial but I'm wondering how can I solve the problem. Great video, very good explanations!
  7. I tried to copy your program on to Quantopian, but I kept getting a statement that there was a problem starting my back test. The only errors I could find were highlighted program statements that the log info command was void and I had void record series. Has the Quantopian program changed some of its commands since you made the video, or do I still have a mistake in my programming. Thanks
  8. This is my first time to create an algorithm. I'm not a finance guy, nor do I know Python (I'm hoping to learn both by doing this), so I apologize for the elementary nature of my question...

    If I wanted to buy at 90% of MA2, and then sell at 110% of MA2, would it look something like this?


    if (current_price < (MA2 * 0.9)) and current_positions ==0:
    number_of_shares = int(cash/current_price)
    order (context.security, number_of_shares)
    log.info('Buying shares')

    elif (current_price > (1.1 * MA2)) and current_positions != 0:
    #order(context.security, -current_positions)
    order_target(context.security, 0)
    log.info('Selling Shares')
  9. I'm getting a syntax error for the current_price = data[context.security].price

    Any ideas? thanks!
  10. I have a little problem... when I run backtests, it puts :"These deprecated APIs will stop working in the future. Please switch to the new APIs as soon as possible." and that mavg is obsolete....
    HOW CAN I SOLVE THIS PROBLEM??
  11. Thanks Sentdex,,,You are awesome. I am trying to run the algorithm on quantopian, it seems mavg and context.security are deprecated
  12. Hey, how do I change from minute to daily. Nowadays I don't see a button to change that
  13. At first thank you very much sentdex. Great Videos. I have 2 Questions. I don't know python only Java Basics. Should I learn Python before following the videos? Secondly are the Tutorials still up to date? Quantopian seems to have changed a little bit. Thanks
  14. Thank you for your help. That is great for me!
  15. Hey! My algorithm is taking a really long time to backtest in quantopian. Is it an error on my part?

    I really like your videos!
  16. Hi Sentdex,

    First of all thank you very much for your tutorials, this is very interesting.
    I'm wondering, providing you're confident in the algorithm would you actually go ahead and link it to a broker to trade real money using this system?

    Thanks!
  17. There's a line of code i dont really understand, print(data).
    Where do the data is printed? why it isnt print(data[context.security])?
  18. Can anyone teach me the code to command a buy if the RSI (monthly) hit 50 for the first time in 1 year? Ty
  19. def initialize(context):
    context.security = symbol("SPY")

    def handle_data(context, data):
    print(data)
    MA1 = data[context.security].mavg(50)
    MA2 = data[context.security].mavg(200)

    current_price = data[context.security].price
    current_positions = context.portfolio.positions[symbol("spy")].amount
    cash = context.portfolio.cash

    if (MA1 > MA2) and current_positions == 0:
    number_of_shares = int(cash/current_price)
    order(context.security. number_of_shares)
    log.info("Buying Shares")

    elif (MA1 < MA2) and current_positions != 0:
    order_target(context.security, 0)
    log.info("Selling Shares")


    record(MA1 = MA1, MA2 = MA2, price = current_price)


    I have two errors, Line 14: Warning Local variable 'number_of_shares' is assigned to but never used
    Line 15: order expects at least 2 argument(s), you only passed 1.


Additional Information:

Visibility: 24759

Duration: 9m 54s

Rating: 176