Exemple #1
0
        private void OnInstrumentUpdate(Tick m_Tick)
        {
            m_TickList.Add(m_Tick);

            m_RSI = 50;
            m_RS = 0;

            // Begin calculation
            if (m_Ticks > 0 && m_TickList.Count > m_Ticks)
            {
                // Calculate the RS and RSI.
                m_UpTotal=0;
                m_DownTotal=0;
                for (int i = m_TickList.Count - m_Ticks; i < m_TickList.Count - 1; i++)
                {
                    m_Diff = m_TickList[i].Price - m_TickList[i-1].Price;
                    if (m_Diff > 0)
                        m_UpTotal += m_Diff;
                    else if (m_Diff < 0)
                        m_DownTotal -= m_Diff;
                }
                m_RS = m_UpTotal / m_DownTotal;
                m_RSI = 100 - 100 / (1 + m_RS);
                Debug.WriteLine(m_RSI);

                //// Set the Value State.
                //if (m_RSI < 40)
                //    m_State = Value_State.LOW;
                //else if (m_RSI < 60)
                //    m_State = Value_State.MID;
                //else
                //    m_State = Value_State.HIGH;
            }

            // 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_RSI >= 60)
                        m_State = Value_State.HIGH;
                    else if (m_RSI >= 40)
                        m_State = Value_State.MID;
                    else
                        m_State = Value_State.LOW;
                    m_Start = false;
                }

                // Has there been oversold?
                if (m_RSI < 40 && m_State != Value_State.LOW)
                {
                    // Change state.
                    m_State = Value_State.LOW;

                    // 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?
                if (m_RSI >= 60 && m_State != Value_State.HIGH)
                {
                    // Change state.
                    m_State = Value_State.HIGH;

                    // 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_RSI, m_RS, m_Target, m_Stop);
        }
        private void OnInstrumentUpdate(Tick m_Tick)
        {
            m_TickList.Add(m_Tick);

            m_MFI = 50;
            m_MF  = 0;

            // Begin calculation
            if (m_Ticks > 0 && m_TickList.Count > m_Ticks)
            {
                // Calculate the RS and RSI.
                m_UpTotal   = 0;
                m_DownTotal = 0;
                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;
                    if (m_WeightedDiff > 0)
                    {
                        m_UpTotal += m_WeightedDiff;
                    }
                    else if (m_WeightedDiff < 0)
                    {
                        m_DownTotal -= m_WeightedDiff;
                    }
                }
                m_MF  = m_UpTotal / m_DownTotal;
                m_MFI = 100 - 100 / (1 + m_MF);
                Debug.WriteLine(m_MFI);

                //// Set the Value State.
                //if (m_MFI < 40)
                //    m_State = Value_State.LOW;
                //else if (m_MFI < 60)
                //    m_State = Value_State.MID;
                //else
                //    m_State = Value_State.HIGH;
            }

            // 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_MFI >= 60)
                    {
                        m_State = Value_State.HIGH;
                    }
                    else if (m_MFI >= 40)
                    {
                        m_State = Value_State.MID;
                    }
                    else
                    {
                        m_State = Value_State.LOW;
                    }
                    m_Start = false;
                }

                // Has there been oversold?
                if (m_MFI < 40 && m_State != Value_State.LOW)
                {
                    // Change state.
                    m_State = Value_State.LOW;

                    // 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?
                if (m_MFI >= 60 && m_State != Value_State.HIGH)
                {
                    // Change state.
                    m_State = Value_State.HIGH;

                    // 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_MFI, m_MF, m_Target, m_Stop);
        }
        private void OnInstrumentUpdate(Tick m_Tick)
        {
            m_TickList.Add(m_Tick);

            m_Max = m_Tick.Price;
            m_Min = m_Tick.Price;

            // Begin calculation
            if (m_Ticks > 0 && m_TickList.Count > m_Ticks)
            {
                // Calculate the Force and Force Index.
                m_Max = 0;
                m_Min = 999999999;
                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);
                }
                Debug.WriteLine(m_Max);
            }

            // 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_Tick.Price > m_Max)
                    {
                        m_State = Value_State.HIGH;
                    }
                    else if (m_Tick.Price >= m_Min)
                    {
                        m_State = Value_State.MID;
                    }
                    else
                    {
                        m_State = Value_State.LOW;
                    }
                    m_Start = false;
                }

                // Has there been a crossover up?  Buy Signal
                if (m_Tick.Price > m_Max && m_State != Value_State.HIGH)
                {
                    // Change state.
                    m_State = Value_State.HIGH;

                    // 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_Tick.Price < m_Min && m_State != Value_State.LOW)
                {
                    // Change state.
                    m_State = Value_State.LOW;

                    // 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_Min, m_Max, m_Target, m_Stop);
        }