FIFO for SH's shell EA

Post Reply
User avatar
mobthehop
Trader
Posts: 362
Joined: Wed Nov 16, 2011 12:16 am

FIFO for SH's shell EA

Post by mobthehop »

I need some advise on how best to implement US FIFO compliance for a multi-lot, same lot size EA based on SH's shell EA in the close order portion of shell.

I probably need to to put all open orders into an array, sort it on order entry date, oldest to newest and then close trades in that sorting order but I have no idea how to code it....

There are so many EAs based on this shell code - any kind soul willing to share....

I would be grateful

Mop
jlcgarcia

Re: FIFO for SH's shell EA

Post by jlcgarcia »

mobthehop wrote:I need some advise on how best to implement US FIFO compliance for a multi-lot, same lot size EA based on SH's shell EA in the close order portion of shell.

I probably need to to put all open orders into an array, sort it on order entry date, oldest to newest and then close trades in that sorting order but I have no idea how to code it....

There are so many EAs based on this shell code - any kind soul willing to share....

I would be grateful

Mop

Hi mobthehop,

I think you will need something like this:

Code: Select all

	bool Success;
	int Closed,CloseCount;
	int CloseTrades[,2];
	double OPrice;
	string s;

for(y=OrdersTotal()-1;y>=0;y--)
	{	if(!OrderSelect(y,SELECT_BY_POS,MODE_TRADES))continue;ArrayResize		(CloseTrades,CloseCount+1);
		CloseTrades[CloseCount,0]=OrderOpenTime();
		CloseTrades[CloseCount,1]=OrderTicket();
		CloseCount++;

	}

if(CloseCount>0)
	{	if(!UseFIFO)ArraySort(CloseTrades,WHOLE_ARRAY,0,MODE_DESCEND);
		else if(CloseCount!=ArraySort(CloseTrades))Print("Error sorting CloseTrades Array");
		for(y=0;y<CloseCount;y++)
		{	if(!OrderSelect(CloseTrades[y,1],SELECT_BY_TICKET))continue;
			while(IsTradeContextBusy())Sleep(100);
			if(IsStopped())return(-1);
			if(OrderType()>OP_SELL)Success=OrderDelete(OrderTicket(),Color);
			else
			{	if(OrderType()==OP_BUY)OPrice=ND(MarketInfo(OrderSymbol(),MODE_BID),MarketInfo(OrderSymbol(),MODE_DIGITS));
				else OPrice=ND(MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS));
				Success=OrderClose(OrderTicket(),OrderLots(),OPrice,slip,Color);
			}
			if(Success)Closed++;
			
		}
		if(Closed==CloseCount)ca=0;
	}
	
	else ca=0;
	if(Closed>0)
	{	if(Closed!=1)s="s";
		Print("Closed "+Closed+" position"+s);
		if(PlaySounds)PlaySound(AlertSound);
	}

Regards


José Luis
User avatar
mobthehop
Trader
Posts: 362
Joined: Wed Nov 16, 2011 12:16 am

Re: FIFO for SH's shell EA

Post by mobthehop »

JL,

great stuff! Too late for me now but will will stick the snippet in tomorrow my time and let you know...

Cheers - and thank you very very much! Mop
jlcgarcia

Re: FIFO for SH's shell EA

Post by jlcgarcia »

mobthehop wrote:JL,

great stuff! Too late for me now but will will stick the snippet in tomorrow my time and let you know...

Cheers - and thank you very very much! Mop

Hi Mob,

A more workable and ready version will be this:

Code: Select all




extern bool UseFIFO                 = true;

....
void CloseAllActiveOrders()
{
  while (AllActiveOrders()>0)
  {  
 
      bool Success;
   int Closed,CloseCount;
   int CloseTrades[,2];
   double OPrice;
   
for(int y=OrdersTotal()-1;y>=0;y--)
   {   if(!OrderSelect(y,SELECT_BY_POS,MODE_TRADES))continue;ArrayResize      (CloseTrades,CloseCount+1);
      CloseTrades[CloseCount,0]=OrderOpenTime();
      CloseTrades[CloseCount,1]=OrderTicket();
      CloseCount++;

   }

if(CloseCount>0)
   {   if(!UseFIFO)ArraySort(CloseTrades,WHOLE_ARRAY,0,MODE_DESCEND);
      else if(CloseCount!=ArraySort(CloseTrades))Print("Error sorting CloseTrades Array");
      for(y=0;y<CloseCount;y++)
      {   if(!OrderSelect(CloseTrades[y,1],SELECT_BY_TICKET))continue;
         while(IsTradeContextBusy())Sleep(100);
         if(IsStopped())return(-1);
         if(OrderType()>OP_SELL)Success=OrderDelete(OrderTicket(),CLR_NONE);
         else
         {   if(OrderType()==OP_BUY)OPrice=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),MarketInfo(OrderSymbol(),MODE_DIGITS));
            else OPrice=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS));
            Success=OrderClose(OrderTicket(),OrderLots(),OPrice,Slippage,CLR_NONE);
         }
         if(Success)Closed++;
         
      }
     
   }
    
  }
 Print("All Active Orders Have Been Closed!");
  
}


int AllActiveOrders()
{
   int num = 0;
   for(int i=0;i<OrdersTotal();i++)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderType() == OP_BUY || OrderType() == OP_SELL )  num++;
     }
   return(num);
}

Pleased check, and thell me if it works.

Regards


José Luis
User avatar
mobthehop
Trader
Posts: 362
Joined: Wed Nov 16, 2011 12:16 am

Re: FIFO for SH's shell EA

Post by mobthehop »

JL,

took me a while to figure out - please note that you forgot to copy in one line in the "ready version" posted above this, but no biggy....

Code: Select all

if(Closed==CloseCount) return (true); 
So, for any member in "FIFO jail" using SH's Shell, here is the code snippet adapted for SH's naming convention - first and last code line included for easier orientation where to place the snippet..... OpenTrades is set via CountOpenTrades() section of shell.....

Code: Select all

//void LookForTradingOpportunities()

bool CloseTrade(int ticket)
{   
   bool result;
   //if (OpenTrades = 1)
   //result = OrderClose(ticket, OrderLots(), OrderClosePrice(), 1000, CLR_NONE);
   
   //else 
   {
      while (OpenTrades > 0)
      {
       int Closed,CloseCount;
       int CloseTrades[,2];
       double OPrice;

         for(int y=OrdersTotal()-1;y>=0;y--)
         {   if(!OrderSelect(y,SELECT_BY_POS,MODE_TRADES))continue;ArrayResize      (CloseTrades,CloseCount+1);
          CloseTrades[CloseCount,0]=OrderOpenTime();
          CloseTrades[CloseCount,1]=OrderTicket();
          CloseCount++;

         }

        if(CloseCount>0)
       {   if(!UseFIFO)ArraySort(CloseTrades,WHOLE_ARRAY,0,MODE_DESCEND);
          else if(CloseCount!=ArraySort(CloseTrades))Print("Error sorting CloseTrades Array");
          for(y=0;y<CloseCount;y++)
          {   if(!OrderSelect(CloseTrades[y,1],SELECT_BY_TICKET))continue;
             while(IsTradeContextBusy())Sleep(100);
             if(IsStopped())return(-1);
             //if(OrderType()>OP_SELL)result=OrderDelete(OrderTicket(),CLR_NONE);
             else
             {   if(OrderType()==OP_BUY)OPrice=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),MarketInfo(OrderSymbol(),MODE_DIGITS));
                else OPrice=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS));
                result=OrderClose(OrderTicket(),OrderLots(),OPrice,1000,CLR_NONE);
             }
             if(result)Closed++;
          }
         if(Closed==CloseCount) return (true);
       }

      }
      }
   //Actions when trade send succeeds


JL, thank you very much for helping out / Mop
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Re: FIFO for SH's shell EA

Post by SteveHopwood »

Great stuff mop. I never thought of the FIFO stuff. When you are confident your code works, I will add it to my shell then have a InFifoJail input for the user.

:D
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
User avatar
mobthehop
Trader
Posts: 362
Joined: Wed Nov 16, 2011 12:16 am

Re: FIFO for SH's shell EA

Post by mobthehop »

SH,

thanks but all credit goes to JL! I just did the adaptation to your variable nomenclature...

Not sure that this needs to be added to the shell as it only applies to same-pair-multiple-orders EA for our friends across the pond.... but leave that in your capable hands...

The code works just fine, it is in proper use for a couple of days already (on demo)....

Cheers & JL really appreciate your coding skill, would love if you could share more so that we all can learn from you! (Hint!)

Mop
User avatar
mobthehop
Trader
Posts: 362
Joined: Wed Nov 16, 2011 12:16 am

Re: FIFO for SH's shell EA

Post by mobthehop »

Sorry, there is a bug in (my) the code.... I am trying to locate it... Goes without saying, dont use it!

JL, or anybody else familiar with arrays, where would be the best place to stick this additional check in my code above:

if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;

would this work:

{ if(!OrderSelect(y,SELECT_BY_POS,MODE_TRADES)&& (OrderSymbol()!=Symbol() || OrderMagicNumber()!=MagicNumber) continue;ArrayResize (CloseTrades,CloseCount+1);

Sorry for the bother but the error is only apparent in multi-pair / multi lot situations and somebody with proper coding knowledge would save me a lot of time to field test this....

Cheers

Mop
jlcgarcia

Re: FIFO for SH's shell EA

Post by jlcgarcia »

mobthehop wrote:Sorry, there is a bug in (my) the code.... I am trying to locate it... Goes without saying, dont use it!

JL, or anybody else familiar with arrays, where would be the best place to stick this additional check in my code above:

if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=magic) continue;

would this work:

{ if(!OrderSelect(y,SELECT_BY_POS,MODE_TRADES)&& (OrderSymbol()!=Symbol() || OrderMagicNumber()!=MagicNumber) continue;ArrayResize (CloseTrades,CloseCount+1);

Sorry for the bother but the error is only apparent in multi-pair / multi lot situations and somebody with proper coding knowledge would save me a lot of time to field test this....

Cheers

Mop

Hi Mob,

I think you are right with your coding. The code must be something like this (if you want to use the same magicnumber for several pairs:

Code: Select all

//void LookForTradingOpportunities()





bool CloseTrade(int ticket)
{   
   bool result;
   //if (OpenTrades = 1)
   //result = OrderClose(ticket, OrderLots(), OrderClosePrice(), 1000, CLR_NONE);
   
   //else 
   {
      while (OpenTrades > 0)
      {
       int Closed,CloseCount;
       int CloseTrades[,2];
       double OPrice;

         for(int y=OrdersTotal()-1;y>=0;y--)
         {   if(!OrderSelect(y,SELECT_BY_POS,MODE_TRADES))continue;
	
	if(OrderSymbol()!=Symbol() && OrderMagicNumber()!=Magic)continue; //This is the sentece added
	ArrayResize      (CloseTrades,CloseCount+1);
          CloseTrades[CloseCount,0]=OrderOpenTime();
          CloseTrades[CloseCount,1]=OrderTicket();
          CloseCount++;

         }

        if(CloseCount>0)
       {   if(!UseFIFO)ArraySort(CloseTrades,WHOLE_ARRAY,0,MODE_DESCEND);
          else if(CloseCount!=ArraySort(CloseTrades))Print("Error sorting CloseTrades Array");
          for(y=0;y<CloseCount;y++)
          {   if(!OrderSelect(CloseTrades[y,1],SELECT_BY_TICKET))continue;
             while(IsTradeContextBusy())Sleep(100);
             if(IsStopped())return(-1);
             //if(OrderType()>OP_SELL)result=OrderDelete(OrderTicket(),CLR_NONE);
             else
             {   if(OrderType()==OP_BUY)OPrice=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),MarketInfo(OrderSymbol(),MODE_DIGITS));
                else OPrice=NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS));
                result=OrderClose(OrderTicket(),OrderLots(),OPrice,1000,CLR_NONE);
             }
             if(result)Closed++;
          }
         if(Closed==CloseCount) return (true);
       }

      }
      }
   //Actions when trade send succeeds

Regards


José Luis
User avatar
mobthehop
Trader
Posts: 362
Joined: Wed Nov 16, 2011 12:16 am

Re: FIFO for SH's shell EA

Post by mobthehop »

José Luis,

Thank you for taking the time! Actually each pair should use it's own magic number, but the check only open orders of same pair are closed is absolutely necessary [if(OrderSymbol()!=Symbol()] & was not not present before... I give it it try and will report back....

Muchas gracias JL....
Post Reply

Return to “Coders Hangout”