Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _babysit(self):
""" (See strategy.py for documentation) """
Log.write('fifty.py babysit(): _open_tade_ids: {}'.format(self.open_trade_ids))
for open_trade_id in self.open_trade_ids:
is_closed = Broker.is_trade_closed(open_trade_id)
if is_closed[0]:
Log.write('"fifty.py" _babysit(): Trade ({}) has closed with reason: {}'
.format( open_trade_id, str(is_closed[1]) )
)
# If SL hit, reverse direction.
if is_closed[1] == TradeClosedReason.STOP_LOSS_ORDER:
if self.go_long:
self.go_long = False
else:
self.go_long = True
self.open_trade_ids.remove(open_trade_id)
else:
trade = Broker.get_trade(open_trade_id)
instrument = trade.instrument
sl = round( float(trade.stop_loss), 2 )
go_long = int(trade.units) > 0
instrument = trade.instrument
sl = round( float(trade.stop_loss), 2 )
go_long = int(trade.units) > 0
if go_long: # currently long
cur_bid = Broker.get_bid(instrument)
if cur_bid != None:
if cur_bid - sl > self.sl_price_diff:
new_sl = cur_bid - self.sl_price_diff
resp = Broker.modify_trade(
trade_id=open_trade_id,
stop_loss_price=str(round(new_sl, 2))
)
if resp == None:
Log.write('"fifty.py" _babysit(): Modify failed. Checking if trade is closed.')
closed = Broker.is_trade_closed(open_trade_id)
Log.write('fifty.py babysit(): is_trade_closed returned:\n{}'.format(closed))
if closed[0]:
Log.write('"fifty.py" _babysit(): BUY trade has closed. (BUY)')
self.open_trade_ids.remove(open_trade_id)
# If SL hit, reverse direction.
if closed[1] == TradeClosedReason.STOP_LOSS_ORDER:
self.go_long = False
else:
Log.write('"fifty.py" _babysit(): Failed to modify BUY trade.')
raise Exception
else:
Log.write('"fifty.py" _babysit(): Modified BUY trade with ID (',\
open_trade_id, ').')
else:
Log.write('"fifty.py" _babysit(): Failed to get bid while babysitting.')
raise Exception
Log.write('"fifty.py" _babysit(): Modified BUY trade with ID (',\
open_trade_id, ').')
else:
Log.write('"fifty.py" _babysit(): Failed to get bid while babysitting.')
raise Exception
else: # currently short
cur_bid = Broker.get_bid(instrument)
if cur_bid != None:
if sl - cur_bid > self.sl_price_diff:
new_sl = cur_bid + self.sl_price_diff
resp = Broker.modify_trade(
trade_id=open_trade_id,
stop_loss_price=str(round(new_sl, 2))
)
if resp == None:
closed = Broker.is_trade_closed(open_trade_id)
Log.write('fifty.py babysit(): is_trade_closed returned:\n{}'.format(closed))
if closed[0]:
Log.write('"fifty.py" _babysit(): SELL trade has closed. (BUY)')
self.open_trade_ids.remove(open_trade_id)
# If SL hit, reverse direction.
if closed[1] == TradeClosedReason.STOP_LOSS_ORDER:
self.go_long = True
else:
Log.write('"fifty.py" in _babysit(): Failed to modify SELL trade.')
raise Exception
else:
Log.write('"fifty.py" _babysit(): Modified SELL trade with ID (',\
open_trade_id, ').')
else:
Log.write('"fifty.py" _babysit(): Failed to get ask while babysitting.')
raise Exception
# Delete any trades from the database that are no longer open.
# First, ignore trades that the broker has open.
db_trades = DB.execute('SELECT trade_id FROM open_trades_live')
Log.write('"daemon.py" recover_trades():\ndb open trades: {}\nbroker open trades: {}'
.format(db_trades, open_trades_broker))
for index, dbt in enumerate(db_trades): # O(n^3)
for otb in open_trades_broker:
if str(dbt[0]) == str(otb.trade_id): # compare trade_id
del db_trades[index]
# The remaining trades are in the "open trades" db table, but
# the broker is not listing them as open.
# They may have closed since the daemon last ran; confirm this.
# Another cause is that trades are automatically removed from
# Oanda's history after much time passes.
for dbt in db_trades:
if Broker.is_trade_closed(dbt[0])[0]:
# Trade is definitely closed; update db.
Log.write('"daemon.py" recover_trades(): Trade {} is closed. Deleting from db.'
.format(dbt[0]))
else:
# Trade is in "open trades" db table and the broker
# says the trade is neither open nor closed.
DB.bug('Trade w/ID ({}) is neither open nor closed.'
.format(dbt[0]))
Log.write('"daemon.py" recover_trades(): Trade w/ID (',
'{}) is neither open nor closed.'.format(dbt[0]))
DB.execute('DELETE FROM open_trades_live WHERE trade_id="{}"'
.format(dbt[0]))
"""
Fill in info not provided by the broker, e.g.
the name of the strategy that opened the trade.