private void OnInstrumentUpdate_EventHandler(Instrument pInstr) { m_LongMA = 0; m_ShortMA = 0; if (m_Go) { // If we already have a position on, and have either met our target or stop price, get out. if (pInstr.Position > 0 && (pInstr.Price >= pInstr.Target || pInstr.Price <= pInstr.Stop)) { bool m_Bool = pInstr.EnterOrder("S", m_Qty, "TARGET/STOP OUT"); pInstr.Position -= 1; } if (pInstr.Position < 0 && (pInstr.Price <= pInstr.Target || pInstr.Price >= pInstr.Stop)) { bool m_Bool = pInstr.EnterOrder("B", m_Qty, "TARGET/STOP OUT"); pInstr.Position += 1; } if (pInstr.TickList.Count > m_LongMATicks) { //Calculate the long moving average. for (int i = pInstr.TickList.Count - m_LongMATicks; i < pInstr.TickList.Count; i++) { m_LongMA += pInstr.TickList[i]; } m_LongMA /= m_LongMATicks; //Calculate the short moving average. for (int i = pInstr.TickList.Count - m_ShortMATicks; i < pInstr.TickList.Count; i++) { m_ShortMA += pInstr.TickList[i]; } m_ShortMA /= m_ShortMATicks; // First time only and on reset, set initial state. if (m_Start) { if (m_ShortMA > m_LongMA) { m_State = MA_State.ABOVE; } else { m_State = MA_State.BELOW; } m_Start = false; } // Has there been a crossover up? if (m_ShortMA > m_LongMA && m_State == MA_State.BELOW) { // Change state. m_State = MA_State.ABOVE; // If we are already short, first get flat. if (pInstr.Position < 0) { bool m_Bool = pInstr.EnterOrder("B", m_Qty * 2, "SWITCH"); pInstr.Position += (m_Qty * 2); } else { // Go long. bool result = pInstr.EnterOrder("B", m_Qty, "OPEN"); pInstr.Position += m_Qty; } // Set target price and stop loss price. pInstr.Target = pInstr.Price + m_TargetTicks * pInstr.TickSize; pInstr.Stop = pInstr.Price - m_StopTicks * pInstr.TickSize; } // Has there been a crossover down? if (m_ShortMA < m_LongMA && m_State == MA_State.ABOVE) { // Change state. m_State = MA_State.BELOW; // If we are already long, first get flat. if (pInstr.Position > 0) { bool m_Bool = pInstr.EnterOrder("S", m_Qty * 2, "SWITCH"); pInstr.Position -= (m_Qty * 2); } else { // Go short. bool result = pInstr.EnterOrder("S", m_Qty, "OPEN"); pInstr.Position -= m_Qty; } // Set target price and stop loss price. pInstr.Target = pInstr.Price - m_TargetTicks * pInstr.TickSize; pInstr.Stop = pInstr.Price + m_StopTicks * pInstr.TickSize; } } } //Send the data to the GUI. OnSystemUpdate(pInstr.BidQty, pInstr.Bid, pInstr.Ask, pInstr.AskQty, m_LongMA, m_ShortMA, pInstr.Target, pInstr.Stop); }
//homes of the strategy private void OnInstrumentUpdate(Tick m_Tick) { //Debug::WriteLine( m_Tick.Price ); //Add the tick object to the SortedList. m_TickList.Add(m_Tick); m_LongMA = 0; m_ShortMA = 0; if (m_Go) { // If we already have a position on, and have either met our target or stop price, get out. if (m_Position > 0 && (m_Tick.Price > m_Target || m_Tick.Price < m_Stop)) { bool m_Bool = m_Instrument.EnterOrder("S", m_Qty, "TARGET/STOP OUT"); } if (m_Position < 0 && (m_Tick.Price < m_Target || m_Tick.Price > m_Stop)) { bool m_Bool = m_Instrument.EnterOrder("B", m_Qty, "TARGET/STOP OUT"); } if (m_TickList.Count > m_LongMATicks) { //Calculate the long moving average. for (int x = m_TickList.Count - m_LongMATicks; x <= m_TickList.Count - 1; x++) { m_LongMA += m_TickList[x].Price; } m_LongMA /= m_LongMATicks; //Calculate the short moving average. for (int x = m_TickList.Count - m_ShortMATicks; x <= m_TickList.Count - 1; x++) { m_ShortMA += m_TickList[x].Price; } m_ShortMA /= m_ShortMATicks; // First time only and on reset, set initial state. if (m_Start) { if (m_ShortMA > m_LongMA) { m_State = MA_State.ABOVE; } else { m_State = MA_State.BELOW; } m_Start = false; } // Has there been a crossover up? if (m_ShortMA > m_LongMA && m_State == MA_State.BELOW) { // Change state. m_State = MA_State.ABOVE; // If we are already short, first get flat. if (m_Position < 0) { m_Bool = m_Instrument.EnterOrder("B", m_Qty, "GET OUT"); } // Go long. m_Bool = m_Instrument.EnterOrder("B", m_Qty, "OPEN"); // Set target price and stop loss price. m_Target = m_Tick.Price + m_TargetTicks * m_Instrument.TickSize(); m_Stop = m_Tick.Price - m_StopTicks * m_Instrument.TickSize(); } // Has there been a crossover down? if (m_ShortMA < m_LongMA && m_State == MA_State.ABOVE) { // Change state. m_State = MA_State.BELOW; // If we are already long, first get flat. if (m_Position > 0) { m_Bool = m_Instrument.EnterOrder("S", m_Qty, "GET OUT"); } // Go short. m_Bool = m_Instrument.EnterOrder("S", m_Qty, "OPEN"); // Set target price and stop loss price. m_Target = m_Tick.Price - m_TargetTicks * m_Instrument.TickSize(); m_Stop = m_Tick.Price + m_StopTicks * m_Instrument.TickSize(); } } } //Send the data to the GUI. OnSystemUpdate(m_Tick.Price, m_Tick.Qty, m_LongMA, m_ShortMA, m_Target, m_Stop); }