Hedging With The Man

I’m gonna make it strange, for once in my life.

I’m flirting hedging with the Man in the Mirror.

You can try it at home, but do not bother with IC Markets, for they would suspend your account using the bandwidth for placing and deleting pending orders (hyperactivity they call it).

// Hedge 50 pips out for 1-position by Macdulio (forexfore.blog) in 2022

#include <stdlib.mqh>
extern int magic_number = 60;
extern int magic_number2 = 61;
int profits;
double Lots;
  double nakedshorts[];
  double nakedlongs[];
  
  double open_price;
  double stop_loss_price;
  double take_profit_price;
  double open_price2;
  double stop_loss_price2;
  double take_profit_price2;  
     
   double OrderOpenPrice;
 double OrderProfit;
 double orderstoploss = OrderStopLoss();
  string symbol = Symbol();  

int init() {
   return(0);
}

int deinit() {
  return(0);
}

int start() {
  double longaveragebuffer;
double shortaveragebuffer;
int order_type;
   
int i, counter;
int counted_bars=IndicatorCounted();
int longcount = 0;
int shortcount = 0;
double nlongs =0;
double nshorts= 0;
double shortopenedat;
double longopenedat;
double longsize;
double shortsize;

profits = 0;
 int hstTotal=OrdersHistoryTotal();

 counter = 0;
int overdrivecounter =0;


 for(i=OrdersTotal()-1; i>=0 ; i--)
 {   
   
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
        { 
           Print("Access to orders list failed with error (",GetLastError(),")");
           break;
        }
             if (OrderType() == OP_BUY && OrderMagicNumber()!=magic_number) 
             {
               nlongs = nlongs+OrderLots();
               longcount = longcount+1;
               longaveragebuffer = longaveragebuffer+(OrderOpenPrice()*OrderLots()); 
               longopenedat = NormalizeDouble(OrderOpenPrice(),5);   
               longsize = NormalizeDouble(OrderLots(),2);
             }  
               
             if (OrderType() == OP_SELL && OrderMagicNumber()!=magic_number2 )
             {
               nshorts = nshorts+OrderLots(); 
               shortcount = shortcount+1;         
               shortaveragebuffer = shortaveragebuffer+(OrderOpenPrice()*OrderLots());
               shortopenedat = NormalizeDouble(OrderOpenPrice(),5);
               shortsize = NormalizeDouble(OrderLots(),2);              
             }
                      
   }

if (shortsize!=0) Print ("shortsize:",shortsize);
if (longsize!=0) Print ("longsize:",longsize);
   
 
double  account = AccountEquity(); 
//double MaxLots = NormalizeDouble(AccountEquity()*.00265,2);    
double MaxLots = NormalizeDouble(AccountEquity()/2/1000,2);     

   double longsz;
   if (nshorts==0 && Close[0])  longsz=NormalizeDouble((MaxLots-nlongs),2);
   else longsz = NormalizeDouble((nshorts-nlongs),2);
   if (longsz>MarketInfo(symbol,MODE_MAXLOT)) longsz=MarketInfo(symbol,MODE_MAXLOT); 
    
    

  if (nlongs-nshorts<=0 && shortsize>0 && shortopenedat+500*Point>Ask)
  {
   open_price = NormalizeDouble(shortopenedat+500*Point, 5);
    stop_loss_price = NormalizeDouble(0.0,Digits);
    take_profit_price = NormalizeDouble(0,Digits);
 for (i = OrdersTotal() - 1; i >= 0; i--)

      if (OrderSelect(i, SELECT_BY_POS))
        if (OrderMagicNumber() == magic_number) {
          order_type = OrderType();
          if (order_type == ORDER_TYPE_BUY_STOP) {
            if ((NormalizeDouble(OrderOpenPrice(), Digits) != open_price) || (NormalizeDouble(OrderStopLoss(), Digits) != stop_loss_price) || (NormalizeDouble(OrderTakeProfit(), Digits) != take_profit_price)) {
              if (!OrderModify(OrderTicket(), open_price, stop_loss_price, take_profit_price, OrderExpiration()))
                Print("Error: ", ErrorDescription(_LastError));
            }
            break;
          }
          else if (order_type == ORDER_TYPE_BUY)
            break;
        }
    if (i < 0)
      if (OrderSend(symbol, OP_BUYSTOP,  shortsize, open_price, 3, stop_loss_price, take_profit_price, magic_number+" Hedge Break Of - BUY", magic_number,0) < 0)
        Print("Error: ", ErrorDescription(_LastError));
  }

 
   double shortsz;
   
  if (nlongs==0) shortsz = NormalizeDouble((MaxLots-nshorts),2);
  else shortsz = NormalizeDouble((nlongs-nshorts),2);
  if (shortsz>MarketInfo(symbol,MODE_MAXLOT)) shortsz=MarketInfo(symbol,MODE_MAXLOT);

 
 
 if (nshorts-nlongs<=0 && longsize>0 && longopenedat-500*Point<Bid)
   {
  
   open_price2 = NormalizeDouble(longopenedat-500*Point, 5);
    stop_loss_price2 = NormalizeDouble(0,Digits);
    take_profit_price2 = NormalizeDouble(0,Digits);
    for (i = OrdersTotal() - 1; i >= 0; i--)
      if (OrderSelect(i, SELECT_BY_POS))
        if (OrderMagicNumber() == magic_number2) {
          order_type = OrderType();
          if (order_type == ORDER_TYPE_SELL_STOP) {
            if ((NormalizeDouble(OrderOpenPrice(), Digits) != open_price2) || (NormalizeDouble(OrderStopLoss(), Digits) != stop_loss_price2) || (NormalizeDouble(OrderTakeProfit(), Digits) != take_profit_price2)) {
              if (!OrderModify(OrderTicket(), open_price2, stop_loss_price2, take_profit_price2, OrderExpiration()))
                Print("Error: ", ErrorDescription(_LastError));
            }
            break;
          }
          else if (order_type == ORDER_TYPE_SELL)
            break;
        }
    if (i < 0)
      if (OrderSend(symbol, OP_SELLSTOP, longsize, open_price2, 3, stop_loss_price2, take_profit_price2, magic_number2+" Hedge Break Of - SELL", magic_number2,0) < 0)
        Print("Error: ", ErrorDescription(_LastError));
  }
  
        
  return(0);
}

This is one free piece of the puzzle. The 50 pip was designed for an accurate entry, an oversold / overbought neckline relative to the current consolidation mean. In theory it should never get a fill if you can figure the direction right (where was the last qualifying head & shoulder) and if you don’t get unlucky overnight with an extra 10 pips in both direction on the bid and the ask from your broker.

I wrote the program blind (offline, over the weekend), if it does not function well, come back I’ll update it.

It was designed for one single entry, yes. What does the routine do if you open a second position? Leave it in the comments.

CI has not recharged yet for a new center to be in place.

The thinking is of course simplifying things. One single position, that is the max size that a 50-pip move against would not result in a 50% draw down. The management of the hedge is based on understanding where to exit it and capturing back some / all of that temporary loss during a counter move. Protective stop loss once in the clear is a good idea. Gotta learn when to have the automated trading button temporarily off.

Guy’s a genius.

Ok, Madis is Bringin’ it too.

Now, let’s talk about that close again. It settled on a Green C, because the stochastic went higher than the target range (buying got too eager). You could still see a green period after this with a Green Laser Beam on Monday or simply a float sideways to up, but I would surrender all upside hopes upon tagging the E-9 (Red).