House +45 Overhedger

I ran out of color options a long time ago, but the arrow shows the 45-pip displacement beyond the edge of the house.

What I am trying to capitalize in on is the knowledge that price would turn back 75% of the time before that value. If not, something else is up, so you need to pick up hedge / overhedge (10% or what you change it to) to make up for some loss if you were underweight in that direction.

I have not tried it, but I trust it works. Have to peg it to a 1-hour corresponding EURUSD ticker, which is the “+” in my case at my broker, for I trade the raw spread, not the fixed one.

You can comment out with “//” in front of the two

“if (comparison<Ratio) RemoveStopsandTargets();” function calls if you do not want the stops and targets removed below the equity/balance ratio before compiling it.

// House + 45 Overhedger by Macdulio for Automated Trading

#include <stdlib.mqh>
extern int percentage = 10;
extern int magic_number = 55;
extern int magic_number2 = 66;
extern int magic_number3 = 77;
extern double Ratio = 0.6;
#property copyright "by Macdulio in 2025" 
#property link      "https://forexfore.blog" 
#property description "House + 45 Overhedger"  

int profits;

  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 RSI2[];

 double OrderOpenPrice;
 double OrderProfit;
double ExtUpperBuffer[];
double ExtLowerBuffer[];
double ExtUpperBuffer_[],ExtLowerBuffer_[];
double ExtUpperBuffer_d[],ExtLowerBuffer_d[];
  string symbol = Symbol();   
int init() {
   return(0);
}

int deinit() {
  return(0);
}

int start() {

  ArrayResize(RSI2, 600);   
ArrayInitialize(RSI2, 0); 

  ArrayResize(ExtUpperBuffer, Bars);
  ArrayInitialize(ExtUpperBuffer,EMPTY_VALUE); 
     ArrayResize(ExtLowerBuffer, Bars);
  ArrayInitialize(ExtLowerBuffer, EMPTY_VALUE); 
 ArrayResize(ExtUpperBuffer_, Bars);
  ArrayInitialize(ExtUpperBuffer_,EMPTY_VALUE); 
     ArrayResize(ExtLowerBuffer_, Bars);
  ArrayInitialize(ExtLowerBuffer_, EMPTY_VALUE);  
ArrayResize(ExtUpperBuffer_d, Bars);
  ArrayInitialize(ExtUpperBuffer_d,EMPTY_VALUE); 
     ArrayResize(ExtLowerBuffer_d, Bars);
  ArrayInitialize(ExtLowerBuffer_d, EMPTY_VALUE); 

int i, counter;
int counted_bars=IndicatorCounted();
int longcount, shortcount;
double nlongs;
double nshorts;
double longaveragebuffer;
double shortaveragebuffer;
int order_type;
  
profits = 0;
 int hstTotal=OrdersHistoryTotal();

 counter = 0;

 // Print("Ratio = ", Ratio);


for(i=100; i>=0; i--){
  RSI2[i]=iRSI(symbol,30,2,PRICE_MEDIAN,i);
   ExtUpperBuffer[i]=iLow(symbol,0,iLowest(symbol,0,MODE_LOW,12,i))+460*Point;
       ExtLowerBuffer[i]=iHigh(symbol,0,iHighest(symbol,0,MODE_HIGH,12,i))-460*Point;
         
         if (ExtUpperBuffer[i]>ExtLowerBuffer[i]) {ExtUpperBuffer_[i]=ExtUpperBuffer[i]; ExtLowerBuffer_[i]=ExtLowerBuffer[i];
         
         ExtUpperBuffer_d[i]=ExtUpperBuffer_[i]+450*Point; ExtLowerBuffer_d[i]=ExtLowerBuffer_[i]-450*Point;
         }

} 

 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 ( Symbol()=="EURUSD" && OrderStopLoss()==0 ) {
          // Print("OderProfit: ", OrderProfit());
          
          //Print("AB:",AccountBalance()*-1*.025);
             if (OrderType() == OP_BUY) 
             {
               nlongs = nlongs+OrderLots();
               longcount = longcount+1;
               longaveragebuffer = longaveragebuffer+(OrderOpenPrice()*OrderLots()); 
                  
             }  
               
             if (OrderType() == OP_SELL )
             {
               nshorts = nshorts+OrderLots(); 
               shortcount = shortcount+1;         
               shortaveragebuffer = shortaveragebuffer+(OrderOpenPrice()*OrderLots()); 
             }
         // }
   }
   
   if (nlongs!=nshorts){
   
   
   double comparison = (AccountEquity()/AccountBalance())*100;
   
 //   Print("comparison = ", comparison);


              
              

  // Overghedge for shorts
  if (nlongs<nshorts && ExtUpperBuffer_d[1]!=EMPTY_VALUE && Close[0]>ExtUpperBuffer_d[1] && Close[1]<ExtUpperBuffer_d[1]) {
  
   if (comparison<Ratio) RemoveStopsandTargets();
    open_price2 = NormalizeDouble(Ask, Digits);
    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_BUY) {
            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_BUY)
            break;
        }
    if (i < 0)
      if (OrderSend(symbol, OP_BUY,  NormalizeDouble(nshorts*((100+percentage)/100)-nlongs,2), open_price2, 3, stop_loss_price2, take_profit_price2, magic_number2+" House + 45 Overhedger BUY 0/0",  magic_number2) < 0)
        
               Print("Error: ", ErrorDescription(_LastError));
  }
  else
    for (i = OrdersTotal() - 1; i >= 0; i--)

      if (OrderSelect(i, SELECT_BY_POS))
        if (OrderMagicNumber() == magic_number2 )
          if (OrderType() == ORDER_TYPE_BUY_STOP)
            if (!OrderDelete(OrderTicket()))
              Print("Error: ", ErrorDescription(_LastError));              
              
 
              
              
              
              
// Overhedge For Longs
 if (nshorts<nlongs && ExtLowerBuffer_d[1]!=EMPTY_VALUE && Close[0]<ExtLowerBuffer_d[1] && Close[1]>ExtLowerBuffer_d[1] ) {
  
 if (comparison<Ratio) RemoveStopsandTargets();
    open_price2 = NormalizeDouble(Bid, Digits);
    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) {
            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_SELL, NormalizeDouble(nlongs*((100+percentage)/100)-nshorts,2), open_price2, 3, stop_loss_price2, take_profit_price2, magic_number2+"  House + 45 Overhedger 0/0", magic_number2) < 0)
        Print("Error: ", ErrorDescription(_LastError));
  }
  else
    for (i = OrdersTotal() - 1; i >= 0; i--)
      if (OrderSelect(i, SELECT_BY_POS))
        if (OrderMagicNumber() == magic_number2)
          if (OrderType() == ORDER_TYPE_SELL_STOP)
            if (!OrderDelete(OrderTicket()))
              Print("Error: ", ErrorDescription(_LastError));              
 
 

        
  return(0);
}
}

double RemoveStopsandTargets()
{
   int i;
   for (i = OrdersTotal() - 1; i >= 0; i--){
   if( OrderSelect(i,SELECT_BY_POS, MODE_TRADES)) 
    if( OrderType()==OP_BUYSTOP && MathAbs(OrderOpenPrice()-Ask)<.003) 
       OrderDelete( OrderTicket() );
       else if( OrderType()==OP_SELLSTOP && MathAbs(OrderOpenPrice()-Bid)<.003) 
                 OrderDelete(OrderTicket());}
                 
 
 for (i = OrdersTotal() - 1; i >= 0; i--){
  
   if( OrderSelect(i,SELECT_BY_POS)){

  

          if (OrderType() == OP_SELL && OrderMagicNumber()!=magic_number3){
              if (!OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(0,4),  NormalizeDouble(0,4), OrderExpiration()))
                Print("Error: ", ErrorDescription(_LastError));
           }
   
     if (OrderType() == OP_BUY && OrderMagicNumber()!=magic_number3) { 
                   
              if (!OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(0,4),  NormalizeDouble(0,4), OrderExpiration()))
                Print("Error: ", ErrorDescription(_LastError));
          }
     }     
   }
 return(0);
 
}