相場商売

トレーディングビジネスあれこれ

16章

Ruleクラスとその子クラス。

かなり複雑になってきた。
チェックの部分では、ストックのデータがないとエラーが出たため
株価データを入れている。
またEntryのチェックで'volume'のデータを入れないとエラーになったので
とりあえず1で入れている。

他に気になる部分としては、
モジュールのインポートの問題でlibフォルダにばかりファイルをつくっていたが、
そろそろ変更しないといけない。
また以前の章で作ったファイルもチェックで動かなかったため修正したりしているので、
一段落したら以前のファイルも修正してアップしなおす予定。

rule.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from trade import Trade
import Tick
    
# 各ルールの親クラス
class Rule:
    
    def __init__(self):
        self.stock = None
        pass
        
    def calculate_indicators(self):
        pass
    
    def _with_valid_indicators(self, func):
        try:
            return func
        except ValueError:
            pass

entry.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from rule import Rule
from trade import Trade

# 仕掛けルールの親クラス
class Entry(Rule):
    
    def check_long_entry(self, index):
        return self._with_valid_indicators(self.check_long(index))
    
    def check_long(self, index):
        pass
    
    def check_short_entry(self, index):
        return self._with_valid_indicators(self.check_short(index))
    
    def check_short(self, index):
        pass
        
    def _enter(self, index, price, long_short, entry_time):
        return Trade({'stock_code': self.stock.code,
                      'trade_type': long_short,
                      'entry_date': self.stock.dates()[index],
                      'entry_price': price,
                      'volume': 1,
                      'entry_time': entry_time})
        
    def _enter_long(self, index, price, entry_time):
        return self._enter(index, price, 'long', entry_time)
    
    def _enter_short(self, index, price, entry_time):
        return self._enter(index, price, 'short', entry_time)    

exit.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from rule import Rule

# 仕掛けルールの親クラス
class Exit(Rule):
        
    def check_exit(self, trade, index):
        def cefunc():
            if trade.long_check():
                self.check_long(trade, index)
            elif trade.short_check():
                self.check_short(trade, index)            
        self._with_valid_indicators(cefunc())
    
    def _exit(self, trade, index, price, time):
        trade.exit({'exit_date': self.stock.dates()[index],
                    'exit_price': price,
                    'exit_time': time})

stop.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from rule import Rule

# ストップクラス
class Stop(Rule):
    
    def get_stop(self, position, index):
        def gsfunc():
            if position.long_check() == True:
                return self.stop_price_long(position, index)
            elif position.short_check() == True:
                return self.stop_price_short(position, index)
        return self._with_valid_indicators(gsfunc())

filter.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from rule import Rule

# フィルタークラス
# 仕掛けを制限する
class Filter(Rule):
    
    def get_filter(self, index):
        return self._with_valid_indicators(self.filter(index))
    
    def filter(self, index):
        pass

rule_check.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from stock import Stock
import Tick

# Entryから仕掛けクラスを作るテスト
from entry import Entry

class MyEntry(Entry):
    
    def check_long(self, index):
        if index % 2 == 0:
            return self._enter_long(index, 100, 'close')
            
    def check_short(self, index):
        if index % 2 == 1:
            return self._enter_short(index, 100, 'close')
            
stock = Stock(1000, 't', 100)

stock.add_price('2011-07-01', 402, 402, 395, 397, 17495700)
stock.add_price('2011-07-04', 402, 404, 400, 403, 18819300)
stock.add_price('2011-07-05', 402, 408, 399, 401, 20678000)

entry = MyEntry()
entry.stock = stock

t = entry.check_long_entry(0)
print(t)
print(t.entry_date)
print(t.entry_price)
print(t.entry_time)
print(t.length)
print(t.stock_code)
print(t.trade_type)
print(t.volume)
print(entry.check_long_entry(1))

print(entry.check_short_entry(0))
t = entry.check_short_entry(1)
print(t)
print(t.entry_date)
print(t.entry_price)
print(t.entry_time)
print(t.length)
print(t.stock_code)
print(t.trade_type)
print(t.volume)

from exit import Exit
# Exitから手仕舞いクラスを作るテスト
class MyExit(Exit):

    def check_long(self, trade, index):
        if index % 2 == 1:
            return self._exit(trade, index, 105, 'close')
            
    def check_short(self, trade, index):
        if index % 2 == 0:
            return self._exit(trade, index, 95, 'close')
        
my_exit = MyExit()
my_exit.stock = stock
trade1 = entry.check_long(0)
my_exit.check_exit(trade1, 1)
print(trade1.entry_price)
print(trade1.exit_price)

trade2 = entry.check_short(1)
my_exit.check_exit(trade2, 2)
print(trade2.entry_price)
print(trade2.exit_price)

from stop import Stop
# Stopからすトップクラスを作るテスト
class MyStop(Stop):
    
    def stop_price_long(self, position, index):
        return Tick.down(position.entry_price, 5)
    
    def stop_price_short(self, position, index):
        return Tick.up(position.entry_price, 5)
    
stop = MyStop()
trade3 = entry.check_long(0)
print(stop.get_stop(trade3, 0))
trade4 = entry.check_short(1)
print(stop.get_stop(trade4, 1))

from filter import Filter
# Filter からフィルタークラスを作るテスト
class MyFilter(Filter):
    
    def filter(self, index):
        if index % 4 == 0:
            return 'long_only'
        elif index % 4 == 1:
            return 'short_only'
        elif index % 4 == 2:
            return 'no_entry'
        elif index % 4 == 3:
            return 'long_and_short'

filter = MyFilter()
print(filter.get_filter(0))
print(filter.get_filter(1))    
print(filter.get_filter(2))    
print(filter.get_filter(3))