PyAlgoTrade - An excellent open source python library

pannet1

Well-Known Member
#1
Hi,

I came across PyAlgoTrade and have modified the existing program to automatically download instrument data as CSV from yahoo feed, run a predefined test and display the result. This all done in a single command from command line in linux.

python simple_moving_average.py SBIN.NS

This program should run on any environment (windows, mac etc) which has necessary python libraries.

The idea is to make a scanner that will be run once from the command line. It will scan all the predefined set of instruments' EOD data and look for a BUY / SELL signal. It could then login into Zerodha account and place a trade.

How sweet it would be?
 

pannet1

Well-Known Member
#2
Seems it is not interesting for many. I have slightly modified the code available in the public domain to suit my needs

Code:
from pyalgotrade import strategy
from pyalgotrade.technical import ma
from pyalgotrade.technical import cross

#from pyalgotrade.talibext import indicator
#from pyalgotrade.technical import highlow

from pyalgotrade.stratanalyzer import trades

tradesAnalyzer = trades.Trades()

class SMACrossOver(strategy.BacktestingStrategy):
    lastprice = 0.0
    fibonum   = 0
    share    = 1
    
    def __init__(self, feed, instrument, smaPeriod):
        super(SMACrossOver, self).__init__(feed)
        self.__instrument = instrument
        self.__position = None
        # We'll use adjusted close values instead of regular close values.
        self.setUseAdjustedValues(True)
        self.__prices = feed[instrument].getPriceDataSeries()
        self.__sma = ma.SMA(self.__prices, smaPeriod)
        #self.__high = highlow.High(feed[instrument].getOpenDataSeries(), 20)

    def getSMA(self):
        return self.__sma

    def onEnterCanceled(self, position):
        self.__position = None

    def onExitOk(self, position):
        self.__position = None

    def onExitCanceled(self, position):
        # If the exit was canceled, re-submit it.
        self.__position.exitMarket()

    def onBars(self, bars):
        #barDs = self.getFeed()[self.__instrument]
        #print self.__high[-1], indicator.MAX(barDs.getOpenDataSeries(), 100, 20)[-1]

        # If a position was not opened, check if we should enter a long position.
        if self.__position is None:
            if cross.cross_above(self.__prices, self.__sma) > 0:
                profits = tradesAnalyzer.getProfits()
                if self.lastprice < self.__prices or profits.mean>0:
                    #shares = int(self.getBroker().getCash() * 0.9 / bars[self.__instrument].getPrice())
                    # Enter a buy market order. The order is good till canceled.
                    self.__position = self.enterLong(self.__instrument, self.share, True)
                    if self.__prices or profits.mean>0:                        
                        self.share = self.share + self.fibonum
                        self.fibonum = self.share                                    
        # Check if we have to exit the position.
        elif not self.__position.exitActive() and cross.cross_below(self.__prices, self.__sma) > 0:
            self.__position.exitMarket()
 

pannet1

Well-Known Member
#5
Ppl have been accustomed to AFL so much that python would either be greek or mundane :lol::lol:
OK. Got it. i am not proceeding further on this here. I hope one day I would be able to use Python and place my options trade with Z.
 
#6
OK. Got it. i am not proceeding further on this here. I hope one day I would be able to use Python and place my options trade with Z.
Those people who use AFL are either naive or a non porgrammer. If you are a professional quant/algo trader then they would prefer python instead of AFL.

  1. Its free
  2. Its open source
  3. Community of developers
  4. its DAMN fast

Comparing to python; AFL is nothing especially for algo trading. Most of the quant/algo traders uses python.

That said I have tried a long time ago PyAlgoTrade but I find it very complex and lack of tutorials make it much more difficult. It would be helpful if you can continue with some small sample codes.

A REQUEST: I hate classes in python can you rewrite the codes with only functions.
 
Last edited:
#7
OK. Got it. i am not proceeding further on this here. I hope one day I would be able to use Python and place my options trade with Z.
Please dont stop posting. Keep sharing the code like you did. Ofcourse most of the members are with amibroker, but that doesn't mean no one is interested in this thread.

Some questions I have regarding pyalgotrade, is it possible to do intraday strategy testing, and multiple timeframe strategy testings? I hope it should be possible, any idea or have you done it? I will also be giving a try this weekend.
 
#8
Those people who use AFL are either naive or a non porgrammer. If you are a professional quant/algo trader then they would prefer python instead of AFL.

  1. Its free
  2. Its open source
  3. Community of developers
  4. its DAMN fast

Comparing to python; AFL is nothing especially for algo trading. Most of the quant/algo traders uses python.

That said I have tried a long time ago PyAlgoTrade but I find it very complex and lack of tutorials make it much more difficult. It would be helpful if you can continue with some small sample codes.

A REQUEST: I hate classes in python can you rewrite the codes with only functions.
mutualguru, as per pyalgotrade you need to have a class which inherits strategy.BacktestingStrategy
 

pannet1

Well-Known Member
#9
Those people who use AFL are either naive or a non porgrammer. If you are a professional quant/algo trader then they would prefer python instead of AFL.

  1. Its free
  2. Its open source
  3. Community of developers
  4. its DAMN fast

Comparing to python; AFL is nothing especially for algo trading. Most of the quant/algo traders uses python.

That said I have tried a long time ago PyAlgoTrade but I find it very complex and lack of tutorials make it much more difficult. It would be helpful if you can continue with some small sample codes.

A REQUEST: I hate classes in python can you rewrite the codes with only functions.
I am not expert either. In fact this was my 2nd or 3rd Python program which involved tinkering existing programs and modifying it to suit my need.

There was small program to download EOD from yahoo server to an excel sheet. Then there was a program to run test on the downloaded CSV data. What i just did was merging both of them into one.

I will try to understand each and every line of code and repost it here when I have time. Until such time, please bear with me.
 

pannet1

Well-Known Member
#10
Please dont stop posting. Keep sharing the code like you did. Ofcourse most of the members are with amibroker, but that doesn't mean no one is interested in this thread.

Some questions I have regarding pyalgotrade, is it possible to do intraday strategy testing, and multiple timeframe strategy testings? I hope it should be possible, any idea or have you done it? I will also be giving a try this weekend.
Yes, you are right. The test is as good as the data we have. Problem is I did not have a free source of intraday data.

I am a novice. If I can do anyone can do it. With a little bit of programing experience it should be very easy.

Good luck. Please feel free to shoot your questions and I will try to do my best.
 

Similar threads