Demo_MACrossover PDF Print E-mail
Written by David Durant   
Friday, 01 June 2007

-- This demo enters long whenever the medium MA is above the slow MA, and enters short when
-- the medium MA is below the slow, and the fast MA is moving in the proper direction.
-- Generally not a profitable system.

function SystemDemo_MACrossover(securityData)
    RetrieveStandardVars(securityData)  -- Do some common setup.
    TrailingStopsInit(securityData)     -- Set up our TrailingStops function.

    -- Get all our stored UserVariables from the database.
    fastMovPeriodUV   = UserVar.Create(securityData, "FastMovPeriod", 10, 6, 20, 2, 0, true, true, 6, 20)
    mediumMovPeriodUV = UserVar.Create(securityData, "MediumMovPeriod", 5, 5, 50, 5, 0, true, true, 5, 50)
    slowMovPeriodUV   = UserVar.Create(securityData, "SlowMovPeriod", 5, 5, 50, 5, 0, true, true, 5, 50)
    crossoverMinUV    = UserVar.Create(securityData, "CrossoverMin", 0.0005, 0.0001, 0.0010, 0.0001, 4, true, true, 0.0001, 0.0010)

    -- Get the values of our UserVars
    fastMovPeriod   = UserVar.GetValue(securityData, fastMovPeriodUV)
    mediumMovPeriod = UserVar.GetValue(securityData, mediumMovPeriodUV) + fastMovPeriod
    slowMovPeriod   = UserVar.GetValue(securityData, slowMovPeriodUV) + mediumMovPeriod
    crossoverMin    = UserVar.GetValue(securityData, crossoverMinUV)


    -- Create out DataArrays (holders for the Indicators)
    enterPriceDA = DataArray.Create(securityData, "EnterPrice", true, false)
    exitPriceDA  = DataArray.Create(securityData, "ExitPrice", true, false)
    fastMovDA    = DataArray.Create(securityData, "FastMov", true, false)
    mediumMovDA  = DataArray.Create(securityData, "MediumMov", true, false)
    slowMovDA    = DataArray.Create(securityData, "SlowMov", true, false)
    crossoverDA  = DataArray.Create(securityData, "Crossover", true, true)

    -- Set the colors of the various Indicators.
    DataArray.SetColor(securityData, enterPriceDA, "Green")
    DataArray.SetColor(securityData, exitPriceDA, "Red")
    DataArray.SetColor(securityData, fastMovDA, "Red")
    DataArray.SetColor(securityData, mediumMovDA, "LightBlue")
    DataArray.SetColor(securityData, slowMovDA, "Blue")
    DataArray.SetColor(securityData, crossoverDA, "Blue")

    -- Calculate the Moving Averages (MA).
    -- The MAType parameters may be changed to any other MA type available (see Utilities.lua)
    ExecuteIndicator(securityData, "MA", fastMovDA, openDA, fastMovPeriod, MAType_EMA)
    ExecuteIndicator(securityData, "MA", mediumMovDA, openDA, mediumMovPeriod, MAType_EMA)
    firstValidMA, lastValidMA = ExecuteIndicator(securityData, "MA", slowMovDA, openDA, slowMovPeriod, MAType_EMA)

    -- Adjust our start and end indices according to the slowest MA.
    -- firstValid and lastValid are set in the function RetrieveStandardVars.
    startIndex = math.max(firstValid, firstValidMA)
    endIndex   = math.min(lastValid, lastValidMA)

    currentSignal = SIGNAL_NONE;
    enterPrice = 0
    exitPrice = 0

    -- Process each price bar one at a time.
    -- Start at (startIndex + 1) because we look back in time by one bar (fastMovValue1...)
    for dataIndex = startIndex + 1, endIndex do
        -- open0 is the current bar's open price
        open0 = DataArray.GetValue(securityData, openDA, dataIndex)
        -- fastMovValue0 is the current bar's fast MA
        fastMovValue0 = DataArray.GetValue(securityData, fastMovDA, dataIndex)
        -- fastMovValue1 is the previous bar's fast MA
        fastMovValue1 = DataArray.GetValue(securityData, fastMovDA, dataIndex - 1)
        mediumMovValue0 = DataArray.GetValue(securityData, mediumMovDA, dataIndex)
        mediumMovValue1 = DataArray.GetValue(securityData, mediumMovDA, dataIndex - 1)
        slowMovValue0 = DataArray.GetValue(securityData, slowMovDA, dataIndex)
        slowMovValue1 = DataArray.GetValue(securityData, slowMovDA, dataIndex - 1)

        -- crossOver0 is the distance between this bar's medium MA and slow MA.
        crossOver0 = mediumMovValue0 - slowMovValue0
        crossOver1 = mediumMovValue1 - slowMovValue1

        -- Set the value of our CrossOver Indicator to crossOver0 for this price bar.
        DataArray.SetValue(securityData, crossoverDA, dataIndex, crossOver0)

        -- If we're not already in a long position, test if we should enter one.
        if (currentSignal ~= SIGNAL_BUY) and
           (crossOver0 > crossoverMin) and
           (crossOver0 > crossOver1) and
           (fastMovValue0 > fastMovValue1) and
           (mediumMovValue0 > mediumMovValue1) then

            enterPrice = open0      -- Enter at this bar's open price
            -- Create a long entry.  Handled by the TrailingStops function.
            DataArray.SetValue(securityData, buySignalsDA, dataIndex, -open0)
        end

        -- If we're not already in a short position, test if we should enter one.
        if (currentSignal ~= SIGNAL_SELL) and
           (crossOver0 < -crossoverMin) and
           (crossOver0 < crossOver1) and
           (fastMovValue0 < fastMovValue1) and
           (mediumMovValue0 < mediumMovValue1) then

            enterPrice = open0      -- Enter at this bar's open price
            -- Create a short entry.  Handled by the TrailingStops function.
            DataArray.SetValue(securityData, sellSignalsDA, dataIndex, -open0)
        end

        -- The function TrailingStops will set the variable currentSignal to the proper value.
        TrailingStops(securityData, dataIndex)

        -- If TrailingStops caused an exit, then exit0 or stop0 will be non-zero.
        -- If exit0 ~= 0, we exited with a profit.
        -- If stop0 ~= 0, we exited with a loss.
        exit0 = DataArray.GetValue(securityData, exitSignalsDA, dataIndex)
        stop0 = DataArray.GetValue(securityData, stopSignalsDA, dataIndex)

        -- If we exited on this price bar, save the exit price for display to the user.
        if exit0 ~= 0 then
            exitPrice = exit0
        elseif stop0 ~= 0 then
            exitPrice = stop0
        end

        -- Save the enterPrice and exitPrice to a DataArray (Indicator) for display to the user.
        DataArray.SetValue(securityData, enterPriceDA, dataIndex, enterPrice)
        DataArray.SetValue(securityData, exitPriceDA, dataIndex, exitPrice)

    end -- for dataIndex = startIndex + 1, endIndex do
end     -- function

Last Updated ( Friday, 08 June 2007 )