Help with for statement - Signal recognition to TWS

I have the following statement that generates arrows & Buy/Sell prices on chart but TWS ignores the signal. Any suggestions would be greatly appreciated.


for( i = 1 ; i < BarCount-2; i++ ) 
{
if (con_1[i] <con_1[i-1] && con_1[i] <con_1[i+1]) B_sig[i]=1; 
else
B_sig[i] =0;
if (con_1[i] >con_1[i-1] && con_1[i] >con_1[i+1]) S_sig[i]=1;
else
S_sig[i] =0;
}

B_sig =ExRem(B_sig,S_sig);
S_sig =ExRem(S_sig,B_sig);

Buy = B_sig==1;
Sell = S_sig==1;
Short =  S_sig==1;
Cover =B_sig==1;

Thanks

Greg,

Before you worry about TWS and auto-trading, you should probably become more proficient at AFL. The logic you posted could easily be implemented with 4 lines of efficient AFL. In particular, you need to understand how AFL array processing works. A refresher on boolean variables would be a good idea as well.

Regarding TWS specifically, it’s very difficult for anyone to offer any assistance when you don’t post the relevant code or even describe your logic.

Matt

I guess that @GTK5 may be expecting that somehow “automagically” these signals would be sent to TWS. But they won’t unless you specifically tell the program to place orders. This is done via auto-trading interface http://www.amibroker.com/at/ but as @mradtke said be careful with that as for proper auto-trading you need to be really proficient with AFL coding.

Users’ KB has heaps of practical examples for auto trading http://www.amibroker.org/userkb/ courtesy of Herman (aka @SpinTrader)

Thanks Tomasz for the quick response - I only posted the section of code that is not recognized - Here is the full AFL

_N(SectionName = "ABS");

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +
WriteVal( V, 1.0 ) +
" {{VALUES}}",
O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorBlack ),
ParamStyle("Style",styleNoTitle|styleBar,maskAll) );
RequestTimedRefresh( 1 );
ibc = GetTradingInterface("IB");
EnableAutoTrade = ParamList("Enable Autotrade", "No|Yes", 0);
NQty = Param("Qty to Buy/Sell?",500,100,5000,100);
Transmit = ParamToggle("Transmit","OFF|ON",0);
Reset = ParamTrigger("Reset All","RESET");
LastTrade = StaticVarGetText("LastTrade");
BuyOrderID = StaticVarGetText("BuyOrderID");
SellOrderID = StaticVarGetText("SellOrderID");
BuyPending = ibc.IsOrderPending(BuyOrderID);
SellPending = ibc.IsOrderPending(SellOrderID);
IBPosSize = ibc.GetPositionSize( Name() );
BuyStatus = ibc.GetStatus( BuyORderID, True );
SellStatus = ibc.GetStatus( SellORderID, True);
OptimizerSetEngine("trib");
OptimizerSetOption("Runs", 5 );
OptimizerSetOption("MaxEval", 1000 ); // 5000 evaluations max
Buy=Sell=Short=Cover=O; 

A_F=Param("P_Factor",1,1,180,0.001);

FT_1 =(Param("FT1",-2,-2,30,0.001));
FT_2 =Param("FT2",-2,-2,30,0.001);
FT_3 =Param("FT3",61.743,-2,30,0.001);
FT_4=Param("P_Factor",29,1,180,0.001);

pi=22/7;
Rtd = 180 / Pi;
Dtr = 1 / Rtd;
x=(O+C+H+L)/4;
 
FT_1=FT_1*(A_F*DTR);
FT_2=FT_2*(A_F*DTR);
FT_3=FT_3*(A_F*DTR);
y3=FT_1*x*exp(3) + FT_2*x*exp(2) + FT_3*x;
con_1=WMA(x,y3);

for( i = 1 ; i < BarCount-2; i++ ) 
{
if (con_1[i] <con_1[i-1] && con_1[i] <con_1[i+1]) B_sig[i]=1; 
else
B_sig[i] =0;
if (con_1[i] >con_1[i-1] && con_1[i] >con_1[i+1]) S_sig[i]=1;
else
S_sig[i] =0;
}

B_sig =ExRem(B_sig,S_sig);
S_sig =ExRem(S_sig,B_sig);

Buy = B_sig==1;
Sell = S_sig==1;
Sell =  S_sig==1;
Cover =B_sig==1;

Buy 	=	ExRem(Buy,Sell);
Short	=	ExRem(Short,Cover);
Sell	=	ExRem(Sell,Buy);
Cover 	=	ExRem(Cover,Short);

////////////////////////////// EXECEUTION ENGINE //////////////////////////
if(EnableAutoTrade == "Yes")
{
if( LastValue(Buy ) )
{
if( LastTrade == "Sell" OR LastTrade == "" )
{
BuyOrderID= ibc.ModifyOrder( BuyOrderID, Name(), "Buy", NQty, "MKT", 0, 0, "Day", Transmit);
StaticVarSetText("BuyOrderID",BuyOrderID);
StaticVarSetText("LastTrade", "Buy");
SetChartBkColor( colorBrightGreen ) ;
}
}
if( LastValue(Sell ) )
{
if( LastTrade == "Buy" OR LastTrade == "" )
{
SellORderID = ibc.ModifyOrder( SellORderID , Name(), "Sell", NQty, "MKT", 0, 0, "Day", Transmit);
StaticVarSetText("SellOrderID",SellOrderID);
StaticVarSetText("LastTrade", "Sell");
SetChartBkColor( colorRed ) ;
}
}
else if( Reset )
{
StaticVarSetText("BuyOrderID","");
if( BuyPending ) ibc.CancelOrder( BuyOrderID );
StaticVarSetText("SellOrderID","");
if( SellPending ) ibc.CancelOrder( SellOrderID );
StaticVarSetText("LastTrade", "");
ibc.CloseAllOpenPositions();
}
LastTWSMsg = ibc.getLastError( 0 );
BuyStatus = WriteIf( BuyPending, BuyOrderID+", Status: "+BuyStatus,"");
SellStatus= WriteIf( SellPending, SellOrderID+", Status: "+SellStatus,"");
Title = "\n"+
"Last TWS Error Msg: "+LastTWSMsg+"\n"+
" BuyOrderID: "+BuyOrderID+" "+BuyStatus+"\n"+
" SellOrderID: "+SellOrderID+" "+SellStatus+"\n"+
" Last Trade: "+LastTrade+"\n"+
" TWS Position Size: "+NumToStr(IBPosSize,1.0,False);
Plot(C,"Close",colorBlack,styleBar);
}

PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,H,con_1,-45);
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorBrightGreen,L,con_1,-45);


dist = 1*ATR(60);
for( i = 0; i < BarCount; i++ )
{ 
{if( Buy[i] ) PlotText( "B  $" + C[ i ], i-1, L[ i ]-dist[i], colorbrightGreen,Null,-95 ); 
if( Short[i] ) PlotText( "S  $" + C[ i ], i-1, H[ i ]+dist[i], colorRed,Null,40 );}
}

Moderator comment: The ending (closing) tag for the code section needs to have slash like this [/code] You have used ending tag the same as opening tag (without slash). I corrected it.

Thanks Matt - I concur. This is a hobby for me & I stated that I was a rookie coder. I am constantly working on proficiency.

regards

Put _TRACE inside those if statements so you know what condition is met.

1 Like

Thanks for the guidance - not looking for someone to write it for me but when every other “cross” type formula has no problem executing trades in TWS - this one was exceptionally frustrating. I will dive into _TRACE()

regards

Once you put _TRACE() calls everywhere and watch the output it will all become clear. Trust me.

Your code should look like this:


////////////////////////////// EXECEUTION ENGINE //////////////////////////
if( EnableAutoTrade == "Yes" )
{
	_TRACE("InAutoTrade\n");
    if( LastValue( Buy ) )
    {
		_TRACE("LastBuy is true\n");
        if( LastTrade == "Sell" OR LastTrade == "" )
        {
			_TRACE("We are modifying order here ID=" + BuyOrderID ); 
            BuyOrderID = ibc.ModifyOrder( BuyOrderID, Name(), "Buy", NQty, "MKT", 0, 0, "Day", Transmit );
            _TRACE("Modify order returned new id=" + BuyOrderID );
            StaticVarSetText( "BuyOrderID", BuyOrderID );
            StaticVarSetText( "LastTrade", "Buy" );
            SetChartBkColor( colorBrightGreen ) ;
        }
    }
2 Likes

Thank you Tomasz - If I don’t reply right away it’s because i’m managing intraday positions