Backtesting strategies are not too bad for me but implementing them in IbridgePy is where I am starting to become unstuck. I am using examples to piece code together but I am not sure what I am doing. I probably need some confirmation that what I am doing is correct. Can I get any help for this please?
Thank you.
import pandas as pd
import talib as ta
import numpy as np
def initialize(context):
context.run_once=False
context.security_symbol('AAPL')
context.stop_loss_multiple = 1.5
context.stop_loss_multiple = 2
context.stop_loss = np.nan
context.take_profit = np.nan
def handle_data(context,data):
sTime=get_datetime()
if sTime.weekday()<=4:
if sTime.hour==15 and sTime.minute==28 and context.run_once=True:
context.run_once=False
if sTime.hour==15 and sTime.minute==29 and context.run_once=False:
data=request_historical_data(context.security, '1 day', '30 D')
prices=data.history(
context.security, ['open', 'high', 'low', 'close', 'adj close'], 60, '1m')
prices['ATR'] = talib.ATR(prices['high'], prices['low'],
prices['close'], timeperiod=30)
adj_factor = prices['adj close']/prices['close']
adj_open = adj_factorprices['open']
returns = (adj_open-prices['adj close'].shift(1))/prices['adj close']
std = returns.rolling(90).std()
long_entry = returns > 0.25std
long_exit = prices['close'][-1] > context.stop_loss or prices['close'][-1] < context.take_profit
short_entry = returns < 0.25std()
short_exit = prices['close'][-1] < context.stop_loss or prices['close'][-1] > context.take_profit
if long_entry and context.security == 0:
order(order.security, 100)
context.position = 1
print('Long Entry')
context.stop_loss = prices['ATR'][-1](1-context.stop_loss_multiple)
context.take_profit = prices['ATR'][-1](1+context.take_profit_multiple)
elif long_exit and context.position == 1:
order_target_percent(context.security, 0)
context.position = 0
print('Long Exit')
context.stop_loss = np.nan
context.take_profit = np.nan
if short_entry and context.security == 0:
order(order.security, 100)
context.position = -1
print('Short Entry')
context.stop_loss = prices['ATR'][-1](1+context.stop_loss_multiple)
context.take_profit = prices['ATR'][-1]*(1-context.take_profit_multiple)
elif long_exit and context.position == 1:
order_target_percent(context.security, 0)
context.position = 0
print('Long Exit')
context.stop_loss = np.nan
context.take_profit = np.nan
else:
orderID=order_target(context.security, 0)
order_status_monitor(orderID, target_status='Filled')
context.run_once=True
Hi Jessy,
There are some minor errors in the code:
- You are using some assignment operators instead of comparison operators in the if statement. For example, context.run_once=True
2. context.stop_loss_multiple has been set twice
3. context.security_symbol('AAPL'), this is created incorrectly. If should be context.security = symbol('AAPL')
4. context.security == 0 , context.security is an instance of security. Hence, not sure about its comparison with 0
These are some of the errors. I would suggest you add multiple print statements in the code and try to debug the errors in a systematic manner. Also, you can go through the documentation of IBridgePy here to get more idea about the syntax and declarations.
Hope this helps!