stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

FIFO for SH's shell EA
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=2095
Page 1 of 1
Author:  mobthehop [ Tue May 07, 2013 12:22 pm ]
Post subject:  FIFO for SH's shell EA

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
Author:  jlcgarcia [ Tue May 07, 2013 12:54 pm ]
Post subject:  Re: FIFO for SH's shell EA

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
Author:  mobthehop [ Tue May 07, 2013 2:07 pm ]
Post subject:  Re: FIFO for SH's shell EA

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
Author:  jlcgarcia [ Tue May 07, 2013 3:38 pm ]
Post subject:  Re: FIFO for SH's shell EA

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
Author:  mobthehop [ Wed May 08, 2013 5:41 am ]
Post subject:  Re: FIFO for SH's shell EA

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
Author:  SteveHopwood [ Sun May 12, 2013 5:32 pm ]
Post subject:  Re: FIFO for SH's shell EA

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
Author:  mobthehop [ Sun May 12, 2013 5:59 pm ]
Post subject:  Re: FIFO for SH's shell EA

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
Author:  mobthehop [ Tue May 14, 2013 1:20 pm ]
Post subject:  Re: FIFO for SH's shell EA

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
Author:  jlcgarcia [ Tue May 14, 2013 3:16 pm ]
Post subject:  Re: FIFO for SH's shell EA

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
Author:  mobthehop [ Tue May 14, 2013 3:42 pm ]
Post subject:  Re: FIFO for SH's shell EA

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....
All times are UTC Page 1 of 1