"Hedging" in Forex trading -Why do it? - page 7

 
//+------------------------------------------------------------------+
//|                                          Sonic the hedge hog.mq4 |
//|      Copyright 2018, Marco vd Heijden, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//|             The code should be used for educational purpose only.|
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, Marco vd Heijden, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

datetime time;
int TrailingStop=100;
double Lots=0.01;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   time=iTime(_Symbol,PERIOD_D1,0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   exit();
   enter();
  }
//+------------------------------------------------------------------+
//| enter
//+------------------------------------------------------------------+
void enter()
  {
//--- new bar ?
   if(time!=iTime(_Symbol,PERIOD_D1,0))
     {
      int ticket_buy=-1;
        {
         while(ticket_buy<0)
           {
            ticket_buy=OrderSend(_Symbol,OP_BUY,Lots,Ask,3,NULL,NormalizeDouble(Bid+100*Point,Digits),"Sonic",1234,0,clrGreen);
           }
        }
      int ticket_sell=-1;
        {
         while(ticket_sell<0)
           {
            ticket_sell=OrderSend(_Symbol,OP_SELL,Lots,Bid,3,NULL,NormalizeDouble(Ask-100*Point,Digits),"Sonic",1234,0,clrRed);
           }
        }
      if(ticket_buy>0 && ticket_sell>0)
        {
         time=iTime(_Symbol,PERIOD_D1,0);
        }
     }
  }
//+------------------------------------------------------------------+
//| exit
//+------------------------------------------------------------------+
void exit()
  {
//--- it is important to enter the market correctly, but it is more important to exit it correctly...   
   for(int cnt=OrdersTotal(); cnt>=0; cnt--)
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(TimeDay(OrderOpenTime())!=TimeDay(time))
           {
            bool close=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrBlue);
              {
               if(close==0)
                 {
                  Print("OrderCloseError"+IntegerToString(OrderTicket()));
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+

Pretty straightforward.

I run it over last year.

So i expect the same results, or as some say, even better from the other technique.

I won't have any problem accepting what comes out of this, and i am not trying to push through invalid truths.

Just trying to keep the forum vibrant, a little competition in stead of all the i need, i want, topics.

 
Marco vd Heijden:

Pretty straightforward.

I run it over last year.

So i expect the same results, or as some say, even better from the other technique.

I won't have any problem accepting what comes out of this, and i am not trying to push through invalid truths.

Just trying to keep the forum vibrant, a little competition in stead of all the i need, i want, topics.

High Marco,

Here's the code modified to work without hedging.


	          
 

Sorry,

small error.

Be back soon.

 

Here's the code for no hedging

//+------------------------------------------------------------------+
//|                                          Sonic the hedge hog.mq4 |
//|      Copyright 2018, Marco vd Heijden, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//|             The code should be used for educational purpose only.|
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, Marco vd Heijden, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

datetime time;
//int TrailingStop=100;
double Lots=0.01;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   time=iTime(_Symbol,PERIOD_D1,0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   exit();
   enter();
  }
//+------------------------------------------------------------------+
//| enter
//+------------------------------------------------------------------+
void enter()
  {
   static double BuyAt=0,SellAt=0,BuyTP=0,SellTP=0;

//--- new bar ?
   if(time!=iTime(_Symbol,PERIOD_D1,0))
     {
      SellAt=NormalizeDouble(Bid+100*Point,Digits);
      SellTP=NormalizeDouble(Ask-100*Point,Digits);
      BuyAt=NormalizeDouble(Ask-100*Point,Digits);
      BuyTP=NormalizeDouble(Bid+100*Point,Digits);
      time=iTime(_Symbol,PERIOD_D1,0);
     }

   if(SellAt>0 && Bid>=SellAt)
     {
      int ticket_sell=-1;
      ticket_sell=OrderSend(_Symbol,OP_SELL,Lots,Bid,3,NULL,SellTP,"Sonic",1234,0,clrRed);
      if(ticket_sell>0)
        {
         BuyAt=0;
         SellAt=0;
        }
     }

   if(BuyAt>0 && Ask<=BuyAt)
     {
      int ticket_buy=-1;
      ticket_buy=OrderSend(_Symbol,OP_BUY,Lots,Ask,3,NULL,BuyTP,"Sonic",1234,0,clrGreen);
      if(ticket_buy>0)
        {
         BuyAt=0;
         SellAt=0;
        }
     }
  }
//+------------------------------------------------------------------+
//| exit
//+------------------------------------------------------------------+
void exit()
  {
//--- it is important to enter the market correctly, but it is more important to exit it correctly...   
   for(int cnt=OrdersTotal()-1; cnt>=0; cnt--)
      //+------------------------------------------------------------------+
      //|                                                                  |
      //+------------------------------------------------------------------+
     {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(TimeDay(OrderOpenTime())!=TimeDay(time))
           {
            bool close=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrBlue);
              {
               if(close==false)
                 {
                  Print("OrderCloseError"+IntegerToString(OrderTicket()));
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
 

Okay thanks, i compiled it and here is the result:


 
Marco vd Heijden:

Okay thanks, i compiled it and here is the result:


Please try it on Every Tick, Open prices are no good for testing this type of trading where trades are entered at certain levels.

And, of course, test it against yours over the same data.

 
Okay they both look very much alike when run every tick.
 
Marco vd Heijden:
Okay they both look very much alike when run every tick.


So ... without applying the so called hedging technique mentioned in this topic, the respected coders on our forum showed that the strategy proposed by Marco can also be implemented in a different way with some additional financial benefits.

In fact, "any arbitrarily selected" strategy can be shown so ... and this universally proves the statement of Keith in his first post "it is not just my opinion that "hedging" in Forex is unproductive and pointless, it is a fact."


Noticed that?

This result agrees with the Occam's Razor, stated as "plurality should not be posited without necessity" or as "entities are not to be multiplied beyond necessity".

So, be simple for perfection ... or namely be simple for saving yourself from additional costs and unnecessary risks without any real benefit in this case.


Happy strategy designing )

 

Well the show isn't over till the fat lady sings.

Stay tuned.

 
Marco vd Heijden:
Okay they both look very much alike when run every tick.

Did you not see that my version produced a slight increase in profit?

Reason: