Manual Trailing Stop

#1
Hi all,

I need a Percent Trailing Stop not based on buy signal, but with a manual value (Parameter). It shoul trailig on the high for long and low for short positions I tried to write the code, but it didn't work:

// -----------------------------------------------

StopLevel = Param("trailing stop %", 1, 0.01, 50, 0.01 );
Entry = Param("Entry", 1, 0.01, 1000, 0.01);

SetTradeDelays(0,0,0,0);

ApplyStop( stopTypeTrailing, stopModePercent, StopLevel, True );

Equity( 1, 0 );

InTrade = Flip( Buy, Sell );

SetOption("EveryBarNullCheck", True );
stopline = HighestSince( Entry, High ) * ( 1 - 0.01 * StopLevel );

Plot( Close,"Price",colorBlack,styleBar);
Plot( stopline, "trailing stop line", colorRed );


// -----------------------------------------------



What I need is: I give a manual price (ie 300,00) and Amibroker plots a trailing stop % (ie 3%) for long or shortl.

Can someone help me?

Thanks!

Jacare



---------------------------------------------

Hi,

I did some progess, it works more or less for long position, but not yet for the short ones:




_SECTION_BEGIN("@Trailing 03");

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

StopLevel = Param("Trailing Stop %", 3, 0.01, 50, 0.01 );

SetTradeDelays(0,0,0,0);

Buy = Cross( MACD(), Signal() );
Sell = 0;
ApplyStop( stopTypeTrailing, stopModePercent, StopLevel, True );

Equity( 1, 0 );

InTrade = Flip( Buy, Sell );

SetOption("EveryBarNullCheck", True );
stopline = HighestSince( Buy, low ) * ( 1 - 0.01 * StopLevel );

PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);

Plot( Close,"Price",colorBlack,styleBar);

Plot( stopline, "trailing stop line", ParamColor( "Color", colorRed ), ParamStyle("Style") );

_SECTION_END();


// ------------------------------------



Any help??

Thanks,

Jacare
 
Last edited:

mastermind007

Well-Known Member
#3
Hi all,

I need a Percent Trailing Stop not based on buy signal, but with a manual value (Parameter). It shoul trailig on the high for long and low for short positions I tried to write the code, but it didn't work:

// -----------------------------------------------

StopLevel = Param("trailing stop %", 1, 0.01, 50, 0.01 );
Entry = Param("Entry", 1, 0.01, 1000, 0.01);

SetTradeDelays(0,0,0,0);

ApplyStop( stopTypeTrailing, stopModePercent, StopLevel, True );

Equity( 1, 0 );

InTrade = Flip( Buy, Sell );

SetOption("EveryBarNullCheck", True );
stopline = HighestSince( Entry, High ) * ( 1 - 0.01 * StopLevel );

Plot( Close,"Price",colorBlack,styleBar);
Plot( stopline, "trailing stop line", colorRed );


// -----------------------------------------------



What I need is: I give a manual price (ie 300,00) and Amibroker plots a trailing stop % (ie 3%) for long or shortl.

Can someone help me?

Thanks!

Jacare



---------------------------------------------

Hi,

I did some progess, it works more or less for long position, but not yet for the short ones:




_SECTION_BEGIN("@Trailing 03");

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));

StopLevel = Param("Trailing Stop %", 3, 0.01, 50, 0.01 );

SetTradeDelays(0,0,0,0);

Buy = Cross( MACD(), Signal() );
Sell = 0;
ApplyStop( stopTypeTrailing, stopModePercent, StopLevel, True );

Equity( 1, 0 );

InTrade = Flip( Buy, Sell );

SetOption("EveryBarNullCheck", True );
stopline = HighestSince( Buy, low ) * ( 1 - 0.01 * StopLevel );

PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);

Plot( Close,"Price",colorBlack,styleBar);

Plot( stopline, "trailing stop line", ParamColor( "Color", colorRed ), ParamStyle("Style") );

_SECTION_END();


// ------------------------------------



Any help??

Thanks,

Jacare
Jacare

As far as I know, ApplyStop function does not work in regular Visual UI mode. It will work only during the backtesting. Also, your code is designed to only handle the Long code. There is no provision in it to handle the short trades.
 
#4
Hello all,

thanks for the help.

@colion: I will read (and learn) the link you wrote. Thanks!

I wish you all a happy new year!

Jacare
 
#5
Hi guys,

it didn't work. My problem is, that I don't open a position in the same time that Amibroker gives me a Buy/Sell signal. I become the Amibroker signal and wait some days, until the volume or another indicator confirm it (I can not bring all the rules to aone AFL formula).

How can I make this Buy/Sell point manually? I think something like at the Parameter, I give the date, price level and Buy or Sell and Amibroker starts to plot the Trailing Stop from this date and price level.

Is that possible? Could someone help me with the code?

My actual Trailing stop has this formula:



//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------




_SECTION_BEGIN("Long Short % Stop");
//---long var
Stoplong = 1 - Param("stop % Long", 3, 0.01, 99.9, 0.01)/100;
Plong = ParamField("Price Field Long", 2);
Longcol= ParamColor( "Color Long Stop", colorCustom11 );


Buy = Cross(ma(c,10),ma(c,20));


Sell = 0;

traillongARRAY = Null;
trailstoplong = 0;

//----short var


StopLevel = 1 - Param("Stop Short %", 3, 0.01, 99.9, 0.01)/100;
P = ParamField("Price Field Short", 1);
Shortcol=ParamColor( "Color Short Stop", colorCustom12 );


Short = Cross(ma(c,20),ma(c,10));


Cover = 0;

trailARRAY = Null;
trailstop = 0;

//---calculate
for( i = 2; i < BarCount; i++ )
{
//---long
if( trailstoplong == 0 AND Buy[ i ] )
{
trailstoplong =Plong[ i ] * Stoplong;
}
else Buy[ i ] = 0; // remove excess buy signals

if( trailstoplong > 0 AND Plong[ i ] < trailstoplong )
{
Sell[ i ] = 1;
SellPrice[ i ] = trailstoplong;
trailstoplong = 0;
}

if( trailstoplong > 0 )
{
trailstoplong = Max( Plong[ i ] * Stoplong, trailstoplong );
traillongARRAY[ i ] = trailstoplong;
}


//---short


if( trailstop == 0 AND Sell[ i ] )
{

trailstop = P[ i ] / StopLevel;// set intinal % stop then trailing stop takes over

}
else Short[ i ] = 0; // remove excess short signals

if( trailstop > 0 AND P[ i ] > trailstop )
{
Cover[ i ] = 1;
CoverPrice[ i ] = trailstop;
trailstop = 0;
}

if( trailstop > 0 )
{
trailstop = Min(P[ i ]/ stoplevel, trailstop );
trailARRAY[ i ] = trailstop;
}

}

// ----display results

Plot( traillongARRAY,"Stop Long",Longcol );
Plot( trailARRAY,"Stop Short", shortcol );

_N(Title = StrFormat("{{NAME}} - {{DATE}} - Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));



PlotShapes(Short*shapeUpArrow,ParamColor( "Arrow Short Color", colorCycle),0,Low);
PlotShapes(Cover*shapeDownArrow,ParamColor( "Arrow Cover Color", colorCycle),0,High);
PlotShapes(Buy*shapeUpArrow,ParamColor( "Arrow Buy Color", colorCycle),0,Low);
PlotShapes(Sell*shapeDownArrow,ParamColor( "Arrow Sell Color", colorCycle),0,High);

Plot( Close,"Price",colorBlack,styleBar);


_SECTION_END();

//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------



Here a screeshot. As you can see, my entry point is not the same from Amibroker - the trailing stop is wrong! That's way I need to put a manual date and price:





Couls someone help me?

Thanks!
 

Similar threads