Skip to content

Instantly share code, notes, and snippets.

@lionelyoung
Created January 24, 2019 17:12
Show Gist options
  • Save lionelyoung/593bf98d6fb84c60b03378b429f1c8ae to your computer and use it in GitHub Desktop.
Save lionelyoung/593bf98d6fb84c60b03378b429f1c8ae to your computer and use it in GitHub Desktop.
from qtpylib.algo import Algo
from qtpylib import futures
class DebugPosition(Algo):
def on_start(self):
self.counter = 0
self.counter_at_entry = 0
def on_fill(self, instrument, order):
pass
def on_quote(self, instrument):
pass
def on_orderbook(self, instrument):
pass
def on_tick(self, instrument):
pass
def on_bar(self, instrument):
positions = instrument.get_positions()
if not instrument.pending_orders and positions["position"] == 0:
print('BUY pos:{} counter:{}'.format(positions['position'], self.counter))
instrument.buy(1)
self.counter_at_entry = self.counter
elif positions["position"] != 0 and self.counter > self.counter_at_entry + 5:
print('SELL pos:{} counter:{}'.format(positions['position'], self.counter))
instrument.exit()
else:
print('NOACTION pos:{} counter:{}'.format(positions['position'], self.counter))
self.counter += 1
if __name__ == "__main__":
ACTIVE_MONTH = '201903'
if not ACTIVE_MONTH:
print('Getting active contract')
ACTIVE_MONTH = futures.get_active_contract("ES")
print("Active month for ES is:", ACTIVE_MONTH)
# 5 min bars
strategy = DebugPosition(instruments = [ ("ES", "FUT", "GLOBEX", "USD", ACTIVE_MONTH, 0.0, "") ],
resolution = "5T",
preload = "1D",
bar_window = 50,
ibport = 7497,
blotter='MainBlotter')
strategy.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment