private void OnInstrumentUpdate(Tick m_Tick) { m_TickList.Add(m_Tick); m_K = 0; m_D = 0; // Begin calculating if (m_Ticks > 0 && m_TickList.Count > m_Ticks) { // Calculate the K and D values. m_Max = 0; m_Min = 1000000000; for (int i = m_TickList.Count - m_Ticks; i < m_TickList.Count - 1; i++) { m_Max = Math.Max(m_Max, m_TickList[i].Price); m_Min = Math.Min(m_Min, m_TickList[i].Price); } m_RSV.Add((m_TickList.Last().Price - m_Min) / (m_Max - m_Min) * 100); //Debug.WriteLine(m_RSV.Last()); if (m_RSV.Count >= 3) { m_D = (m_RSV[m_RSV.Count - 1] + m_RSV[m_RSV.Count - 2] + m_RSV[m_RSV.Count - 3]) / 3; } m_K = m_RSV.Last(); //Debug.WriteLine(m_K); //Debug.WriteLine(m_D); //// Set the Cross State. //if (m_K > m_D) // m_State = Cross_State.ABOVE; //else // m_State = Cross_State.BELOW; } // START/STOP Switch if (m_Go) { // If we already have a position on, and have either met out target or stop price, get out. if (m_Position > 0 && (m_Tick.Price > m_Target || m_Tick.Price < m_Stop)) { m_Instrument.EnterMarketOrder("S", m_Qty.ToString()); } if (m_Position < 0 && (m_Tick.Price < m_Target || m_Tick.Price > m_Stop)) { m_Instrument.EnterMarketOrder("B", m_Qty.ToString()); } // First time only and on reset, set initial state. if (m_Start) { if (m_K > m_D) { m_State = Cross_State.ABOVE; } else { m_State = Cross_State.BELOW; } m_Start = false; } // Has there been a crossover up? if (m_K > m_D && m_State == Cross_State.BELOW) { // Change state. m_State = Cross_State.ABOVE; // If we are already short, first get flat. if (m_Position < 0) { m_Instrument.EnterMarketOrder("B", m_Qty.ToString()); } // Go long. m_Instrument.EnterMarketOrder("B", m_Qty.ToString()); // Set target price and stop loss price. m_Target = m_Tick.Price + m_TargetTicks * m_Instrument.get_TickSize(); m_Stop = m_Tick.Price - m_StopTicks * m_Instrument.get_TickSize(); } // Has there been a crossover down? if (m_K < m_D && m_State == Cross_State.ABOVE) { // Change state. m_State = Cross_State.BELOW; // If we are already long, first get flat. if (m_Position > 0) { m_Instrument.EnterMarketOrder("S", m_Qty.ToString()); } // Go short. m_Instrument.EnterMarketOrder("S", m_Qty.ToString()); // Set target price and stop loss price. m_Target = m_Tick.Price - m_TargetTicks * m_Instrument.get_TickSize(); m_Stop = m_Tick.Price + m_StopTicks * m_Instrument.get_TickSize(); } } // Send the data to the GUI. OnSystemUpdate(m_Tick.Price, m_Tick.Qty, m_D, m_K, m_Target, m_Stop); }
private void OnInstrumentUpdate(Tick m_Tick) { m_TickList.Add(m_Tick); m_K = 0; m_D = 0; // Begin calculating if (m_Ticks > 0 && m_TickList.Count > m_Ticks) { // Calculate the K and D values. m_Max = 0; m_Min = 1000000000; for (int i = m_TickList.Count - m_Ticks; i < m_TickList.Count - 1; i++) { m_Max = Math.Max(m_Max, m_TickList[i].Price); m_Min = Math.Min(m_Min, m_TickList[i].Price); } m_RSV.Add((m_TickList.Last().Price - m_Min) / (m_Max - m_Min) * 100); //Debug.WriteLine(m_RSV.Last()); if (m_RSV.Count >= 3) m_D = (m_RSV[m_RSV.Count - 1] + m_RSV[m_RSV.Count - 2] + m_RSV[m_RSV.Count - 3]) / 3; m_K = m_RSV.Last(); //Debug.WriteLine(m_K); //Debug.WriteLine(m_D); //// Set the Cross State. //if (m_K > m_D) // m_State = Cross_State.ABOVE; //else // m_State = Cross_State.BELOW; } // START/STOP Switch if (m_Go) { // If we already have a position on, and have either met out 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"); } // First time only and on reset, set initial state. if (m_Start) { if (m_K > m_D) m_State = Cross_State.ABOVE; else m_State = Cross_State.BELOW; m_Start = false; } // Has there been a crossover up? if (m_K > m_D && m_State == Cross_State.BELOW) { // Change state. m_State = Cross_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_K < m_D && m_State == Cross_State.ABOVE) { // Change state. m_State = Cross_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_D, m_K, m_Target, m_Stop); }
private void OnInstrumentUpdate(Tick m_Tick) { m_TickList.Add(m_Tick); m_FI = 0; m_F = 0; // Begin calculation if (m_Ticks > 0 && m_TickList.Count > m_Ticks) { // Calculate the Force and Force Index. for (int i = m_TickList.Count - m_Ticks; i < m_TickList.Count - 1; i++) { m_WeightedDiff = (m_TickList[i].Price - m_TickList[i - 1].Price) * m_TickList[i].Qty; } m_F += m_WeightedDiff; m_FI = m_F / m_Ticks; Debug.WriteLine(m_FI); } // START/STOP Switch if (m_Go) { // If we already have a position on, and have either met out target or stop price, get out. if (m_Position > 0 && (m_Tick.Price > m_Target || m_Tick.Price < m_Stop)) { 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)) { m_Bool = m_Instrument.EnterOrder("B", m_Qty, "TARGET/STOP OUT"); } // First time only and on reset, set initial state. if (m_Start) { if (m_FI > 0) { m_State = Cross_State.ABOVE; } else { m_State = Cross_State.BELOW; } m_Start = false; } // Has there been a crossover up? Buy Signal if (m_FI > 0 && m_State == Cross_State.BELOW) { // Change state. m_State = Cross_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 overbought? Sell Signal if (m_FI < 0 && m_State == Cross_State.ABOVE) { // Change state. m_State = Cross_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_FI, m_F, m_Target, m_Stop); }