Hi there, I was wondering if anyone has used IBridgePy and their backtesting feature, my code seems to be fine (no errors/bugs when running it) but then when putting it through the TEST_ME module, no trades are made…this is frustrating as I can't pinpoint the problem, however I feel like it's due to my ifstatements. If anyone could check it out that would be great, here is the part of the code I believe is giving me trouble:
import pandas as pd
import talib as ta
def initialize(context):
context.security = symbol('QQQ')
# the defined security is the NASDAQ 100
def handle_data(context, data):
sTime = get_datetime('US/Eastern')
#sTime is the IB server time, get_datetime() is the build-in function to obtain IB server time
hist = request_historical_data(context.security,'1 min','2 D')
#First we calculate the 26 and 12 day exponential moving averages
hist['ema_26'] = ta.EMA(hist.close,timeperiod = 26)
hist['ema_12'] = ta.EMA(hist.close, timeperiod = 12)
#Creating a column in the data for the MACD calculation
hist['macd'] = hist.ema_12 - hist.ema_26
#Creating a column in the data for the RSI Calculation
hist['rsi'] = ta.RSI(hist.close, timeperiod = 14)
if hist['macd'][-1] < 0 and hist['macd'][-2] > 0 and hist['rsi'][-1] > 70:
#Parameters were triggered to short the stock
if count_positions(context.security) == 0:
print('Trigger Short:')
orderId = order_target_percent(context.security,-1,style=MarketOrder())
#order_status_monitor(orderId, target_status = 'Filled')
display_all()
elif hist['macd'][-1] > 0 and hist['macd'][-2] < 0 and hist['rsi'][-1] <3o0o90:
#Parameters were triggered to buy the stock
if count_positions(context.security) == 0:
print('Trigger Buy:')
orderId = order_target_percent(context.security, 1, style=MarketOrder())
#order_status_monitor(orderId, target_status = 'Filled')
display_all()
You need to run the code through the RUN_ME.py file. In the RUN_ME.py file, you need to change the fileName and accountCode.
Vibhu, thanks for your response but I've already done these. After rerunning the code, the problem is in the speed in which it backtests. I am using min by min data but the backtest is taking a ridiculously long time, to a point that I don't even know if the code is running on the compiler or not.
You can print the variables used in the if condition just before the if condition to confirm if it is running as expected and then can investigate further in similar manner.
Example:
print(sTime, hist['macd'][-1], hist['macd'][-2], hist['rsi'][-1])
Hi Ishan, yes I've able to print, seems that the code is working but at a ridiculously slow rate. Might you know the reason why?
If you could give me your contact info to discuss this issue I'm more than glad to pay you for compensation of half an hour of your time if you can help me figure this out.
There could be three reasons for slow execution
- In the RUN_ME.py, add a line. “repBarFreq = 5”. Then, handle_data ( ) will run every 5 seconds. You can just keep below lines to check if handle_data is called every 5 seconds.
def handle_data(context, data):
sTime = get_datetime('US/Eastern')
print(Time)
- If the above code is called every 5 seconds then some piece of your code is taking more time to execute, you can check execution time for each code as below
import time
start_time = time.time()
<<Python code>>
end_time = time.time()
print(end_time-start_time, "For If condition....")
- Your hardware might be old. This seems like a very low likelihood.
I hope this helps.
Hi Ishan, could you give me a quick email at kwong7555@gmail.com
I've tried your approaches and it still doesn't seem to work or I am just implementing your suggestions into my code incorrectly.
I am also looking to backtest this, so the file I am using more is the TEST_ME.py file.
Thank you so much for your suggestions thus far.
If you are looking to backtest, then you can use blueshift: https://blueshift.quantinsti.com/
The good part is that code which you have written can work on blueshift with slight to no modification. And your strategy works on the cloud so you need not worry about your local machine hardware configuration.
If you want to share screenshots of your error message then you can send mail to quantra@quantinsti.com.
Thank you!