Child pages
  • Creating Custom Strategies

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
firstline1
titleMy Strategy Sends Orders
linenumberstrue
collapsetrue
package com.mycompany.strategy;

import java.math.BigDecimal;

import org.marketcetera.event.TradeEvent;
import org.marketcetera.marketdata.Content;
import org.marketcetera.marketdata.MarketDataRequestBuilder;
import org.marketcetera.marketdata.bogus.BogusFeedModuleFactory;
import org.marketcetera.strategy.java.Strategy;
import org.marketcetera.trade.Factory;
import org.marketcetera.trade.OrderSingle;
import org.marketcetera.trade.OrderType;
import org.marketcetera.trade.Side;
import org.marketcetera.trade.TimeInForce;
import org.marketcetera.util.log.SLF4JLoggerProxy;

/* $License$ */

/**
 * My sample strategy.
 *
 * @author <a href="mailto:colin@marketcetera.com">Colin DuPlantis</a>
 * @version $Id$
 * @since $Release$
 */
public class MyStrategy
        extends Strategy
{
    /* (non-Javadoc)
     * @see org.marketcetera.strategy.java.Strategy#onStart()
     */
    @Override
    public void onStart()
    {
        info("Starting");
        requestMarketData(MarketDataRequestBuilder.newRequest()
                          .withSymbols("AAPL")
                          .withProvider(BogusFeedModuleFactory.IDENTIFIER)
                          .withContent(Content.LATEST_TICK).create());
    }
    /* (non-Javadoc)
     * @see org.marketcetera.strategy.java.Strategy#onTrade(org.marketcetera.event.TradeEvent)
     */
    @Override
    public void onTrade(TradeEvent inTradeEvent)
    {
        info("Received " + inTradeEvent);
        // place an order just under the most recent trade
        OrderSingle newOrder = Factory.getInstance().createOrderSingle();
        newOrder.setInstrument(inTradeEvent.getInstrument());
        newOrder.setOrderType(OrderType.Limit);
        newOrder.setPrice(inTradeEvent.getPrice().subtract(ONE_PENNY));
        newOrder.setQuantity(new BigDecimal(100));
        newOrder.setSide(Side.Buy);
        newOrder.setTimeInForce(TimeInForce.ImmediateOrCancel);
        SLF4JLoggerProxy.info(this,
                              "Created order {}",
                              newOrder);
        send(newOrder);
    }
    /**
     * constant value used to represent 0.01
     */
    private static final BigDecimal ONE_PENNY = new BigDecimal("0.01");
}

...