Simple Coding Help - No Promise.

Nehal_s143

Well-Known Member
need afl code for maximun number of trades per day
Code:
Buy1  =C>O;
Sell1 =C<O;
Short1=C<O;
Cover1=C>O;

Buy1=ExRem(Buy1,Sell1);
Short1=ExRem(Short1,Cover1);
Sell1=ExRem(Sell1,Buy1);
Cover1=ExRem(Cover1,Short1);

PlotShapes(Buy1*shapeUpArrow,colorBlue,0,L,-33);
PlotShapes(Short1*shapeDownArrow,colorRed,0,H,-33);
I think no other code will produce more traders then above one....
 
The first code can be written much shorter (included shapes at last bar only and toggle option showing historical ones).

Code:
_SECTION_BEGIN("IDNRs");
// mods by trash 
// http://www.traderji.com/amibroker/90119-simple-coding-help-no-promise-348.html#post1086940
ShowINRs = ParamToggle( "Inside Bar (NR1,NR4,NR7)", "OFF|ON", 1 );
Showhistory = ParamToggle( "Show historical inside bars", "OFF|ON", 0);

R = H - L;
NR7 = R == LLV( R, 7 );
NR4 = R == LLV( R, 4 ) AND NOT NR7;

IDNR7 = Inside() * NR7;
IDNR4 = Inside() * NR4;
ID = Inside();

MarkerIDNR7 = MarkerIDNR4 = shapeStar;

Marker7 = shapeDigit7;
NR7Color = colorGreen;

Marker4 = shapeDigit4;
NR4Color = colorOrange;

MarkerID = shapeCircle;
IDColor = colorRed;

IDNR7Color = colorBrightGreen;
IDNR4Color = colorLightOrange;

Plot( C, "", colorDefault, GetPriceStyle() );

bi = BarIndex();
if( Showhistory )
    lastbicond = 1;
else
    lastbicond = bi == LastValue( bi );

if( ShowINRs ) {
    PlotShapes( IIf( IDNR7 AND lastbicond, MarkerIDNR7, shapeNone ), IDNR7Color, 0, H, MarkerDist = 15 );
    PlotShapes( IIf( IDNR4 AND NOT IDNR7 AND lastbicond, MarkerIDNR4, shapeNone ), IDNR4Color, 0, H, MarkerDist );
    PlotShapes( IIf( NR7 AND NOT ID, Marker7 AND lastbicond, shapeNone ), NR7Color, 0, L, -MarkerDist  );
    PlotShapes( IIf( NR4 AND NOT NR7 AND NOT ID AND lastbicond, Marker4, shapeNone ), NR4Color, 0, L, -MarkerDist );
    PlotShapes( IIf( ID AND NOT NR7 AND NOT NR4 AND lastbicond, MarkerID, shapeNone ), IDColor, 0, H, MarkerDist );
}
_SECTION_END();
Dear Sir, Thanks a lot for ur kind help and much better and compact coding. However, it somehow doesn't show the only last shapes. If showhistory is OFF, no shape appears on chart. Please look at it again. .....[Edit] I get it. I checked again. You used barindex.. so naturally it will appear and disappear on the latest bar. I tried to find the latest one on offline chart.. so I wrote about no shape appear. That is fine now. Thanks, sir.
In this code you very nicely avoided the "for loop" by using inside function. I am sure you can suggest a way to avoid "for loop" in DeMark trend code of my second afl code help.
I am very grateful to you for taking your valuable time and providing help. Thanks again.
 
Last edited:
Hi,
in this link www.amibroker.com/docs/MTFIndicators.html

first indicator MTMACD not showing me exact buy/sell arrow as it was mentioned in amibroker site.

Code:
Count = 0; 
result = 0; 

for( i = 10; i <= 300; i++ ) 
{ 
   TimeFrameSet( i * in1Minute ); 
   m = MACD(12, 26 ); 
   TimeFrameRestore(); 
   m = IIf( TimeFrameExpand( m, i * in1Minute ) > 0, 1, -1 ); 
   result = result + m; 
   Count++; 
} 

AddToComposite( result / Count, "~MACD"+Name(), "X" ); 
Buy=1;

x = Foreign("~MACD"+Name(),"C"); 

Plot( x, "MTMACD", colorBlue , styleThick ) ; 

PlotGrid(0.5 ); 
PlotGrid(-0.5);
PLease Help me
 

Nehal_s143

Well-Known Member
Hi,
in this link www.amibroker.com/docs/MTFIndicators.html

first indicator MTMACD not showing me exact buy/sell arrow as it was mentioned in amibroker site.

Code:
Count = 0; 
result = 0; 

for( i = 10; i <= 300; i++ ) 
{ 
   TimeFrameSet( i * in1Minute ); 
   m = MACD(12, 26 ); 
   TimeFrameRestore(); 
   m = IIf( TimeFrameExpand( m, i * in1Minute ) > 0, 1, -1 ); 
   result = result + m; 
   Count++; 
} 

AddToComposite( result / Count, "~MACD"+Name(), "X" ); 
Buy=1;

x = Foreign("~MACD"+Name(),"C"); 

Plot( x, "MTMACD", colorBlue , styleThick ) ; 

PlotGrid(0.5 ); 
PlotGrid(-0.5);
PLease Help me
what is sell condition ? after defining sell condition add this code at bottom to get buy sell signals

Code:
Buy1  =buy;
Sell1 =sell;
Short1=sell;
Cover1=buy;

Buy1=ExRem(Buy1,Sell1);
Short1=ExRem(Short1,Cover1);
Sell1=ExRem(Sell1,Buy1);
Cover1=ExRem(Cover1,Short1);

PlotShapes(Buy1*shapeUpArrow,colorBlue,0,L,-33);
PlotShapes(Short1*shapeDownArrow,colorRed,0,H,-33);
 
Please help me change this afl code for trendline. this afl draws trendline but it hangs the amibroker as it is very memory intensive because of For loop used in it. I am sure there must be a way to do it differently without using "for loop". The afl is as follows:

_SECTION_BEGIN("Demark Trend lines");
pc = Param("Perbent", 0.05, 0.01, 10 );
percent = pc * 1; /* Adjust this percent as necessary, I use .01 because FOREX is a 0.0000 number */
ShowTrLines = ParamToggle("Show TrLines", "No|Yes", 1);
firstpointL = 2;
firstpointH = 2;
y0=LastValue(Trough(L,percent,firstpointL));
y1=LastValue(Trough(Ref(L,-1),percent,1));

for( i = 1; i < BarCount AND y0 >= y1; i++ )
{

firstpointL++;
y0=LastValue(Trough(L,percent,firstpointL));
}
if(ShowTrLines)
{
x0=BarCount - 1 - LastValue(TroughBars(L,percent,firstpointL));
x1=BarCount - 1 - LastValue(TroughBars(Ref(L,-1),percent,1));
LineL = LineArray( x0, y0, x1, y1, 1 );
Plot( LineL, " Support", colorBrown,1 +8 + 4096);

yt0=LastValue(Peak(H,percent,firstpointH));
yt1=LastValue(Peak(Ref(H,-1),percent,1));

for(i = 1; i < BarCount AND yt0 <= yt1; i++ )
{

firstpointH++;
yt0=LastValue(Peak(H,percent,firstpointH));
}
xt0=BarCount - 1 - LastValue(PeakBars(H,percent,firstpointH));
xt1=BarCount - 1 - LastValue(PeakBars(Ref(H,-1),percent,1));
LineH = LineArray( xt0, yt0, xt1, yt1, 1 );

Plot( LineH, "Resistance", colorGreen,1 + 8 + 4096);
}
_SECTION_END();
I will be very grateful.
 
what is sell condition ? after defining sell condition add this code at bottom to get buy sell signals

Code:
Buy1  =buy;
Sell1 =sell;
Short1=sell;
Cover1=buy;

Buy1=ExRem(Buy1,Sell1);
Short1=ExRem(Short1,Cover1);
Sell1=ExRem(Sell1,Buy1);
Cover1=ExRem(Cover1,Short1);

PlotShapes(Buy1*shapeUpArrow,colorBlue,0,L,-33);
PlotShapes(Short1*shapeDownArrow,colorRed,0,H,-33);


Hi,
I put this code and it's not working.. it's both for buy and sell .. you can see on that link.. mtmacd indicator that's the first indicator.

Code:
_SECTION_BEGIN("Unnamed 61");
Count = 0; 
result = 0; 

for( i = 10; i <= 300; i++ ) 
{ 
   TimeFrameSet( i * in1Minute ); 
   m = MACD(12, 26 ); 
   TimeFrameRestore(); 
   m = IIf( TimeFrameExpand( m, i * in1Minute ) > 0, 1, -1 ); 
   result = result + m; 
   Count++; 
} 

AddToComposite( result / Count, "~MACD"+Name(), "X" ); 
Buy=1;

x = Foreign("~MACD"+Name(),"C"); 

Plot( x, "MTMACD", colorBlue , styleThick ) ; 

PlotGrid(0.5 ); 
PlotGrid(-0.5);

Buy1  =Buy;
Sell1 =Sell;
Short1=Sell;
Cover1=Buy;

Buy1=ExRem(Buy1,Sell1);
Short1=ExRem(Short1,Cover1);
Sell1=ExRem(Sell1,Buy1);
Cover1=ExRem(Cover1,Short1);

PlotShapes(Buy1*shapeUpArrow,colorBlue,0,L,-33);
PlotShapes(Short1*shapeDownArrow,colorRed,0,H,-33);
_SECTION_END();
you can see on that amibroker link.. mtmacd pic have both signal..
it's not working same way..

Thanks for answering.
 

trash

Well-Known Member
Hi,
I put this code and it's not working.. it's both for buy and sell .. you can see on that link.. mtmacd indicator that's the first indicator.

Code:
_SECTION_BEGIN("Unnamed 61");
Count = 0; 
result = 0; 

for( i = 10; i <= 300; i++ ) 
{ 
   TimeFrameSet( i * in1Minute ); 
   m = MACD(12, 26 ); 
   TimeFrameRestore(); 
   m = IIf( TimeFrameExpand( m, i * in1Minute ) > 0, 1, -1 ); 
   result = result + m; 
   Count++; 
} 

AddToComposite( result / Count, "~MACD"+Name(), "X" ); 
Buy=1;

x = Foreign("~MACD"+Name(),"C"); 

Plot( x, "MTMACD", colorBlue , styleThick ) ; 

PlotGrid(0.5 ); 
PlotGrid(-0.5);

Buy1  =Buy;
Sell1 =Sell;
Short1=Sell;
Cover1=Buy;

Buy1=ExRem(Buy1,Sell1);
Short1=ExRem(Short1,Cover1);
Sell1=ExRem(Sell1,Buy1);
Cover1=ExRem(Cover1,Short1);

PlotShapes(Buy1*shapeUpArrow,colorBlue,0,L,-33);
PlotShapes(Short1*shapeDownArrow,colorRed,0,H,-33);
_SECTION_END();
you can see on that amibroker link.. mtmacd pic have both signal..
it's not working same way..

Thanks for answering.
Once again, do not use code that you do not understand yourself.
Better read what is written on the Amibroker page.

In the figure below we can see daily chart of ORCL (upper pane) with buy and sell arrows generated using multiple time-frame MACD and multiple time-frame MACD indicator plotted in the bottom pane. Buy signal is generated when MTMACD crosses above -0.5 line and sell is generated when MTMACD drops below +0.5 line. Note the MTMACD saturation levels at -1 and +1 which indicate bars when all timeframes voted for sell or for buy.
So YOU have to add buy and sell signal yourself. They are not added to the code on that page. But it is explained an example.

Code:
// http://www.amibroker.com/docs/MTFIndicators.html
// First 'Scan' on 1-minute time frame
// Then click 'Backtest'
// Then right-click result list and choose "Show arrows for actual trades"
// mod. by trash
// http://www.traderji.com/amibroker/90119-simple-coding-help-no-promise-349.html#post1087794

SetOption( "FuturesMode", False );
// etc.
SetPositionSize( 1, spsShares );

// calling stored custom MACD line
// It is dependent on the Symbol is was created with!
nm = Name(); // insert the name of the symbol the composite was created with
x = StaticVarGet( "~MACD" + nm ); //Foreign( "~MACD" + Name(), "C" );
// create buy sell signal
Buy = Cross( x, -0.5 ); // buy when x crosses ABOVE -0.5
Sell = Cross( 0.5, x ); // sell when x crosses BELOW 0.5
Short = Cover = 0;

// create custom MACD composite via Scan
if( Status( "action" ) == actionScan ) {
    Count = 0;
    result = 0;
    for( i = 10; i <= 300; i++ ) {
        TimeFrameSet( i * in1Minute );
        m = MACD( 12, 26 );
        TimeFrameRestore();
        m = IIf( TimeFrameExpand( m, i * in1Minute ) > 0, 1, -1 );
        result += m;
        Count++;
    }

    // storing result to static variable. 
    // It is dependent on the Symbol is was created with!
    StaticVarSet( "~MACD" + Name(), result / Count, persistent = true );
    //AddToComposite( result / Count, "~MACD" + Name(), "X" );
    Buy = 0;
}

// for insertion to separate indicator pane, NOT for price pane! 
if( Status( "action" ) == actionIndicator ) {
    // plot stored MTMACD
    Plot( x, "MTMACD", colorBlue, styleThick );

    // two grid lines
    PlotGrid( 0.5, colorRed );
    PlotGrid( -0.5, colorGreen );

    // removing excessive signals in indicator pane
    Buy = ExRem( Buy, Sell );
    Sell = ExRem( Sell, Buy );
    Short = ExRem( Short, Cover );
    Cover = ExRem( Cover, Short );

    // plotting buy and sell shapes
    PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow,
                IIf( Buy, colorGreen, colorRed ), layer = 0,
                y = x, dist = -12 );
    PlotShapes( Buy * shapeHollowUpArrow + Sell * shapeHollowDownArrow,
                colorLightGrey, layer, y, dist );
}
Again don't use code that you don't understand what it is doing. Study the code and figure out what it is doing instead of blindly using it.
 
Last edited:
Once again, do not use code that you do not understand yourself.
Better read what is written on the Amibroker page.



So YOU have to add buy and sell signal yourself. They are not added to the code on that page. But it is explained an example.

Code:
// http://www.amibroker.com/docs/MTFIndicators.html
// First 'Scan' on 1-minute time frame
// Then click 'Backtest'
// Then right-click result list and choose "Show arrows for actual trades"
// mod. by trash
// http://www.traderji.com/amibroker/90119-simple-coding-help-no-promise-349.html#post1087794

SetOption( "FuturesMode", False );
// etc.
SetPositionSize( 1, spsShares );

// calling stored custom MACD line
// It is dependent on the Symbol is was created with!
nm = Name(); // insert the name of the symbol the composite was created with
x = StaticVarGet( "~MACD" + nm ); //Foreign( "~MACD" + Name(), "C" );
// create buy sell signal
Buy = Cross( x, -0.5 ); // buy when x crosses ABOVE -0.5
Sell = Cross( 0.5, x ); // sell when x crosses BELOW 0.5
Short = Cover = 0;

// create custom MACD composite via Scan
if( Status( "action" ) == actionScan ) {
    Count = 0;
    result = 0;
    for( i = 10; i <= 300; i++ ) {
        TimeFrameSet( i * in1Minute );
        m = MACD( 12, 26 );
        TimeFrameRestore();
        m = IIf( TimeFrameExpand( m, i * in1Minute ) > 0, 1, -1 );
        result += m;
        Count++;
    }

    // storing result to static variable. 
    // It is dependent on the Symbol is was created with!
    StaticVarSet( "~MACD" + Name(), result / Count, persistent = true );
    //AddToComposite( result / Count, "~MACD" + Name(), "X" );
    Buy = 0;
}

// for insertion to separate indicator pane, NOT for price pane! 
if( Status( "action" ) == actionIndicator ) {
    // plot stored MTMACD
    Plot( x, "MTMACD", colorBlue, styleThick );

    // two grid lines
    PlotGrid( 0.5, colorRed );
    PlotGrid( -0.5, colorGreen );

    // removing excessive signals in indicator pane
    Buy = ExRem( Buy, Sell );
    Sell = ExRem( Sell, Buy );
    Short = ExRem( Short, Cover );
    Cover = ExRem( Cover, Short );

    // plotting buy and sell shapes
    PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow,
                IIf( Buy, colorGreen, colorRed ), layer = 0,
                y = x, dist = -12 );
    PlotShapes( Buy * shapeHollowUpArrow + Sell * shapeHollowDownArrow,
                colorLightGrey, layer, y, dist );
}
Again don't use code that you don't understand what it is doing. Study the code and figure out what it is doing instead of blindly using it.



After putting your code, it's showing no results..
i've 3 years historical data.
yes 1 min. setting, click backtest showing nothing. same for explore.
Thanks for answering..
 

trash

Well-Known Member
After putting your code, it's showing no results..
i've 3 years historical data.
yes 1 min. setting, click backtest showing nothing. same for explore.
Thanks for answering..

The code works!
Read what is written in the code and on AB website. Read carefully and study what the code is doing!

End of conversation....
 

Similar threads