Exemplo n.º 1
0
def update_output(n_intervals, value, chart_type):
    #stock = redis_conn.get('stock')
    stock = value
    hash_key = stock + str(current_user.id)
    pdebug1("In update output: {}".format(stock))

    try:
        fh = open('log/freedom_trade.log', 'r')
        logMsg = fh.read()
        fh.close()
        #logMsg = redis_conn.get('logMsg'+cache_type)
    except:
        logMsg = "Something went wrong !!"

    fig = ''
    trade_summary = 'Ongoing ...'

    if stock == '':
        return fig, 'logMsg', 'No option selected', ''

    summary_stat = datetime.fromtimestamp(
        float(backtest_cache.hget(hash_key + cache_type, 'last_processed')))

    if backtest_cache.get('done' + str(current_user.id) +
                          cache_type) == "1":  ## to reduce load on processor
        #pinfo(hash_key)
        fig = freedom_chart(hash_key, cache_type, chart_type)
        trade_df = backtest_cache.getTrades(hash_key)

        try:
            (total_profit, max_loss, max_profit, total_win, total_loss,
             max_winning_streak, max_loosing_streak,
             trade_log_df) = trade_analysis_raw(trade_df)

            summary_stat = 'Total Profit: {:.02f}, Max Drawdown: {:.02f}, Max Profit {:.02f}'.format(
                total_profit, max_loss, max_profit)
            #summary_stat = summary_stat + html.Br()
            summary_stat = summary_stat + '\n Win: {}, Loss: {}, Longest Streak => Win: {}, Loss: {}'.format(
                total_win, total_loss, max_winning_streak, max_loosing_streak)

            trade_log_df['profit'] = trade_log_df['profit'].map(
                "{:,.02f}".format)
            trade_log_df['CumProfit'] = trade_log_df['CumProfit'].map(
                "{:,.02f}".format)
            trade_log_df['date'] = trade_log_df.index
            #trade_log_df = trade_log_df.map("{:,.0f}".format) backtest_cache
            if 'mode' in trade_log_df.columns:
                trade_summary = df_to_table(
                    trade_log_df[[
                        'date', 'mode', 'buy', 'sell', 'profit', 'CumProfit'
                    ]], 'trade_summary_table', False)
            else:
                trade_summary = df_to_table(
                    trade_log_df[[
                        'date', 'mode', 'buy', 'sell', 'profit', 'CumProfit'
                    ]], 'trade_summary_table', False)
        except:
            trade_summary = 'not enough data'

    return fig, logMsg, trade_summary, summary_stat
Exemplo n.º 2
0
def update_intervals(n_intervals, clicks):
    pdebug1("Update Intervals: {}: {}".format(n_intervals, backtest_cache.get('done')))
 
    # if done is set to 1 then backtest is complete -> Time to disable interval and enable backtest button
    if backtest_cache.get('done'+cache_type) == "1": # Backtest complete
        pdebug("Returning True: Disable Interval")
        return True, False, 'Go' 
    else: # Backtest is in progress
        pdebug1("Returning False: Enable Interval")
        return False, True, 'Wait'
Exemplo n.º 3
0
def update_intervals(n_intervals, clicks):
    hash_key = 'done' + str(current_user.id) + cache_type

    if clicks == 0:
        backtest_cache.set(hash_key, 1)
    pdebug1("Update Intervals: {}: {}".format(n_intervals,
                                              backtest_cache.get(hash_key)))

    # if done is set to 1 then backtest is complete -> Time to disable interval and enable backtest button
    if backtest_cache.get(hash_key) == "1":  # Backtest complete
        pdebug("Returning True: Disable Interval")
        return True, False, 'Go'
    else:  # Backtest is in progress
        pdebug1("Returning False: Enable Interval")
        return False, True, 'Wait'
Exemplo n.º 4
0
def start_backtest(n_clicks, stocks, qty, sl, target, start_date, end_date, algo, freq, algo_name, mode ):
    toDate = end_date
    fromDate = start_date

    try:
        pinfo("==========: {} :========".format(session["access_token"]))
    except:
        perror("Access Token Not found")
        #return 0

    if n_clicks == 0:
        return 0
    
    if len(mode) > 0:
        backtest = 'quick'
    else:
        backtest = 'full'

    if not isinstance(stocks,list):
        stocks = [stocks]

    pdebug1(stocks)
    # Step 1: Create the msg for initiating backtest
    #pinfo(freq)
    backtest_msg={'stock':stocks,'sl':sl,'target':target,'qty':qty,'algo':algo_name,'fromDate':fromDate,'toDate':toDate,'freq':freq, 'mode':backtest}

    try:
        store_algo(algo, algo_name)
    except:
        perror('Something went wrong while saving algo')

    pdebug1(backtest_msg)
    # Step 2: Store the stock name under backtest in the redis cache
    for stock in stocks:
        backtest_cache.set('stock',stock) #TODO: replace

    # Step 4: Done is set to 0: Backtest is in progress, will be resetted by backtest job
    backtest_cache.set('done'+cache_type,0)
    # Step 5: Send the msg to backtest thread to initiate the back test
    pdebug(json.dumps(backtest_msg))
    backtest_cache.publish('kite_simulator'+cache_type,json.dumps(backtest_msg))
    
    # Step 6: Return 0 to reset n_intervals count
    return 0