Package com.collective2.signalEntry.adapter

Examples of com.collective2.signalEntry.adapter.IterableXMLEventReader


    }

    @Test
    public void quantityPercentTest() {
        // 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);

        assertEquals(0, portfolio.position("msft").quantity().intValue());

        Response openResponse = sentryService.stockSignal(ActionForStock.BuyToOpen)
                .marketOrder().accountPercent(new BigDecimal("0.08143")).symbol("msft")
                .duration(Duration.GoodTilCancel).send();

        Integer signalId = openResponse.getInteger(C2Element.ElementSignalId);

        assertEquals(0,signalId.intValue());

        long timeStep = 60000l*60l*24l;
        long time = 100000l;
        BigDecimal fixedPrice = new BigDecimal("80.43");

        DynamicSimulationMockDataProvider dataProvider = new DynamicSimulationMockDataProvider(time,fixedPrice,fixedPrice,fixedPrice,fixedPrice,time+timeStep);

        simulationAdapter.tick(dataProvider,sentryService);

        Number buyPower = sentryService.buyPower(); // 10000 - ((10 * 80.43)+10) = 10000-814.30
        assertEquals(9185.7d,buyPower.doubleValue(),DELTA);

        Number systemEquity = sentryService.systemEquity(); //10 * 80.43 = 804.30
View Full Code Here


        String mySystemName = "testSystem";
        BigDecimal commission = new BigDecimal("10");

        // simplest possible back test simulator setup
        try {
            DynamicSimulationAdapter adapter = new DynamicSimulationAdapter(marginAccount);

            //register our system and get our id from the simulator
            Integer systemId = adapter.createSystem(startingBuyPower, mySystemName, password, commission);

            C2ServiceFactory factory = new C2ServiceFactory(adapter);
            C2EntryService entryService = factory.signalEntryService(password,systemId);
            assertNotNull(entryService);
        } catch (Exception e) {
View Full Code Here

                assertTrue(((String)request.get(Parameter.Symbol)).compareTo(lastValue)>0);
                //setup for next test
                lastValue = (String)request.get(Parameter.Symbol);

                try {
                    return new IterableXMLEventReader(super.transmit(request));
                } catch (XMLStreamException e) {
                    fail(e.getMessage());
                    throw new C2ServiceException(e,false);
                }
            }
View Full Code Here

    @Override
    public void visitC2Elements(C2ElementVisitor c2ElementVisitor, C2Element ... expected) {
        assert(validate(expected));
       
        IterableXMLEventReader reader = getXMLEventReader();
        try {
            Deque<C2Element> stack = new ArrayDeque<C2Element>();
            while (reader.hasNext()) {
                XMLEvent event =  reader.nextEvent();
                if (event.isEndElement()) {
                    stack.pop();
                } else if (event.isStartElement()) {
                   
                    String name = event.asStartElement().getName().getLocalPart();
                    int i =expected.length;
                    C2Element found = C2Element.None;
                    while(--i>=0) {
                        if (name.equals(expected[i].localElementName())) {
                            found = expected[i];
                            break;
                        }
                    }
                    stack.push(found);
                } else if (event.isCharacters()) {
                    if (C2Element.None != stack.peek()) {
                        c2ElementVisitor.visit(stack.peek(), event.asCharacters().getData().trim());
                    }
                }
            }
        } catch (XMLStreamException e) {
            logger.warn("visitC2Elements: \n"+request.buildURL(),e);
        } finally {
            try {
                reader.close();
            } catch (XMLStreamException e) {
                logger.warn("Unable to close xml stream", e);
            }
        }
    }
View Full Code Here

                        approvalRequestable.waitForApproval(journal.pending());
                    }

                    if (!journalInstance.isApproved()) {
                       journal.markRejected(journalInstance);
                       return new IterableXMLEventReader("<rejected>not approved</rejected>");
                    };

                    //was validated upon construction but assert it was not changed in the meantime
                    assert(request.validate());
                    //all down stream requests must see the same halting exception until its reset.
                    if (haltingException != null ) {
                        throw haltingException;
                    }

                    boolean  tryAgain = false;
                    do {
                        try {
                            //exceptions thrown here are because
                            // * the network is down and we should try later
                            // * the response was not readable - must stop all

                            //transmit to the adapter
                            IterableXMLEventReader eventReader = adapter.transmit(request);
                            synchronized (ResponseManager.this) {
                                //exceptions thrown here are because
                                // * database was unable to change flag on request to sent - must stop all
                                journal.markSent(journalInstance);
                            }
View Full Code Here


    @Test
    public void  marketBTOTest() {

        Portfolio portfolio = new SimplePortfolioFactory().createPortfolio(new BigDecimal("1000.00"));

        int id = 42;
        long time = stop;
        Instrument instrument = Instrument.Forex;
        String symbol = "GG";
        SignalAction action = SignalAction.BTO;
        Integer quantity = 10;
        QuantityComputable quantityComputable = new QuantityComputableFixed(quantity);
        long cancelAtMs = Long.MAX_VALUE;
        Duration timeInForce = Duration.GoodTilCancel;
        OrderProcessorMarket processor = new OrderProcessorMarket(time, symbol);
        Order order = new Order(null, id,instrument,symbol,action,quantityComputable,cancelAtMs,timeInForce,processor, null);

        //test only the processor and do it outside the order
        boolean processed = processor.process(dataProvider, portfolio, commission, order, action, quantityComputable, null);

        assertTrue(processed);
        assertEquals(new BigDecimal("961.00"),portfolio.cash());
        assertEquals(quantity,portfolio.position("GG").quantity());
        assertEquals(new BigDecimal("60"),portfolio.equity(dataProvider));

        //sell to close this open position.

        action = SignalAction.STC;
        order = new Order(null, id,instrument,symbol,action,quantityComputable,cancelAtMs,timeInForce,processor, null);

        //test only the processor and do it outside the order
        processed = processor.process(dataProvider, portfolio, commission, order, action, quantityComputable, null);

        assertTrue(processed);
        assertEquals(new BigDecimal("982.00"),portfolio.cash());
        assertEquals(Integer.valueOf(0),portfolio.position("GG").quantity());
        assertEquals(new BigDecimal("0"),portfolio.equity(dataProvider));

    }
View Full Code Here

    }


    private void  marketShortTest(SignalAction sellAction) {

        Portfolio portfolio = new SimplePortfolioFactory().createPortfolio(new BigDecimal("1000.00"));

        int id = 42;
        long time = stop;
        Instrument instrument = Instrument.Forex;
        String symbol = "GG";
        Integer quantity = 10;
        QuantityComputable quantityComputable = new QuantityComputableFixed(quantity);
        long cancelAtMs = Long.MAX_VALUE;
        Duration timeInForce = Duration.GoodTilCancel;
        OrderProcessorMarket processor = new OrderProcessorMarket(time, symbol);
        Order order = new Order(null, id,instrument,symbol,sellAction,quantityComputable,cancelAtMs,timeInForce,processor, null);

        //test only the processor and do it outside the order
        boolean processed = processor.process(dataProvider, portfolio, commission, order, sellAction, quantityComputable, null);

        assertTrue(processed);
        assertEquals(new BigDecimal("1021.00"),portfolio.cash());
        assertEquals(Integer.valueOf(-quantity),portfolio.position("GG").quantity());
        assertEquals(new BigDecimal("-60"),portfolio.equity(dataProvider));

        //Buy to cover this short position

        SignalAction action = SignalAction.BTC;
        order = new Order(null, id,instrument,symbol,action,quantityComputable,cancelAtMs,timeInForce,processor, null);

        processed = processor.process(dataProvider, portfolio, commission, order, action, quantityComputable, null);

        assertTrue(processed);
        assertEquals(new BigDecimal("982.00"),portfolio.cash());
        assertEquals(Integer.valueOf(0),portfolio.position("GG").quantity());
        assertEquals(new BigDecimal("0"),portfolio.equity(dataProvider));

    }
View Full Code Here


    @Test
    public void  conditionalOrderTest() {

        Portfolio portfolio = new SimplePortfolioFactory().createPortfolio(new BigDecimal("1000.00"));

        int id = 42;
        long time = stop;
        Instrument instrument = Instrument.Forex;
        String symbol = "GG";
        SignalAction action = SignalAction.BTO;
        Integer quantity = 10;
        QuantityComputable quantityComputable = new QuantityComputableFixed(quantity);
        long cancelAtMs = Long.MAX_VALUE;
        Duration timeInForce = Duration.GoodTilCancel;
        OrderProcessorMarket processor = new OrderProcessorMarket(time, symbol);
        Order buyOrder = new Order(null, id,instrument,symbol,action,quantityComputable,cancelAtMs,timeInForce,processor, null);

        //final quantity is not known until this order is processed
        //this however has the quantity because QuantityComputableFixed is used above
        assertEquals(Integer.valueOf(10), Integer.valueOf(buyOrder.quantity()));

        action = SignalAction.STC;
        quantityComputable = new QuantityComputableEntry(buyOrder);
        Order sellOrder = new Order(null, id,instrument,symbol,action,quantityComputable,cancelAtMs,timeInForce,processor, buyOrder);

        //final quantity is not known until this order is processed
        //this however has the quantity because QuantityComputableFixed is used above
        assertEquals(Integer.valueOf(10), Integer.valueOf(sellOrder.quantity()));

        assertTrue(buyOrder.process(dataProvider,portfolio,commission,null));

        assertEquals(quantity, Integer.valueOf(buyOrder.quantity()));
        assertEquals(quantity, Integer.valueOf(sellOrder.quantity()));

        assertTrue(sellOrder.process(dataProvider, portfolio, commission,null));

        assertEquals(new BigDecimal("982.00"),portfolio.cash());
        assertEquals(Integer.valueOf(0),portfolio.position("GG").quantity());
        assertEquals(new BigDecimal("0"),portfolio.equity(dataProvider));

    }
View Full Code Here

    }

    private void limitBTOTest(RelativeNumber buyBelow, RelativeNumber sellAbove, BigDecimal expectedBuy, BigDecimal expectedSell) {

        BigDecimal startingCash = new BigDecimal("1000.00");
        Portfolio portfolio = new SimplePortfolioFactory().createPortfolio(startingCash);

        int id = 42;
        long time = stop;
        Instrument instrument = Instrument.Forex;
        String symbol = "GG";
        SignalAction action = SignalAction.BTO;
        Integer quantity = 10;
        QuantityComputable quantityComputable = new QuantityComputableFixed(quantity);
        long cancelAtMs = Long.MAX_VALUE;
        Duration timeInForce = Duration.GoodTilCancel;


        OrderProcessorLimit processor = new OrderProcessorLimit(time, symbol, buyBelow);
        Order order = new Order(null,id,instrument,symbol,action,quantityComputable,cancelAtMs,timeInForce,processor,null);

        //test only the processor and do it outside the order
        boolean processed = processor.process(dataProvider, portfolio, commission, order, action, quantityComputable,null);

        assertTrue(processed);
        BigDecimal expectedCash = startingCash.subtract(expectedBuy.multiply(new BigDecimal(quantity)).add(commission));

        assertEquals(expectedBuy, processor.transactionPrice());
        assertEquals(expectedCash, portfolio.cash());
        assertEquals(quantity,portfolio.position("GG").quantity());
        assertEquals(new BigDecimal("60"),portfolio.equity(dataProvider));

        //sell to close this open position.

        action = SignalAction.STC;

        OrderProcessorLimit sellProcessor = new OrderProcessorLimit(time, symbol, sellAbove);
        order = new Order(null, id,instrument,symbol,action,quantityComputable,cancelAtMs,timeInForce,sellProcessor,null);

        //test only the processor and do it outside the order
        processed = sellProcessor.process(dataProvider, portfolio, commission, order, action, quantityComputable,null);

        assertTrue(processed);
        expectedCash = expectedCash.add(expectedSell.multiply(new BigDecimal(quantity))).subtract(commission);

        assertEquals(expectedSell, sellProcessor.transactionPrice());
        assertEquals(expectedCash,portfolio.cash());
        assertEquals(Integer.valueOf(0),portfolio.position("GG").quantity());
        assertEquals(new BigDecimal("0"),portfolio.equity(dataProvider));

    }
View Full Code Here


    private void  limitShortTest(SignalAction sellAction, RelativeNumber sellAbove, RelativeNumber buyBelow, BigDecimal expectedSell, BigDecimal expectedBuy) {

        BigDecimal startingCash = new BigDecimal("1000.00");
        Portfolio portfolio = new SimplePortfolioFactory().createPortfolio(startingCash);

        int id = 42;
        long time = stop;
        Instrument instrument = Instrument.Forex;
        String symbol = "GG";
        Integer quantity = 10;
        QuantityComputable quantityComputable = new QuantityComputableFixed(quantity);
        long cancelAtMs = Long.MAX_VALUE;
        Duration timeInForce = Duration.GoodTilCancel;

        OrderProcessorLimit processor = new OrderProcessorLimit(time,symbol,sellAbove);
        Order order = new Order(null, id,instrument,symbol,sellAction,quantityComputable,cancelAtMs,timeInForce,processor,null);

        //test only the processor and do it outside the order
        boolean processed = processor.process(dataProvider, portfolio, commission, order, sellAction, quantityComputable,null);

        BigDecimal expectedCash = startingCash.add(expectedSell.multiply(new BigDecimal(quantity)).subtract(commission));

        assertTrue(processed);
        assertEquals("sell ", expectedSell, processor.transactionPrice());
        assertEquals(expectedCash,portfolio.cash());
        assertEquals(Integer.valueOf(-quantity),portfolio.position("GG").quantity());
        assertEquals(new BigDecimal("-60"),portfolio.equity(dataProvider));

        //Buy to cover this short position

        SignalAction action = SignalAction.BTC;
        OrderProcessorLimit buyProcessor = new OrderProcessorLimit(time,symbol,buyBelow);
        order = new Order(null, id,instrument,symbol,action,quantityComputable,cancelAtMs,timeInForce,buyProcessor,null);

        processed = buyProcessor.process(dataProvider, portfolio, commission, order, action, quantityComputable,null);
        expectedCash = expectedCash.subtract(expectedBuy.multiply(new BigDecimal(quantity))).subtract(commission);

        assertTrue(processed);
        assertEquals("buy ",expectedBuy, buyProcessor.transactionPrice());
        assertEquals(expectedCash,portfolio.cash());
        assertEquals(Integer.valueOf(0),portfolio.position("GG").quantity());
        assertEquals(new BigDecimal("0"),portfolio.equity(dataProvider));

    }
View Full Code Here

TOP

Related Classes of com.collective2.signalEntry.adapter.IterableXMLEventReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.