GORES GBP JPY

#property  copyright "alohafx"

 extern int MagicNumber = 2008112901;
 extern double TakeProfit = 0;
 extern double StopLoss = 0;
 extern int Slippage = 3;
 extern int POS_n_total = 0;
 int bilhete;/* i; */
 int Lotes = 3;
 int Prazo;
 int slippage=4;
 int start ()
   {
    if (Bars <100 || IsTradeAllowed ()!= true) return (0);
      count_position ();
      if (POS_n_total> 0)
       {
       Close_MACD ();
       return (0);
       }
      else
       {
       Call_MACD ();
       Call_Bid ();
       Call_Trade ();
       }
    return (0);
   }

 int POS_n_BUY,POS_n_SELL;

 void count_position ()
 {
     POS_n_BUY = 0;
     POS_n_SELL = 0;

     for (int i = 0; i <OrdersTotal (); i++) {
         if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES) == false) {
             break;
         }
         if (OrderMagicNumber ()!= MagicNumber) {
             continue;
         }
         if (OrderSymbol ()!= Symbol ()) {
             continue;
         }
         if (OrderType () == OP_BUY) {
             POS_n_BUY ++;
         }
         else if (OrderType () == OP_SELL) {
             POS_n_SELL ++;
         }
     }
     POS_n_total = POS_n_BUY + POS_n_SELL;
     return (0);
 }

 extern int TF = 5;
 extern int fMAperiod = 5;
 extern int sMAperiod = 21;
 extern int SIGperiod = 20;
 extern int desloca = 0;
 extern int SHIFTm = 0;
 int tempo,MACDm0,dupla,MACDm1,MACDs0,MACDs1;
 bool MACD_sell;

 void Close_MACD ()
 {
 if (TF == 0) tempo = 0;
 if (TF == 1) Prazo = 1;
 if (TF == 2) Prazo = 5;
 if (TF == 3) Prazo = 15;
 if (TF == 4) Prazo = 30;
 if (TF == 5) Prazo = 60;
 if (TF == 6) Prazo = 240;
 if (TF == 7) Prazo = 1440;
 if (TF == 8) Prazo = 10080;
 if (TF == 9) Prazo = 43200;

    MACDs0 = iMACD (NULL, 0, fMAperiod, sMAperiod, SIGperiod, PRICE_CLOSE, MODE_SIGNAL, 0 + SHIFTm);
    MACDs1 = iMACD (NULL, 0, fMAperiod, sMAperiod, SIGperiod, PRICE_CLOSE, MODE_SIGNAL, 1 + SHIFTm);

    if (MACDs0> 0 && MACDs1 <0)
     OrderClose (OrderTicket (), OrderLots (), Ask, 3, Red);

    return (0);
 }

 void Call_MACD ()
 {

    MACDm0 = iMACD (NULL, 0, fMAperiod, sMAperiod, SIGperiod, PRICE_CLOSE, MODE_MAIN, 0 + SHIFTm);
    MACDm1 = iMACD (NULL, 0, fMAperiod, sMAperiod, SIGperiod, PRICE_CLOSE, MODE_MAIN, 1 + SHIFTm);
     
    if (MACDm1> 0 && MACDm0 <0)
       MACD_sell = true;
    else
       MACD_sell = false;

    return (0);
 }

 bool BID_sell;

 void Call_Bid ()
 {
    double Pbid;
    Pbid = iLow (NULL, PERIOD_H1, 1);
  
    if (Pbid> Bid)
       BID_sell = true;
    else
       BID_sell = false;


    return (0);
 }

int TakeP,StopL;

 void Call_Trade ()
 {
 if (Volume [0]> 1) return (0);

    if (BID_sell && MACD_sell)
       {
       if (TakeProfit> 0)  TakeP= Bid-TakeProfit * Point;
       else TakeP = 0;
       if (StopLoss> 0)  StopL = Bid + StopLoss * Point;
       else StopL = 0;
     
     
       bilhete = OrderSend (Symbol (), OP_SELLSTOP, Lotes, Bid, slippage, StopL, TakeP, "vender", MagicNumber, 0, Red);
       if (bilhete> 0)
          {
          if (OrderSelect (bilhete, SELECT_BY_TICKET, MODE_TRADES)) Print ( "Vender forma aberta:", OrderOpenPrice (), "Livre Margem =", AccountFreeMargin(), " TakeProfit:", TakeP, "StopLoss:", StopL);
          }
       else
          {
          Print ( "Erro ao abrir Vender ordem:", GetLastError ());
          }
       }
    return (0);
    
 }