Generate and Execute
Generate and Execute
import numpy as np
import talib as ta
import oandapyV20
from oandapyV20 import API
from oandapyV20.exceptions import V20Error
import oandapyV20.endpoints.orders as orders
import time
# Generate signals
if df['rsi'].iloc[-1] > 70 and df['close'].iloc[-1] > df['ma_50'].iloc[-1] and
df['close'].iloc[-1] > df['ma_200'].iloc[-1] and df['close'].iloc[-1] >
df['fib_23.6'].iloc[-1] and df['close'].iloc[-1] > df['upper_band'].iloc[-1]:
return 'buy'
elif df['rsi'].iloc[-1] < 30 and df['close'].iloc[-1] < df['ma_50'].iloc[-1]
and df['close'].iloc[-1] < df['ma_200'].iloc[-1] and df['close'].iloc[-1] <
df['fib_61.8'].iloc[-1] and df['close'].iloc[-1] < df['lower_band'].iloc[-1]:
return 'sell'
else:
return 'hold'
# Set order price and stop loss/take profit levels based on signal
if signal == 'buy':
order_params['order']['price'] = price
order_params['order']['stopLossOnFill']['price'] = price -
stop_loss_distance = 0.0025 # 25 pips
order_params['order']['takeProfitOnFill']['price'] = price +
take_profit_distance = 0.0050 # 50 pips
else:
order_params['order']['price'] = price
order_params['order']['stopLossOnFill']['price'] = price +
stop_loss_distance = 0.0025 # 25 pips
order_params['order']['takeProfitOnFill']['price'] = price -
take_profit_distance = 0.0050 # 50 pips
# Execute order
r = orders.OrderCreate(accountID=accountID, data=order_params)
client.request(r)
while True:
# Generate signal
signal = generate_signal()