Colour SAR

Here you can post and download custom indicators. PLEASE: Do not start topics unless you are posting your own indicator, they will be moved to appropriate section even if you do.

Moderator: admin

Colour SAR

Postby Nikolay.Gekht » Tue Feb 09, 2010 3:35 pm

This is a slightly modified version of the standard TS/Marketscope SAR indicator which:
- lets choose different color for up and down parts of SAR
- lets choose step and maximum values (default values are 0.02 and 0.2)

csar.PNG


Code: Select all
-- The indicator corresponds to the Parabolic indicator in MetaTrader.
-- The formula is described in the Kaufman "Trading Systems and Methods" chapter 5 "Trend Systems" (page 98-99)

-- Indicator profile initialization routine
-- Defines indicator profile properties and indicator parameters
function Init()
    indicator:name("Color SAR");
    indicator:description("Helps to define the direction of the prevailing trend and the moment to close positions opened during the reversal.");
    indicator:requiredSource(core.Bar);
    indicator:type(core.Indicator);
    indicator.parameters:addDouble("Step", "Step", "", 0.02, 0.001, 1);
    indicator.parameters:addDouble("Max", "Max", "", 0.2, 0.001, 10);
    indicator.parameters:addColor("clrUp", "Up Line Color", "", core.rgb(255, 0, 0));
    indicator.parameters:addColor("clrDown", "Down Line Color", "", core.rgb(0, 255, 0));
end

-- Indicator instance initialization routine
-- Processes indicator parameters and creates output streams
-- Parameters block

local first;
local source = nil;
local tradeHigh = nil;
local tradeLow = nil;
local parOp = nil;
local position = nil;
local af = nil;
local Step;
local Max;

-- Streams block
local SAR = nil;
local UP = nil;
local DOWN = nil;

-- Routine
function Prepare()
    source = instance.source;
    first = source:first() + 1;

    Step = instance.parameters.Step;
    Max = instance.parameters.Max;

    local name = profile:id() .. "(" .. source:name() .. "," .. Step .. "," .. Max .. ")";
    instance:name(name);


    tradeHigh = instance:addInternalStream(0, 0);
    tradeLow = instance:addInternalStream(0, 0);
    parOp = instance:addInternalStream(0, 0);
    position = instance:addInternalStream(0, 0);
    af = instance:addInternalStream(0, 0);
    SAR = instance:addInternalStream(first, 0);
    UP = instance:addStream("UP", core.Dot, name .. ".Up", "UP", instance.parameters.clrUp, first)
    DOWN = instance:addStream("DN", core.Dot, name .. ".Dn", "DN", instance.parameters.clrDown, first)
end

-- Indicator calculation routine
function Update(period)
    local init = Step;
    local quant = Step;
    local maxVal = Max;
    local lastHighest = 0;
    local lastLowest = 0;
    local high = 0;
    local low = 0;
    local prevHigh = 0;
    local prevLow = 0;
    if period >= first then
        high = source.high[period];
        low = source.low[period];
        prevHigh = source.high[period - 1];
        prevLow = source.low[period - 1];
        if (period == first) then
            tradeHigh[period] = prevHigh;
            tradeLow[period] = prevLow;
            position[period] = -1;
            parOp[period] = prevHigh;
            af[period] = 0;
        else
            parOp[period] = parOp[period - 1];
            position[period] = position[period - 1];
            tradeHigh[period] = tradeHigh[period - 1];
            tradeLow[period] = tradeLow[period - 1];
            af[period] = af[period - 1];
        end
        lastHighest = tradeHigh[period];
        lastLowest = tradeLow[period];
        if high > lastHighest then
            tradeHigh[period] = high;
        end

        if low < lastLowest then
            tradeLow[period] = low;
        end

        if position[period] == 1 then
            if (low < parOp[period]) then
                position[period] = -1;
                SAR[period] = lastHighest;
                tradeHigh[period] = high;
                tradeLow[period] = low;
                af[period] = init;
                parOp[period] = SAR[period] + af[period] * (tradeLow[period] - SAR[period]);
                if (parOp[period] < high) then
                    parOp[period] = high;
                end
                if (parOp[period] < prevHigh) then
                    parOp[period] = prevHigh;
                end
            else
                SAR[period] = parOp[period];
                if (tradeHigh[period] > tradeHigh[period - 1] and af[period] < maxVal) then
                    af[period] = af[period] + quant;
                    if af[period] > maxVal then
                        af[period] = maxVal;
                    end
                end

                parOp[period] = SAR[period] + af[period] * (tradeHigh[period] - SAR[period]);
                if (parOp[period] > low) then
                    parOp[period] = low;
                end
                if (parOp[period] > prevLow) then
                    parOp[period] = prevLow;
                end
            end
        else
            if (high > parOp[period]) then
                position[period] = 1;
                SAR[period] = lastLowest;
                tradeHigh[period] = high;
                tradeLow[period] = low;
                af[period] = init;
                parOp[period] = SAR[period] + af[period] * (tradeHigh[period] - SAR[period]);
                if (parOp[period] > low) then
                    parOp[period] = low;
                end
                if (parOp[period] > prevLow) then
                    parOp[period] = prevLow;
                end
            else
                SAR[period] = parOp[period];
                if (tradeLow[period] < tradeLow[period - 1] and af[period] < maxVal) then
                    af[period] = af[period] + quant;
                    if af[period] > maxVal then
                        af[period] = maxVal;
                    end
                end

                parOp[period] = SAR[period] + af[period] * (tradeLow[period] - SAR[period]);
                if (parOp[period] < high) then
                    parOp[period] = high;
                end
                if (parOp[period] < prevHigh) then
                    parOp[period] = prevHigh;
                end
            end
        end

        if position[period] == 1 then
            DOWN[period] = SAR[period];
        else
            UP[period] = SAR[period];
        end
    end
end


Download indicator:
CSAR.lua
(5.93 KiB) Downloaded 1932 times
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: Colour SAR

Postby maani1972 » Wed Apr 21, 2010 7:25 am

Hi Nikolay,

You added a great feature to the SAR, congratulation. Do you think it would be possible to add a way to make the dots bigger, more visible? And when the SAR changes from up to down and the opposite, it can generate at the first dot, a bigger dot than the others or an arrow? And do you think having a sound alert of our choice and e-mail alerts can be done? If so do you think with your skills you can do that? If so let me know. Have a great day.
maani1972
 
Posts: 13
Joined: Wed Apr 21, 2010 7:15 am

Re: Colour SAR

Postby chriz2110 » Sat Jan 08, 2011 10:53 am

Hi Nikolay,

Would it be possible for someone to write an original Parabolic Sar strategy like this Expert Advisor:

http://www.tradingsystemforex.com/exper ... ar-ea.html.

Thank you & kindly regards,

chriz2110
chriz2110
 
Posts: 1
Joined: Fri Dec 31, 2010 4:23 am

Re: Colour SAR

Postby Apprentice » Sat Jan 08, 2011 12:44 pm

Added to the development cue.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36435
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia


Re: Colour SAR

Postby bbpulse » Wed May 23, 2012 10:25 am

I am trying to Load the SAR indicator with sound and bigger color dots but it keeps giving me an error which reads incorrect file format. I see the code is saved as .lua I'm not familiar with this. I copied the code and saved it on Notepad but the error continues.

Any thoughts?
bbpulse
 
Posts: 1
Joined: Wed May 23, 2012 10:21 am

Re: Colour SAR

Postby Apprentice » Thu May 24, 2012 5:58 am

Can you post, or send this code to my privat email.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36435
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Colour SAR

Postby DanPhi74 » Tue Jul 12, 2016 5:18 am

Hello,

could you code a version of Color SAR with the step until 0.0000001 ?

Best regards
Daniel
DanPhi74
 
Posts: 69
Joined: Tue Dec 22, 2015 4:47 am


Re: Colour SAR

Postby DanPhi74 » Wed Jul 13, 2016 10:08 am

Many Thanks to you !
DanPhi74
 
Posts: 69
Joined: Tue Dec 22, 2015 4:47 am

Next

Return to Custom Indicators

Who is online

Users browsing this forum: Bing [Bot] and 28 guests