private void OnFill_A(String product, String key, String BS, int qty, int price, String time) { // When fill is received in A (ES), enter market order in B (NQ). // This method will run on the RTD thread. FillPrice_A = price; if (Pos_A == Position.FLAT) { // This is an openning trade. Place trade in Instrument_B (NQ) // in the opposite direction. if (BS == "B") { Pos_A = Position.LONG; BuyOrderBook.Remove(key); bool m_Bool = _Instr_B.EnterMarketOrder("S", 2, "OPEN"); } else { Pos_A = Position.SHORT; SellOrderBook.Remove(key); bool m_Bool = _Instr_B.EnterMarketOrder("B", 2, "OPEN"); } } else { // this is a closing trade. Pos_A = Position.FLAT; } // Update form on Main thread. OnFillUpdate(product, key, BS, qty, price, time); }
private void OnInstrUpdate() { // Get latest Bid/Ask data. Bid_A = _Instr_A.Bid; Ask_A = _Instr_A.Ask; Bid_B = _Instr_B.Bid; Ask_B = _Instr_B.Ask; // Calculate the spread Bid/Ask for 1x2 and add it to the list. SpreadPrice = -1 * Bid_A + 2 * Ask_B; _PriceList.Add(SpreadPrice); // Calculate the normalized price. NormPrice = NormCalc.CalcNormalizedPrice(_PriceList); if (Go) { // Have we hit a stop or have we hit our target? If so, close positions. if (SpreadPos == Position.LONG && (SpreadPrice <= StopPrice || NormPrice > 0)) { bool m_Bool = _Instr_A.EnterMarketOrder("B", 1, "CLOSE"); m_Bool = _Instr_B.EnterMarketOrder("S", 2, "CLOSE"); } if (SpreadPos == Position.SHORT && (SpreadPrice >= StopPrice || NormPrice < 0)) { bool m_Bool = _Instr_A.EnterMarketOrder("S", 1, "CLOSE"); m_Bool = _Instr_B.EnterMarketOrder("B", 2, "CLOSE"); } // If the reason for buying or selling the spread no longer exists, // cancel the working order. i.e. if we missed the trade, cancel it. if (NormPrice < 2 && BuyOrderBook.Count > 0) { // Cancel buy order and remove from order book. _Instr_A.CancelOrder(((Order)(BuyOrderBook.GetByIndex(0))).Key); BuyOrderBook.RemoveAt(0); } if (NormPrice > -2 && SellOrderBook.Count > 0) { // Cancel sell order and remove from order book. _Instr_A.CancelOrder(((Order)(SellOrderBook.GetByIndex(0))).Key); SellOrderBook.RemoveAt(0); } // Make a decision as to whether or not to enter a trade. // Make a long trade in A if normalized price > 2 and we are // flat and not already working an order. // Enter order method calls run on the Strategy thread. if (NormPrice > 2 && Pos_A == Position.FLAT && BuyOrderBook.Count == 0) { // Try to buy 1 on the bid. Order m_Order = _Instr_A.EnterLimitOrder("B", 1, Bid_A, "OPEN"); BuyOrderBook.Add(m_Order.Key, m_Order); } // Make a short in A if normalized price < -2 and we are flat and not already working an order. if (NormPrice < -2 && Pos_A == Position.FLAT && SellOrderBook.Count == 0) { // Try to sell 1 on the ask. Order m_Order = _Instr_A.EnterLimitOrder("S", 1, Ask_A, "OPEN"); SellOrderBook.Add(m_Order.Key, m_Order); } } // Update the form on the main thread. OnPriceUpdate(); }