Hi fellows, im trying to develop an algo which include an RSI and MACD signal to get in and out from the market.
But i did figure out when i compare the values with the RSI and MACD from tradinview indicators, them differs.
Any idea how to fix the indicators?
import talib
# Setup our variables
def initialize(context):
context.stocks = sid(24)
context.short = False
context.longon = False
context.close = False
schedule_function(rebalance, date_rules.every_day(), time_rules.market_open())
def rebalance(context, data):
prices = data.history(context.stocks, "close",20, "1d")
prices40 = data.history(context.stocks, "close",40, "1d")
rsi = talib.RSI(prices, timeperiod=14)[-1]
macd, signal, hist = talib.MACD(prices40, fastperiod=12, slowperiod=26, signalperiod=9)
record(rsi=rsi)
if rsi < 30 and hist[-1] < -0.9 and not context.longon:
order_target_percent( context.stocks,1)
context.longon = True
context.short = False
print("BUYING")
print(rsi)
if rsi > 70 and hist[-1] > 0.9 and not context.short:
order_target_percent( context.stocks,-1)
context.short = True
context.longon = False
print("SELLING")
print(rsi)
if context.longon or context.short:
if 49 < rsi < 51 or -0.05 < hist[-1] < 0.05:
order_target_percent( context.stocks,0)
context.short = False
context.longon = False
print("close")
print(rsi)