예제 #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))
                {
                    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_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_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 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_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_RSI, m_RS, m_Target, m_Stop);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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_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_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_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 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_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_FI, m_F, m_Target, m_Stop);
        }