// validates commands and returns hard coded (canned) responses
DynamicSimulationAdapter simulationAdapter = new DynamicSimulationAdapter(false);
String password = "P455w0rd";
String eMail = "someone@somewhere.com";
Portfolio portfolio = new SimplePortfolioFactory().createPortfolio(new BigDecimal("10000"));
BigDecimal commission = new BigDecimal("10.00");
Integer systemId = simulationAdapter.createSystem("first system",password,portfolio,commission);
simulationAdapter.subscribe(eMail,systemId,password);
C2ServiceFactory factory = new C2ServiceFactory(simulationAdapter);
C2EntryService sentryService = factory.signalEntryService(password, systemId, eMail);
validateStartingBalances(systemId, sentryService);
BigDecimal stopLoss = new BigDecimal("20.50");
BigDecimal profitTarget = new BigDecimal("120.50");
assertEquals(0, portfolio.position("msft").quantity().intValue());
Response openResponse = sentryService.stockSignal(ActionForStock.BuyToOpen)
.marketOrder().quantity(10).symbol("msft")
.stopLoss(stopLoss).profitTarget(BasePrice.Absolute,profitTarget,noOCA)
.duration(timeInForce).send();
final AtomicInteger signalId = new AtomicInteger(0);
final AtomicInteger profitTargetSignalId = new AtomicInteger(0);
final AtomicInteger stopLossSignalId = new AtomicInteger(0);
openResponse.visitC2Elements(new C2ElementVisitor() {
@Override
public void visit(C2Element element, String data) {
switch(element) {
case ElementSignalId:
signalId.set(Integer.valueOf(data));
break;
case ElementProfitTaretSignalId:
profitTargetSignalId.set(Integer.valueOf(data));
break;
case ElementStopLossSignalId:
stopLossSignalId.set(Integer.valueOf(data));
break;
default:
//nothing
}
}
}, C2Element.ElementSignalId, C2Element.ElementStopLossSignalId, C2Element.ElementProfitTaretSignalId );
sentryService.awaitPending();
assertTrue(signalId.intValue()>=0);
assertTrue(profitTargetSignalId.intValue()>0);
assertTrue(stopLossSignalId.intValue()>0);
long timeStep = 60000l*60l*24l;
long openTime = 0l;
long closeTime = openTime+timeStep;
BigDecimal closePrice = new BigDecimal("160.86");
BigDecimal lowPrice = new BigDecimal("80");
BigDecimal highPrice = new BigDecimal("100");
DynamicSimulationMockDataProvider dataProvider = new DynamicSimulationMockDataProvider(
openTime,lowPrice,highPrice,lowPrice,highPrice,closeTime);
simulationAdapter.tick(dataProvider,sentryService);
assertEquals(10, portfolio.position("msft").quantity().intValue());
//confirm that a price below target will not trigger
dataProvider = dataProvider.incTime(timeStep,new BigDecimal("119"));
simulationAdapter.tick(dataProvider,sentryService);
assertEquals(10, portfolio.position("msft").quantity().intValue());
final Set<Integer> pendingSignalIdSet = new HashSet<Integer>();
sentryService.sendAllSignalsRequest().visitC2Elements(new C2ElementVisitor() {
@Override
public void visit(C2Element element, String data) {