0
Окончательная отладка в понедельник будет: www.opentraders.ru/downloads/878/

avatar

AM2

  • 27 сентября 2015, 12:09
0
Это уже в понедельник буду смотреть.
avatar

AM2

  • 25 сентября 2015, 17:15
0
Готово: www.opentraders.ru/downloads/879/


//+------------------------------------------------------------------+
//|                                                          OTL.mq4 |
//|                                            Copyright © 2013, AM2 |
//|                                      http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, AM2"
#property link      "http://www.forexsystems.biz"

// 1. В зависимости от цвета предыдущей свечи выставляем отложку на расстоянии Delta от цены в StartHour часов на пробой.
// 2. Время истечения ордера Expiration часов.
// 3. Ордер выставляется с установленными значениями стоплосс и тейкпрофит.
// 4. Торговля фиксированным лотом.
// 5. Торговля на открытии свечи на часовом графике.

extern int    StopLoss   = 1000;   //Стоплосс ордера  
extern int    TakeProfit = 1000;   //Тейкпрофит ордера
extern int    StartHour  = 7;      //Час начала торговли(терминальное время)
extern int    Delta      = 100;    //Расстояние от цены для установки ордера
extern int    Expiration = 4;      //Время истечения ордера
extern double Lot        = 1;      //Объём позиции
extern double KLot       = 2;      //Увеличение лота
extern double MaxLot     = 5;      //Максимальный oбъём позиции
extern int    Magic      = 130228; //Магик

double LastOpen=0;
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double Lots()
  {
   double lot;
   if(OrdersHistoryTotal()==0)
     {
      lot=Lot;
     }
   if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))
     {
      if(OrderProfit()>0)
        {
         lot=Lot;
        }
      if(OrderProfit()<=0)
        {
         lot=OrderLots()*KLot;
        }
     }
   if(lot>MaxLot)lot=Lot;
   return(lot);
  }
//+------------------------------------------------------------------+
//| OnTick function                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//---- 
   if(LastOpen==Open[0])return;
   
   int b,s,p,res;
   datetime expiration=TimeCurrent()+3600*Expiration;
   double BuyPrice=Ask+Delta*Point;
   double SellPrice=Bid-Delta*Point;

   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS)==true)
        {
         if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;
         if(OrderType()==OP_BUY || OrderType()==OP_SELL) p++;
         if(OrderType()==OP_BUYSTOP) b++;
         if(OrderType()==OP_SELLSTOP) s++;
        }
     }
      //---- buy stop
      if(Close[1]-Open[1]>0 && Hour()==StartHour && p<1 && b<1)
        {
         res=OrderSend(Symbol(),OP_BUYSTOP,Lots(),
         NormalizeDouble(BuyPrice,Digits),0,
         NormalizeDouble(BuyPrice-StopLoss*Point,Digits),
         NormalizeDouble(BuyPrice+TakeProfit*Point,Digits),
         "",Magic,expiration,Blue);
         return;
        }
        
//---- sell stop  
   if(Open[1]-Close[1]>0 && Hour()==StartHour && p<1 && s<1)
     {
      res=OrderSend(Symbol(),OP_SELLSTOP,Lots(),
      NormalizeDouble(SellPrice,Digits),0,
      NormalizeDouble(SellPrice+StopLoss*Point,Digits),
      NormalizeDouble(SellPrice-TakeProfit*Point,Digits),
      "",Magic,expiration,Red);
      return;
     }
     
   LastOpen=Open[0]; 
//----
  }
//--------------------------------------------------------------------

avatar

AM2

  • 25 сентября 2015, 13:35
0
Нужно подробное ТЗ.
avatar

AM2

  • 25 сентября 2015, 09:26
0
Нужно попросить Bishopа перенести ваш топик в стол заказов и поправить ссылки.
avatar

AM2

  • 24 сентября 2015, 19:49
0
Закрылся по двум тейкам и стопу в профите.

avatar

AM2

  • 24 сентября 2015, 17:53
0
У меня на отладке вот этот код. Можете тоже смотреть.


//+------------------------------------------------------------------+
//|                                                     CloseLot.mq4 |
//|                                              Copyright 2015, AM2 |
//|                                      http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, AM2"
#property link      "http://www.forexsystems.biz"
#property description "Simple expert advisor"

#include <stdlib.mqh>

//--- Inputs
extern int StopLoss     = 500;
extern int StopLoss1    = 0;
extern int TakeProfit   = 20;
extern int TakeProfit1  = 5;
extern int TakeProfit2  = 10;
extern int TakeProfit3  = 15;
extern int OpenOrder=2;    //0-off. 1-buy. 2-sell.
extern int Slip         = 30;
extern double Lot       = 1;
extern double Lot1      = 0.1;
extern double Lot2      = 0.2;
extern double Lot3      = 0.3;
extern double StopLot   = 0.4;
extern int    Magic     = 0;

bool CloseOrd1=true,CloseOrd2=true,CloseOrd3=true,CloseOrd4=true;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void OpenPos()
  {
   int res=0,err=0;
   double sl;

//--- sell 
   if(OpenOrder==2)
     {
      double bid=MarketInfo(NULL,MODE_BID);
      if(StopLoss==0) sl=0; else sl=bid+StopLoss*Point;
      res=OrderSend(Symbol(),OP_SELL,Lot,bid,Slip,sl,bid-TakeProfit*Point,"",Magic,0,Red);
      if(res<0)
        {
         err=GetLastError();
         Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА SELL: ",err,"(",ErrorDescription(err),")");
           } else {
         RefreshRates();
        }
     }

//--- buy 
   if(OpenOrder==1)
     {
      double ask=MarketInfo(NULL,MODE_ASK);
      if(StopLoss==0) sl=0; else sl=ask-StopLoss*Point;
      res=OrderSend(Symbol(),OP_BUY,Lot,ask,Slip,sl,ask+TakeProfit*Point,"",Magic,0,Blue);
      if(res<0)
        {
         err=GetLastError();
         Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА BUY: ",err,"(",ErrorDescription(err),")");
           } else {
         RefreshRates();
        }
     }
//---
  }
//+------------------------------------------------------------------+  
void CloseOrders()
  {
   bool cl;
   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol())
           {
            if(OrderMagicNumber()==Magic)
              {
               if(OrderType()==OP_BUY)
                 {
                  if(Bid-OrderOpenPrice()>TakeProfit1*Point)
                    {
                     if(CloseOrd1) cl=OrderClose(OrderTicket(),Lot1,Bid,Slip,Blue);
                     CloseOrd1=false;
                    }

                  if(Bid-OrderOpenPrice()>TakeProfit2*Point)
                    {
                     if(CloseOrd2) cl=OrderClose(OrderTicket(),Lot2,Bid,Slip,Blue);
                     CloseOrd2=false;
                    }

                  if(Bid-OrderOpenPrice()>TakeProfit3*Point)
                    {
                     if(CloseOrd3) cl=OrderClose(OrderTicket(),Lot3,Bid,Slip,Blue);
                     CloseOrd3=false;
                    }

                  if(Bid-OrderOpenPrice()>TakeProfit*Point)
                    {
                     cl=OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Blue);
                    }

                  if(Bid-OrderOpenPrice()<StopLoss1*Point && StopLoss1>0)
                    {
                     if(CloseOrd4) cl=OrderClose(OrderTicket(),StopLot,Bid,Slip,Blue);
                     CloseOrd4=false;
                    }
                 }

               if(OrderType()==OP_SELL)
                 {
                  if(OrderOpenPrice()-Ask>TakeProfit1*Point)
                    {
                     if(CloseOrd1) cl=OrderClose(OrderTicket(),Lot1,Ask,Slip,Red);
                     CloseOrd1=false;
                    }

                  if(OrderOpenPrice()-Ask>TakeProfit2*Point)
                    {
                     if(CloseOrd2) cl=OrderClose(OrderTicket(),Lot2,Ask,Slip,Red);
                     CloseOrd2=false;
                    }

                  if(OrderOpenPrice()-Ask>TakeProfit3*Point)
                    {
                     if(CloseOrd3) cl=OrderClose(OrderTicket(),Lot3,Ask,Slip,Red);
                     CloseOrd3=false;
                    }

                  if(OrderOpenPrice()-Ask>TakeProfit*Point)
                    {
                     cl=OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
                    }

                  if(OrderOpenPrice()-Ask<StopLoss1*Point && StopLoss1>0)
                    {
                     if(CloseOrd4) cl=OrderClose(OrderTicket(),StopLot,Ask,Slip,Blue);
                     CloseOrd4=false;
                    }
                 }
              }
           }
        }
     }
   return;
  }
//+------------------------------------------------------------------+
//| OnTick function                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   int p=0;

   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL) p++;
           }
        }
     }

   if(p<1)
     {
      CloseOrd1=true;
      CloseOrd2=true;
      CloseOrd3=true;
      CloseOrd4=true;
      OpenPos();
     }
   if(p>0) CloseOrders();
   Comment("\n Positions: ",p,
           "\n CloseOrd1: ",CloseOrd1,
           "\n CloseOrd2: ",CloseOrd2,
           "\n CloseOrd3: ",CloseOrd3,
           "\n CloseOrd4: ",CloseOrd4);
//---
  }
//+------------------------------------------------------------------+

avatar

AM2

  • 24 сентября 2015, 17:47
0
Мне все интересно :) 
avatar

AM2

  • 24 сентября 2015, 17:16
0
Я поставлю на отладку, посмотрю.
avatar

AM2

  • 24 сентября 2015, 16:54
0
Я отладил еще 2 функции. Продолжаю дальше :) 
avatar

AM2

  • 24 сентября 2015, 16:14
0
Вот эта функция возвращает правильный тикет:


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int ticket()
  {
   int tik;
   if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))tik=OrderTicket();
   return(tik);
  }


А почти тоже самое для лота, не возвращает нужный лот:


//+------------------------------------------------------------------+
//|              Last lot size                                       |
//+------------------------------------------------------------------+
double LastLot()
  {
   double lot=0;
   if(OrderSelect(OrdersHistoryTotal()-1,SELECT_BY_POS,MODE_HISTORY))lot=OrderLots();
   return(lot);
  }
avatar

AM2

  • 24 сентября 2015, 15:50
0
Добавил функцию. Изначально новый стоп равен нулю.


extern int StopLoss1    = 0;


Закроет часть позы лотом:

extern double StopLot   = 0.4;


Код еще не гонял.


//+------------------------------------------------------------------+
//|                                                     CloseLot.mq4 |
//|                                              Copyright 2015, AM2 |
//|                                      http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, AM2"
#property link      "http://www.forexsystems.biz"
#property description "Simple expert advisor"

#include <stdlib.mqh>

//--- Inputs
extern int StopLoss     = 500;
extern int StopLoss1    = 0;
extern int TakeProfit   = 20;
extern int TakeProfit1  = 5;
extern int TakeProfit2  = 10;
extern int TakeProfit3  = 15;
extern int OpenOrder=2;    //0-off. 1-buy. 2-sell.
extern int Slip         = 30;
extern double Lot       = 1;
extern double Lot1      = 0.1;
extern double Lot2      = 0.2;
extern double Lot3      = 0.3;
extern double StopLot   = 0.4;
extern int    Magic     = 0;

bool CloseOrd1=true,CloseOrd2=true,CloseOrd3=true,CloseOrd4=true;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void OpenPos()
  {
   int res=0,err=0;
   double sl;

//--- sell 
   if(OpenOrder==2)
     {
      double bid=MarketInfo(NULL,MODE_BID);
      if(StopLoss==0) sl=0; else sl=bid+StopLoss*Point;
      res=OrderSend(Symbol(),OP_SELL,Lot,bid,Slip,sl,bid-TakeProfit*Point,"",Magic,0,Red);
      if(res<0)
        {
         err=GetLastError();
         Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА SELL: ",err,"(",ErrorDescription(err),")");
           } else {
         RefreshRates();
        }
     }

//--- buy 
   if(OpenOrder==1)
     {
      double ask=MarketInfo(NULL,MODE_ASK);
      if(StopLoss==0) sl=0; else sl=ask-StopLoss*Point;
      res=OrderSend(Symbol(),OP_BUY,Lot,ask,Slip,sl,ask+TakeProfit*Point,"",Magic,0,Blue);
      if(res<0)
        {
         err=GetLastError();
         Print("ОШИБКА ВЫСТАВЛЕНИЯ ОРДЕРА BUY: ",err,"(",ErrorDescription(err),")");
           } else {
         RefreshRates();
        }
     }
//---
  }
//+------------------------------------------------------------------+  
void CloseOrders()
  {
   bool cl;
   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol())
           {
            if(OrderMagicNumber()==Magic)
              {
               if(OrderType()==OP_BUY)
                 {
                  if(Bid-OrderOpenPrice()>TakeProfit1*Point)
                    {
                     if(CloseOrd1) cl=OrderClose(OrderTicket(),Lot1,Bid,Slip,Blue);
                     CloseOrd1=false;
                    }

                  if(Bid-OrderOpenPrice()>TakeProfit2*Point)
                    {
                     if(CloseOrd2) cl=OrderClose(OrderTicket(),Lot2,Bid,Slip,Blue);
                     CloseOrd2=false;
                    }

                  if(Bid-OrderOpenPrice()>TakeProfit3*Point)
                    {
                     if(CloseOrd3) cl=OrderClose(OrderTicket(),Lot3,Bid,Slip,Blue);
                     CloseOrd3=false;
                    }

                  if(Bid-OrderOpenPrice()>TakeProfit*Point)
                    {
                     cl=OrderClose(OrderTicket(),OrderLots(),Bid,Slip,Blue);
                    }

                  if(Bid-OrderOpenPrice()<StopLoss1*Point && StopLoss1>0)
                    {
                     cl=OrderClose(OrderTicket(),StopLot,Bid,Slip,Blue);
                     CloseOrd4=false;
                    }

                 }

               if(OrderType()==OP_SELL)
                 {
                  if(OrderOpenPrice()-Ask>TakeProfit1*Point)
                    {
                     if(CloseOrd1) cl=OrderClose(OrderTicket(),Lot1,Ask,Slip,Red);
                     CloseOrd1=false;
                    }

                  if(OrderOpenPrice()-Ask>TakeProfit2*Point)
                    {
                     if(CloseOrd2) cl=OrderClose(OrderTicket(),Lot2,Ask,Slip,Red);
                     CloseOrd2=false;
                    }

                  if(OrderOpenPrice()-Ask>TakeProfit3*Point)
                    {
                     if(CloseOrd3) cl=OrderClose(OrderTicket(),Lot3,Ask,Slip,Red);
                     CloseOrd3=false;
                    }

                  if(OrderOpenPrice()-Ask>TakeProfit*Point)
                    {
                     cl=OrderClose(OrderTicket(),OrderLots(),Ask,Slip,Red);
                    }

                  if(OrderOpenPrice()-Ask<StopLoss1*Point && StopLoss1>0)
                    {
                     cl=OrderClose(OrderTicket(),StopLot,Ask,Slip,Blue);
                     CloseOrd4=false;
                    }
                 }
              }
           }
        }
     }
   return;
  }
//+------------------------------------------------------------------+
//| OnTick function                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   int p=0;

   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL) p++;
           }
        }
     }

   if(p<1)
     {
      CloseOrd1=true;
      CloseOrd2=true;
      CloseOrd3=true;
      OpenPos();
     }
   if(p>0) CloseOrders();
   Comment("\n Positions: ",p,
           "\n CloseOrd1: ",CloseOrd1,
           "\n CloseOrd2: ",CloseOrd2,
           "\n CloseOrd3: ",CloseOrd3);
//---
  }
//+------------------------------------------------------------------+

avatar

AM2

  • 24 сентября 2015, 15:28
0
Вы же писали, что программируете?
avatar

AM2

  • 24 сентября 2015, 15:10
0
Проверил ваш код, ордера открывает и закрывает как надо, только пока не увеличивает ставки.


Пытались отладить функции?
avatar

AM2

  • 24 сентября 2015, 15:01
0
На каком брокере гоняете? Я пытаюсь отладить на гранде, там какая то путаница с историей.
avatar

AM2

  • 24 сентября 2015, 14:54
0
Чтобы цена прошла меньше какого то уровня, скажем 50п. она должна пройти выше скажем до 75 и уже потом опуститься ниже 50. До какого уровня цена должна подняться, по аналогии как я указал 75?
avatar

AM2

  • 24 сентября 2015, 14:25