Welcome to the new Traders Laboratory! Please bear with us as we finish the migration over the next few days. If you find any issues, want to leave feedback, get in touch with us, or offer suggestions please post to the Support forum here.

• ## Welcome Guests

Welcome. You are currently viewing the forum as a guest which does not give you access to all the great features at Traders Laboratory such as interacting with members, access to all forums, downloading attachments, and eligibility to win free giveaways. Registration is fast, simple and absolutely free. Create a FREE Traders Laboratory account here.

# Help Understanding This Code

## Recommended Posts

i want to use the following code in my indicator.

however, i cannot read it correctly as what its doing.

i am an ok TS programmer. i have my own indictor that i am working on.

i want to integrate the following code so that it only draws the buy or sell lines when i am long or short.

{Type : Indicator, Name : Variable Volatility Stops}

INPUT: LENGTH (21), CONST(3.05);

vaR: SWITCH(1), TR(0), ARC(0), SAR(0),HISIC(0),LOSIC(0);

TR= VOLATILITY (LENGTH);

ARC= TR* CONST;

IF CURRENTbar = length then begin

hisic=c;

losic=c;

if h>= xaverage (h,length-1) [1] then begin

hisic= highest(c,length);

Sar=hisic-arc;

switch= 1;

end;

if l <= xaverage (l,length-1) [1] then begin

losic= lowest (c,length);

sar = losic +arc;

switch=0;

end;

end;

if switch =1 then begin

if c>hisic then hisic=c;

sar=hisic - arc;

if c< sar then begin

switch=0;

losic=c;

sar = losic + arc;

end;

end;

if switch=0 then begin

if c< losic then losic=c;

sar= losic +arc;

if c> sar then begin

switch=1;

hisic=c;

saR=HISIC- ARC;

END;

END;

IF C > SAR { AND C[1] < SAR } THEN

Plot1(SAR,"Sell");

IF C < SAR { AND C[1] > SAR} THEN

##### Share on other sites

SWITCH is a reserved word. You cannot use it as a variable.

##### Share on other sites

yeah but it still works. nonetheless, i think might have it figured out.

##### Share on other sites

I don't understand what you need.

##### Share on other sites

Use the TS I_MarketPosition (Reserved Word) to show volatility stop only when a position is in the market. Strategy not included, code can be verified as indicator or showme.

I_MarketPosition can only be used in a study.

I_MarketPosition will only return a value if a strategy is applied to the same data.

```inputs:
ATRLength( 10 ) ,
NumATRs( 3 ) ;

variables:
ATRCalc( 0 ),
MP( 0 ),
PosHigh( 0 ),
PosLow( 0 ) ;

ATRCalc = AvgTrueRange( ATRLength ) * NumATRs ;
MP = I_MarketPosition ;

if MP > 0 then
begin
if MP[1] <> 1 or High > PosHigh then
PosHigh = High ;
Plot1( PosHigh - ATRCalc, "Plot1" ) ;
end
else
if MP < 0 then
begin
if MP[1] <> -1 or Low < PosLow then
PosLow = Low ;
Plot2 (PosLow + ATRCalc, "Plot2");
end ;
```

##### Share on other sites

hi guys,

thank you for helping me. i just wanted to know what the heck the code was doing. i wanted the sar to paint based on my position. if u use the code as an indicator you will see that the it switches to long or short whenever the price closes above the SAR value.

which did not play well within my code.

i think i have it figured out. as well as therunners help is making much more sense now.

i will try to integrate your code within my strategy today.

however, i have made the following changes. please let me know if the following looks ok to make sure i was understanding the code correctly.

TR= VOLATILITY (VOL_LENGTH);

ARC= TR * VOL_CONST;

IF CURRENTbar = length then begin

HISIC = close;

LOSIC = close;

if high >= xaverage (high,VOL_LENGTH-1) [1] then begin

HISIC = highest(close,VOL_LENGTH);

SAR = HISIC-arc;

SWITCH_IT= 1;

end;

if low <= xaverage (low,VOL_LENGTH-1) [1] then begin

LOSIC = lowest (close,VOL_LENGTH);

SAR = LOSIC + ARC;

SWITCH_IT=0;

end;

end;

if SWITCH_IT = 1 then begin

if close >hisic then hisic = close;

SAR = hisic - arc;

if close < SAR and AreWeShort = true then begin

SWITCH_IT = 0;

LOSIC = close;

SAR = LOSIC + ARC;

end;

end;

if SWITCH_IT=0 then begin

if close < losic then losic = close;

SAR = losic +arc;

if close > SAR and AreWeLong = true then begin

SWITCH_IT = 1;

HISIC = close;

SAR = HISIC- ARC;

END;

END;

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×

• 0
• 0
• 0
• -1
• 0