This is read-only version of AFL library entry. Ability to add user formulas
and comment is only available from members-only area.
Details:
Formula name:
Visualization of stoploses and profit in chart
Author/Uploader:
Marcus Davidsson - davidsson_marcus [at] hotmail.com
Date/Time added:
2006-07-31 09:09:04
Origin:
Keywords:
Level:
basic
Flags:
system,exploration,indicator,commentary,function
DISCLAIMER: Most formulas present in AFL on-line library are submitted
by the users and are provided here on an "as is" and "as available" basis.
AmiBroker.com
makes no representations or warranties of any kind to the contents or the operation
of material presented here. We do not maintain nor provide technical support
for 3rd party formulas. Description:
Clear visualization of stoplosses, ordinary entry/exit signals, profit and closing prices in the chart !!!
Takes the analytical part of the backtesting to an entire new level in my opion (reduces the importance of backtesting
Formula:
// Visualization of Buy/Sell levels, profit and exitsignals( ie stoplossses) in
the chart////////////
// Takes the analytical part of the backtesting to an entire new level in my
opion (reduces the importance of backtesting)!
// The code is far from complete and perfect but I dont have the energy to do
more. Maybe someone
// could countineou developing the code to include for example:
// 1) an option in the parameter window, to show Buy/Sell/Cover statistics only
// if one would place the mousepointer on each "buy shapes". (
GetCursorYPosition() GetCursorXPosition() )
// The display becomes quite messy if you display a Low time resolution Chart
(monthly, yearly) One alternative would also to use
// the code for displaying digits based on the diffrent sell signal
(shapeDigit1). Then it would be sufficient
// to display the profit/loss for each trade.
// 2) Another drawback is the parameter hiracy. Each parameter
// subcategories should only be able to work if the user choose Yes on the main
category question.
// Now you have to set all sub-parameters equal to zero instead of
activative/deactivating the major ones.
// 3) Some overal statistics displayed on the lefthand side of the chart
// (# of trades, # winning/lossing trades, total profit/loss etc)
//The following code works best with a black background
//would also like to take the opportunity to thank Marcin Gorzynski for his
help with this one
////////////////////////////// BASIC STRATEGY AND PLOTS ////////////////////
Buy = Cross( Close, MA(Close, 35) );
Short= Cross( MA(Close, 35), Close );
Sell=LinRegSlope( MA(Close,18), 2 )<0;
Cover=LinRegSlope( MA(Close,18), 2 )>0;
Plot( Close, "C", colorWhite, styleLine);
Plot( MA(Close,100), "MA-100", colorRed, styleLine);
// I am sceptical to if the original applystop functions (trailing etc) really
works!?
//I have therefor coded my own stop functions below.
////////////////////////////// STOP LOSS PARAMETERS ////////////////////
e = Equity(1,0); /* Highly Important!!. "Removes all extra signals, evaluates
stops AND writes BACK signals to Sell/Cover arrays". As it should be!!*/
Lprofit = e - ValueWhen( Buy, e);
Sprofit = e - ValueWhen( Short, e);
/////////////////////////////// TRAILING PROFIT
STOP////////////////////////////
TL=ParamToggle("FANCY A TRAILING STOP?" , "No|Yes",0);
TLL= ParamToggle(" DO YOU WANT TO PLOT PROFIT + CRITICAL TRAILING
LINE IN GRAPH?" , "No|Yes",0);
x2=Param(" SET MAX ACCEPTED DECLINE OF PROFIT IN PERCENT ",1 ,0 ,100
,1);
//x3=Param(" SET MAX ACCEPTED DECLINE OF PROFIT IN POINTS ",1 ,0 ,100
,1);
////////FOR LONG POSITION PERCENT////
XXL=HighestSince( Buy==1, Lprofit, 1 ); //returns the highest profit since last
buy signal. the basis for the trailing calculation.
XXXL= XXL*(1-(x2/100)); // if trailing turned on =ok otherwise =null
ZL= ExRem( Cross(XXXL, Lprofit), Buy==1); // Just first signal counts. highly
important!!!!!!!! Also calculates critical sell levels
Sell= IIf(ZL==1 AND TL==True, 4, 0); // return a sell signal=4 if z1=1 and
TL=true (yes)
////////FOR SHORT POSITION PERCENT////
XXS=HighestSince( Short==1, Sprofit, 1 ); // same as above
XXXS= XXS*(1-(x2/100));
ZS= ExRem( Cross(XXXS, Lprofit), Short==1);
Cover= IIf(ZS==1 AND TL==True, 4, 0);
PlotShapes( Buy* shapeUpArrow , colorGreen, 0);
PlotShapes( Short* shapeDownArrow , colorGreen, 0);
PlotShapes( Sell* shapeDigit1 , colorRed, 0);
PlotShapes( Cover* shapeDigit2 , colorRed, 0);
if(TLL==True AND TL=True) Plot(Lprofit,"L
PROFIT",colorYellow,styleLeftAxisScale, styleLine) AND
Plot(Sprofit,"S PROFIT",colorYellow,styleLeftAxisScale, styleLine)
AND
Plot(XXXL, "L TRAILING LEVEL",colorGreen,styleLeftAxisScale,
styleLine) AND
Plot(XXXS, "S TRAILING LEVEL",colorGreen,styleLeftAxisScale,
styleLine);
//////////ABSOLUTE PROFIT STOP////////////////////////////
ML=ParamToggle("FANCY A MAX STOP IN PERCENT?", "No|Yes",0);
x1=Param( " SET MAX ACCEPTED LOSS PER TRADE IN PERCENT", 1, 0 ,50
,1);
XS =ExRem(Cross (1-(x1/100),e), Buy==1);
Sell=IIf( XS==1 AND ML==True, 2, 0);
////////////////////////////// SETTINGS AND BASIC DEFINITIONS
////////////////////
SetOption("MaxOpenPositions", 2 );
PositionSize = 10000;
GraphXSpace=10; /*"adds 10% extra space above AND below the graph line." In
order to fit the extra text
"When GraphXSpace is NOT defined in the formula then default 2% is
used."*/
dist=200;
bcolor=scolor=colorBlue;
////////////////////////////// EXIT AND ENTRY MARKERS DEFINED
////////////////////
PlotShapes( Buy* shapeUpArrow , bcolor, 0);
PlotShapes( Short* shapeDownArrow , scolor, 0);
sellshape = IIf( Sell == 1, shapeSquare + shapePositionAbove,
IIf( Sell == 2, shapeSquare + shapePositionAbove,
IIf( Sell == 3, shapeSquare + shapePositionAbove,
IIf( Sell == 4, shapeSquare + shapePositionAbove,
IIf( Sell == 5, shapeSquare + shapePositionAbove,0 )))));
Covershape= IIf( Cover == 1, shapeSquare,
IIf( Cover == 2, shapeSquare,
IIf( Cover == 3, shapeSquare,
IIf( Cover == 4, shapeSquare,
IIf( Cover == 5, shapeSquare,0 )))));
LColor= IIf(Lprofit>0, colorGreen, colorRed);
Scolor=IIf(Sprofit>0, colorGreen, colorRed);
PlotShapes( SellShape, Lcolor, 0, C);
PlotShapes( covershape, Scolor, 0, C);
////////////////////////////// FOR LOOP FOR PLOTING ENTRY AND EXIT TEXT
////////////////////
for( i = 0; i < BarCount; i++ )
{
xx = Sum(Buy,i ); // counting of long trades. xx will not get a higher value
until a new long position is open
// therefor if we only allow one long and one short position (Exrem() )
xx can also be used as an identification
// number for the diffrent positions.
yy = Sum(Short,i);
if( Buy[i]==1) PlotText("LONG : " + xx[i] + "\nBuyPrice: "+ BuyPrice[ i ],
i, H[ i ]-dist, colorBlack, bcolor );
if( Sell[i]==1 ) PlotText( "LONG : " +xx[i]+ "\nREGULAR EXIT\n"+ "Sell:
"+ SellPrice[ i ]+"\nP/ L: "
+Lprofit[ i ], i, H[ i ]+dist, colorBlack, Lcolor[i] );
if( Sell[i]==2 ) PlotText( "LONG : " +xx[i]+ "\nMAXIMUM LOSS\n"+ "Sell:
"+ SellPrice[ i ] +"\nP/ L: "
+Lprofit[ i ], i, H[ i ]+dist, colorBlack, Lcolor[i] );
if( Sell[i]==3 ) PlotText( "LONG : " +xx[i]+ "\nPROFIT TARGET\n"+ "Sell:
"+ SellPrice[ i ] +"\nP/ L: "
+Lprofit[ i ], i, H[ i ]+dist, colorBlack, Lcolor[i] );
if( Sell[i]==4 ) PlotText( "LONG : " +xx[i]+ "\nTRAILING STOP\n"+ "Sell:
"+ SellPrice[ i ] +"\nP/ L: "
+Lprofit[ i ], i, H[ i ]+dist, colorBlack, Lcolor[i] );
if( Sell[i]==5 ) PlotText( "LONG ; " +xx[i]+ "\nNBAR STOP\n"+ "Sell:
"+ SellPrice[ i ] +"\nP/ L: "
+Lprofit[ i ], i, H[ i ]+dist, colorBlack, Lcolor[i] );
if( Short[i]==1 ) PlotText( "SHORT : "+ yy[i]+ "\nShortprice: "+ ShortPrice[
i ], i, L[ i ]-dist, colorBlack, bcolor );
if( Cover[i]==1 ) PlotText( "SHORT : " +yy[i]+ "\nREGULAR EXIT\n"+ "Cover:
"+ CoverPrice[ i ]+"\nP/ L: "
+Sprofit[ i ], i, H[ i ]+dist, colorBlack, Scolor[i] );
if( Cover[i]==2 ) PlotText( "SHORT : " +yy[i]+ "\nMAXIMUM LOSS\n"+ "Cover:
"+ CoverPrice[ i ]+"\nP/ L: "
+Sprofit[ i ], i, H[ i ]+dist, colorBlack, Scolor[i] );
if( Cover[i]==3 ) PlotText( "SHORT : " +yy[i]+ "\nPROFIT TARGET\n"+ "Cover:
"+ CoverPrice[ i ]+"\nP/ L: "
+Sprofit[ i ], i, H[ i ]+dist, colorBlack, Scolor[i] );
if( Cover[i]==4 ) PlotText( "SHORT : " +yy[i]+ "\nTRAILING STOP\n"+ "Cover:
"+ CoverPrice[ i ]+"\nP/ L: "
+Sprofit[ i ], i, H[ i ]+dist, colorBlack, Scolor[i] );
if( Cover[i]==5 ) PlotText( "SHORT : " +yy[i]+ "\nNBAR STOP\n"+ "Cover:
"+ CoverPrice[ i ]+"\nP/ L: "
+Sprofit[ i ], i, H[ i ]+dist, colorBlack, Scolor[i] );
}
Comments:
Pini lpin99 [at] yahoo.com 2006-08-01 18:16:42
There is an error
Jack
2006-08-02 04:10:20
Yes, there is one syntax error coming from this line: if( Buy[i]==1) PlotText("LONG : " + xx[i] + "\nBuyPrice: "+ BuyPrice[ i ],
i, H[ i ]-dist, colorBlack, bcolor );
Is it an AB version problem again?
Marcus Davidsson davidsson_marcus [at] hotmail.com 2006-08-02 08:16:02
Hi all,
thanks for your comments. The syntax errors comes from that sometimes the comments, for some reasons, contineous on to the next row. Just go through the code and press del to get the comment
sentence on the same line. This will solve the problem.
When this is done just drag the formula to the price chart to get all visualizations. I recommend using a low time resolution chart (daily, weekly, monthly etc)
best regards marcus
Jack
2006-08-03 04:51:11
Marcus,
Yiu are right. 9 times out of 10 this is the problem. I use AB 4.7 and I checked the code carefully before sending my previous code and now again. The syntax error showes again as stated before. Is it the "if" or plot command that my AB version does not like? Or maybe the number of"(....)" brackets are not equal.
Hello,
Wrapping any line in your formula between
the quotes "" where the text belongs is
not likely to work.
If you want to wrap lines in your formula without them
causing errors it should be done so that the last space in
the line is punctuation such as a comma or period or
the closing quotes for text strings or the plus sign outside of
a text string. If it needs to be done in the middle of a
long text string, use the close quotes then a plus sign and
start the new line with open quotes and the remainder of
the text string.
In the line below you notice the plus sign that was needed
as part of the text string is still within the text string as
it cannot be used as the end of a line.
TLL= ParamToggle(" DO YOU WANT TO PLOT PROFIT"+
"+ CRITICAL TRAILING LINE IN GRAPH?" , "No|Yes",1);