Hi there guys.
I have an issue with this trailstop. When I have the time frame on 1hr (which is what the timeframe is set to). The value of the trail stop is 14.3121. But when I change to say a 40min chart trail stop value is 14.2956. Could anyone perhaps explain what I'm doing wrong here ? Thanks very much!
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
function vstop_func(trBull,trBear)
{
trailArray[0]=C[0];
for(i=1;i<BarCount;i++)
{
prev=trailArray[i-1];
if(C[i]>prev AND C[i-1]>prev)
{
trailArray[i]=Max(prev,C[i]-trBull[i]);
}
else if(C[i]<prev AND C[i-1]< prev)
{
trailArray[i]=Min(prev,C[i]+trBear[i]);
}
else if (C[i]>prev)
{
trailArray[i]=C[i]-trBull[i];
}
else
{
trailArray[i]=C[i]+trBear[i];
}
}
return trailArray;
}
////////////////////////Short Term Trail/////////////////////////
ST_TRAIL=Param("ST Trail (hrs)",1,1,10,1)*60;
ST_TRAIL_1=in1Minute*ST_TRAIL;
TimeFrameSet(ST_TRAIL_1);
ST_Periods=Param("ST_periods",100,1,500,1);
ST_dev=Param("ST_deviation",2.5,0.05,20,0.05);
ST_trBull=ST_dev*ATR(ST_Periods);
ST_trBear=ST_dev*ATR(ST_Periods);
ST_trailArray = vstop_func(ST_trBull,ST_trBear);
ST_ts=IIf(ST_trailArray>C,ST_trailArray,Null);
ST_tl=IIf(ST_trailArray<C,ST_trailArray,Null);
TimeFrameRestore();
ST_ts=TimeFrameExpand(ST_ts,ST_TRAIL_1,expandFirst);
ST_tl=TimeFrameExpand(ST_tl,ST_TRAIL_1,expandFirst);
if ( ParamToggle( "1hr Trail Stop", "OFF|ON", 1 ) )
{
Plot(ST_ts , "", colorYellow,styleDashed,0,0,0,0,0 );
Plot(ST_tl , "", colorYellow,styleDashed,0,0,0,0,0 );
}