diff --git a/Breakout/main.py b/Breakout/main.py index 3205840..a38238e 100644 --- a/Breakout/main.py +++ b/Breakout/main.py @@ -1,5 +1,6 @@ from AlgorithmImports import QCAlgorithm, Resolution, BrokerageName, OrderProperties, TimeInForce from datetime import timedelta, datetime +from Breakout.position import Position from indicators import SymbolIndicators @@ -55,7 +56,7 @@ def OnData(self, data): if not self.symbol_map[symbol].ready: continue if self.sell_signal(symbol, data): - self.Liquidate(symbol) + self.sell(symbol) if self.symbol_map[symbol].uptrending and not self.ActiveSecurities[symbol].Invested: symbols.append(symbol) self.live_log("processing on data") @@ -85,8 +86,14 @@ def buy(self, symbol, order_tag=None, order_properties=None, price=None): else: self.live_log(f"Market order {symbol.Value} {position_value}: {order_tag or 'no tag'}") self.MarketOrder(symbol, position_size, tag=order_tag) + self.ObjectStore.save(symbol.Value, Position(self.Time)) else: self.live_log(f"insufficient cash ({self.Portfolio.Cash}) to purchase {symbol.Value}") + + def sell(self, symbol): + self.Liquidate(symbol) + if self.ObjectStore.ContainsKey(symbol.Value): + self.ObjectStore.Delete(symbol.Value) def good_for_a_day(self): """ diff --git a/Breakout/position.py b/Breakout/position.py new file mode 100644 index 0000000..5cec259 --- /dev/null +++ b/Breakout/position.py @@ -0,0 +1,34 @@ +import datetime +from dateutil.parser import parse +import json + + +class Position: + """ + Stores information relative to an open position. + Intended to be persisted in object storage as a json string. + """ + start: datetime.datetime + + def __init__(self, start: datetime.datetime = None) -> None: + self.start = start + + def __str__(self) -> str: + return json.dumps({ + "start": str(self.start), + }) + + +def position_from_str(content: str) -> Position: + """ + Method for parsing a Position from object storage. + + Args: + content (str): the Position content as a json string. + + Returns: + Position: The position class. + """ + content = json.loads(content) + content['start'] = parse(content['start']) + return Position(**content)