Back to Community
help with forecast algorithm

Hi,
I have forecasts to 6 time frames
3 days
7 days
14 days
31 days
92 days
365 days
I've come up with an algorithm.
Do you have a any suggestion how to improve it?
10x

Clone Algorithm
7
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import pandas as pd
from datetime import datetime
from datetime import timedelta
import math
def initialize(context):
    fetch_csv("https://dl.dropboxusercontent.com/s/wcj1uoxkd8toy95/stocks1.csv", date_column = 'date')
    schedule_function(func = rebalance, date_rule = date_rules.every_day(), time_rule = time_rules.market_open())
    context.sell = {}

def rebalance(context,data):
    exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')
    daily_buy = 0
    daily_stocks = []
    for stock in data:
      if data[stock]['dt'].date() == exchange_time.date():
          if data[stock]['signal31'] > 70 and data[stock]['signal3'] > 17 and context.portfolio.positions[stock].amount == 0 and context.portfolio.cash - daily_buy > 0:
                daily_stocks.append( data[stock] )
          elif (data[stock]['signal31'] < 33 or (data[stock]['signal3'] < 0 and data[stock]['signal7'] < 0)) and context.portfolio.positions[stock].amount > 0:
              order(stock,-1*context.portfolio.positions[stock].amount)
              daily_buy -= context.portfolio.positions[stock].last_sale_price * context.portfolio.positions[stock].amount
              # log.info('{d} sell {s} {si}'.format(s=stock.symbol,d=data[stock]['dt'].date(),si=data[stock]['close']))
      if context.portfolio.positions[stock].amount > 0 and context.sell[stock.sid] < exchange_time:
          order(stock,-1 * context.portfolio.positions[stock].amount)
          daily_buy -= context.portfolio.positions[stock].last_sale_price * context.portfolio.positions[stock].amount
          # log.info('{d} sell {s} {si}'.format(s=stock.symbol,d=data[stock]['dt'].date(),si=data[stock]['close']))
    daily_stocks = sorted(daily_stocks,key=getSignal31)
    for stock in daily_stocks:
      if context.portfolio.cash - daily_buy > 0:
        order_value(stock.sid,2000)
        daily_buy += 2000
        context.sell[stock.sid] = exchange_time + timedelta(days=31)
        # log.info('{d} buy {s} {si}'.format(s = stock.sid, d = stock['dt'].date(), si = stock['close']))

def getSignal31(stock):
    return stock['signal31']
        
There was a runtime error.