Just finished developing a strategy in Wealth Lab (pretty much C#) which works in AB. The Forum moderator (Eugene) was very big on not sending alerts for trades using the same bar, since it's unrealistic and results in a type of leading indicator.
Using the intraday code below for Interactive Brokers (IB), is there a way to send Buy and Sell triggers to IB at the next, e.g., 1-min bar?
OptimizerSetEngine("cpso");
VarPfx = Name();
openposition = StaticVarGet(VarPfx + "openposition");
numtrades = StaticVarGet(VarPfx + "numtrades");
numwins = StaticVarGet(VarPfx + "numwins");
numlosses = StaticVarGet(VarPfx + "numlosses");
entrydtg = StaticVarGetText(VarPfx + "entrydtg");
entryprice = StaticVarGet(VarPfx + "entryprice");
exitdtg = StaticVarGetText(VarPfx + "exitdtg");
exitprice = StaticVarGet(VarPfx + "exitprice");
totsharesowned=StaticVarGet(VarPfx + "totsharesowned");
netprofitthisasset = StaticVarGet(VarPfx + "netprofitthisasset");
netprofitsession = StaticVarGet("netprofitsession");
availfunds=StaticVarGet("availfunds");
currvalue=StaticVarGet("currvalue");
if (IsEmpty(openposition))
{
openposition=0;
entryprice=0;
exitprice=0;
numtrades = 0;
numwins = 0;
numlosses = 0;
netprofitthisasset = 0;
netprofitsession = 0;
totsharesowned=0;
StaticVarSet(VarPfx + "openposition",0);
StaticVarSet(VarPfx + "numtrades",0);
StaticVarSet(VarPfx + "numwins",0);
StaticVarSet(VarPfx + "numlosses",0);
StaticVarSet(VarPfx + "entryprice",0);
StaticVarSet(VarPfx + "exitprice",0);
StaticVarSet(VarPfx + "netprofitthisasset",0);
StaticVarSet("netprofitsession",0);
StaticVarSet(VarPfx + "totsharesowned",0);
}
purchaseamount=Param("Purchase amount",500,100,1000,50);
RequestTimedRefresh( 1 );
Transmit = ParamToggle("Transmit to IB","OFF|ON",0);
if (Transmit){
StartIBClockTime = Now(4);
ibc = GetTradingInterface("IB");
}
Buy = ExRem(Buy,Sell); //Removing Excessive Buy Signals At each bars
Sell = ExRem(Sell,Buy); //Removing Excessive Sell Signals at each bars
IIf( (Buy),PlotShapes(shapeUpTriangle*Buy,colorGreen),0);
IIf( (Sell),PlotShapes(shapeDownTriangle*Sell,colorRed),0);
if( LastValue(Buy) AND openposition==0 AND transmit==1)
{
price=LastValue(C);
numbuyshares= Max(round(purchaseamount/price),1);
orderid=ibc.PlaceOrder( Name(), "BUY", 1, "MKT", 0, 0, "Day", Transmit );
totsharesowned=totsharesowned+numbuyshares;
StaticVarSet(VarPfx + "totsharesowned", totsharesowned);
openposition=1;
StaticVarSet(VarPfx + "openposition", 1);
numtrades=numtrades+1;
StaticVarSet(VarPfx + "numtrades",numtrades);
entryprice = LastValue(C);
StaticVarSet(VarPfx + "entryprice",entryprice);
}
if( LastValue(Sell) AND openposition==1 AND transmit==1 )
{
ibc.PlaceOrder( Name(), "SELL", 1, "MKT", 0, 0, "Day", Transmit );
StaticVarSet(VarPfx + "totsharesowned", 0);
openposition=0;
StaticVarSet(VarPfx + "openposition", 0);
exitprice=LastValue(C);
StaticVarSet(VarPfx + "exitprice",exitprice);
if (exitprice > entryprice)
{
numwins+=1;
StaticVarSet(VarPfx + "numwins",numwins);
}
if (exitprice < entryprice)
{
numlosses+=1;
StaticVarSet(VarPfx + "numlosses",numlosses);
}
netprofitthisasset=netprofitthisasset+(exitprice-entryprice);
StaticVarSet(VarPfx + "netprofitthisasset",netprofitthisasset);
netprofitsession=netprofitsession+(exitprice-entryprice);
StaticVarSet("netprofitsession",netprofitsession);
}
if (transmit==1){
totsharesowned=ibc.GetPositionSize( VarPfx );
StaticVarSet(VarPfx + "totsharesowned", totsharesowned);
currvaluestr= ibc.GetAccountValue("[USD]TotalCashBalance");
currvalue=StrToNum(currvaluestr);
realizedpnlstr= ibc.GetAccountValue("[USD]RealizedPnL");
realizedpnl=StrToNum(realizedpnlstr);
unrealizedpnlstr= ibc.GetAccountValue("[USD]UnRealizedPnL");
unrealizedpnl=StrToNum(unrealizedpnlstr);
StaticVarSet("realizedpnl",realizedpnl);
StaticVarSet("unrealizedpnl",unrealizedpnl);
StaticVarSet("currvalue",currvalue);
if(unrealizedpnl !=0 && realizedpnl !=0){
netprofitsession=unrealizedpnl-realizedpnl;
}
StaticVarSet("netprofitsession",netprofitsession);
availfundsstr= ibc.GetAccountValue("[USD]AvailableFunds");
availfunds=StrToNum(availfundsstr);
StaticVarSet("availfunds",availfunds);
}
profitboard = ParamToggle("Profit Board","Show|Hide",1);
if (profitboard == 1 )
{
GfxSelectFont( "Tahoma", 8, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite);
if ( netprofitthisasset> 0)
{
GfxSelectSolidBrush( colorGreen ); // this is the box background color
}
else
{
GfxSelectSolidBrush( colorRed ); // this is the box background color
}
pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 310;
x = 5;
x2 = 290;
y = pxHeight;
GfxSelectPen( colorGreen, 1); // broader color
GfxRoundRect( x, y - 98, x2, y , 7, 7 ) ;
GfxTextOut( ( "Profit Board"),13,y-100);
GfxTextOut( ("Shares owned:" + totsharesowned ), 13, y-90) ; // The text format location
GfxTextOut( ("#Wins:" + numwins ), 13, y-80) ; // The text format location
GfxTextOut( ("#Losses:" + numlosses ), 13, y-70) ; // The text format location
GfxTextOut( ("CurrentValue:" + currvalue ), 13, y-60) ; // The text format location
GfxTextOut( ("Net profit this session:" + netprofitsession ), 13, y-50) ; // The text format location
GfxTextOut( ("Available funds:" + availfunds ), 13, y-40) ; // The text format location
GfxTextOut( ("openposition:" + openposition ), 13, y-30) ; // The text format location
}