Seek AFL coding expertise

#21
Hope this helps (Do play with the parameters):
SuperTrend on Standard or HA Candles
C-like:
function fSuperTrend( CustHi, CustLo, CustCl, Mult, P, UsingHA )
{
     // Based on AFL KB Article "How to plot a trailing stop in the Price chart" (http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/)
     if( UsingHA )
     {
         CustATR = 0;
         for( i = 1; i <= BarCount - 1; i++ ) CustATR[ i ] = ( Max( CustHi[ i ] - CustLo[ i ], Max( abs( CustHi[ i ] - CustCl[ i - 1 ] ), abs( CustCl[ i - 1 ] - CustLo[ i ] ) ) ) + CustATR[ i - 1 ] * ( P - 1 ) ) / P;
         ModATR = CustATR;
     }
     else ModATR = ATR( P );

     // Alternately you can change "( H + L ) / 2" below to "Close" or anything else that pleases you
     UprBandArr = ( CustHi + CustLo ) / 2 + ( Mult * ModATR );
     LwrBandArr = ( CustHi + CustLo ) / 2 - ( Mult * ModATR );

     UprBand = 0;
     LwrBand = 0;
     ST = 0;
     for( i = 1; i <= BarCount - 1; i++ )
     {
         if( ( UprBandArr[ i ] < UprBand[ i - 1 ] ) || ( CustCl[ i - 1 ] > UprBand[ i - 1 ] ) ) UprBand[ i ] = UprBandArr[ i ];
         else UprBand[ i ] = UprBand[ i - 1 ];

         if( ( LwrBandArr[ i ] > LwrBand[ i - 1 ] ) || ( CustCl[ i - 1 ] < LwrBand[ i - 1 ] ) ) LwrBand[ i ] = LwrBandArr[ i ];
         else LwrBand[ i ] = LwrBand[ i - 1 ];

         // Uptrend
         if( ST[ i - 1 ] == LwrBand[ i - 1 ] )
         {
             if( CustCl[ i ] > LwrBand[ i ] ) ST[ i ] = LwrBand[ i ];
             else if( CustCl[ i ] < LwrBand[ i ] ) ST[ i ] = UprBand[ i ];
             else ST[ i ] = Null;
         }

         // DownTrend
         if( ST[ i - 1 ] == UprBand[ i - 1 ] )
         {
             if( CustCl[ i ] < UprBand[ i ] ) ST[ i ] = UprBand[ i ];
             else if( CustCl[ i ] > UprBand[ i ] ) ST[ i ] = LwrBand[ i ];
             else ST[ i ] = Null;
         }
      }
      return ST;
}

_SECTION_BEGIN( "SuperTrend on Standard or HA Candles" );
     Version( 6.0 );
     SetChartOptions( 0, chartShowDates );

     CandleTyp = ParamToggle( "Choose Candle Type:", "Standard|Heikin-Ashi", 0 );
     if( CandleTyp )
     {
         HaClose = ( O + H + L + C ) / 4;
         HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
         HaHigh = Max( H, Max( HaClose, HaOpen ) );
         HaLow = Min( L, Min( HaClose, HaOpen ) );
         PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", colorDefault, styleCandle | styleNoLabel | styleNoTitle, Null, Null, 0, 0 );
     }
     else Plot( C, "Close", colorDefault, styleCandle | styleNoTitle );

     Factor = Param( "Factor", 3, 1, 10, 1 );
     Per = Param( "Period", 10, 1, 100, 1 );
     StCalc = ParamToggle( "SuperTrend Calculation Type:", "Standard|Heikin-Ashi", 0 );
     if( StCalc )
     {
         HaClose = ( O + H + L + C ) / 4;
         HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
         HaHigh = Max( H, Max( HaClose, HaOpen ) );
         HaLow = Min( L, Min( HaClose, HaOpen ) );
         SuperTrend = fSuperTrend( HaHigh, HaLow, HaClose, Factor, Per, 1 );
         ColorST = IIf( SuperTrend < HaClose, colorGreen, IIf( SuperTrend > HaClose, colorRed, colorBlack ) );
     }
     else
     {
         SuperTrend = fSuperTrend( High, Low, Close, Factor, Per, 0 );
         ColorST = IIf( SuperTrend < Close, colorGreen, IIf( SuperTrend > Close, colorRed, colorBlack ) );
     }

     Plot( SuperTrend, "SuperTrend", ColorST, styleStaircase | styleThick | styleNoRescale | styleNoTitle );
_SECTION_END();
// Code by @Loss_Lover for https://www.traderji.com/community/threads/seek-afl-coding-expertise.107862/post-1350642
MACD using Standard or HA Calculations
C-like:
function fMACD( cTyp, fPer, sPer, sigPer )
{
     if( cTyp )
     {
         HaClose = ( O + H + L + C ) / 4;
         HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
         HaHigh = Max( H, Max( HaClose, HaOpen ) );
         HaLow = Min( L, Min( HaClose, HaOpen ) );

         arr = ( HaHigh + HaLow ) / 2; // Use this
         //arr = HaClose; // Or this
         VarSet( "CustMACD", EMA( arr, fPer ) - EMA( arr, sPer ) );
         VarSet( "CustSignal", EMA( VarGet( "CustMACD" ), sigPer ) );
     }
     else
     {
         arr = ( H + L ) / 2; // Use this
         //arr = C; // Or this
         VarSet( "CustMACD", EMA( arr, fPer ) - EMA( arr, sPer ) );
         VarSet( "CustSignal", EMA( VarGet( "CustMACD" ), sigPer ) );
     }
}

_SECTION_BEGIN( "MACD using Standard or HA Calculations" );
     Version( 6.0 );

     CalcTyp = ParamToggle( "Calculation Type:", "Standard|Heikin-Ashi", 0 );
     FastPer = Param( "Fast EMA Per", 12, 1, 100, 1 );
     SlowPer = Param( "Slow EMA Per", 26, 1, 100, 1 );
     SignalPer = Param( "Signal Per", 9, 1, 100, 1 );

     fMACD( CalcTyp, FastPer, SlowPer, SignalPer );

     Plot( VarGet( "CustMACD" ), "MACD", colorOrange );
     Plot( VarGet( "CustSignal" ), "Signal", colorWhite );
     Plot( VarGet( "CustMACD" ) - VarGet( "CustSignal" ), "", colorLightBlue, styleHistogram | styleThick );
_SECTION_END();
// Code by @Loss_Lover for https://www.traderji.com/community/threads/seek-afl-coding-expertise.107862/post-1350642
When you make a million with these, pay me 10% then :)

Edit Reason: Updated a bit.
Thanks so much for sharing these. Let me try them out and I'll get back. Are these assuring me a million ? :)

I'm currently in the R&D mode - figuring out if there is a definite way to make consistent returns month on month. What are your thoughts? Would be great to hear...
 
#22
Hope this helps (Do play with the parameters):
SuperTrend on Standard or HA Candles
C-like:
function fSuperTrend( CustHi, CustLo, CustCl, Mult, P, UsingHA )
{
     // Based on AFL KB Article "How to plot a trailing stop in the Price chart" (http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/)
     if( UsingHA )
     {
         CustATR = 0;
         for( i = 1; i <= BarCount - 1; i++ ) CustATR[ i ] = ( Max( CustHi[ i ] - CustLo[ i ], Max( abs( CustHi[ i ] - CustCl[ i - 1 ] ), abs( CustCl[ i - 1 ] - CustLo[ i ] ) ) ) + CustATR[ i - 1 ] * ( P - 1 ) ) / P;
         ModATR = CustATR;
     }
     else ModATR = ATR( P );

     // Alternately you can change "( H + L ) / 2" below to "Close" or anything else that pleases you
     UprBandArr = ( CustHi + CustLo ) / 2 + ( Mult * ModATR );
     LwrBandArr = ( CustHi + CustLo ) / 2 - ( Mult * ModATR );

     UprBand = 0;
     LwrBand = 0;
     ST = 0;
     for( i = 1; i <= BarCount - 1; i++ )
     {
         if( ( UprBandArr[ i ] < UprBand[ i - 1 ] ) || ( CustCl[ i - 1 ] > UprBand[ i - 1 ] ) ) UprBand[ i ] = UprBandArr[ i ];
         else UprBand[ i ] = UprBand[ i - 1 ];

         if( ( LwrBandArr[ i ] > LwrBand[ i - 1 ] ) || ( CustCl[ i - 1 ] < LwrBand[ i - 1 ] ) ) LwrBand[ i ] = LwrBandArr[ i ];
         else LwrBand[ i ] = LwrBand[ i - 1 ];

         // Uptrend
         if( ST[ i - 1 ] == LwrBand[ i - 1 ] )
         {
             if( CustCl[ i ] > LwrBand[ i ] ) ST[ i ] = LwrBand[ i ];
             else if( CustCl[ i ] < LwrBand[ i ] ) ST[ i ] = UprBand[ i ];
             else ST[ i ] = Null;
         }

         // DownTrend
         if( ST[ i - 1 ] == UprBand[ i - 1 ] )
         {
             if( CustCl[ i ] < UprBand[ i ] ) ST[ i ] = UprBand[ i ];
             else if( CustCl[ i ] > UprBand[ i ] ) ST[ i ] = LwrBand[ i ];
             else ST[ i ] = Null;
         }
      }
      return ST;
}

_SECTION_BEGIN( "SuperTrend on Standard or HA Candles" );
     Version( 6.0 );
     SetChartOptions( 0, chartShowDates );

     CandleTyp = ParamToggle( "Choose Candle Type:", "Standard|Heikin-Ashi", 0 );
     if( CandleTyp )
     {
         HaClose = ( O + H + L + C ) / 4;
         HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
         HaHigh = Max( H, Max( HaClose, HaOpen ) );
         HaLow = Min( L, Min( HaClose, HaOpen ) );
         PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", colorDefault, styleCandle | styleNoLabel | styleNoTitle, Null, Null, 0, 0 );
     }
     else Plot( C, "Close", colorDefault, styleCandle | styleNoTitle );

     Factor = Param( "Factor", 3, 1, 10, 1 );
     Per = Param( "Period", 10, 1, 100, 1 );
     StCalc = ParamToggle( "SuperTrend Calculation Type:", "Standard|Heikin-Ashi", 0 );
     if( StCalc )
     {
         HaClose = ( O + H + L + C ) / 4;
         HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
         HaHigh = Max( H, Max( HaClose, HaOpen ) );
         HaLow = Min( L, Min( HaClose, HaOpen ) );
         SuperTrend = fSuperTrend( HaHigh, HaLow, HaClose, Factor, Per, 1 );
         ColorST = IIf( SuperTrend < HaClose, colorGreen, IIf( SuperTrend > HaClose, colorRed, colorBlack ) );
     }
     else
     {
         SuperTrend = fSuperTrend( High, Low, Close, Factor, Per, 0 );
         ColorST = IIf( SuperTrend < Close, colorGreen, IIf( SuperTrend > Close, colorRed, colorBlack ) );
     }

     Plot( SuperTrend, "SuperTrend", ColorST, styleStaircase | styleThick | styleNoRescale | styleNoTitle );
_SECTION_END();
// Code by @Loss_Lover for https://www.traderji.com/community/threads/seek-afl-coding-expertise.107862/post-1350642
MACD using Standard or HA Calculations
C-like:
function fMACD( cTyp, fPer, sPer, sigPer )
{
     if( cTyp )
     {
         HaClose = ( O + H + L + C ) / 4;
         HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
         HaHigh = Max( H, Max( HaClose, HaOpen ) );
         HaLow = Min( L, Min( HaClose, HaOpen ) );

         arr = ( HaHigh + HaLow ) / 2; // Use this
         //arr = HaClose; // Or this
         VarSet( "CustMACD", EMA( arr, fPer ) - EMA( arr, sPer ) );
         VarSet( "CustSignal", EMA( VarGet( "CustMACD" ), sigPer ) );
     }
     else
     {
         arr = ( H + L ) / 2; // Use this
         //arr = C; // Or this
         VarSet( "CustMACD", EMA( arr, fPer ) - EMA( arr, sPer ) );
         VarSet( "CustSignal", EMA( VarGet( "CustMACD" ), sigPer ) );
     }
}

_SECTION_BEGIN( "MACD using Standard or HA Calculations" );
     Version( 6.0 );

     CalcTyp = ParamToggle( "Calculation Type:", "Standard|Heikin-Ashi", 0 );
     FastPer = Param( "Fast EMA Per", 12, 1, 100, 1 );
     SlowPer = Param( "Slow EMA Per", 26, 1, 100, 1 );
     SignalPer = Param( "Signal Per", 9, 1, 100, 1 );

     fMACD( CalcTyp, FastPer, SlowPer, SignalPer );

     Plot( VarGet( "CustMACD" ), "MACD", colorOrange );
     Plot( VarGet( "CustSignal" ), "Signal", colorWhite );
     Plot( VarGet( "CustMACD" ) - VarGet( "CustSignal" ), "", colorLightBlue, styleHistogram | styleThick );
_SECTION_END();
// Code by @Loss_Lover for https://www.traderji.com/community/threads/seek-afl-coding-expertise.107862/post-1350642
When you make a million with these, pay me 10% then :)

Edit Reason: Updated a bit.
I don't see much difference between heiken ashi and standard candle plotting. Is that always the case ? Also can't we use period =1 in the supertrend?
 
#23
Also can't we use period =1 in the supertrend?
Working fine and correct at my end!

1.gif


In order to find the correct set of parameters that give maximum profits, you need to "optimize" your strategy and figure out the best set of parameters accordingly that would individually best suit an instrument which you would be trading.

I don't see much difference between heiken ashi and standard candle plotting. Is that always the case ?
Don't know, what you mean?
 
Last edited:
#24
I don't see much difference between heiken ashi and standard candle plotting. Is that always the case ? Also can't we use period =1 in the supertrend?
Ya, just imagine you paid 10-15K for developing this code, and then you discover the same thing . . . :DD

Happy :)
 
#25
Working fine and correct at my end!

View attachment 34404

In order to find the correct set of parameters that give maximum profits, you need to "optimize" your strategy and figure out the best set of parameters accordingly that would individually best suit an instrument which you would be trading.


Don't know, what you mean?
I try using candle type = standard and supertrend calculation as standard with the period = 1 and it doesn't show me any trend indicator. Strange . Works for other periods. What could be the reason?
 

Similar threads