//+------------------------------------------------------------------+
//|                                                   TradeHedge.mq4 |
//|                                      Copyright © 2008, Trader101 |
//|                                          trader101@optonline.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Trader101"
#property link      "trader101@optonline.net"

// Mods by Ron Thompson (ForexMT4.com)


// allow script to accept inputs from user
#property show_inputs

//user input
extern double    Lots = 1.0;
extern int       Slippage = 2;
extern bool      isIBFXmini=false;

// globals 
string mySymbol;
string postfix;

// trade management
double ask;
double bid;

string TradeComment="TradeHedgeBUYALL";
int MagicNumber=200809241653;


// used for verbose error logging
#include <stdlib.mqh>


//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int init()
  {

   if(isIBFXmini)
     {
      mySymbol="GBPUSDm";  OpenBuy();
      mySymbol="EURGBPm";  OpenBuy();
      mySymbol="GBPJPYm";  OpenBuy();
      mySymbol="USDCHFm";  OpenBuy();
      mySymbol="NZDUSDm";  OpenBuy();
      mySymbol="AUDJPYm";  OpenBuy();
      mySymbol="EURJPYm";  OpenBuy();

      mySymbol="EURUSDm";  OpenBuy();
      mySymbol="USDJPYm";  OpenBuy();
      mySymbol="AUDUSDm";  OpenBuy();
      mySymbol="NZDJPYm";  OpenBuy();
      mySymbol="GBPCHFm";  OpenBuy();
      mySymbol="CHFJPYm";  OpenBuy();
      mySymbol="EURCHFm";  OpenBuy();
     }
   else
     {
      mySymbol="EURJPY";  OpenBuy();
      mySymbol="GBPUSD";  OpenBuy();
      mySymbol="EURGBP";  OpenBuy();
      mySymbol="GBPCHF";  OpenBuy();
      mySymbol="CHFJPY";  OpenBuy();
      mySymbol="USDCHF";  OpenBuy();
      mySymbol="AUDJPY";  OpenBuy();

      mySymbol="USDJPY";  OpenBuy();
      mySymbol="EURUSD";  OpenBuy();
      mySymbol="EURCHF";  OpenBuy();
      mySymbol="GBPJPY";  OpenBuy();
      mySymbol="USDCAD";  OpenBuy();
      mySymbol="CADJPY";  OpenBuy();
      mySymbol="AUDUSD";  OpenBuy();
     }

  }


int deinit()
  {
  }


int start()
  {
  }



//ENTRY LONG (buy, Ask) 
void OpenBuy()
     {
      int      gle=0;
      int      ticket=0;
      
      double SL=0;
      double TP=0;
      int loopcount;

      loopcount=0;
      while(true)          
        {
         bid=MarketInfo(mySymbol,MODE_BID);
         ask=MarketInfo(mySymbol,MODE_ASK);

         // place order - NO TP OR SL
         ticket=OrderSend(mySymbol,OP_BUY,Lots,ask,Slippage,0,0,TradeComment,MagicNumber,White);
         gle=GetLastError();
         if(gle==0)
           {
            Print("BUY PLACED Ticket="+ticket+" Ask="+ask+" Lots="+Lots);
            break;
           }
          else 
           {
            Print("-----ERROR-----  Placing BUY order: Lots="+Lots+" SL="+SL+" TP="+TP+" Bid="+bid+" Ask="+ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); 
            
            RefreshRates();
            Sleep(500);

            // give up after 10 tries (~5 seconds)
            loopcount++;
            if(loopcount>10)
              {
               Alert("-----ERROR-----  Giving up on placing BUY order"); 
               return(gle);
              }
           }

        }//while - place order 

     }//BUYme




