private XMLEventReader simpleTransmit(Request request) {
logger.trace("transmit "+request.toString());
synchronized(lock) {
SystemManager system = null;
if (request.containsKey(Parameter.SystemId)) {
system = lookupSystem(request);
if (system==null) {
return new SimulatedResponseError(ERROR,"no system","");
}
}
XMLEventReader xmlEventReader;
Command command = request.command();
switch (command) {
case Signal:
long timeToExecute = extractTimeToExecute(request);
int[] signalIdArray = system.scheduleSignal(timeToExecute,request);
int signalId = signalIdArray[0];
int stopLossSignalId = signalIdArray[1];
int profitTargetSignalId = signalIdArray[2];
return new SimulatedResponseSignal(signalId,
stopLossSignalId==SystemManager.NO_ID?null:stopLossSignalId,
profitTargetSignalId==SystemManager.NO_ID?null:profitTargetSignalId,
OK);
case GetBuyPower:
// long lastTickTime = lastTickDataProvider.endingTime();
BigDecimal buyPower = system.portfolio().cash().add(system.totalMargin());
return new SimulatedResponseGetBuyPower(OK,time,buyPower);
case Cancel:
Integer id = (Integer)request.get(Parameter.SignalId);
assert(id>=0) : "DynamicSimulator only uses positive signalIds";
system.cancelSignal(id,time);
return new SimulatedResponseCancel(OK);
case CancelAllPending:
system.cancelAllPending(time);
return new SimulatedResponseCancelAllPending(OK);
case CloseAllPositions:
system.portfolio().closeAllPositions();
return new SimulatedResponseCloseAllPositions(OK);
case FlushPendingSignals:
system.flushPendingSignals(lastTickDataProvider);
return new SimulatedResponseFlushPendingSignals(OK);
case AllSignals:
//not filled, cancelled or expired!
//only for this subscriber
String subscriberEmail = (String)request.get(Parameter.EMail);
Map<Integer,List<Integer>> allPendingSignals = new HashMap<Integer,List<Integer>>();
for(SystemManager sys:systems) {
if (sys.isSubscribed(subscriberEmail)) {
Integer sysId = sys.id();
allPendingSignals.put(sysId,sys.allPendingSignals());
}
}
return new SimulatedResponseGetAllSignals(OK, allPendingSignals);
case GetSystemEquity:
long lastTickTime = lastTickDataProvider.endingTime();
Number equity = system.portfolio().equity(lastTickDataProvider);
return new SimulatedResponseGetSystemEquity(OK,lastTickTime,equity);
case GetSystemHypothetical:
List<Map<C2Element, Object>> data = new ArrayList<Map<C2Element, Object>>();
for(SystemManager sys:systems) {
Map<C2Element, Object> map = new HashMap<C2Element, Object>();
//populate this map for this system.
map.put(C2Element.ElementSystemId, sys.id());
map.put(C2Element.ElementSystemName, sys.name());
//NOTE: very basic margin calculations where negative cash is margin, should improve some day.
BigDecimal sysCash = sys.portfolio().cash();
BigDecimal sysEquity = sys.portfolio().equity(lastTickDataProvider);
BigDecimal totalEquityAvail = sysCash.add(sysEquity);
BigDecimal marginUsed = (sysCash.compareTo(BigDecimal.ZERO)<0 ? sysCash.negate() : BigDecimal.ZERO);
map.put(C2Element.ElementTotalEquityAvail, totalEquityAvail);
map.put(C2Element.ElementCash, sysCash);
map.put(C2Element.ElementEquity, sysEquity);
map.put(C2Element.ElementMarginUsed, marginUsed);
data.add(map);
}
return new SimulatedResponseGetSystemHypothetical(data);
case PositionStatus:
String symbol = (String)request.get(Parameter.Symbol);
Integer position = system.portfolio().position(symbol).quantity();
return new SimulatedResponsePositionStatus(OK,time,symbol,position);
case RequestOCAId:
Integer ocaid = system.generateNewOCAId();
return new SimulatedResponseRequestOCAId(ocaid,OK);
case Reverse:
//the same as a signal but its done now and can not be parked
Integer reverseId = system.scheduleSignal(time, request)[0];
return new SimulatedResponseReverse(OK);
case SendSubscriberBroadcast:
String email = (String)request.get(Parameter.EMail);
String message = (String)request.get(Parameter.Message);
system.sendSubscriberBroadcast(email,message);
return new SimulatedResponseSendSubscriberBroadcast(OK);
case SetMinBuyPower:
Number minBuyPower = (Number)request.get(Parameter.BuyPower);
system.minBuyPower(minBuyPower);
return new SimulatedResponseSetMinBuyPower(OK);
case NewComment:
Integer signalIdForComment = (Integer)request.get(Parameter.SignalId);
String comment = (String)request.get(Parameter.Commentary);
String previousComment = system.newComment(signalIdForComment,comment);
return new SimulatedResponseNewComment(OK,signalIdForComment,previousComment);
case AllSystems: //lookup the systems here
throw new UnsupportedOperationException("Not implemented at this time");
case AddToOCAGroup: //lookup existing signalid and add it to this existing oca group
throw new UnsupportedOperationException("Not implemented at this time");
case SignalStatus:
Integer signalIdInput = (Integer)request.get(Parameter.SignalId);
Integer systemId = SystemManager.extractSystemId(signalIdInput);
SystemManager systemForSignal = systems.get(systemId);
String signalSubscriberEmail = (String)request.get(Parameter.EMail);
boolean isSubscribed = systemForSignal.isSubscribed(signalSubscriberEmail);
if (!isSubscribed) {
return new SimulatedResponseError(ERROR,"email not subscribed",signalSubscriberEmail);
}
String subscriberPassword = (String)request.get(Parameter.Password);
boolean isSystemPassword = systemForSignal.isPassword(signalSubscriberEmail,subscriberPassword);
if (!isSystemPassword) {
return new SimulatedResponseError(ERROR,"not system password",signalSubscriberEmail);
}
Order order = systemForSignal.lookupOrder(signalIdInput);
String systemName = systemForSignal.name();
String postedwhen = order.postedWhen();
String emailedWhen = order.eMailedWhen();
String killedWhen = order.killedWhen();
String tradedWhen = order.tradedWhen();
BigDecimal tradePrice = order.tradePrice();