Sunday, April 13, 2014

Super Trend - Amibroker AFL Code - A Different Version

Super Trend Amibroker AFL . This is a new version of Super Trend Amibroker AFL Code and needs to be paper traded by the trader . Do not straight away take positions based on the signals generated by the AFL.

Lot of precision i sneeded to come with a trading system based on Super Trend as during sideways moves of market it might bring lot of whipsaws. This AFL at this blog has been posted as is written by the author. The AFL has been taken from an internet source. Use the AFL for educational purpose.
Image of the AFL in Amibroker can be seen below the post.

========================= Copy The Code ================================

//FREE SUPER TREND

_SECTION_BEGIN("Free SuperTrend");
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
SetChartOptions(1,chartShowArrows|chartShowDates|chartWrapTitle);


GraphXSpace = 15;

SetBarFillColor(IIf(C>O,ParamColor("Candle UP Color", colorGreen),IIf(C<=O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
Plot(C,"",IIf(C>O,ParamColor("Wick UP Color", colorDarkGreen),IIf(C<=O,ParamColor("Wick Down Color", colorDarkRed),colorLightGrey)),64,0,0,0,0);
SetChartBkGradientFill( ParamColor( "TopColor", ColorRGB( 217, 197, 137 ) ), ParamColor( "BottomColor", ColorRGB( 254, 244, 224 ) ) );

//SetTradeDelays(1,1,1,1);
Period = Param("Period", 150, 1, 2400, 1);
mult = Param("Multiplier", 4, 1.1, 20.0, 0.1); // set for 5 minutes chart.

f=ATR(period);

VS[0] = Close[0];
trend[0] = 0;
HighC[0]=0;
Lowc[0]=0;

for( i = period+1; i < BarCount; i++ )
{

vs[i] = vs[i-1];
trend[i] = trend[i-1];
highC[i] = HighC[i-1];
lowc[i] = lowc[i-1];

if ((trend[i]>=0) && ( C[i] <VS[i] ))
{
trend[i] =-1;
HighC[i] = C[i];
lowc[i] = C[i];
}

if ((trend[i]<=0) && (C[i] >VS[i]))
{
trend[i]=1;
HighC[i] = C[i];
lowc[i] = C[i];
}

if (trend[i]==-1)
{
if (C[i]<lowc[i]) lowc[i] = C[i];
VS[i]= lowc[i]+ (mult*f[i]);
}


if (trend[i]==1)
{
if (C[i]>HighC[i]) HighC[i] = C[i];
VS[i]= HighC[i]-(mult*f[i]);
}

}

Plot(VS, "Vol Stop",IIf(trend==1,colorRed,colorYellow ),styleThick);

Buy=Cross(Trend,0);
Short=Cross(0, Trend);
Buy = Ref(Buy, -1);
Short = Ref(Short, -1);

Hp = HHV( H, 40 );
Lp = LLV( L, 40 );

BarsSincebuy = BarsSince( Buy );
BarsSinceshort = BarsSince( Short );
LastSignal = IIf( BarsSincebuy < BarsSinceshort, 1, -1 );
Sig = WriteIf( BarsSincebuy < BarsSinceshort, "BUY", "SELL" );

slPrice = IIf( LastSignal == 1, HighestSince( Buy, Lp ), LowestSince( Short, Hp ) );
initialrisk = IIf( LastSignal == 1, BuyPrice - SLPrice, SLPrice - ShortPrice );
CurrentPL = IIf( LastSignal == 1, C - BuyPrice, SellPrice - C );

BuyPrice=ValueWhen(Buy,O);
ShortPrice=ValueWhen(Short,O);

entry = IIf( LastSignal == 1, BuyPrice, ShortPrice );

PlotShapes(Buy * shapeUpArrow,colorGreen, 0,L, Offset=-45);
PlotShapes(Short * shapeDownArrow,colorRed, 0,H, Offset=-45);


bars = LastValue( IIf(BarsSincebuy < BarsSinceshort, BarsSincebuy, BarsSinceshort));
Offset = 15;
Clr = IIf(LastValue(LastSignal) == 1, colorGreen, colorRed);


if ( ParamToggle( "Message Board ", "Show|Hide", 1 ) )
{
GfxSelectFont( "Tahoma", 11, 700 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );

if ( SelectedValue( LastSignal ) == 1 )
{
GfxSelectSolidBrush( colorBrightGreen );
Datetim = "" + ValueWhen( Buy, Day(), 1 ) + "/" + ValueWhen( Buy, Month(), 1 ) + "/" + ValueWhen( Buy, Year(), 1 ) + " " + ValueWhen( Buy, Hour(), 1 ) + ":" + ValueWhen( Buy, Minute(), 1 );
}
else
{
GfxSelectSolidBrush( colorOrange );
Datetim = "" + ValueWhen( Short, Day(), 1 ) + "/" + ValueWhen( Short, Month(), 1 ) + "/" + ValueWhen( Short, Year(), 1 ) + " " + ValueWhen( Short, Hour(), 1 ) + ":" + ValueWhen( Short, Minute(), 1 );
}

pxHeight = Status( "pxchartheight" ) ;

xx = Status( "pxchartwidth" );
Left = 1100;
width = 310;
x = 1.5;
x2 = 235;

y = pxHeight / 1;

GfxSelectPen( colorLightBlue, 1 );
GfxRoundRect( x, y - 105, x2, y , 7, 7 ) ;
GfxTextOut( ( "RoboTrade SuperTrend" ), 25, y - 100 );
GfxTextOut( ( " ..........................................." ), 18, y - 90 );
GfxTextOut( ( "Last Signal" ), 10, y - 65 ) ;
GfxTextOut( ( ": " + Datetim ), 110, y - 65 ) ;
GfxTextOut( ( "" + sig + " Entry @" ), 10, y - 45 );
GfxTextOut( ( ": " + entry ), 110, y - 45 );
GfxTextOut( ( "Current P/L" ), 10, y - 25 );
GfxTextOut( ( ": " + WriteVal( IIf( sig == "BUY", ( C - entry ), ( entry - C ) ), 2.2 ) ), 110, y - 25);;
x = 290;
x2 = 500;

}


_SECTION_END();

=========================   =============  ========   ====================


6 comments:

  1. fully blinking :( ... singals r good but some technical issue

    ReplyDelete
    Replies
    1. Hi Kishore,

      What actually do you imply by Fully Blinking ? I could not get where are you getting problem. I will test it again to confront the technical problem which you are mentioning.

      Do you suggest that I should post some other AFL for Super Trend ? I could certainly update another AFL if this one is giving problem.

      Happy Trading

      Delete
  2. Good day....i have learnt a lot from your valuable posts, data sharing and feedback and also grown as a successful investor. But for this formula given above i have one doubt and more than doubt a concern that "Buy = Ref(Buy, -1);" will always ref to the -1 bar and hence while calculating profit it will show great results but real time on live trading the signal will pop up/appear in scan/backtasting only when the O of candle after -1 so technically you cannot trade on such shadow signals .....please correct me if i am wrong.

    ReplyDelete
    Replies
    1. Hi ,

      You are right in what you say. Such AFL do give problems. It is better to make changes in the formula to get signals after the close of the candle on which the super trend flipped. I will try to update this code with a candle close code.

      Happy Trading.

      Delete
  3. what change do i make if i need in below formula for 7 period and 1.5 on 5 in chart?
    //SetTradeDelays(1,1,1,1);
    Period = Param("Period", 150, 1, 2400, 1);
    mult = Param("Multiplier", 4, 1.1, 20.0, 0.1); // set for 5 minutes chart.

    ReplyDelete
  4. Hi Martin,

    You can change the values through parameters in Period & Multiplier.

    But if you want to change the default values for the AFL then replace the following code with the above

    //SetTradeDelays(1,1,1,1);
    Period = Param("Period", 7, 1, 2400, 1);
    mult = Param("Multiplier", 1.5, 1.1, 20.0, 0.1); // set for 5 minutes chart.

    ReplyDelete

Required US Government Disclaimer & CTFC Rule 4.41

Futures trading contains substantial risk and is not suitable for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only consider risk capital that should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. CTFC RULE 4.41 – HYPOTHETICAL OR SIMULATED PERFORMANCE RESULTS HAVE CERTAIN LIMITATIONS. UNLIKE AN ACTUAL PERFORMANCE RECORD, SIMULATED RESULTS DO NOT REPRESENT ACTUAL TRADING. ALSO, SINCE THE TRADES HAVE NOT BEEN EXECUTED, THE RESULTS MAY HAVE UNDER-OR-OVER COMPENSATED FOR THE IMPACT, IF ANY, OF CERTAIN MARKET FACTORS SUCH AS LIQUIDITY. SIMULATED TRADING PROGRAMS IN GENERAL ARE ALSO SUBJECT TO THE FACT THAT THEY ARE DESIGNED WITH THE BENEFIT OF HINDSIGHT. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL OR IS LIKELY TO ACHIEVE PROFIT OR LOSSES SIMILAR TO THOSE SHOWN. All trades, patterns, charts, systems, etc., discussed in this website or advertisement are for illustrative purposes only and not construed as specific advisory recommendations. All ideas and materials presented herein are for information and educational purposes only. No system or trading methodology has ever been developed that can guarantee profits or prevent losses. The testimonials and examples used herein are exceptional results which do not apply to average people and are not intended to represent or guarantee that anyone will achieve the same or similar results. Trades placed on the reliance of Trend Methods systems are taken at your own risk for your own account. This is not an offer to buy or sell futures interests.