# MA Cross.pt -- 10/40移動平均交差システム by Richard Donchian
# MA(10)がMA(40)を上抜いたらentry、下抜いたらexit。-10%の逆指値でexit。
# ただし、終値50円以下、出来高1000万円以下の場合はentryしない。
# 価格は翌日の寄り付きか当日の終値。買い数量は約100万円。
# written by panacoran <panacoran@users.sourceforge.jp>

require "TIlib"

def Num(price)
	num = 1000000/price
	if num >= 1000
		num = (num/1000)*1000
	elsif num >= 100
		num = (num/100)*100
	elsif num == 0
		num = 1
	end
	return num
end

if !$__INIT__
	$MA_fast = MA_new(10)
	$MA_slow = MA_new(40)
	$Cross = Crossover_new(0, 0.0)
	$__INIT__ = 1
else
	MA_next($MA_fast)
	MA_next($MA_slow)
end

ma_fast = MA_value($MA_fast)
ma_slow = MA_value($MA_slow)
price = {+1}Open
if !price
	price = Close
end

if ma_fast == null || ma_slow == null || !price
else
	cross = Crossover_next($Cross, ma_fast, ma_slow)
	if !$long && cross > 0 && Close > 50 && Volume * Close > 10000
		$long = Num(price)
		Buy(price, $long)
		$loss_cut = Yobine(price * 0.90)
	elsif $long
		if Low > 0 && Low <= $loss_cut
			Sell(Min($loss_cut, Open), $long)
			$long = 0
		elsif cross < 0
			Sell(price, $long)
			$long = 0
		end
	end
end

# Local Variables:
# tab-width: 4
# End:
