service.hibernate()
service. (method) Overview:Unload the instance of strategy from memory after the current method finishes, and call on_save() to save data for restoration on the next simulation day.
Interface: hibernate()
```<p class="">Stops execution of the current strategy, and calls the 'on_save' method with an empty dictionary. Anything put into that dictionary will be saved until the strategy is restarted on the next simulation day.</p>
## Parameters:
- None
**Sample**
```python
service.hibernate()
- None
- Can be called any time service is availabe.
- Instance doesn't terminate until the current Strategy method returns from being called. In other words, the system doesn't process the terminate request until after returning from the system method.
- on_finish() is called before the instance unloads.
Backtest as a single multiday job. Backtest for at least 2 days.
from cloudquant.interfaces import Strategy
class RestoreData(Strategy):
# note that this doesn't start with "self" because it's a @staticmethod
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
return symbol == 'AAL'
# called at the beginning of each instance
def on_start(self, md, order, service, account):
print 'in on_start\t' + service.time_to_string(service.system_time) + '\n'
# initialize data to be save in on_save()
self.entry_price = 0
self.shares = 0
self.entry_time = service.system_time
# enter a position if no position is held
if account[self.symbol].position.capital_long == 0:
order.algo_buy(self.symbol, 'market', 'init', order_quantity=100)
# otherwise, exit the position and terminate
else:
print 'exit order sent, terminating\n'
order.algo_sell(self.symbol, 'market', 'exit')
service.terminate()
# 3:55 PM.
self.end_time = md.market_close_time - service.time_interval(minutes=5)
def on_save(self, state_dict):
# Add data values to state_dict to be saved
state_dict['entry_time'] = self.entry_time
state_dict['entry_price'] = self.entry_price
state_dict['shares'] = self.shares
print 'saving data', state_dict
def on_restore(self, state_dict):
# print the previously saved data
print 'restoring data ', state_dict
def on_finish(self, md, order, service, account):
print '\nScript Ending at ' + service.time_to_string(service.system_time)
def on_minute_bar(self, event, md, order, service, account, bar):
# if the current time is after the end time for the script, set data values and hibernate
if service.system_time > self.end_time:
self.entry_price= account[self.symbol].position.entry_price
self.shares = account[self.symbol].position.shares
service.hibernate()
Day 1 Console
in on_start 2017-02-07 09:30:00.000000 saving data {'entry_price': 45.439998626708984, 'entry_time': 1486477800000000, 'shares': 100} Script Ending at 2017-02-07 15:55:00.603000
Day 2 Console
in on_start 2017-02-08 09:30:00.000000 exit order sent, terminating restoring data {'entry_price': 45.439998626708984, 'entry_time': 1486477800000000, 'shares': 100} Script Ending at 2017-02-08 09:30:00.001000