Hidden windows don't paint in Windows OS, was: Signal not getting fired, unable to reproduce

Hi,

Below is my code. I am running this on a chart. In a day I may have multiple symbols for trading. So I am opening this AFL in multiple chart windows (MDI window). Sometimes stop loss is not getting fired, sometimes the target is not getting fired. It is not happening for all the trades. I am not able to reproduce the issue. I am even plotting BUY, SHORT, COVER, SELL arrays with arrows on a chart. That's working fine. I am able to see proper arrow marks on a chart. There is no problem with even signal repainting. But sometimes service call is not happening at all. I have put TRACE statements as well. TRACE never gets printed which means control never enters to that particular code block.

I have even used RequestTimedRefresh( 1, False ); so that every time chart gets refreshed even if it's not in an active state. I have one question here though. If I had minimized Amibroker and doing some other work like opening other applications, Does Amibroker charts keep working in the background?

I am unable to find reasons why sometimes service calls are not happening. Please help. I am not sure what wrong I am doing currently. Earlier I used to run this program in scanner mode, even then I faced the same problem. Any help would be greatly appreciated.


_SECTION_BEGIN( "GAP UP DOWN STRATEGY 2.0 - EXECUTION" );

Version(6.20);

RequestTimedRefresh( 1, False );

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

function plotline( color, value, baridx, penstyle )
{
    GfxSetOverlayMode( 1 );
    GfxSetCoordsMode( 1 );

    GfxSelectPen( color, 1, penstyle );
    GfxMoveTo( baridx - 5, value );
    GfxLineTo( baridx + 20, value );

    GfxSetOverlayMode( 0 );
    GfxSetCoordsMode( 0 );
}
function GetStoplossThreshold(price)
{
	local threshold;
	threshold = 0.0009;
	
	if(price < 60) {
		threshold = 0.0009;
	} else if(price >= 60 && price < 500) {
		threshold = 0.0008;
	} else if(price >= 500 && price < 700) {
		threshold = 0.0007;
	} else if(price >= 700) {
		threshold = 0.0006;
	}
	return threshold;
}

/************** Begin Parameters *************/
OPEN_ABOVE = 2;
STOPLOSS_PERCENT = 1;
TARGET_PERCENT = 3;
PRICE_ABOVE = 55;
VOLUME_ABOVE = 300000;
AVERAGE_VOLUME_ABOVE = 300000;
AVERAGE_VOLUME_DAYS = 3;
CONSIDER_VOLUME = 1;
/************** End Parameters *************/
tn = TimeNum();
NewDay = (Day()!= Ref(Day(), -1)) OR BarIndex() == 0;
datearr  = DateNum();
TradeEndTime = 151300;  // in HHMMSS format

//SOME BUY AND SHORT CONDITION
BUY0 = 1;
SHORT0 = 0;


//VOLUME VALIDATION

TimeFrameSet( inDaily );
VALID_VOLUME = MA(V, AVERAGE_VOLUME_DAYS) >= AVERAGE_VOLUME_ABOVE AND Ref(V, 0) >= VOLUME_ABOVE;
TimeFrameRestore();
VALID_VOLUME = TimeFrameExpand( VALID_VOLUME, inDaily );

if(CONSIDER_VOLUME) {
	BUY0 = BUY0 AND VALID_VOLUME;
	SHORT0 = SHORT0 AND VALID_VOLUME;
}

Buy = Sell = Short = Cover = BuyPrice = ShortPrice = SellPrice = CoverPrice = Null;

Long_Trade_Entry = Long_Trade_Stoploss = Long_Trade_Target = 0;
Short_Trade_Entry = Short_Trade_Stoploss = Short_Trade_Target = 0;

bflag  = 0;
sflag = 0;

for( i = 4; i < BarCount; i++ )
{
	//BUY CONDITION
    if( BUY0[i] AND bflag == 0)
    {
        Buy[i] = 1;
        bflag = 1;
        BuyPrice[i] = O[i];

        Long_Trade_Entry = BuyPrice[i];
        Long_Trade_Stoploss = Long_Trade_Entry - (Long_Trade_Entry * (STOPLOSS_PERCENT/100));
        Long_Trade_Stoploss_Threshold = Long_Trade_Stoploss + (Long_Trade_Stoploss * GetStoplossThreshold(Long_Trade_Stoploss));
        
        plotline( colorRed, Long_Trade_Stoploss, i, 0 );
        Plot(Long_Trade_Stoploss, "Long Trade Stoploss", colorDarkRed, styleNoLine | styleNoRescale | styleNoTitle);
        
        Long_Trade_Stoploss = Long_Trade_Stoploss_Threshold;
        
        Long_Trade_Target = Long_Trade_Entry + (Long_Trade_Entry * (TARGET_PERCENT/100));
		
		plotline( colorYellow, Long_Trade_Entry, i, 0 );
		plotline( colorRed, Long_Trade_Stoploss_Threshold, i, 1);
		
		plotline( colorGreen, Long_Trade_Target, i, 1 );
		
		plotline( colorGreen, Long_Trade_Entry + (Long_Trade_Entry * 0.01), i, 1 );
		plotline( colorGreen, Long_Trade_Entry + (Long_Trade_Entry * 0.02), i, 1 );
		
		Plot(Long_Trade_Entry, "Long Trade Entry", colorYellow, styleNoLine | styleNoRescale | styleNoTitle);
		Plot(Long_Trade_Stoploss_Threshold, "Long Trade Threshold Stoploss", colorRed, styleNoLine | styleNoRescale| styleNoTitle);
		Plot(Long_Trade_Target, "Long Target", colorGreen, styleNoLine | styleNoRescale | styleNoTitle);
    }
    
    if( ( 	tn[i-1] == tradeEndTime  OR L[i] <= Long_Trade_Stoploss OR H[i] >= Long_Trade_Target) AND bflag == 1 )
    {
        Sell[i] = 1;
        bflag = 0;
        SellPrice[i] = C[i];
		
		if(L[i] <= Long_Trade_Stoploss) {
			SellPrice[i] = Long_Trade_Stoploss;
		} else if(tn[i-1] == tradeEndTime) {
			SellPrice[i] = O[i];
		} else if(H[i] >= Long_Trade_Target ) {
			SellPrice[i] = Long_Trade_Target;
		}
		
    }
    
    //SHORT CONDITION
    if( SHORT0[i] AND sflag == 0)
    {
        Short[i] = 1;
        sflag = 1;
        ShortPrice[i] = O[i];

        Short_Trade_Entry = ShortPrice[i];
        Short_Trade_Stoploss = Short_Trade_Entry + (Short_Trade_Entry * (STOPLOSS_PERCENT/100));
        Short_Trade_Stoploss_Threshold = Short_Trade_Stoploss - (Short_Trade_Stoploss * GetStoplossThreshold(Short_Trade_Stoploss));
        
        plotline( colorRed, Short_Trade_Stoploss, i, 0 );
        Plot(Short_Trade_Stoploss, "Short Trade Stoploss", colorDarkRed, styleNoLine | styleNoRescale | styleNoTitle);
        Short_Trade_Stoploss = Short_Trade_Stoploss_Threshold;
         
        Short_Trade_Target = Short_Trade_Entry - (Short_Trade_Entry * (TARGET_PERCENT/100));
		
		plotline( colorYellow, Short_Trade_Entry, i, 0 );
		plotline( colorRed, Short_Trade_Stoploss_Threshold, i, 1);
		
		plotline( colorGreen, Short_Trade_Target, i, 1 );
		plotline( colorGreen, Short_Trade_Entry - (Short_Trade_Entry * 0.01), i, 1 );
		plotline( colorGreen, Short_Trade_Entry - (Short_Trade_Entry * 0.02), i, 1 );
		
		Plot(Short_Trade_Entry, "Short Trade Entry", colorYellow, styleNoLine | styleNoRescale | styleNoTitle);
		Plot(Short_Trade_Stoploss_Threshold, "Short Trade Threshold Stoploss", colorRed, styleNoLine | styleNoRescale | styleNoTitle);
		Plot(Short_Trade_Target, "Short Target", colorGreen, styleNoLine | styleNoRescale | styleNoTitle );
    }
   
    if( ( tn[i-1] == tradeEndTime  OR H[i] >= Short_Trade_Stoploss OR L[i] <= Short_Trade_Target) AND sflag == 1 )
    {
		
        Cover[i] = 1;
        sflag = 0;
        CoverPrice[i] = C[i];
        
		if(H[i] >= Short_Trade_Stoploss) {
			CoverPrice[i] = Short_Trade_Stoploss;
		} else if(tn[i-1] == tradeEndTime) {
			CoverPrice[i] = O[i];
		} else if(L[i] <= Short_Trade_Target ) {
			CoverPrice[i] = Short_Trade_Target;
		}

    }
   
}

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( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, 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( Cover, shapeUpArrow, shapeNone ), colorBlue, 0, L, Offset = -45 );



#include_once "Formulas\Include\ServiceLib40.afl";

/************** Begin Service Parameters *************/
SERVICE_URI = "http://localhost:50607/api/messages";
SERVICE_STRATEGY_CODE = "GUD01";
/************** End Service Parameters *************/

SymbolName = Name();
TotalBars = BarCount;
Dates = DateNum();
Plot(Now(3), "CURRENT Date", colorDefault, styleNoLine | styleNoRescale | styleNoTitle);
Plot(Dates, "Date", colorDefault, styleNoLine | styleNoRescale | styleNoTitle);

//ONLY SEND ORDER FOR THE CURRENT DAY.. ADDITIONAL CONDITION TO MAKE SURE 
if(Now(3) == Dates[TotalBars - 1] ) {

	if(Buy[TotalBars - 1] == 1 || Buy[TotalBars - 2] == 1) {
		ALERT_NAME = SERVICE_STRATEGY_CODE + SymbolName + GetChartID() + "BUY";
		if(Nz(StaticVarGet(ALERT_NAME)) == 0) {
			_TRACE("BEGINING TO SEND BUY ORDER FOR " + SymbolName);
			ServiceCall(SymbolName, 1, 1, SERVICE_URI, SERVICE_STRATEGY_CODE);
			StaticVarSet(ALERT_NAME, 1);
			_TRACE("BUY ORDER SENT FOR " + SymbolName);
		}
	} else if(Sell[TotalBars - 1] == 1 || Sell[TotalBars - 2] == 1) {
		ALERT_NAME = SERVICE_STRATEGY_CODE + SymbolName + GetChartID() + "SELL";
		if(Nz(StaticVarGet(ALERT_NAME)) == 0) {
			_TRACE("BEGINING TO SEND SELL ORDER FOR " + SymbolName);
			ServiceCall(SymbolName, 2, 1, SERVICE_URI, SERVICE_STRATEGY_CODE);
			StaticVarSet(ALERT_NAME, 1);
			_TRACE("SELL ORDER SENT FOR " + SymbolName);
		}
	} else if(Short[TotalBars - 1] == 1 || Short[TotalBars - 2] == 1) {
		ALERT_NAME = SERVICE_STRATEGY_CODE + SymbolName + GetChartID() + "SHORT";
		if(Nz(StaticVarGet(ALERT_NAME)) == 0) {
			_TRACE("BEGINING TO SEND SHORT ORDER FOR " + SymbolName);
			ServiceCall(SymbolName, 3, 2, SERVICE_URI, SERVICE_STRATEGY_CODE);
			StaticVarSet(ALERT_NAME, 1);
			_TRACE("SHORT ORDER SENT FOR " + SymbolName);
		}
	} else if(Cover[TotalBars - 1] == 1 || Cover[TotalBars - 2] == 1) {
		ALERT_NAME = SERVICE_STRATEGY_CODE + SymbolName + GetChartID() + "COVER";
		if(Nz(StaticVarGet(ALERT_NAME)) == 0) {
			_TRACE("BEGINING TO SEND COVER ORDER FOR " + SymbolName);
			ServiceCall(SymbolName, 4, 2, SERVICE_URI, SERVICE_STRATEGY_CODE);
			StaticVarSet(ALERT_NAME, 1);
			_TRACE("COVER ORDER SENT FOR " + SymbolName);
		}
	}

}


_SECTION_END();

Thanks,
Vinay V

Completely unrelated question about custom backtest was moved to new topic: ApplyTo filter must include all symbols you are trading

In Windows generally HIDDEN and INVISIBLE windows are NEVER repainted.

That is a Operating System rule. Without that it would be nightmare to use if you had open more than 1 app and everything invisible would be repainting without any sense.

So if you want refreshes for INVISIBLE windows you have to use forced timed refreshes http://www.amibroker.com/guide/afl/requesttimedrefresh.html

Second thing to understand that you have to place your code in active sheet. Active sheet within Chart window is controlled bottom (not UPPER) tab http://www.amibroker.com/guide/h_sheets.html.

Also you should place _TRACE outside of any if-else statements to know that your formula is actually executing.

And quite frankly due to this:

I seriously think that your problem is TOTALLY ELSEWHERE, so first you need to eliminate other factors (you functions like ServiceCall that may be not working), place the _TRACE completely outside any conditional statements and see if you get proper timed executions of the formula.

Do more debugging as per How do I debug my formula?