Hello AM,
Thanks - your test is attached for others to see. I've asked for feedback from Quantopian about the syntax of stop and limit orders. Looking back at some zipline work I did I was writing this i.e. a market order followed by a stop order:
from datetime import datetime
import pytz
from zipline.transforms.utils import EventWindow
from zipline.algorithm import TradingAlgorithm
from zipline.utils.factory import load_from_yahoo
class BuyApple(TradingAlgorithm):
def initialize(self):
self.firstOrderplaced = False
def handle_data(self, data):
print "--------------------"
print "Datetime in handle_data : " + str(self.datetime)
print data['AAPL'].price
print self.portfolio.cash
if not self.firstOrderplaced:
self.order('AAPL', 100,)
self.order('AAPL', -100, stop_price=118.00)
#slf.order('AAPL', -100, limit_price=131.00)
#self.order('AAPL', -100, limit_price=123.00, stop_price=125.60)
self.firstOrderplaced = True
if __name__ == '__main__':
start = datetime(2008, 1, 26, 0, 0, 0, 0, pytz.utc)
end = datetime(2008, 4, 1, 0, 0, 0, 0, pytz.utc)
data = load_from_yahoo(stocks=['AAPL'], indexes={}, start=start,
end=end)
simple_algo = BuyApple(instant_fill=True)
results = simple_algo.run(data)
When run in IDLE the output is:
>>>
AAPL
--------------------
Datetime in handle_data : 2008-01-28 00:00:00+00:00
125.71
100000.0
--------------------
Datetime in handle_data : 2008-01-29 00:00:00+00:00
127.19
87413.429
--------------------
Datetime in handle_data : 2008-01-30 00:00:00+00:00
127.81
87413.429
--------------------
Datetime in handle_data : 2008-01-31 00:00:00+00:00
130.88
87413.429
--------------------
Datetime in handle_data : 2008-02-01 00:00:00+00:00
129.32
87413.429
--------------------
Datetime in handle_data : 2008-02-04 00:00:00+00:00
127.29
87413.429
--------------------
Datetime in handle_data : 2008-02-05 00:00:00+00:00
125.08
87413.429
--------------------
Datetime in handle_data : 2008-02-06 00:00:00+00:00
117.96
87413.429
--------------------
Datetime in handle_data : 2008-02-07 00:00:00+00:00
117.23
99194.633
--------------------
P.