Торорговый план на золото

 

В интернете нашел ссылку на торговлю золотом:

***

Сделал простенькую программу:

Кому не сложно, подправьте пожалуйста , для торговли на демо.

Торговый план :

-четверг на закрытии рынка - ордер BUY,

- пятница на закрытии  рынка закрыть BUY, открыть ордер SELL,

- понедельник на закрытии рынка - закрыть ордер SELL.

//+------------------------------------------------------------------+
//|                                                Manhattan_m30.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

extern bool      Use_Symbol1   = true;
extern string    Symbol1       = "XAUUSD";

//==========================================================================
extern double Lots               = 1.0;
//extern double mysl=1000;
//extern double mytp;
//extern double kfprofit=3;
//extern int tp;
extern int MagicMAN;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+-----------------------------------------------------------------+
int init()
  {
//----
  return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
/*
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
 //  int Magic;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic4)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
 //double MaximumRisk        = 0.02;
 //double DecreaseFactor     = 4;
// int Magic;
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1); //10000*0,04/1000=0,4
//---- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>3) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);  // 0,4-0,4*4/8=0
     }
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }
  */
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {  //aaa
  
  //----------------------------1  
 //  if (Use_Symbol1 && !ExistPositions(Symbol1,name_op,MagicNumber)) 
 //   OpenPosition(Symbol1,name_op,Symbol1_Lot,0,0,MagicNumber);
  //----------------------------2     
   if(Use_Symbol1==true) OpenSymbol(Symbol1);
    if(ExistPositions(Symbol1))   CloseSymbol(Symbol1);
 //------------------------------------------------------------------------------
   return(0);
  } // ааа
//+----------------------------------------------------------------------------+
//|  Описание : Открытие позиций                                               |
//+----------------------------------------------------------------------------+
   int OpenSymbol(string vSymbol)
  { //ccc
  // int Magic;
   bool error=true;
   bool error1=true;
   double poit=MarketInfo(vSymbol,MODE_POINT);
   double dig=MarketInfo(vSymbol,MODE_DIGITS);
   double mybid   = MarketInfo(vSymbol,MODE_BID);
   double myask   = MarketInfo(vSymbol,MODE_ASK);
   double cl1=NormalizeDouble(iClose(vSymbol,PERIOD_D1,1),dig);     //m5
     //------------------------------------------------------------------------------------------------------------------------------
   if(!ExistPositions(vSymbol))// &&  (peremen!=Bars))
   { //west
        if(DayOfWeek()==THURSDAY && Hour()==23 && Minute()>=59)
   {
         {  error =OrderSend(vSymbol,OP_BUY,Lots,cl1,30,0,0,"gold",MagicMAN,Green);}
 //---------------------------------------------------------------------------------
     }
      if(DayOfWeek()==FRIDAY && Hour()==22 && Minute()>=59)
      {
          { error1 =OrderSend(vSymbol,OP_SELL,Lots,cl1,0,0,0,"gold",MagicMAN,Green);}
     
      }
    }
   return(0);
  } //
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 01.09.2005                                                     |
//|  Описание : Возвращает флаг существования позиций                          |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//|    ot - время открытия             ( 0   - любое время открытия)           |
//+----------------------------------------------------------------------------+
 bool ExistPositions(string sy="",int op=-1,int mn=-1,datetime ot=0) 
  {
   int i,k=OrdersTotal();
   if(sy=="0") sy=Symbol();
   for(i=0; i<k; i++) //i=0
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
        {
         if(OrderSymbol()==sy || sy=="") 
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL || OrderType()==OP_BUYSTOP  ||OrderType()== OP_SELLSTOP) 
              {
               if(op<0 || OrderType()==op) 
                 {
                  if(mn<0 || OrderMagicNumber()==mn) 
                    {
                     if(ot<=OrderOpenTime()) return(true);
                    }
                 }
              }
           }
        }
     }
   return(false);
  }
//+----------------------------------------------------------------------------+
//|  Описание :       Функция возвращает сигнал закрытия                       |
//+----------------------------------------------------------------------------+
 int CloseSymbol(string vSymbol)
  {
    bool error1=true;
   double mybid   = MarketInfo(vSymbol,MODE_BID);
   double myask   = MarketInfo(vSymbol,MODE_ASK);
    double dig=MarketInfo(vSymbol,MODE_DIGITS);
 //-------------------------------------------------------------------  
  // if(DayOfWeek==FRIDAY && Hour()==22 && Minute()>=55)
  // {
  for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderMagicNumber()!=MagicMAN || OrderSymbol()!=vSymbol) continue;
      if(DayOfWeek()==FRIDAY && Hour()==22 && Minute()>=55)
      {
      //--- check order type 
      if(OrderType()==OP_BUY)
        {
      if(!OrderClose(OrderTicket(),OrderLots(),mybid,30,White))
               Print("OrderClose error ",GetLastError());
               //  DeleteStopOrders(vSymbol);
        }
       }
       if(DayOfWeek()==MONDAY && Hour()==23 && Minute()>=59)
       {
       if(OrderType()==OP_SELL)
        {
   //  if((clos1<ma_sred60) && ( totalprofit>0))
          // {
            if(!OrderClose(OrderTicket(),OrderLots(),myask,30,White))
               Print("OrderClose error ",GetLastError());
             //    DeleteStopOrders(vSymbol);
        //   }
      //   break;для циклов for, while прекращает их действия
        }
     }
     }
    return(0);
 }  //end
//+------------------------------------------------------------------+
//| удаляем отложенные стоп-ордера                                   |
//+------------------------------------------------------------------+
/*void DeleteStopOrders(string vSymbol)
{
  int res;
  for(int i=0;i<OrdersTotal();i++)
  {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
    if(OrderSymbol()== vSymbol && OrderMagicNumber()==Magic60R)
    {
      if(OrderType()==OP_SELLSTOP)
        res=OrderDelete(OrderTicket());
      if(OrderType()==OP_BUYSTOP)
        res=OrderDelete(OrderTicket());
     
    }
  }   

}
*/
//===================================================================================================================