forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
PythonApiCodingStyle2
OpenTrading edited this page Aug 29, 2016
·
1 revision
The proposed style conforms to the current Python recommended naming conventions but completely breaks with the naming conventions in the C# code, making it much harder to find things between the languages.
def initialize(self):
self.set_start_date(2013,10,07) #Set Start Date
self.set_end_date(2013,10,11) #Set End Date
self.set_cash(100000) #Set Strategy Cash
self.add_security(PySecurityType.equity, "SPY",
PyResolution.second)
def on_data(self, data):
if not self.portfolio.invested:
self.set_holdings("SPY", 1) def init(self):
self.__macd = None
self.__symbol = "SPY"
def initialize(self):
self.security = self.add_security(PySecurityType.equity,
self.__symbol)
# define our daily macd(12,26) with a 9 day signal
self.__macd = self.macd(self.__symbol, 9, 26, 9,
PyMovingAverageType.exponential, PyResolution.daily)
def on_data(self, data):
if not self.__macd.is_ready: return
tolerance = 0.0025;
holdings = self.security.holdings.quantity
signal_delta_percent =
(self.__macd.current.value -
self.__macd.signal.current.value)/
self.__macd.fast.current.value
if holdings <= 0 and signal_delta_percent > tolerance:
self.set_holdings(self.__Symbol, 1.0)
elif holdings >= 0 and signal_delta_percent < -tolerance:
self.liquidate(self.__Symbol) def __init__(self):
self.__changes = PySecurityChanges.none
def initialize(self):
self.PyUniverseSettings.resolution = PyResolution.daily
self.add_universe(self.coarse_selection_function)
def on_data(self, data):
if self.__changes == PySecurityChanges.none: return
for security in self.__changes.removed_securities:
if security.invested:
self.liquidate(security.symbol)
for security in self.__changes.added_securities:
self.set_holdings(security.symbol, Decimal(0.2))
self.__changes = PySecurityChanges.none;
def on_securities_changed(self, changes):
self.__changes = changes
def coarse_selection_function(self, coarse):
sorted_by_dollar_volume =
sorted(coarse, key=lambda x: x.dollar_volume, reverse=True)
top5 = sorted_by_dollar_volume [:5]
return [x.symbol for x in top5] def __init__(self):
self.__symbol = PySymbol.create("SPY", PySecurityType.equity,
"USA")
self.__open_market_on_open_orders = []
def on_data(self, data):
if self.TimeIs(8, 12 + 2, 0):
self.log("Submitting MarketOnOpenOrder")
new_ticket = self.market_on_open_order(self.__symbol , 50)
self.__open_market_on_open_orders.append(new_ticket)
if len(self.__open_market_on_open_orders) == 1
and datetime(self.time).minute == 59:
ticket = self.__open_market_on_open_orders[0]
if ticket.status == PyOrderStatus.filled:
self.__open_market_on_open_orders = []
return
quantity = ticket.quantity + 1
self.log("Updating quantity - New Quantity: {0}"
.format(quantity))
update_order_fields = update_order_fields()
update_order_fields.quantity = quantity
update_order_fields.tag = "Update #{0}"
.format(len(ticket.update_requests) + 1)
ticket.update(update_order_fields)
def on_order_event(self, order_event):
order = self.transactions.get_order_by_id(order_event.order_id)
self.log("{0}: {1}: {2}".format(self.time, order.type, order_event))
def initialize(self):
self.set_brokerage_message_handler(
CustomBrokerageMessageHandler(self))
class CustomBrokerageMessageHandler(PyBrokerageMessageHandler):
def __init__(self, algo):
self._algo = algo
def handle(self, message):
to_log = "{0} Event: {1}"
.format(self._algo.time, message.message)
self._algo.debug(to_log)
self._algo.log(to_log)