NexusFi: Find Your Edge


Home Menu

 





PlotText result is overlapped


Discussion in Platforms and Indicators

Updated
    1. trending_up 2,191 views
    2. thumb_up 2 thanks given
    3. group 2 followers
    1. forum 2 posts
    2. attach_file 1 attachments




 
Search this Thread
  #1 (permalink)
raghavan
Bangalore + India
 
Posts: 13 since Jan 2015
Thanks Given: 3
Thanks Received: 2

I use below code

priceatbuy=0;
for( i = 0; i < BarCount; i++ )
{
if( priceatbuy == 0 && Buy[ i ] )
priceatbuy = BuyPrice[ i ];

PlotText("P" + priceatbuy, SelectedValue(BarIndex()), L[SelectedValue(BarIndex())] - ist[SelectedValue(BarIndex())], colorWhite);
if( priceatbuy > 0 && SellPrice[ i ] < (priceatbuy - (0.027 * priceatbuy/100)) )
{
Sell[ i ] = 1;
SellPrice[ i ] = (priceatbuy - (0.027 * priceatbuy/100)) ;
plotShapes(shapeDownArrow*sell, colorpink );
priceatbuy = 0;
}
else
Sell[ i ] = 0;
}

With above plotText, when i print the variable "priceatBuy", i see the text is overlapped and I am not able to see the value correctly. Is it because that SellPrice[i] and BuyPrice[i] returning some other value that Plottext doesnt understand? If I print Sell[i] or Buy[i], it prints correctly. May I know how to get this value correctly.

PFA image


Attached Thumbnails
Click image for larger version

Name:	Overlapped.PNG
Views:	272
Size:	24.7 KB
ID:	184935  
Reply With Quote

Can you help answer these questions
from other members on NexusFi?
CFTC Workforce Shrinks 24% to 15-Year Low While Predicti …
Traders Hideout
Iran War Prediction Markets: Ceasefire 16%, Ground Invas …
Prediction Markets & Event Contracts
MegaETH Proves the Crowd Right: Prediction Markets Calle …
Prediction Markets & Event Contracts
South Korea Suspends Bithumb for Six Months Over AML Fai …
Cryptocurrency
Orban Crashes to 21pct on Record Turnout -- McIlroy Drop …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
24 thanks
2026 Jlab journal
10 thanks
Lady Vols Primer: Trading Volatility Journal
7 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Trying to learn Volume and price action correlation
5 thanks
  #2 (permalink)
 prouser 
Zurich/Switzerland
 
Posts: 79 since Oct 2014

Your code is pretty much incorrect because of lacking basic skills in programming and mathematics.

Your usage of plot is not properly done. Plot shapes moves outside of loop.
And Plottext needs iteration of visible chart area only to get some signal text output on chart instead of iteration of entire barcount. Latter one is rather overkill. And there should be an additional condition inside such loop where there is text output at signal occurrence only. Another major mistake in your code is that you use array functions inside of barcount loop. That is similar horror as fingernails on a blackboard. In short it's a no go.

So better consult the AB documention first -> i.e. "Coding mistakes"
Also search "Understanding how AFL works".

Here is corrected SL loop

 
Code
percent = 2.7; // percent SL
delay = 1; // buy entry bar delay

Buycond = Cross( MACD(), Signal() ); // buy when macd crosses above of signal line
Buy = Ref( Buycond, -delay );
BuyPrice = Open;

Sell = 0;
priceatbuy = 0;
for( i = delay; i < BarCount; i++ ) {
    // setting SL line
    if( priceatbuy == 0 && Buy[ i ] )
        priceatbuy = BuyPrice[ i ] * ( 1 - percent / 100 );  
    else {
        Buy[ i ] = 0;
    }
    // if SL is reached exit and reset
    Sellsignal = L[ i ] < priceatbuy;
    if( priceatbuy > 0 && Sellsignal ) {
        Sell[ i ] = 1;
        SellPrice[ i ] = Min( O[ i ], priceatbuy );       
        priceatbuy = 0;
    } else
        Sell[ i ] = 0;
}
Keep in mind that upper loop contains SL exit only but no profit exit.
So you have to add some profit exit too otherwise you always exit at loss.

As seen here


Reply With Quote
  #3 (permalink)
 prouser 
Zurich/Switzerland
 
Posts: 79 since Oct 2014


Here is code including plot of shapes, text and SL line.
Again, the loop contains SL exit only. So you need to add some profit exit condition too.


 
Code
// fix of SL plot & text
// by prouser
// source https://nexusfi.com/amibroker/36055-plottext-result-overlapped.html

percent = 2.7; // percent SL
delay = 1; // buy entry bar delay

Buycond = Cross( MACD(), Signal() ); // buy when macd crosses above of signal line
Buy = Ref( Buycond, -delay );
BuyPrice = Open;

Sell = 0;
SLlong = Null;
SLLineLong = Null; // array
for( i = delay; i < BarCount; i++ ) {
    // setting long SL line
    if( IsNull( SLlong ) && Buy[ i ] )
        SLlong = BuyPrice[ i ] * ( 1 - percent / 100 );  
    else { 
        Buy[ i ] = 0;
    }
    // no shape buy signal if SL not hit
    if( NOT IsNull( SLlong ) && BuyCond[ i ] )
        Buycond[ i ] = 0; 
    // if SL is reached exit and reset
    Sellsignal = L[ i ] < SLlong;
    if( NOT IsNull( SLlong ) && Sellsignal ) {
        Sell[ i ] = 1;
        SellPrice[ i ] = Min( O[ i ], SLlong );       
        SLlong = Null;
    } else
        Sell[ i ] = 0;
    //
    // for plotting SL line array is required
    SLLineLong[i] = SLlong;
}

// Chart pane section
if( Status( "action" ) == actionIndicator ) {
    SetChartOptions( 0, chartShowDates | chartShowArrows | chartWrapTitle );
    // Plot price
    Plot( C, "", colorDefault, GetPriceStyle() );
    // Plot SL line
    Plot( SLLineLong, "\nSL long", colorRed, styleLine | styleNoRescale );
    //
    // Plot Shapes
    dist = -12;
    PlotShapes( shapeUpArrow * Buycond, colorGreen, 0, L, dist );
    PlotShapes( shapeSmallCircle * Buy, colorGreen, 0, BuyPrice, 0 );
    PlotShapes( shapeDownArrow * Sell, colorPink, 0, H, dist );
    PlotShapes( shapeSmallCircle * Sell, colorPink, 0, SellPrice, 0 );
    //
    // Plot signal text
    fnt = "Arial";
    fntsize = 8;
    txtdist = 25 - dist;
    bi = Barindex();
    fvb = FirstVisiblevalue( bi );
    lvb = LastVisiblevalue( bi );
    for( i = fvb; i <= lvb; i++ ) { // iterate through visible chart area only
        if( Buy[i] )  // plot text at signal occurences only
            PlotTextSetFont( StrFormat( "BP@\n%g", BuyPrice[ i ] ), fnt, fntsize, i, L[ i ], colorGreen, colorDefault, -txtdist + fntsize );
        if( Sell[i] )
            PlotTextSetFont( StrFormat( "SP@\n%g", SellPrice[ i ] ), fnt, fntsize, i, H[ i ], colorPink, colorDefault, txtdist );
    }
    //
    _N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%), Vol %g {{VALUES}}",
                           O, H, L, C, SelectedValue( ROC( C, 1 ) ), V ) );
    //
    SetBarsRequired( 10000, 10000 );
}


EDIT: some code fix added


Reply With Quote




Last Updated on June 17, 2015


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts