Alternate Buys and Sells Please HELP!!

 

Hi All,

I'm a new programmer and I've written this program that trades good. I'm trying to get it to alternate between Buys and Sells.

I'm using "j" as the qualifier. "j" starts as "1", then after the "Buys" run we add "1" to "j" ("j" is two at this point) The Sells are passed by until orders close and adds another "1" to "j" and it the becomes a "3".... so the Buys don't execute but the sells which wants to have a "3" does....after the sells runs "j" is reset to zero... after the sells close orders close "j" gets 1 added to it and the Buys execute and the process starts again. The only problem is it doesn't work !! What have I done wrong?? Please help Thanks in advance.




int i = 1;
int j = 1;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----




if(TradeLong)
if (j == 1)

if(ActionsPerBarLong == True)
{

//----
OrderSend(Symbol(),OP_BUY,LE1, Ask,5,Ask-sl1*Point,0,"",255,0,CLR_NONE);
Sleep(2);
OrderSend(Symbol(),OP_BUYLIMIT, LE2, Ask-sh2*Point,5,Ask-sl2*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_BUYLIMIT, LE3, Ask-sh3*Point,5,Ask-sl3*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_BUYLIMIT, LE4, Ask-sh4*Point,5,Ask-sl4*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_BUYLIMIT, LE5, Ask-sh5*Point,5,Ask-sl5*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_BUYLIMIT, LE6, Ask-sh6*Point,5,Ask-sl6*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_BUYLIMIT, LE7, Ask-sh7*Point,5,Ask-sl7*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_BUYLIMIT, LE8, Ask-sh8*Point,5,Ask-sl8*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_BUYLIMIT, LE9, Ask-sh9*Point,5,0,0,"",255,0,CLR_NONE);
ActionsPerBarLong =False;


j++;

}



//----

//----

//+------------------------------------------------------------------+


if(TradeShort)

if (j == 3)

if(ActionsPerBarShort==True)
{
OrderSend(Symbol(),OP_SELL,LE1, Bid,5,Bid+sl1*Point,0,"",255,0,CLR_NONE);
Sleep(2);
OrderSend(Symbol(),OP_SELLLIMIT, LE2, Bid+sh2*Point,5,Bid+sl2*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_SELLLIMIT, LE3, Bid+sh3*Point,5,Bid+sl3*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_SELLLIMIT, LE4, Bid+sh4*Point,5,Bid+sl4*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_SELLLIMIT, LE5, Bid+sh5*Point,5,Bid+sl5*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_SELLLIMIT, LE6, Bid+sh6*Point,5,Bid+sl6*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_SELLLIMIT, LE7, Bid+sh7*Point,5,Bid+sl7*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_SELLLIMIT, LE8, Bid+sh8*Point,5,Bid+sl8*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_SELLLIMIT, LE9, Bid+sh9*Point,5,0,0,"",255,0,CLR_NONE);
ActionsPerBarShort=False;
j--;
j--;
j--;

}

//----



if(AccountEquity()-AccountBalance()>=AcctPrftClseAllRestart*OrdersTotal()*1.5)

{
for(i=OrdersTotal()-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();

bool result = false;

switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Pink);
break;

//Close opened short positions
case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,Pink);
case OP_BUYLIMIT : result = OrderDelete( OrderTicket() );
Sleep(10);
case OP_BUYSTOP : result = OrderDelete( OrderTicket() );
Sleep(10);
case OP_SELLLIMIT : result = OrderDelete( OrderTicket() );
Sleep(10);
case OP_SELLSTOP : result = OrderDelete( OrderTicket() );
Sleep(600);


j++;

}


}


}



if (OrdersTotal()==0)
{
ActionsPerBarLong =True;

ActionsPerBarShort =True;
}


return(0);
}



 
n8937g:

Hi All,

I'm a new programmer and I've written this program that trades good. I'm trying to get it to alternate between Buys and Sells.

I'm using "j" as the qualifier. "j" starts as "1", then after the "Buys" run we add "1" to "j" ("j" is two at this point) The Sells are passed by until orders close and adds another "1" to "j" and it the becomes a "3".... so the Buys don't execute but the sells which wants to have a "3" does....after the sells runs "j" is reset to zero... after the sells close orders close "j" gets 1 added to it and the Buys execute and the process starts again. The only problem is it doesn't work !! What have I done wrong?? Please help Thanks in advance.




int i = 1;
int j = 1;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----




if(TradeLong)
if (j == 1)

if(ActionsPerBarLong == True)
{

//----
OrderSend(Symbol(),OP_BUY,LE1, Ask,5,Ask-sl1*Point,0,"",255,0,CLR_NONE);
Sleep(2);
OrderSend(Symbol(),OP_BUYLIMIT, LE2, Ask-sh2*Point,5,Ask-sl2*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_BUYLIMIT, LE3, Ask-sh3*Point,5,Ask-sl3*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_BUYLIMIT, LE4, Ask-sh4*Point,5,Ask-sl4*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_BUYLIMIT, LE5, Ask-sh5*Point,5,Ask-sl5*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_BUYLIMIT, LE6, Ask-sh6*Point,5,Ask-sl6*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_BUYLIMIT, LE7, Ask-sh7*Point,5,Ask-sl7*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_BUYLIMIT, LE8, Ask-sh8*Point,5,Ask-sl8*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_BUYLIMIT, LE9, Ask-sh9*Point,5,0,0,"",255,0,CLR_NONE);
ActionsPerBarLong =False;


j++;

}



//----

//----

//+------------------------------------------------------------------+


if(TradeShort)

if (j == 3)

if(ActionsPerBarShort==True)
{
OrderSend(Symbol(),OP_SELL,LE1, Bid,5,Bid+sl1*Point,0,"",255,0,CLR_NONE);
Sleep(2);
OrderSend(Symbol(),OP_SELLLIMIT, LE2, Bid+sh2*Point,5,Bid+sl2*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_SELLLIMIT, LE3, Bid+sh3*Point,5,Bid+sl3*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_SELLLIMIT, LE4, Bid+sh4*Point,5,Bid+sl4*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_SELLLIMIT, LE5, Bid+sh5*Point,5,Bid+sl5*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_SELLLIMIT, LE6, Bid+sh6*Point,5,Bid+sl6*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_SELLLIMIT, LE7, Bid+sh7*Point,5,Bid+sl7*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_SELLLIMIT, LE8, Bid+sh8*Point,5,Bid+sl8*Point,0,"",255,0,CLR_NONE);
Sleep(20);
OrderSend(Symbol(),OP_SELLLIMIT, LE9, Bid+sh9*Point,5,0,0,"",255,0,CLR_NONE);
ActionsPerBarShort=False;
j--;
j--;
j--;

}

//----



if(AccountEquity()-AccountBalance()>=AcctPrftClseAllRestart*OrdersTotal()*1.5)

{
for(i=OrdersTotal()-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();

bool result = false;

switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Pink);
break;

//Close opened short positions
case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,Pink);
case OP_BUYLIMIT : result = OrderDelete( OrderTicket() );
Sleep(10);
case OP_BUYSTOP : result = OrderDelete( OrderTicket() );
Sleep(10);
case OP_SELLLIMIT : result = OrderDelete( OrderTicket() );
Sleep(10);
case OP_SELLSTOP : result = OrderDelete( OrderTicket() );
Sleep(600);


j++;

}


}


}



if (OrdersTotal()==0)
{
ActionsPerBarLong =True;

ActionsPerBarShort =True;
}


return(0);
}




All this business of having four states is not the way I'd architect it.

Why not merely interrogate the order type of the order you are closing and set a flag to dictate the next direction to be the opposite?

 
cloudbreaker:

All this business of having four states is not the way I'd architect it.

Why not merely interrogate the order type of the order you are closing and set a flag to dictate the next direction to be the opposite?

Hi Cloudbreaker, Thanks for your advice... I've spent the last 3 days trying every different way to get them to alternate....Nothing has worked ( I was able to get it to alternate but the first cycle was both Buys and Sells BEFORE the alternating worked). I'm still learning. I thought that by keeping it simple with the 4 states...it would have a good chance of working. Can you direct me to a page that explains the concept of using, setting, and changing states of the flag. Possibly an example of the code so I can see how it works? Could you also explain why what I've written isn't alternating?? THANKS!!!

 
n8937g:

Hi Cloudbreaker, Thanks for your advice... I've spent the last 3 days trying every different way to get them to alternate....Nothing has worked ( I was able to get it to alternate but the first cycle was both Buys and Sells BEFORE the alternating worked). I'm still learning. I thought that by keeping it simple with the 4 states...it would have a good chance of working. Can you direct me to a page that explains the concept of using, setting, and changing states of the flag. Possibly an example of the code so I can see how it works? Could you also explain why what I've written isn't alternating?? THANKS!!!

By flag, I just mean a variable which defines either what your last direction was, or what your next direction will be. Your choice.

It could be an int with a value of 0 or 1. Or, a string with a value of "BUY" or "SELL". Or it could be a bool. Your choice.


Meanwhile in order to solve the problems with your own code - why don't you litter it with Print() statements!

Reason: