package almostMonteCarlo;
import java.util.TreeMap;
import util.myAverager;
/**
*
* @author joe mcverry
*
* notes: myaverager is a simple averaging routine where doubles are
* added and get returns the average, weighting is possible.
*
*/
public class RecursionFunction {
enum State {
Cash, Stocks
};
enum CashAction {
HoldCash, BuyStocks
};
enum StockAction {
HoldStocks, GoToCash
};
TreeMap theMap = new TreeMap<>();
public TreeMap getMap() {
return theMap;
}
public double searchAndTest(State inState, double cash$, int shareCount,
int position, int maxPosition, AttributeValueGetter attrGetter,
PriceGetter priceGetter, int maxDepth) {
double myValue = 0;
if (inState == State.Cash) {
myValue = cash$;
if (position >= maxPosition | maxDepth < 0)
return myValue;
double retValue1 = searchAndTest(State.Cash, cash$, shareCount,
position + 1, maxPosition, attrGetter, priceGetter,
maxDepth - 1);
String key = inState.name() + "_"
+ attrGetter.getAttributeValue(position) + "_"
+ CashAction.HoldCash.name();
myAverager mavg = theMap.get(key);
if (mavg == null) {
// System.out.println(key);
mavg = new myAverager();
theMap.put(key, mavg);
}
mavg.add(retValue1);
Double Shares = cash$ / priceGetter.getPrice(position);
shareCount = Shares.intValue();
cash$ -= priceGetter.getPrice(position) * shareCount;
double retValue2 = searchAndTest(State.Stocks, cash$, shareCount,
position + 1, maxPosition, attrGetter, priceGetter,
maxDepth - 1);
key = inState.name() + "_" + attrGetter.getAttributeValue(position)
+ "_" + CashAction.BuyStocks.name();
mavg = theMap.get(key);
if (mavg == null) {
// System.out.println(key);
mavg = new myAverager();
theMap.put(key, mavg);
}
mavg.add(retValue2);
myValue = (retValue1 + retValue2) / 2;
} else {
myValue = cash$ + (shareCount * priceGetter.getPrice(position));
if (position >= maxPosition | maxDepth < 1)
return myValue;
double retValue1 = searchAndTest(State.Stocks, cash$, shareCount,
position + 1, maxPosition, attrGetter, priceGetter,
maxDepth - 1);
String key = inState.name() + "_"
+ attrGetter.getAttributeValue(position) + "_"
+ StockAction.HoldStocks.name();
myAverager mavg = theMap.get(key);
if (mavg == null) {
mavg = new myAverager();
/// System.out.println(key);
theMap.put(key, mavg);
}
mavg.add(retValue1);
cash$ += priceGetter.getPrice(position) * shareCount;
shareCount = 0;
double retValue2 = searchAndTest(State.Cash, cash$, shareCount,
position + 1, maxPosition, attrGetter, priceGetter,
maxDepth - 1);
key = inState.name() + "_" + attrGetter.getAttributeValue(position)
+ "_" + StockAction.GoToCash.name();
mavg = theMap.get(key);
if (mavg == null) {
mavg = new myAverager();
// System.out.println(key);
theMap.put(key, mavg);
}
mavg.add(retValue2);
myValue = (retValue1 + retValue2) / 2;
}
return myValue;
}
}
import java.util.TreeMap;
import util.myAverager;
/**
*
* @author joe mcverry
*
* notes: myaverager is a simple averaging routine where doubles are
* added and get returns the average, weighting is possible.
*
*/
public class RecursionFunction {
enum State {
Cash, Stocks
};
enum CashAction {
HoldCash, BuyStocks
};
enum StockAction {
HoldStocks, GoToCash
};
TreeMap
public TreeMap
return theMap;
}
public double searchAndTest(State inState, double cash$, int shareCount,
int position, int maxPosition, AttributeValueGetter attrGetter,
PriceGetter priceGetter, int maxDepth) {
double myValue = 0;
if (inState == State.Cash) {
myValue = cash$;
if (position >= maxPosition | maxDepth < 0)
return myValue;
double retValue1 = searchAndTest(State.Cash, cash$, shareCount,
position + 1, maxPosition, attrGetter, priceGetter,
maxDepth - 1);
String key = inState.name() + "_"
+ attrGetter.getAttributeValue(position) + "_"
+ CashAction.HoldCash.name();
myAverager mavg = theMap.get(key);
if (mavg == null) {
// System.out.println(key);
mavg = new myAverager();
theMap.put(key, mavg);
}
mavg.add(retValue1);
Double Shares = cash$ / priceGetter.getPrice(position);
shareCount = Shares.intValue();
cash$ -= priceGetter.getPrice(position) * shareCount;
double retValue2 = searchAndTest(State.Stocks, cash$, shareCount,
position + 1, maxPosition, attrGetter, priceGetter,
maxDepth - 1);
key = inState.name() + "_" + attrGetter.getAttributeValue(position)
+ "_" + CashAction.BuyStocks.name();
mavg = theMap.get(key);
if (mavg == null) {
// System.out.println(key);
mavg = new myAverager();
theMap.put(key, mavg);
}
mavg.add(retValue2);
myValue = (retValue1 + retValue2) / 2;
} else {
myValue = cash$ + (shareCount * priceGetter.getPrice(position));
if (position >= maxPosition | maxDepth < 1)
return myValue;
double retValue1 = searchAndTest(State.Stocks, cash$, shareCount,
position + 1, maxPosition, attrGetter, priceGetter,
maxDepth - 1);
String key = inState.name() + "_"
+ attrGetter.getAttributeValue(position) + "_"
+ StockAction.HoldStocks.name();
myAverager mavg = theMap.get(key);
if (mavg == null) {
mavg = new myAverager();
/// System.out.println(key);
theMap.put(key, mavg);
}
mavg.add(retValue1);
cash$ += priceGetter.getPrice(position) * shareCount;
shareCount = 0;
double retValue2 = searchAndTest(State.Cash, cash$, shareCount,
position + 1, maxPosition, attrGetter, priceGetter,
maxDepth - 1);
key = inState.name() + "_" + attrGetter.getAttributeValue(position)
+ "_" + StockAction.GoToCash.name();
mavg = theMap.get(key);
if (mavg == null) {
mavg = new myAverager();
// System.out.println(key);
theMap.put(key, mavg);
}
mavg.add(retValue2);
myValue = (retValue1 + retValue2) / 2;
}
return myValue;
}
}
No comments:
Post a Comment