The last completed bar, was: Auto Trade Problem


#1

Below is my line of codes to send order for auto trade:

if (Buy[BarCount-1] && NewBar && BuyPosHold == 0 ){
		
	myobj.sendmsg("3103,0,1," + login + ",0," + symbol + ",B," + (C[BarCount-1]+10) + "," + qty + ",,,,,,1");
	StaticVarSet("BuyPosHold", 1);
	PlaySound("c:\\Users\\User\\Downloads\\Mp3-alarm-clock");
}

I would expect Amibroker will send the buy order to the broker when my buy signal occured. However, in 1 minutes time frame, the Amibroker send the order to the broker before the 1 minutes chart finished.

Any suggestion.
Mak


#2

First you did not post entire formula so one has to guess what NewBar variable is.
If your read “how to use this site” before posting you would know that you are supposed to
make your post clear and free from guessing work.

Buy[ BarCount -1 ] is the LAST bar as bars are counted FROM ZERO. You would rather need to use the previous bar Buy[ BarCount - 2 ]

BTW: Read “how to use this site” and use CODE TAGS (I corrected your post)


#4

I think this make my code more clear,please take a look. Thanks

//create SPTraderObject
myobj = CreateStaticObject("SPBridge.SPBridge");

// 1) Change this part (symbol, login, qty, stopPriceLevel (in point))
symbol = "MHIG7";
login = "DEMO201701048";
qty = 1;

msg = myobj.DisplayMsg();
if ( msg != "" )
{
	_TRACE(msg);
}

PrevTN = StaticVarGet("TimeNumber");
TN = LastValue(TimeNum());
NewBar = TN != PrevTN;
StaticVarSet("TimeNumber", TN);

BuyPosHold = Nz(StaticVarGet("BuyPosHold"));
ShortPosHold = Nz(StaticVarGet("ShortPosHold"));

//New Buy Signal
if (Buy[BarCount-1] && NewBar && BuyPosHold == 0 ){
	//new long position with no position before
	if(ShortPosHold==0){
		qty=1;
	}
	
	//reverse from short to long
	if(ShortPosHold !=0){
		qty=2;
	}
		
	myobj.sendmsg("3103,0,1," + login + ",0," + symbol + ",B," + (C[BarCount-1]+10) + "," + qty + ",,,,,,1");
	StaticVarSet("BuyPosHold", 1);
	PlaySound("c:\\Users\\User\\Downloads\\Mp3-alarm-clock");
}


//Cover Long
if (Sell[BarCount-1] && NewBar && BuyPosHold > 0){
	myobj.sendmsg("3103,0,1," + login + ",0," + symbol + ",S," + (C[BarCount-1]-10) + "," + qty + ",,,,,,1");
	StaticVarSet("BuyPosHold", 0);
	PlaySound("c:\\Users\\User\\Downloads\\Mp3-alarm-clock");
}


//Sell Signal
if (Short[BarCount-1] &&  NewBar && ShortPosHold == 0){
	//new short position
	if(BuyPosHold==0){
		qty=1;
	}
	//reverse from long to short
	if(BuyPosHold !=0){
		qty=2;
	}
			
	myobj.sendmsg("3103,0,1," + login + ",0," + symbol + ",S," + (C[BarCount-1]-10) + "," + qty + ",,,,,,1");
	StaticVarSet("ShortPosHold", 1);
	PlaySound("c:\\Users\\User\\Downloads\\Mp3-alarm-clock");
}

//Sell Exit
if (Cover[BarCount-1] && NewBar && ShortPosHold > 0){
	myobj.sendmsg("3103,0,1," + login + ",0," + symbol + ",B," + (C[BarCount-1]+10) + "," + qty + ",,,,,,1");
	StaticVarSet("ShortPosHold", 0);
	PlaySound("c:\\Users\\User\\Downloads\\Mp3-alarm-clock");
}


//**************************************************** End AutoTrade***********************************************************


#5

Today ,I try to change to BarCount-2, however there is no order send . In the chart, I can see the buy arrow but no order send to broker.

Do I need the NewBar variable ?

Any advise ?

Thanks for your kind help

Mak


#6

It is your code not mine. You first need to understand yourself what you are doing (see: How do I debug my formula?).
You have added your NewBar for some reason. If you added it without good reason, then it is not needed. Debug and understand your own code. You really need to do that if you want to let the code to trade for you.

If and only if you want to buy on completed last bar then checking Buy condition on the bar before the last is 100% enough, so either this

if( LastValue( Ref( Buy, -1 ) ) )
{
  // a BUY occurred on last COMPLETED bar
}

or

if( BarCount >= 2 && Buy[ BarCount - 2 ] )
{
  // a BUY occurred on last COMPLETED bar
}

is perfectly enough.