if self.volume == 0: return # 这天没有成交量,跳过 self.donchian.update() signal = self.donchian.signal if isinstance(signal, EntrySignal): quantity = self.__get_position_size(signal.price) print '## buy signal', signal.price, quantity if quantity > 0: self.buy('long', signal.price, quantity) self.last_buy_price = signal.price self.stop_price = signal.price - 2 * self.pre_atr print 'buy', self.datetime[0].date( ), signal.price, quantity, self.cash(), signal.desc elif isinstance(signal, ExitSignal): if self.position() > 0: self.sell('long', signal.price, self.position()) self.stop_price = None print 'sel', self.datetime[0].date( ), signal.price, self.position(), signal.desc elif self.stop_price is not None and self.low <= self.stop_price: if self.position() > 0: self.sell('long', self.close, self.position()) self.stop_price = None print 'sel', self.datetime[0].date( ), self.close, self.position(), 'stop' if __name__ == '__main__': import sys code = sys.argv[1] fw.run(TurtleStrategy, code)
P = 10 # 最少交易股数,这里假设是10 quantity = int(self.cash() / self.open / P) * P return quantity def on_bar(self): """ 策略函数,对每根Bar运行一次。""" if self.volume == 0: return # 这天没有成交量,跳过 IN = 20 OUT = 10 price = self.close[0] upper = max([self.close[i] for i in range(0, IN)]) down = min([self.close[i] for i in range(0, OUT)]) if self.position() == 0 and price >= upper: quantity = self.__get_position_size() if quantity > 0: self.buy('long', price, quantity, contract = code) self.buy_price = price self.num_cont += 1 #print 'buy', self.datetime[0].date(), price, quantity elif self.position() > 0 and price <= down: self.sell('long', price, self.position()) #print 'sel', self.datetime[0].date(), price, self.position() #print '---' if price > self.buy_price: self.num_win += 1 if __name__ == '__main__': import sys code = sys.argv[1] fw.run(DonChianStrategy, code)
quantity = int(self.cash() / price / P) * P return quantity def on_bar(self): """ 策略函数,对每根Bar运行一次。""" if self.volume == 0: return # 这天没有成交量,跳过 price = self.close[0] if self.position( ) == 0 and self.mashort > self.malong: # and self.mashort[1] <= self.malong[1]: if self.high <= self.close: print 'fail to buy!' return quantity = self.__get_position_size(price) if quantity > 0: self.buy('long', price, quantity, contract=code) self.buy_price = price self.num_cont += 1 #print 'buy', self.datetime[0].date(), price, quantity elif self.position() > 0 and self.mashort < self.malong: self.sell('long', price, self.position()) #print 'sel', self.datetime[0].date(), price, self.position() #print '---' if price > self.buy_price: self.num_win += 1 if __name__ == '__main__': import sys code = sys.argv[1] fw.run(MaStrategy, code)
def __get_position_size(self, price): P = 10 # 最少交易股数,这里假设是10 quantity = int(self.cash() / price / P) * P return quantity def on_bar(self): """ 策略函数,对每根Bar运行一次。""" if self.volume == 0: return # 这天没有成交量,跳过 price = self.close[0] if self.position() == 0 and self.mashort > self.malong:# and self.mashort[1] <= self.malong[1]: if self.high <= self.close: print 'fail to buy!' return quantity = self.__get_position_size(price) if quantity > 0: self.buy('long', price, quantity, contract = code) self.buy_price = price self.num_cont += 1 #print 'buy', self.datetime[0].date(), price, quantity elif self.position() > 0 and self.mashort < self.malong: self.sell('long', price, self.position()) #print 'sel', self.datetime[0].date(), price, self.position() #print '---' if price > self.buy_price: self.num_win += 1 if __name__ == '__main__': import sys code = sys.argv[1] fw.run(MaStrategy, code)
def on_bar(self): self.__update_atr() if self.volume == 0: return # 这天没有成交量,跳过 self.donchian.update() signal = self.donchian.signal if isinstance(signal, EntrySignal): quantity = self.__get_position_size(signal.price) print '## buy signal', signal.price, quantity if quantity > 0: self.buy('long', signal.price, quantity) self.last_buy_price = signal.price self.stop_price = signal.price - 2 * self.pre_atr print 'buy', self.datetime[0].date(), signal.price, quantity, self.cash(), signal.desc elif isinstance(signal, ExitSignal): if self.position() > 0: self.sell('long', signal.price, self.position()) self.stop_price = None print 'sel', self.datetime[0].date(), signal.price, self.position(), signal.desc elif self.stop_price is not None and self.low <= self.stop_price: if self.position() > 0: self.sell('long', self.close, self.position()) self.stop_price = None print 'sel', self.datetime[0].date(), self.close, self.position(), 'stop' if __name__ == '__main__': import sys code = sys.argv[1] fw.run(TurtleStrategy, code)
quantity = int(self.cash() / price / P) * P return quantity def on_bar(self): """ 策略函数,对每根Bar运行一次。""" if self.volume == 0: return # 这天没有成交量,跳过 IN = 20 OUT = 10 price = self.close[0] upper = max([self.close[i] for i in range(0, IN)]) down = min([self.close[i] for i in range(0, OUT)]) if self.position() == 0 and self.high >= upper: quantity = self.__get_position_size(price) if quantity > 0: self.buy('long', price, quantity) self.buy_price = price self.num_cont += 1 #print 'buy', self.datetime[0].date(), price, quantity elif self.position() > 0 and self.low <= down: self.sell('long', price, self.position()) #print 'sel', self.datetime[0].date(), price, self.position() #print '---' if price > self.buy_price: self.num_win += 1 if __name__ == '__main__': import sys code = sys.argv[1] fw.run(DonChianStrategy, code)