Back to Community
No returns even though Logs says there are HELP!!! #Frustrated


Having trouble in backtester. The logs says I'm making profitable trades but the backtester says I'm not.

This happens with every algo so far...are my entries functions wrong? I'm using... order() and order_target()

Its a simple volume algo. If volume is > upper_limit then trade.

Any help much appreciated...(i'm beyond frustrated haha)

Thank you!!!!

from pytz import timezone  
import datetime  
import pytz  
import pandas as pd  
from quantopian.pipeline import Pipeline  
from quantopian.algorithm import attach_pipeline, pipeline_output  
import numpy as np

def initialize(context):  
    context.future =  continuous_future('CL', offset=0, roll='volume', adjustment='mul')  
    exchange_time = get_datetime('US/Eastern')  


def myfunc(context,data):  

##### ================================================================================================######

def handle_data(context, data):  
    exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')  
    cl_active = data.current(context.future, 'contract')  
    prices_custom_close = data.history(context.future, 'close', 30, '1m').resample('3T').last()   # 3min candles  
    current_price = data.current(cl_active,'price')  
    prices_custom_volume = data.history(context.future, 'volume', 30, '1m').resample('3T').last() # 3min candles  

    prices_custom_close = data.history(context.future, 'close', 20, '1m').resample('3T').last()  
    prices_custom_low = data.history(context.future, 'low', 20, '1m').resample('3T').last()  
    prices_custom_high = data.history(context.future, 'high', 20, '1m').resample('3T').last()  
    custom_volume = data.history(context.future, 'volume', 20, '1m').resample('3T').last()  
    upper_Long_lim = np.percentile(prices_custom_volume,80)  
    upper_Short_lim = np.percentile(prices_custom_volume,80)  
    previous_close  = float(prices_custom_close.iloc[-1])  
    current_close  = float(prices_custom_close.iloc[0])  
    previous_low = float(prices_custom_low.iloc[-1])  
    previous_high  = float(prices_custom_high.iloc[-1])  
    current_volume = float(prices_custom_close.iloc[0])  
    current_position = context.portfolio.positions[cl_active].amount  

### ======= LONG Entry & Exit  ========== ###  
    if  current_volume > upper_Long_lim and current_position==0:  
        order(cl_active, 1) 'Long Entry')   
    if current_price < previous_low and current_position > 0: # stop loss  
        order_target(cl_active, 0) 'Long Stop') 

    elif current_price > (previous_high + .20) and current_position > 0: # profit target  'Long PT')  

##### ============ SHORT Entry & Exit  ============== #####  
    if current_volume > upper_Short_lim and current_position == 0:  
        order(cl_active, -1) 'Short Entry')   

    if current_price > previous_high and current_position < 0: # stop loss  
        order_target(cl_active, 0) 'Short Stop')   
    elif current_price < (previous_close - .20) and current_position < 0:  
        order_target(cl_active,0) 'Short PT')

2 responses

The backtest engine determines the fill volume and price based upon the slippage model and also factors in commissions. One trick is to set the slippage and commissions to zero. This makes calculating the fill price much easier since it will simply be the last close price. Also, in the backtest results, there is a transactions list which shows exactly how orders are filled. Run a full backtest then click on the tab Activity->Transactions. This shows the individual trades which the backtester made. Step through each trade. You should be able see how your orders are filled and why they may differ from your expectations.

Perhaps start there. If there are specific trades which seem off, attach your backtest in a reply. It will be easier to troubleshoot that way.


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.


Try to change this line:

current_volume = float(prices_custom_close.iloc[0])  


current_volume = float(prices_custom_volume.iloc[-1])