MPTM's new home

MPTM's new home
Locked
User avatar
gaheitman
Trader
Posts: 655
Joined: Tue Nov 15, 2011 10:55 pm
Location: Richmond, VA, US

Re: MPTM's new home

Post by gaheitman »

dusktrader wrote:IDEA #1: FIFO compliant with Global Order Closure
I use the Global Order Closure feature almost daily, because my research has shown that trades left open into the Asian session tend to become losers. Around 5pm Eastern (NY close) I implement Global Order Closure to gracefully terminate any open positions. One problem I've seen is that MPTM does not follow FIFO rules, apparently. When it issues a closure order "out of order" I get an error in my platform and that trade does not close. This might be a really simple thing to fix, such as sorting the active orders by order number. I'm not 100% sure how FIFO works, but I think a sequence number could be available to help with this.
I agree with Steve, tramping around inside MPTM is probably not a good idea. There are an awful lot of people very happy with it and using it daily. It would be a shame to break something and even worse to change the behaviour they all expect and rely upon.

In the spirit of the holidays, however, I pulled out some of my existing code and made a FIFO compliant (as I understand the acronym, not the rules :D) GlobalOrderClosure routine. Feel free to add it to YOUR version of MPTM. Of course, be certain to adequately test it before using it in production -- I did not....

In the interest of increasing your knowledge and understanding of MQL, I'd also be happy to review any changes you make to the code to make it more resilient. As you allude to, there is no error checking in the existing routine.

Look for "gah" to see where my changes start.

Code: Select all

void GlobalOrderClosureFIFO()
{
   bool CloseOrders=false;
   double ProfitPercentage=0;
   
   // First calculate whether the upl is >= the point at which the position is to close

   // Profit in dollars enabled
   if (ProfitInDollars)
   {
      if(AccountProfit()>=DollarProfit) CloseOrders=true;
   }
   
   // Profit as percentage of account balance enabled
   if (ProfitAsPercentageOfBalance)
   {
      ProfitPercentage=AccountBalance() * (PercentageProfit/100);
      if (AccountProfit()>= ProfitPercentage) CloseOrders=true;
   }


   //Profit in pips
   if (ProfitInPips)
   {
      Pips = CalculatePipsProfit();
      //Dynamic pips target
      if (UseDynamicPipsProfit)
      {
         PipsProfit = OrdersTotal() * PipsPerTrade;         
      }//if (UseDynamicPipsProfit)     
      
      if (Pips >= PipsProfit) CloseOrders=true;
   }//if (ProfitInPips)
   
   
   // Abort routine if profit has not hit the required level
   if (!CloseOrders) return(0);
   
   // Got this far, so orders are to be closed.
   // Code lifted from CloseAll-PL, so thanks to whoever wrote the ea. Ok, so I could
   // have written my own, buy why re-invent the wheel?
   
   int _total=OrdersTotal(); // number of lots or trades  ????
   int _ordertype;// order type   
   if (_total==0) {return;}  // if total==0
   int _ticket; // ticket number
   double _priceClose;// price to close orders;
   
   //added - gah
   //build an array of all tickets to manage so we don't 
   //have to worry about closing them as we go.
   int OpenTickets[];                         //array to hold all our ticket numbers
   ArrayResize(OpenTickets, _total);           //make it big enough to hold all of them
   ArrayInitialize(OpenTickets, 0);
   int i;                                     //i will iterate over all orders
   for(i = 0; i < _total; i++) {
      if(OrderSelect(i, SELECT_BY_POS)) {
         //skip the ones that don't match our symbol and MN
         OpenTickets[i] = OrderTicket(); //It's a match, save it to the array
       }//if(OrderSelect(i, SELECT_BY_POS))
   }//for(i = 0; i < OrdersTotal(); i++)
   
   for(int _i=0;_i<_total;_i++)
         {  //# for loop
         if (OrderSelect(OpenTickets[_i], SELECT_BY_TICKET) )   //gah - pick from our array
            {
            _ordertype=OrderType();
            _ticket=OrderTicket();
          switch(_ordertype)
               {  //# switch
          case OP_BUYLIMIT:
            if(IncludePendingOrdersInClosure) OrderDelete(OrderTicket());
          case OP_BUYSTOP:
            if(IncludePendingOrdersInClosure) OrderDelete(OrderTicket());
          case OP_BUY:
                  // close buy                
                  _priceClose=MarketInfo(OrderSymbol(),MODE_BID);
                  Print("Close on ",_i," position order with ticket ¹",_ticket);
                  OrderClose(_ticket,OrderLots(),_priceClose,10,Red);
                  break;
          case OP_SELLLIMIT:
            if(IncludePendingOrdersInClosure) OrderDelete(OrderTicket());
          case OP_SELLSTOP:
            if(IncludePendingOrdersInClosure) OrderDelete(OrderTicket());
          case OP_SELL:
                  // close sell
                  _priceClose=MarketInfo(OrderSymbol(),MODE_ASK);
                  Print("Close on ",_i," position order with ticket ¹",_ticket);
                  OrderClose(_ticket,OrderLots(),_priceClose,10,Red);
                  break;
               default:
                  // values from  1 to 5, deleting pending orders
   //               if (PrintToJournal) Print("Delete on ",_i," position order with ticket ¹",_ticket);
   //               OrderDelete(_ticket);  
                  break;
               }    //# switch
         }  // # if 
   }  // # for loop

   // User feedback
   if (ShowAlerts) Alert("Global profit hit your target, so all open trades should have been closed");
   
} //End of GlobalOrderClosureFIFO()
A couple of days ago, I posted another workaround for the FIFO issue someone was having in the Co-integration forum. It uses a different solution, one that checks for the specific FIFO error. It might be helpful to review that as well.

BTW, I also agree with Steve about coding and trading. I would not be a trader if I wasn't a coder already.

George
dusktrader
Trader
Posts: 13
Joined: Tue Nov 22, 2011 3:52 pm
Location: North Carolina

Re: MPTM's new home

Post by dusktrader »

Awesome -- thanks very much Steve and George. I will poke at this code during the next 3 weeks where I have to sit on my hands anyway. I'll let you know if your FIFO routine works. Honestly I'm a bit unclear on it myself. The email and spread handling features are most likely things I can do myself and will share back here if I can get them to work. FIFO handling is more complicated I think, so I really appreciate your input on this! Thanks again and Happy Holidays to you.
Image
dusktrader
Trader
Posts: 13
Joined: Tue Nov 22, 2011 3:52 pm
Location: North Carolina

Re: MPTM's new home

Post by dusktrader »

I just wanted to report back that it seems George's code snippets may be just what is needed for FIFO-picky brokers. I have run repeated tests on Oanda Empty4 demo using this code, and I have yet to see the FIFO violation error popup again. When I resume regular trading in the second week of January, I plan to test your code on my live Oanda Empty4 account.

In the process of all this, I found this FIFO description which helped me understand better what is happening.

ALSO, I was really excited to learn that, apparently, Cowboy IBFX does not have (outwardly visible) FIFO restrictions on US traders. After trying to manually force a FIFO violation and not getting one, I had this discussion with Cowboy IBFX tech support:
Chat InformationThank you for contacting us. The next available agent will be right with you.
Chat InformationYou are now chatting with 'Adminact'
Adminact: Hello, this is Andy. How may I help you?
none: Hello, I'm doing some testing with FIFO on a demo account. Can you verify if FIFO restrictions are in effect for demo acct #7092221? I didnt think I should be allowed to close a trade in the middle of a group of other trades of the same pair? Otherwise, can you point me to a description of how FIFO works with Cowboy IBFX? Thanks
Adminact: With Cowboy IBFX you can trade as you would like, the FIFO will happen automatically on the back end.
none: you mean, i will never get an error message like on my other broker that prevents an EA from closing a trade?
none: on the other broker, it says something like "trade XX cannot be closed due to a FIFO violation"
Adminact: No, that error should not occur with Cowboy IBFX
none: wow, very cool... do you still have a technical description of how FIFO is handled with Cowboy IBFX? i want to understand it better and see if there is anything I should be aware of with my EA and/or manual trading
Adminact: You can just trade how you want and positions equal to your ticket numbers will be closed simultaniously to comply with FIFO requirements.
Adminact: As such the close time of ticket numbers in your Official FIFO compliant statement and Trading platform may not be the exact same all of the time.
none: when you say "equal to my ticket numbers" are you referring to other Cowboy IBFX customers then? or do they have to balance in my own account?
Adminact: So, for example, you open 3 trades, a 1 lot buy, a 0.5 lot buy and a 2 lot buy in that order.
Adminact: you decide you want to close your 0.5 lot buy and leave the 1st and 3rd orders opened.
Adminact: The platform will let you close what you want when you want.
Adminact: however in the official FIFO statement you would show 0.5 lots being closed from the first 1.0 lots rather than the middle 0.5 lot trade being closed
none: what happens if the order stream is like this: first trade=1 lot, 2nd trade=5 lots, 3rd trade =2 lots. When I try to close the second trade of 5 lots, what will happen?
Adminact: you can close any trades you want in any order you want in the platform
none: ok, last questions then... this all sounds great... is it legal for US traders? and why doesn't Cowboy IBFX market this feature as something exclusive? ive never heard of this and other brokers do not work this way
Adminact: Yes, it is legal for US Traders.
Adminact: This is something that has been very good for many traders in the USA.
Adminact: There are other brokers that have different FIFO solutions.
Adminact: We feel that our FIFO solution is very awesome and most of our traders feel the same way.
Image
User avatar
gaheitman
Trader
Posts: 655
Joined: Tue Nov 15, 2011 10:55 pm
Location: Richmond, VA, US

Re: MPTM's new home

Post by gaheitman »

dusktrader wrote:I just wanted to report back that it seems George's code snippets may be just what is needed for FIFO-picky criminals. I have run repeated tests on Oanda Empty4 demo using this code, and I have yet to see the FIFO violation error popup again. When I resume regular trading in the second week of January, I plan to test your code on my live Oanda Empty4 account.

In the process of all this, I found this FIFO description which helped me understand better what is happening.

ALSO, I was really excited to learn that, apparently, Cowboy IBFX does not have (outwardly visible) FIFO restrictions on US traders.
Yep, I'm with them and they made a big deal about their implementation back when they made the change. It is handy. :>
pipmenow
Posts: 5
Joined: Mon Jan 30, 2012 12:39 pm

Re: MPTM's new home

Post by pipmenow »

Hello. I hope I am not out of place asking a question. I set up MPTM EA and am having a fun and an educational time playing with the input settings. Is there a method to include the variable spread differences in the close partial trade at the first jump? The EA is set to manage all pairs and since the variable spread is different for all pairs the partial close at the jumpingstop set to 20 pips results in pairs closing at 12 - 18 pips. Thank you.
User avatar
Alpenkorps
Trader
Posts: 213
Joined: Thu Dec 15, 2011 4:03 am

Re: MPTM's new home

Post by Alpenkorps »

pipmenow wrote:Hello. I hope I am not out of place asking a question. I set up MPTM EA and am having a fun and an educational time playing with the input settings. Is there a method to include the variable spread differences in the close partial trade at the first jump? The EA is set to manage all pairs and since the variable spread is different for all pairs the partial close at the jumpingstop set to 20 pips results in pairs closing at 12 - 18 pips. Thank you.
You need to adjust JS for different pairs. You can run multiple MPTM to manage different pairs.
Take my love, take my land, Take me where I cannot stand
I don't care, I'm still free, You can't take the sky from me
f76bronco
Posts: 5
Joined: Wed Feb 01, 2012 5:41 am

Re: MPTM's new home

Post by f76bronco »

Hi I'm getting a message trade not allowed in experts properties. Any thought's?
f76bronco
Posts: 5
Joined: Wed Feb 01, 2012 5:41 am

Re: MPTM's new home

Post by f76bronco »

Hi I'm getting a message trade not allowed in experts properties. Any thought's?
User avatar
Alpenkorps
Trader
Posts: 213
Joined: Thu Dec 15, 2011 4:03 am

Re: MPTM's new home

Post by Alpenkorps »

f76bronco wrote:Hi I'm getting a message trade not allowed in experts properties. Any thought's?
Its a Crim related problem, not EA. I found same error after Christmas and later found out that bloody crim is not allowing trades on exotics for low liquidity! Check if you can trade manually or not.
Take my love, take my land, Take me where I cannot stand
I don't care, I'm still free, You can't take the sky from me
f76bronco
Posts: 5
Joined: Wed Feb 01, 2012 5:41 am

Re: MPTM's new home

Post by f76bronco »

Tried 3 different brokers. Two were ECN's no luck. The smiley in the upper corner is a frown , a smiley. I wonder if the EA works with the Empty4 verison 4.0 build 409?
Locked

Return to “Utilities Indicators and Scripts”