-Newby-
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')
schedule_function(
myfunc,
date_rules.every_day(),
time_rules.market_open(minutes=5),
True
)
schedule_function(
myfunc,
date_rules.every_day(),
time_rules.market_close(minutes=5),
True
)
def myfunc(context,data):
pass
##### ================================================================================================######
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)
log.info('Long Entry')
log.info(current_price)
if current_price < previous_low and current_position > 0: # stop loss
order_target(cl_active, 0)
log.info('Long Stop')
log.info(current_price)
elif current_price > (previous_high + .20) and current_position > 0: # profit target
log.info('Long PT')
##### ============ SHORT Entry & Exit ============== #####
if current_volume > upper_Short_lim and current_position == 0:
order(cl_active, -1)
log.info('Short Entry')
log.info(current_price)
if current_price > previous_high and current_position < 0: # stop loss
order_target(cl_active, 0)
log.info('Short Stop')
log.info(current_price)
elif current_price < (previous_close - .20) and current_position < 0:
order_target(cl_active,0)
log.info('Short PT')