Moving Average Strategy using hft Python package

Package Installation

For hft package installation, you will need to have Anaconda2 and JDK installed. Please take a look at the manual.

conda install -c portfolioeffect hft

Strategy description

We use price vector to create a strategy based on moving average. We assume
that prices tend to revert to its moving average. Therefore, if the prices are
below its moving average, we get into the position by 1 unit. If prices are above
the moving average, we are out of position. The window length of the moving
average is 150 seconds.

  • ˆ if stock price<MA(price, N-second) => buy 1 share
  • ˆ if stock price>MA(price, N-second) => out of position

Using 3 stocks :

  • ˆ Dr. Reddy’s Laboratories Ltd (RDY)
  • ˆ ICICI Bank Limited (IBN)
  • ˆ Tata Motors Ltd Tata Motors Limited (TTM)





Expected Return


Value at Risk

Implementation of the strategy using hft Python package

 dateStart = "t-7"
 dateEnd = "t"
 portfolio = Portfolio(fromTime=dateStart, toTime=dateEnd)
 for symbol in symbols:
    portfolioTemp = Portfolio(fromTime=dateStart, toTime=dateEnd)
    position = portfolioTemp.add_position(symbol, 1)
    price = compute(position.price())[0]
    price_value = np.asarray(price[1])
    print_time = price[0]
    strategy = np.asarray([0] * len(price_value))
    price_value_MA = np.asarray(MA(price_value, 150))
    strategy[price_value < price_value_MA] = 100
    position = portfolio.add_position(symbol=symbol, quantity=strategy.tolist(),



Leave a Reply

Your email address will not be published. Required fields are marked *