Buy/Signals from Amibroker not reaching Pi (using Pi bridge) unless the chart in Amibroker is the active window

I have 4 AFLs which generate buy/sell signals and connect to Pi using Pi bridge. Signals are reaching Pi from all the AFLs except one. For the problematic AFL, unless the Chart on Amibroker is the active window and I am watching it, the signal doesn’t go to Pi. Note that this doesn’t happen with other 3 AFLs. For the working AFLs, I may not be even watching Amibroker, but if a signal is generated it goes through to Pi.

Note that for all the 4 charts, the AFLs are for Sheet 1 and sheet 1 is active for the 4 charts. What could be missing here?

1 Like

yes you can check the below code with bar reply 

SetChartOptions(0,chartShowArrows|chartShowDates);
SetPositionSize(75,spsShares);
SetBarsRequired( -2, -2 ); 
_SECTION_BEGIN("EMA");
Period1 = 340;
Period2 = 280;
Period3 = 180;
Period1 = Optimize("Period1", Period1, 20, 400, 20);
Period2 = Optimize("Period2", Period2, 20, 400, 20);
Period3 = Optimize("Period3", Period3, 20, 400, 20);
MA1 = EMA( C, Period1 );
MA2 = EMA( C, Period2 );
MA3 = EMA( C, Period3 );
MA1[BarCount-1]=Null;
MA2[BarCount-1]=Null;
MA3[BarCount-1]=Null;
Plot( EMA( C, Period1 ), _DEFAULT_NAME(), ParamColor( "Color1", colorBlue ), ParamStyle("Style", styleDashed) ); 
Plot( EMA( C, Period2 ), _DEFAULT_NAME(), ParamColor( "Color2", colorLightBlue ), ParamStyle("Style", styleDashed) ); 
Plot( EMA( C, Period3 ), _DEFAULT_NAME(), ParamColor( "Color3", colorAqua ), ParamStyle("Style", styleDashed) ); 
_SECTION_END();
_SECTION_BEGIN("MACD");
r1 = 12;
r2 = 26;
r3 = 9;
//r1 = Optimize("Fast Avg", r1, 4, 26, 2);  //Param( "Fast avg", 12, 2, 200, 1 );
//r2 = Optimize("Slow Avg", r2, 10, 40, 2);  //Param( "Slow avg", 26, 2, 200, 1 );
//r3 = Optimize("Signal Avg", r3, 4, 28, 2);  //Param( "Signal avg", 9, 2, 200, 1 );
ml = MACD(r1, r2);
sl = Signal(r1, r2, r3);
ml_sl = ml - sl;
_SECTION_END();
Buy = C > MA1 AND 
      ml_sl > 0 AND 
      MA1 > MA2 AND 
      MA2 > MA3;
Sell = C < MA1 AND ml_sl < 0;
Short = C < MA1 AND 
        ml_sl < 0 AND 
        MA1 <  MA2 AND 
        MA2 < MA3;
Cover = C > MA1 AND ml_sl > 0;
// ----- This code is for non-repainting
// ----- The current bar doesn’t have indicator plotted and the Buy/Signal will apppear only once the candle closes and the condition matches in realtime thus avoiding current bar repainting.
Buy[BarCount-1]=Null;
Sell[BarCount-1]=Null;
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
short=ExRem(short,cover);
cover=ExRem(cover,short);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);                      
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(IIf(Sell, shapeStar, shapeNone),colorGold, 0, L, Offset=-15); 
PlotShapes(IIf(Cover, shapeStar, shapeNone),colorGold, 0,L, Offset=-15); 
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g {{VALUES}}", O, H, L, C ) + EncodeColor(colorAqua) + "\nPeriod1, Period2, Period3=" + Period1 + ", " + Period2 + ", " + Period3);
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
function SayNotTooOften( text, Minperiod ) 
{ 
   elapsed=GetPerformanceCounter()/1000; 
   Lastelapsed = Nz( StaticVarGet("lastsaytime_Nifty_MA") ); 
  if( elapsed - Lastelapsed > Minperiod ) 
   { 
     StaticVarSet("lastsaytime_Nifty_MA", elapsed ); 
     Say( text , False); 
   } 
} 
if (Buy[BarCount-2]==True OR Cover[BarCount-2]==True)
{
    if (Buy[BarCount-2]==True AND NOT Cover[BarCount-2]==True)
    {
        SayNotTooOften("Buy Nifty MA", Interval());
    }
    else if (NOT Buy[BarCount-2]==True AND Cover[BarCount-2]==True)
    {
        SayNotTooOften("Cover Nifty MA", Interval());
    }
    else
    {
        SayNotTooOften("Cover and Buy Nifty MA", Interval());
    }
}
if (short[BarCount-2]==True OR Sell[BarCount-2]==True)
{
    if(short[BarCount-2]==True AND NOT Sell[BarCount-2]==True)
    {
        SayNotTooOften("Short Nifty MA", Interval());
    }
    else if(NOT short[BarCount-2]==True AND Sell[BarCount-2]==True)
    {
        SayNotTooOften("Sell Nifty MA", Interval());
    }
    else
    {
        SayNotTooOften("Sell and Short Nifty MA", Interval());
    }
}
_SECTION_BEGIN("Pi Bridge");
Lots = 2;
//Parameter to set the trading month of the symbols to place orders on Pi
_N(firstmon = "16MAYFUT");
_N(secmon = "16JUNFUT");
_N(thirdmon = "16JULFUT");
_N(symbol = Name());
//Replace Symbol names with format supported by Pi bridge
if(StrFind(Name(),"-1M")) 
{
     symbol =  StrReplace(symbol,"-1M", secmon);
}
else if(StrFind( Name(),"-2M")) 
{
     symbol =  StrReplace(symbol,"-2M", secmon);
}
else if(StrFind( Name(),"-3M")) 
{
     symbol =  StrReplace(symbol,"-3M",thirdmon);
}
//Create Object of Neobridge 
brd=Null;
if(IsNull(brd))
{
      brd=CreateStaticObject("pibridge.Bridge");
}
if(!brd.GetConnectionStatus())  brd.Reconnect();
Checkone=Nz(StaticVarGet("Check_NIFTY_MA"),0);
Checktwo = LastValue(BarIndex(),1);
printf("\nLastValue(BarIndex(),1)="+LastValue(BarIndex(),1));
printf("\nCheckOne="+CheckOne+"\nCheckTwo="+Checktwo+"\n\nLastValue(Ref(Buy,-1))="+LastValue(Ref(Buy,-1))+"\nLastValue(Ref(Cover,-1))="+LastValue(Ref(Cover,-1))+"\nLastValue(Ref(Sell,-1))="+LastValue(Ref(Sell,-1))+"\nLastValue(Ref(Short,-1))="+LastValue(Ref(Short,-1)));
if((LastValue(Ref(Buy,-1)) OR LastValue(Ref(Cover,-1))) AND (CheckOne != CheckTwo))
{
    price = LastValue(Ref(C,-1),1);
    if(LastValue(Ref(Buy,-1)) AND LastValue(Ref(Cover,-1)))
    {
        printf("\n"+"Inside Buy and Cover: Price="+price);
        brd.PlaceOrder("NFO", symbol, "NIFTY", "MA - Nifty", 1, Lots*2, 0, price, 0, "L", "NRML", "DAXXXX","DAY");
        //brd.PlaceOrder("NFO", symbol, "NIFTY", "MA - Nifty", 1, Lots*2, 0, price, 0, "MKT", "NRML", "DAXXXX","DAY");
    }
    else
    {
    printf("\n"+"Inside Buy OR Cover: Price="+price);
    brd.PlaceOrder("NFO", symbol, "NIFTY", "MA - Nifty", 1, Lots, 0, price, 0, "L", "NRML", "DAXXXX","DAY");
    //brd.PlaceOrder("NFO", symbol, "NIFTY", "MA - Nifty", 1, Lots, 0, price, 0, "MKT", "NRML", "DAXXXX","DAY");
    }
    StaticVarSet("Check_NIFTY_MA",CheckTwo);
}
else if((LastValue(Ref(Sell,-1)) OR LastValue(Ref(Short,-1))) AND (CheckOne != CheckTwo))
{
    price = LastValue(Ref(C,-1),1);
    if(LastValue(Ref(Sell,-1)) AND LastValue(Ref(Short,-1)))
    {
        printf("\n"+"Inside Sell AND Short: Price="+price);
        brd.PlaceOrder("NFO", symbol, "NIFTY", "MA - Nifty", 2, Lots*2, 0, price, 0, "L", "NRML", "DAXXXX","DAY");
        //brd.PlaceOrder("NFO", symbol, "NIFTY", "MA - Nifty", 2, Lots*2, 0, price, 0, "MKT", "NRML", "DAXXXX","DAY");
    }
    else
    {
        printf("\n"+"Inside Sell OR Short: Price="+price);
        brd.PlaceOrder("NFO", symbol, "NIFTY", "MA - Nifty", 2, Lots, 0, price, 0, "L", "NRML", "DAXXXX","DAY");
        //brd.PlaceOrder("NFO", symbol, "NIFTY", "MA - Nifty", 2, Lots, 0, price, 0, "MKT", "NRML", "DAXXXX","DAY");
    }
    StaticVarSet("Check_NIFTY_MA",CheckTwo);
}
_SECTION_END();

Amibroker by default refreshes only active charts. To overcome this issue, an AFL function "RequestTimedRefresh" is provided.

RequestTimedRefresh( interval, onlyvisible = True )

Interval = Refresh time interval 

OnlyVisible = True - default value - Will refresh only active charts

                   = False - All charts will get refreshed with specified interval.

Example : 

RequestTimedRefresh(5,0);

Every 5 seconds, all the charts active or inactive will get refreshed.

Hopefully this should resolve your issue.

Warm Regards

Vivith

1 Like

Note that if I do bar replay function in Ami, it works perfectly fine. It is only when I am looking at some other chart in a different tab in Ami, this problem comes up. Here is the complete AFL (in case someone could assist):

SetChartOptions(0,chartShowArrows|chartShowDates);
SetPositionSize(75,spsShares);
SetBarsRequired( -2, -2 );
_SECTION_BEGIN(“EMA”);
Period1 = 340;
Period2 = 280;
Period3 = 180;
Period1 = Optimize(“Period1”, Period1, 20, 400, 20);
Period2 = Optimize(“Period2”, Period2, 20, 400, 20);
Period3 = Optimize(“Period3”, Period3, 20, 400, 20);
MA1 = EMA( C, Period1 );
MA2 = EMA( C, Period2 );
MA3 = EMA( C, Period3 );
MA1[BarCount-1]=Null;
MA2[BarCount-1]=Null;
MA3[BarCount-1]=Null;
Plot( EMA( C, Period1 ), _DEFAULT_NAME(), ParamColor( “Color1”, colorBlue ), ParamStyle(“Style”, styleDashed) );
Plot( EMA( C, Period2 ), _DEFAULT_NAME(), ParamColor( “Color2”, colorLightBlue ), ParamStyle(“Style”, styleDashed) );
Plot( EMA( C, Period3 ), _DEFAULT_NAME(), ParamColor( “Color3”, colorAqua ), ParamStyle(“Style”, styleDashed) );
_SECTION_END();
_SECTION_BEGIN(“MACD”);
r1 = 12;
r2 = 26;
r3 = 9;
//r1 = Optimize(“Fast Avg”, r1, 4, 26, 2); //Param( “Fast avg”, 12, 2, 200, 1 );
//r2 = Optimize(“Slow Avg”, r2, 10, 40, 2); //Param( “Slow avg”, 26, 2, 200, 1 );
//r3 = Optimize(“Signal Avg”, r3, 4, 28, 2); //Param( “Signal avg”, 9, 2, 200, 1 );
ml = MACD(r1, r2);
sl = Signal(r1, r2, r3);
ml_sl = ml - sl;
_SECTION_END();
Buy = C > MA1 AND
ml_sl > 0 AND
MA1 > MA2 AND
MA2 > MA3;
Sell = C < MA1 AND ml_sl < 0;
Short = C < MA1 AND
ml_sl < 0 AND
MA1 < MA2 AND
MA2 < MA3;
Cover = C > MA1 AND ml_sl > 0;
// ----- This code is for non-repainting
// ----- The current bar doesn’t have indicator plotted and the Buy/Signal will apppear only once the candle closes and the condition matches in realtime thus avoiding current bar repainting.
Buy[BarCount-1]=Null;
Sell[BarCount-1]=Null;
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
short=ExRem(short,cover);
cover=ExRem(cover,short);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(IIf(Sell, shapeStar, shapeNone),colorGold, 0, L, Offset=-15);
PlotShapes(IIf(Cover, shapeStar, shapeNone),colorGold, 0,L, Offset=-15);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g {{VALUES}}", O, H, L, C ) + EncodeColor(colorAqua) + “\nPeriod1, Period2, Period3=” + Period1 + “, " + Period2 + “, " + Period3);
Plot( C, “Close”, ParamColor(“Color”, colorDefault ), styleNoTitle | ParamStyle(“Style”) | GetPriceStyle() );
function SayNotTooOften( text, Minperiod )
{
elapsed=GetPerformanceCounter()/1000;
Lastelapsed = Nz( StaticVarGet(“lastsaytime_Nifty_MA”) );
if( elapsed - Lastelapsed > Minperiod )
{
StaticVarSet(“lastsaytime_Nifty_MA”, elapsed );
Say( text , False);
}
}
if (Buy[BarCount-2]==True OR Cover[BarCount-2]==True)
{
if (Buy[BarCount-2]==True AND NOT Cover[BarCount-2]==True)
{
SayNotTooOften(“Buy Nifty MA”, Interval());
}
else if (NOT Buy[BarCount-2]==True AND Cover[BarCount-2]==True)
{
SayNotTooOften(“Cover Nifty MA”, Interval());
}
else
{
SayNotTooOften(“Cover and Buy Nifty MA”, Interval());
}
}
if (short[BarCount-2]==True OR Sell[BarCount-2]==True)
{
if(short[BarCount-2]==True AND NOT Sell[BarCount-2]==True)
{
SayNotTooOften(“Short Nifty MA”, Interval());
}
else if(NOT short[BarCount-2]==True AND Sell[BarCount-2]==True)
{
SayNotTooOften(“Sell Nifty MA”, Interval());
}
else
{
SayNotTooOften(“Sell and Short Nifty MA”, Interval());
}
}
_SECTION_BEGIN(“Pi Bridge”);
Lots = 2;
//Parameter to set the trading month of the symbols to place orders on Pi
_N(firstmon = “16MAYFUT”);
_N(secmon = “16JUNFUT”);
_N(thirdmon = “16JULFUT”);
_N(symbol = Name());
//Replace Symbol names with format supported by Pi bridge
if(StrFind(Name(),”-1M”))
{
symbol = StrReplace(symbol,"-1M", secmon);
}
else if(StrFind( Name(),"-2M"))
{
symbol = StrReplace(symbol,"-2M", secmon);
}
else if(StrFind( Name(),"-3M"))
{
symbol = StrReplace(symbol,"-3M",thirdmon);
}
//Create Object of Neobridge
brd=Null;
if(IsNull(brd))
{
brd=CreateStaticObject(“pibridge.Bridge”);
}
if(!brd.GetConnectionStatus()) brd.Reconnect();
Checkone=Nz(StaticVarGet(“Check_NIFTY_MA”),0);
Checktwo = LastValue(BarIndex(),1);
printf("\nLastValue(BarIndex(),1)="+LastValue(BarIndex(),1));
printf("\nCheckOne="+CheckOne+"\nCheckTwo="+Checktwo+"\n\nLastValue(Ref(Buy,-1))="+LastValue(Ref(Buy,-1))+"\nLastValue(Ref(Cover,-1))="+LastValue(Ref(Cover,-1))+"\nLastValue(Ref(Sell,-1))="+LastValue(Ref(Sell,-1))+"\nLastValue(Ref(Short,-1))="+LastValue(Ref(Short,-1)));
if((LastValue(Ref(Buy,-1)) OR LastValue(Ref(Cover,-1))) AND (CheckOne != CheckTwo))
{
price = LastValue(Ref(C,-1),1);
if(LastValue(Ref(Buy,-1)) AND LastValue(Ref(Cover,-1)))
{
printf("\n"+“Inside Buy and Cover: Price=”+price);
brd.PlaceOrder(“NFO”, symbol, “NIFTY”, “MA - Nifty”, 1, Lots2, 0, price, 0, “L”, “NRML”, “DAXXXX”,“DAY”);
//brd.PlaceOrder(“NFO”, symbol, “NIFTY”, “MA - Nifty”, 1, Lots
2, 0, price, 0, “MKT”, “NRML”, “DAXXXX”,“DAY”);
}
else
{
printf("\n"+“Inside Buy OR Cover: Price=”+price);
brd.PlaceOrder(“NFO”, symbol, “NIFTY”, “MA - Nifty”, 1, Lots, 0, price, 0, “L”, “NRML”, “DAXXXX”,“DAY”);
//brd.PlaceOrder(“NFO”, symbol, “NIFTY”, “MA - Nifty”, 1, Lots, 0, price, 0, “MKT”, “NRML”, “DAXXXX”,“DAY”);
}
StaticVarSet(“Check_NIFTY_MA”,CheckTwo);
}
else if((LastValue(Ref(Sell,-1)) OR LastValue(Ref(Short,-1))) AND (CheckOne != CheckTwo))
{
price = LastValue(Ref(C,-1),1);
if(LastValue(Ref(Sell,-1)) AND LastValue(Ref(Short,-1)))
{
printf("\n"+“Inside Sell AND Short: Price=”+price);
brd.PlaceOrder(“NFO”, symbol, “NIFTY”, “MA - Nifty”, 2, Lots2, 0, price, 0, “L”, “NRML”, “DAXXXX”,“DAY”);
//brd.PlaceOrder(“NFO”, symbol, “NIFTY”, “MA - Nifty”, 2, Lots
2, 0, price, 0, “MKT”, “NRML”, “DAXXXX”,“DAY”);
}
else
{
printf("\n"+“Inside Sell OR Short: Price=”+price);
brd.PlaceOrder(“NFO”, symbol, “NIFTY”, “MA - Nifty”, 2, Lots, 0, price, 0, “L”, “NRML”, “DAXXXX”,“DAY”);
//brd.PlaceOrder(“NFO”, symbol, “NIFTY”, “MA - Nifty”, 2, Lots, 0, price, 0, “MKT”, “NRML”, “DAXXXX”,“DAY”);
}
StaticVarSet(“Check_NIFTY_MA”,CheckTwo);
}
_SECTION_END();

That’s a good option. Will try.

This seems to be the same code that I pasted above. Is there any difference. As I mentioned above, it works fine with bar replay and sends the signals across to Pi. It is only during live market that when I am looking at some other tab in Ami and a signal comes in this AFL, it does not send across the signal. All my other AFLs does not seem to have this problem. Definitely there is something missing here.

ok try with… go to window and tile horizontally and check in live market if orders are firing

Thanks! RequestTimedRefresh was the missing piece.

Dear Mr. Vivith
I would like to use Pie bridge connected to Amibroker with buy and sell signals indicators by manually or automatically when the signal triggers in Amibroker indicators.
I am using third party data reading to Amibroker through nest system. Now, Is it possible to work the same way to pie bridge to amibroker to extract the data ?
Please advise.
Guru