|
From: Márcio V. d. S. <mv...@us...> - 2007-06-14 20:48:03
|
Update of /cvsroot/tail/Tail/src/java/net/sf/tail/strategy In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv3655/src/java/net/sf/tail/strategy Modified Files: MinValueStarterStrategy.java AlwaysOperateStrategy.java DistanceBetweenIndicatorsStrategy.java IndicatorOverIndicatorStrategy.java ResistanceStrategy.java PipeEnterStrategy.java SupportStrategy.java MinValueStopperStrategy.java JustBuyOnceStrategy.java IndicatorCrossedIndicatorStrategy.java Added Files: AbstractStrategy.java Log Message: Refatoração das Strategies (criação do template method AbstractStrategy) Index: PipeEnterStrategy.java =================================================================== RCS file: /cvsroot/tail/Tail/src/java/net/sf/tail/strategy/PipeEnterStrategy.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** PipeEnterStrategy.java 8 Jun 2007 15:11:16 -0000 1.5 --- PipeEnterStrategy.java 14 Jun 2007 20:48:01 -0000 1.6 *************** *** 2,6 **** import net.sf.tail.Indicator; - import net.sf.tail.Strategy; import net.sf.tail.Trade; import net.sf.tail.indicator.helper.CrossIndicator; --- 2,5 ---- *************** *** 15,25 **** * */ ! public class PipeEnterStrategy implements Strategy { ! ! private final Indicator<? extends Number> upper; ! ! private final Indicator<? extends Number> lower; ! ! private final Indicator<? extends Number> value; private final Indicator<Boolean> crossUp; --- 14,18 ---- * */ ! public class PipeEnterStrategy extends AbstractStrategy { private final Indicator<Boolean> crossUp; *************** *** 29,83 **** public PipeEnterStrategy(Indicator<? extends Number> upper, Indicator<? extends Number> lower, Indicator<? extends Number> value) { - this.upper = upper; - this.lower = lower; - this.value = value; crossUp = new CrossIndicator(value, upper); crossDown = new CrossIndicator(lower, value); } ! // public Operation shouldEnter(int index) { ! // if (crossUp.getValue(index)) ! // return new Operation(index, OperationType.SELL); ! // if (crossDown.getValue(index)) ! // return new Operation(index, OperationType.BUY); ! // return null; ! // } ! // ! // public Operation shouldExit(Operation entry, int index) { ! // if (crossDown.getValue(index)) ! // return new Operation(index, OperationType.BUY); ! // if (crossUp.getValue(index)) ! // return new Operation(index, OperationType.SELL); ! // return null; ! // } ! ! public boolean shouldOperate(Trade trade, int index) { ! if (trade.isNew()) { ! if (crossUp.getValue(index)) ! return trade.exit(index); ! ! if (crossDown.getValue(index)) ! return trade.enter(index); ! } ! else if (trade.isOpened()) { ! if (crossDown.getValue(index)) ! return trade.enter(index); ! ! if (crossUp.getValue(index)) ! return trade.exit(index); ! } return false; } ! public Indicator<? extends Number> getLower() { ! return lower; ! } ! ! public Indicator<? extends Number> getUpper() { ! return upper; ! } ! ! public Indicator<? extends Number> getValue() { ! return value; } --- 22,47 ---- public PipeEnterStrategy(Indicator<? extends Number> upper, Indicator<? extends Number> lower, Indicator<? extends Number> value) { crossUp = new CrossIndicator(value, upper); crossDown = new CrossIndicator(lower, value); } ! @Override ! protected boolean shouldEnter(Trade trade, int index) { ! if (crossUp.getValue(index)) ! return trade.exit(index); ! ! if (crossDown.getValue(index)) ! return trade.enter(index); return false; } ! @Override ! protected boolean shouldExit(Trade trade, int index) { ! if (crossDown.getValue(index)) ! return trade.enter(index); ! ! if (crossUp.getValue(index)) ! return trade.exit(index); ! return false; } Index: AlwaysOperateStrategy.java =================================================================== RCS file: /cvsroot/tail/Tail/src/java/net/sf/tail/strategy/AlwaysOperateStrategy.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** AlwaysOperateStrategy.java 8 Jun 2007 15:11:16 -0000 1.2 --- AlwaysOperateStrategy.java 14 Jun 2007 20:48:01 -0000 1.3 *************** *** 1,27 **** package net.sf.tail.strategy; - import net.sf.tail.Strategy; import net.sf.tail.Trade; ! public class AlwaysOperateStrategy implements Strategy { ! ! // public Operation shouldEnter(int index) { ! // // TODO Auto-generated method stub ! // return null; ! // } ! // ! // public Operation shouldExit(Operation entry, int index) { ! // // TODO Auto-generated method stub ! // return null; ! // } ! public boolean shouldOperate(Trade trade, int index) { ! if (trade.isNew()) { ! return trade.enter(index); ! } else if (trade.isOpened()) { ! return trade.exit(index); ! } ! return false; } } --- 1,16 ---- package net.sf.tail.strategy; import net.sf.tail.Trade; ! public class AlwaysOperateStrategy extends AbstractStrategy { ! @Override ! protected boolean shouldEnter(Trade trade, int index) { ! return trade.enter(index); } + @Override + protected boolean shouldExit(Trade trade, int index) { + return trade.exit(index); + } } Index: IndicatorOverIndicatorStrategy.java =================================================================== RCS file: /cvsroot/tail/Tail/src/java/net/sf/tail/strategy/IndicatorOverIndicatorStrategy.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** IndicatorOverIndicatorStrategy.java 8 Jun 2007 15:11:16 -0000 1.4 --- IndicatorOverIndicatorStrategy.java 14 Jun 2007 20:48:01 -0000 1.5 *************** *** 2,9 **** import net.sf.tail.Indicator; - import net.sf.tail.Strategy; import net.sf.tail.Trade; ! public class IndicatorOverIndicatorStrategy implements Strategy { private Indicator<Double> first; --- 2,8 ---- import net.sf.tail.Indicator; import net.sf.tail.Trade; ! public class IndicatorOverIndicatorStrategy extends AbstractStrategy{ private Indicator<Double> first; *************** *** 16,47 **** } ! public boolean shouldOperate(Trade trade, int index) { ! if (trade.isNew()) { ! if (first.getValue(index).compareTo(second.getValue(index)) < 0) { ! return trade.enter(index); ! } } ! else if (trade.isOpened()) { ! if (first.getValue(index).compareTo(second.getValue(index)) > 0) { ! return trade.exit(index); ! } } return false; } - - // public Operation shouldEnter(int index) { - // if (first.getValue(index).compareTo(second.getValue(index)) < 0) { - // return new Operation(index, OperationType.BUY); - // } - // return null; - // } - // - // public Operation shouldExit(Operation entry, int index) { - // if (first.getValue(index).compareTo(second.getValue(index)) > 0) { - // return new Operation(index, OperationType.SELL); - // } - // return null; - // } - } --- 15,32 ---- } ! @Override ! protected boolean shouldEnter(Trade trade, int index) { ! if (first.getValue(index).compareTo(second.getValue(index)) < 0) { ! return trade.enter(index); } + return false; + } ! @Override ! protected boolean shouldExit(Trade trade, int index) { ! if (first.getValue(index).compareTo(second.getValue(index)) > 0) { ! return trade.exit(index); } return false; } } Index: DistanceBetweenIndicatorsStrategy.java =================================================================== RCS file: /cvsroot/tail/Tail/src/java/net/sf/tail/strategy/DistanceBetweenIndicatorsStrategy.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** DistanceBetweenIndicatorsStrategy.java 8 Jun 2007 15:11:15 -0000 1.4 --- DistanceBetweenIndicatorsStrategy.java 14 Jun 2007 20:48:01 -0000 1.5 *************** *** 2,6 **** import net.sf.tail.Indicator; - import net.sf.tail.Strategy; import net.sf.tail.Trade; --- 2,5 ---- *************** *** 15,27 **** * */ ! public class DistanceBetweenIndicatorsStrategy implements Strategy { ! private Indicator<? extends Number> upper; ! private Indicator<? extends Number> lower; ! private double distance; ! private double difference; public DistanceBetweenIndicatorsStrategy(Indicator<? extends Number> upper, Indicator<? extends Number> lower, --- 14,26 ---- * */ ! public class DistanceBetweenIndicatorsStrategy extends AbstractStrategy{ ! Indicator<? extends Number> upper; ! Indicator<? extends Number> lower; ! double distance; ! double difference; public DistanceBetweenIndicatorsStrategy(Indicator<? extends Number> upper, Indicator<? extends Number> lower, *************** *** 33,78 **** } ! // public Operation shouldEnter(int index) { ! // if ((upper.getValue(index).doubleValue() - lower.getValue(index).doubleValue()) >= (difference + 1.0) ! // * distance) { ! // return new Operation(index, OperationType.BUY); ! // } ! // return null; ! // } ! // ! // public Operation shouldExit(Operation entry, int index) { ! // if ((upper.getValue(index).doubleValue() - lower.getValue(index).doubleValue()) <= (1.0 - difference) ! // * distance) { ! // return new Operation(index, OperationType.SELL); ! // } ! // return null; ! // } ! ! public boolean shouldOperate(Trade trade, int index) { ! if (trade.isNew()) { ! if ((upper.getValue(index).doubleValue() - lower.getValue(index).doubleValue()) >= (difference + 1.0) ! * distance) { ! return trade.enter(index); ! } ! } ! else if (trade.isOpened()) { ! if ((upper.getValue(index).doubleValue() - lower.getValue(index).doubleValue()) <= (1.0 - difference) ! * distance) { ! return trade.exit(index); ! } } - return false; } } - - // TODO APAGAR ISTO DEPOIS DA REFATORACAO - //if (trade.isNew()) { - // return trade.enter(index); - // } - //} - //else if (trade.isOpened()) { - // return trade.exit(index); - // } - //} --- 32,51 ---- } ! @Override ! protected boolean shouldEnter(Trade trade, int index) { ! if ((upper.getValue(index).doubleValue() - lower.getValue(index).doubleValue()) >= (difference + 1.0) ! * distance) { ! return trade.enter(index); } return false; } + @Override + protected boolean shouldExit(Trade trade, int index) { + if ((upper.getValue(index).doubleValue() - lower.getValue(index).doubleValue()) <= (1.0 - difference) + * distance) { + return trade.exit(index); + } + return false; + } } Index: MinValueStarterStrategy.java =================================================================== RCS file: /cvsroot/tail/Tail/src/java/net/sf/tail/strategy/MinValueStarterStrategy.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** MinValueStarterStrategy.java 8 Jun 2007 15:11:15 -0000 1.6 --- MinValueStarterStrategy.java 14 Jun 2007 20:48:01 -0000 1.7 *************** *** 10,14 **** * venda nessa mesma {@link Strategy} */ ! public class MinValueStarterStrategy implements Strategy { private Strategy strategy; --- 10,14 ---- * venda nessa mesma {@link Strategy} */ ! public class MinValueStarterStrategy extends AbstractStrategy{ private Strategy strategy; *************** *** 24,54 **** } ! // public Operation shouldEnter(int index) { ! // if (indicator.getValue(index).doubleValue() > start) { ! // return strategy.shouldEnter(index); ! // } ! // ! // return null; ! // } ! // ! // public Operation shouldExit(Operation entry, int index) { ! // return strategy.shouldExit(entry, index); ! // } ! // ! // public Strategy getStrategy() { ! // return strategy; ! // } ! ! public boolean shouldOperate(Trade trade, int index) { ! if (trade.isNew()) { ! if (indicator.getValue(index).doubleValue() > start) { ! return trade.enter(index); ! } ! } ! else if (trade.isOpened()) { ! return strategy.shouldOperate(trade, index); } return false; } } --- 24,39 ---- } ! @Override ! protected boolean shouldEnter(Trade trade, int index) { ! if (indicator.getValue(index).doubleValue() > start) { ! return trade.enter(index); } return false; } + @Override + protected boolean shouldExit(Trade trade, int index) { + return strategy.shouldOperate(trade, index); + } + } Index: JustBuyOnceStrategy.java =================================================================== RCS file: /cvsroot/tail/Tail/src/java/net/sf/tail/strategy/JustBuyOnceStrategy.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** JustBuyOnceStrategy.java 8 Jun 2007 15:11:16 -0000 1.3 --- JustBuyOnceStrategy.java 14 Jun 2007 20:48:01 -0000 1.4 *************** *** 1,32 **** package net.sf.tail.strategy; - import net.sf.tail.Strategy; import net.sf.tail.Trade; ! public class JustBuyOnceStrategy implements Strategy { ! ! // private boolean buyed; ! // private int buyedIndex; ! public JustBuyOnceStrategy() { } ! ! // public Operation shouldEnter(int index) { ! // if(!buyed || buyedIndex == index ){ ! // buyed = true; ! // buyedIndex = index; ! // return new Operation(index, OperationType.BUY); ! // } ! // return null; ! // } ! // ! // public Operation shouldExit(Operation entry, int index) { ! // return null; ! // } ! ! public boolean shouldOperate(Trade trade, int index) { ! if (trade.isNew()) { ! return trade.enter(index); ! } return false; } --- 1,15 ---- package net.sf.tail.strategy; import net.sf.tail.Trade; ! public class JustBuyOnceStrategy extends AbstractStrategy { ! @Override ! protected boolean shouldEnter(Trade trade, int index) { ! return trade.enter(index); } ! ! @Override ! protected boolean shouldExit(Trade trade, int index) { return false; } Index: ResistanceStrategy.java =================================================================== RCS file: /cvsroot/tail/Tail/src/java/net/sf/tail/strategy/ResistanceStrategy.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** ResistanceStrategy.java 8 Jun 2007 15:11:16 -0000 1.5 --- ResistanceStrategy.java 14 Jun 2007 20:48:01 -0000 1.6 *************** *** 13,17 **** * */ ! public class ResistanceStrategy implements Strategy { private final Strategy strategy; --- 13,17 ---- * */ ! public class ResistanceStrategy extends AbstractStrategy { private final Strategy strategy; *************** *** 27,49 **** } ! // public Operation shouldEnter(int index) { ! // return strategy.shouldEnter(index); ! // } ! // ! // public Operation shouldExit(Operation entry, int index) { ! // if (indicator.getValue(index).doubleValue() >= resistance) ! // return new Operation(index, OperationType.SELL); ! // return strategy.shouldExit(entry, index); ! // } ! public boolean shouldOperate(Trade trade, int index) { ! if (trade.isNew()) { ! return strategy.shouldOperate(trade, index); ! } else if (trade.isOpened()) { ! if (indicator.getValue(index).doubleValue() >= resistance) ! return trade.exit(index); ! return strategy.shouldOperate(trade, index); ! } ! return false; } } --- 27,40 ---- } ! @Override ! protected boolean shouldEnter(Trade trade, int index) { ! return strategy.shouldOperate(trade, index); ! } ! @Override ! protected boolean shouldExit(Trade trade, int index) { ! if (indicator.getValue(index).doubleValue() >= resistance) ! return trade.exit(index); ! return strategy.shouldOperate(trade, index); } } Index: SupportStrategy.java =================================================================== RCS file: /cvsroot/tail/Tail/src/java/net/sf/tail/strategy/SupportStrategy.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** SupportStrategy.java 8 Jun 2007 15:11:16 -0000 1.5 --- SupportStrategy.java 14 Jun 2007 20:48:01 -0000 1.6 *************** *** 12,16 **** * */ ! public class SupportStrategy implements Strategy { private final Strategy strategy; --- 12,16 ---- * */ ! public class SupportStrategy extends AbstractStrategy { private final Strategy strategy; *************** *** 26,48 **** } ! // public Operation shouldEnter(int index) { ! // if (indicator.getValue(index).doubleValue() <= support) ! // return new Operation(index, OperationType.BUY); ! // return strategy.shouldEnter(index); ! // } ! // ! // public Operation shouldExit(Operation entry, int index) { ! // return strategy.shouldExit(entry, index); ! // } ! public boolean shouldOperate(Trade trade, int index) { ! if (trade.isNew()) { ! if (indicator.getValue(index).doubleValue() <= support) ! return trade.enter(index); ! return strategy.shouldOperate(trade, index); ! } else if (trade.isOpened()) { ! return strategy.shouldOperate(trade, index); ! } ! return false; } } --- 26,39 ---- } ! @Override ! protected boolean shouldEnter(Trade trade, int index) { ! if (indicator.getValue(index).doubleValue() <= support) ! return trade.enter(index); ! return strategy.shouldOperate(trade, index); ! } ! @Override ! protected boolean shouldExit(Trade trade, int index) { ! return strategy.shouldOperate(trade, index); } } Index: MinValueStopperStrategy.java =================================================================== RCS file: /cvsroot/tail/Tail/src/java/net/sf/tail/strategy/MinValueStopperStrategy.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** MinValueStopperStrategy.java 8 Jun 2007 15:11:16 -0000 1.3 --- MinValueStopperStrategy.java 14 Jun 2007 20:48:01 -0000 1.4 *************** *** 6,16 **** /** ! * MinValueStopperStrategy baseia a compra em uma {@link Strategy} enviada como parâmetro, ! * registrando o valor do {@link Indicator} enviado como parâmetro no mesmo Ãndice de compra, ! * e baseia a venda nessa mesma {@link Strategy} desde que o valor registrado do {@link Indicator} ! * na compra não tenha um decréscimo maior que loss % ! * */ ! public class MinValueStopperStrategy implements Strategy { private Strategy strategy; --- 6,17 ---- /** ! * MinValueStopperStrategy baseia a compra em uma {@link Strategy} enviada como ! * parâmetro, registrando o valor do {@link Indicator} enviado como parâmetro no ! * mesmo Ãndice de compra, e baseia a venda nessa mesma {@link Strategy} desde ! * que o valor registrado do {@link Indicator} na compra não tenha um decréscimo ! * maior que loss % ! * */ ! public class MinValueStopperStrategy extends AbstractStrategy{ private Strategy strategy; *************** *** 28,67 **** } ! // public Operation shouldEnter(int index) { ! // if (strategy.shouldEnter(index) == null) { ! // return null; ! // } ! // value = indicator.getValue(index).doubleValue(); ! // return strategy.shouldEnter(index); ! // ! // } ! // ! // public Operation shouldExit(Operation entry, int index) { ! // if (value - (value * (loss / 100)) > indicator.getValue(index).doubleValue()) { ! // return new Operation(index, OperationType.SELL); ! // } ! // return strategy.shouldExit(entry, index); ! // } ! ! public boolean shouldOperate(Trade trade, int index) { ! // TODO: Código replicado. Template method?? ! if (trade.isNew()) { ! value = indicator.getValue(index).doubleValue(); ! return strategy.shouldOperate(trade, index); ! } else if (trade.isOpened()) { ! if (value - (value * (loss / 100)) > indicator.getValue(index).doubleValue()) { ! return trade.exit(index); ! } ! return strategy.shouldOperate(trade, index); ! } ! return false; ! } ! ! public double getLoss() { ! return loss; } ! public Strategy getStrategy() { ! return strategy; } --- 29,44 ---- } ! @Override ! protected boolean shouldEnter(Trade trade, int index) { ! value = indicator.getValue(index).doubleValue(); ! return strategy.shouldOperate(trade, index); } ! @Override ! protected boolean shouldExit(Trade trade, int index) { ! if (value - (value * (loss / 100)) > indicator.getValue(index).doubleValue()) { ! return trade.exit(index); ! } ! return strategy.shouldOperate(trade, index); } --- NEW FILE: AbstractStrategy.java --- package net.sf.tail.strategy; import net.sf.tail.Strategy; import net.sf.tail.Trade; public abstract class AbstractStrategy implements Strategy { public boolean shouldOperate(Trade trade, int index) { if (trade.isNew()) { return shouldEnter(trade,index); } else if (trade.isOpened()) { return shouldExit(trade,index); } return false; } protected abstract boolean shouldEnter(Trade trade, int index); protected abstract boolean shouldExit(Trade trade, int index); } Index: IndicatorCrossedIndicatorStrategy.java =================================================================== RCS file: /cvsroot/tail/Tail/src/java/net/sf/tail/strategy/IndicatorCrossedIndicatorStrategy.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** IndicatorCrossedIndicatorStrategy.java 8 Jun 2007 15:11:16 -0000 1.5 --- IndicatorCrossedIndicatorStrategy.java 14 Jun 2007 20:48:01 -0000 1.6 *************** *** 2,6 **** import net.sf.tail.Indicator; - import net.sf.tail.Strategy; import net.sf.tail.Trade; import net.sf.tail.indicator.helper.CrossIndicator; --- 2,5 ---- *************** *** 14,18 **** * */ ! public class IndicatorCrossedIndicatorStrategy implements Strategy { --- 13,17 ---- * */ ! public class IndicatorCrossedIndicatorStrategy extends AbstractStrategy{ *************** *** 27,51 **** } ! // public Operation shouldEnter(int index) { ! // if (crossUp.getValue(index)) ! // return new Operation(index, OperationType.BUY); ! // return null; ! // } ! // ! // public Operation shouldExit(Operation entry, int index) { ! // if (crossDown.getValue(index)) ! // return new Operation(index, OperationType.SELL); ! // return null; ! // } ! public boolean shouldOperate(Trade trade, int index) { ! if (trade.isNew()) { ! if (crossUp.getValue(index)) { ! return trade.enter(index); ! } ! } else if (trade.isOpened()) { ! if (crossDown.getValue(index)) { ! return trade.exit(index); ! } } return false; --- 26,41 ---- } ! @Override ! protected boolean shouldEnter(Trade trade, int index) { ! if (crossUp.getValue(index)) { ! return trade.enter(index); ! } ! return false; ! } ! @Override ! protected boolean shouldExit(Trade trade, int index) { ! if (crossDown.getValue(index)) { ! return trade.exit(index); } return false; |