Wysiwyg
Everyone wants money
- Joined
- 8 August 2006
- Posts
- 8,428
- Reactions
- 284
.
I find this methodology to be beneficial in system testing / evaluation.
Mark
MSBE = Param("#DaysB4MoveStopBE", 10, 0,50,1) -1;
// raise stop to BE after x days
if(OpenPos == 1) {
BES[i] = Max(BES[i], BES[i-1]);
BESa[i] = BES[i];
}
if(OpenPos == 1 AND j == MSBE) {
TS = Max(TS[i], BES[i]);
TSa[i] = TS[i];
BESa[i] = BES[i];
}
Careful with my old code - I wasn't handling variables / arrays in the loop properly.
I need a general sell rule. eg. L < Ref(LLV(L,10),-1);
You want to specify one?
a) 'set' stop at break even when high price is >= x% above buy price.
Once this stop is triggered will the position exit at close or intra-day?
Mark
Example 4: partial exit (scaling out) on profit target stops
Example of code that exits 50% on first profit target, 50% on next profit target and everything at trailing stop:
Buy = Cross( MA( C, 10 ), MA( C, 50 ) );
Sell = 0;
// the system will exit
// 50% of position if FIRST PROFIT TARGET stop is hit
// 50% of position is SECOND PROFIT TARGET stop is hit
// 100% of position if TRAILING STOP is hit
FirstProfitTarget = 10; // profit
SecondProfitTarget = 20; // in percent
TrailingStop = 10; // also in percent
priceatbuy=0;
highsincebuy = 0;
exit = 0;
for( i = 0; i < BarCount; i++ )
{
if( priceatbuy == 0 AND Buy[ i ] )
{
priceatbuy = BuyPrice[ i ];
}
if( priceatbuy > 0 )
{
highsincebuy = Max( High[ i ], highsincebuy );
if( exit == 0 AND
High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy )
{
// first profit target hit - scale-out
exit = 1;
Buy[ i ] = sigScaleOut;
}
if( exit == 1 AND
High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy )
{
// second profit target hit - exit
exit = 2;
SellPrice[ i ] = Max( Open[ i ], ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy );
}
if( Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy )
{
// trailing stop hit - exit
exit = 3;
SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01 ) * highsincebuy );
}
if( exit >= 2 )
{
Buy[ i ] = 0;
[COLOR="Red"]Sell[ i ] = exit + 1; // mark appropriate exit code [/COLOR]
exit = 0;
priceatbuy = 0; // reset price
highsincebuy = 0;
}
}
}
SetPositionSize( 50, spsPercentOfEquity );
SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position
SetPositionSize(1, spsPercentOfEquity);
SetTradeDelays(1,1,0,0);
Buy = H > Ref(HHV(H,50),-1); // whatever entry
BuyPrice = O; // must be specified outside loop to work with trade delays?
Sell = L < Ref(LLV(L,50),-1); // whatever exit
SellPrice = O; // must be specified outside loop to work with trade delays?
PctUp = Param("PctUp", 10, 1, 20, 1)/100; // percent above buyprice before shift stop to break even
SellDays = Param("sell if not up # days", 30, 1, 50, 1) -1; // after x days, we'll exit if not up
OpenPos = 0; Stop = 0;j = 0; be = 0; raise = 0; bp = 0;
bea = Null; bpa = Null; stopa = Null; ja = Null; raisea = Null; // arrays filled with null values, these will be plotted
for(i=1; i<BarCount; i++)
{
if(!OpenPos) { // not in an open position
Sell[i] = False; // remove excess sell signals
if(Buy[i]) { // buy signal
OpenPos = 1; // now have an open position
BuyPrice[i] = O[i]; // not necessary as specified outside loop - more for reference
bp = BuyPrice[i];
be = BuyPrice[i] * 1.005; // break even = buyprice + commissions, interest, etc - you decide
raise = (1+PctUp) * BuyPrice[i]; // calculated level to shift stop to break even
j = 0; // just a counter - used for 30 day exit
}
bpa[i] = bp; bea[i] = be; raisea[i] = raise; stopa[i] = stop; ja[i] = j; // storing variables in arrays so they can be plotted
}
else { // we are in an open position
Buy[i] = False; // remove excess buy signals
if(H[i] > raise) // stock up, set stop at break even
stop = be; // used "stop" to make it obvious when checking
if(C[i] < stop) { // stop is at be, we're closing below it = exit
Sell[i] = True;
OpenPos = 0;
SellPrice[i] = O[i]; // again not necessary as it is specified outside loop, there for reference
bp = 0; be = 0; raise = 0; stop = 0; j = 0; // reset all variables back to 0
}
if(j == SellDays AND C[i] < bp) { // sell after 30 bars if stock is below buyprice
Sell[i] = True;
OpenPos = 0;
SellPrice[i] = O[i];
bp = 0; be = 0; raise = 0; stop = 0; j = 0;
}
if(Sell[i]) { // normal sell
OpenPos = 0;
SellPrice[i] = O[i];
bp = 0; be = 0; raise = 0; stop = 0; j = 0;
}
bpa[i] = bp; bea[i] = be; raisea[i] = raise; stopa[i] = stop;
j++; ja[i] = j;
}
}
Plot(bpa, "buyprice", colorGreen, styleLine);
Plot(bea, "BE", colorRed, styleLine);
Plot(raisea, "raise level", colorBrightGreen, styleLine);
Plot(stopa, "stop level", colorBlue, styleLine);
Plot(ja, "J count", colorBlack, styleLine|styleOwnScale);
PlotShapes(Buy*shapeUpArrow, colorGreen, 0,L);
PlotShapes(Sell*shapeDownArrow, colorRed, 0,H);
Plot(L, "low", colorBlack, styleLine);
From the amibroker guideline, it gives an example of the scaleout code. Can anyone please explain this code for me
"Sell[ i ] = exit + 1;". Although commented, I don't quite understand the meaning of "// mark appropriate exit code". As far as I know, the sell function is executed according to either 1(true) or 0(false). How does this code work to only sell when the exit is => 2 which is when secondprofit target (exit code=2) and trailing stop (exit code=3) are crossed.
Code:Example 4: partial exit (scaling out) on profit target stops Example of code that exits 50% on first profit target, 50% on next profit target and everything at trailing stop: Buy = Cross( MA( C, 10 ), MA( C, 50 ) ); Sell = 0; // the system will exit // 50% of position if FIRST PROFIT TARGET stop is hit // 50% of position is SECOND PROFIT TARGET stop is hit // 100% of position if TRAILING STOP is hit FirstProfitTarget = 10; // profit SecondProfitTarget = 20; // in percent TrailingStop = 10; // also in percent priceatbuy=0; highsincebuy = 0; exit = 0; for( i = 0; i < BarCount; i++ ) { if( priceatbuy == 0 AND Buy[ i ] ) { priceatbuy = BuyPrice[ i ]; } if( priceatbuy > 0 ) { highsincebuy = Max( High[ i ], highsincebuy ); if( exit == 0 AND High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) * priceatbuy ) { // first profit target hit - scale-out exit = 1; Buy[ i ] = sigScaleOut; } if( exit == 1 AND High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy ) { // second profit target hit - exit exit = 2; SellPrice[ i ] = Max( Open[ i ], ( 1 + SecondProfitTarget * 0.01 ) * priceatbuy ); } if( Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy ) { // trailing stop hit - exit exit = 3; SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01 ) * highsincebuy ); } if( exit >= 2 ) { Buy[ i ] = 0; [COLOR="Red"]Sell[ i ] = exit + 1; // mark appropriate exit code [/COLOR] exit = 0; priceatbuy = 0; // reset price highsincebuy = 0; } } } SetPositionSize( 50, spsPercentOfEquity ); SetPositionSize( 50, spsPercentOfPosition * ( Buy == sigScaleOut ) ); // scale out 50% of position
I want to have a trailing stop that sells at the open of the following morning.
Thank Captain,
The possible options for the 4th param of the ApplyStop( stopTypeTrailing, stopModePoint,TrailAmount, 0, True ) function are -
ExitAtStop = 0 - means check stops using only trade price and exit at regular trade price
(if you are trading on close it means that only close price will be checked for exits and exit will be done at close price)
ExitAtStop = 1 - check High-Low prices and exit intraday on price equal to stop level on the same bar when stop was triggered
ExitAtStop = 2 - check High-Low prices but exit NEXT BAR on regular trade price.
I don't find the definitions particularly clear - if I want it to check today's close and sell on tomorrow's open I am not sure which to use...
Is there an easy way to verify your system is exiting when you expect it is? I am currently looking at the detailed trade log.
ATR stop plot code posted by GP.
https://www.aussiestockforums.com/forums/showthread.php?t=2452&highlight=trailing+stop&page=4
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?