比特币通信协议 - 币圈消息

比特币通信协议篇11、二、TURN简介。在典型的情况下,TURN客户端连接到内网中,并且通过一个或者多个NAT到 详细

求解决:EA不能自动挂单,附源码。

  [复制链接]
402 5
一直糊涂 发表于 2016-8-17 17:13:01 | 只看该作者 |阅读模式 打印 上一主题 下一主题
EA测试是可以自动下单的,但是挂模拟盘需要手动加载才可以的。请大神们帮我看看是哪里出了问题。
想解决:可以自动挂单即可。有疑问的欢迎提问。
源码如下:

//+------------------------------------------------------------------+
//|                                                          CBS.mq4 |
//|                                        Copyright 2015, Neko Prog |
//|                         https://www.mql5.com/en/users/megahentai |
//|                                                                  |
//|                   THIS EA IS SHAREWARE                           |
//|           WHICH MEANS THAT IT'S NOT A COMMERCIAL PRODUCT         |
//|                   BUT STILL COPYRIGHTED                          |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Todo                                                             |
//|                                                                  |
//| - sharp entry                                                    |
//| - trend surfing                                                  |
//| - trailing loss / breakeven                                      |
//|                                                                  |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| 2015-08-18 by Capella @ http://worldwide-invest.org/             |
//| Version v.Capella_001                                            |
//| - Fixed OrderDelete error bug                                    |
//| - Fixed compiler warnings                                        |
//| - Changed OrderMagic from global to external                     |
//| 2015-09-14 by Capella @ http://worldwide-invest.org/             |
//| Version v.Capella_2                                              |
//| - Changed StartTime, EndTime and CurrentTime from hours only     |
//|   to hour and minute                                             |
//| - Overruled USA am/pm time format to international 24 hour time  |
//| - Added Trailing StopLoss                                        |
//| 2015-11-03 by Capella  @ http://worldwide-invest.org/            |
//| - Corrected bug for printout of minutes on the screen,           |
//|   1 minute is printed as 01 and not as 1.                        |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, Neko Prog"
#property link      "https://www.mql5.com/en/users/megahentai"
#property version   "1.0"
#property description "Candlestick Breakout Scalper. Trade on candlestick breakout. Still in development stage. \n\n"
#property description "I need these 3 trading strategies in order to complete this EA: \n"
#property description " 1. Sharp entry (upon entry, only small price reversal and hit TP) \n"
#property description " 2. Trend surfing (after entry, it will follow current trend or hit TP) \n"
#property description " 3. Trailing stop (while following current trend, it will also lock profit in case of reversal) \n\n\n\n"
#property description "Any suggestions or comments are welcomed."
#property strict
extern string LotsSettings = "==== Lots Settings ====";
extern double LotExponent = 5;
extern double MinLot = 0.01;
extern double MaxLot = 0.01;
extern bool   UseRecoveryLot=true;
extern double MultiplierRecoveryLot=2.0;
extern string TradingSettings = "==== Trade Settings ====";
extern double PendingGap = 0;
extern double TakeProfit = 75;
extern double StopLoss = 50;
extern bool DelPrevPO = false;
extern bool TradeAllCandle = TRUE;
extern int MinCandleVolume = 100;
extern string TimeSettings = "==== Time Settings ====";
extern ENUM_TIMEFRAMES TimeFrame = PERIOD_D1;
extern bool EnableTime = TRUE;
extern int StartHour = 0;
extern int StartMinute = 0;
extern int EndHour = 23;
extern int EndMinute = 00;
extern string TrailSettings = "=== Trail Profit ==="; // Added by Capella
extern bool UseTrailProfit = TRUE; // Trail profit or not
extern double TrailStart = 35; // Points in profit to start trailing StopLoss
extern double TrailGap = 10; // Points distance between current price and new StopLoss
extern double TrailStep = 10;  // Points for a new SL increase before modifying trailing (to avoid unnecessary OrderModify)
extern string OtherSettings = "==== Other Settings ====";
extern int OrderMagic = 44342;   // Changed to extern by Capella
extern string Copyright = "==== \x00A9 2015 Neko Prog ====";
double StartTime, EndTime;
double lots,stoplevel,buystop,sellstop,buytp,selltp,buysl,sellsl;
double orderBuyTP,orderBuySL,orderSellTP,orderSellSL;
double CSPip,prevHigh,prevLow;
double CurrentTime;
int today;
string dispCurrentTime,dispStartTime,dispEndTime,dispToday;
datetime currtime,prevtime;
   int PendingBuy=0;
   int PendingSell=0;
   int MarketBuy=0;
   int MarketSell=0;
//+------------------------------------------------------------------+
//| Expert core function                                             |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   // Convert from sexagesimal to deciomal
   StartTime = (double) StartHour + (double) StartMinute / 60;
   EndTime = (double) EndHour + (double) EndMinute / 60;
     stoplevel = MarketInfo(Symbol(),MODE_STOPLEVEL);
   //Init code block
   currtime=0;
   prevtime=0;
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   if (true) ObjectsDeleteAll(); // clear the chart graphical objects
   Comment(""); // clear the chart comments
   return(0);
}
void sendOrder(int orderPos,double orderLot,double orderPrice,double orderSL,double orderTP,color orderColor)
{
   bool sent; // Added by Capella for the use of OrderSend command, see correction below!
//   OrderSend(Symbol(),orderPos,orderLot,orderPrice,3,orderSL,orderTP,"CBS_nEk0",OrderMagic,0,orderColor);  // Commented out by Capella
   sent = OrderSend(Symbol(),orderPos,orderLot,orderPrice,3,orderSL,orderTP,"CBS_nEk0",OrderMagic,0,orderColor);  // Added by Capella
}
void DelAllStop()
{
   bool deleted;  // Added by Capella for the use of OrderDelete command. See correction below!
   int tip;
   for (int i=0; iMaxLot) lots = MaxLot;
   //---
   if(UseRecoveryLot==true)
   {
   if(MultiplierRecoveryLot=0; i--)
   {                                            
   if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
   {
   if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==OrderMagic))
   {
   if(OrderProfit()+OrderCommission()+OrderSwap()12) dispCurrentTime=StringConcatenate(DoubleToString(CurrentTime-12)," PM");
   if (CurrentTime12) dispStartTime=StringConcatenate(IntegerToString(StartTime-12)," PM");
   if (StartTime12) dispEndTime=StringConcatenate(IntegerToString(EndTime-12)," PM");
   if (EndTime 0 )
       {
        int tip;
        for (int i=0; i0 && Bid>=orderBuyTP) // OrderClose(OrderTicket(),OrderLots(),Bid,3,clrBlue); //TP
                  closed = OrderClose(OrderTicket(),OrderLots(),Bid,3,clrBlue); //TP
                if (StopLoss>0 && Bid0 && Ask0 && Ask>=orderSellSL) // OrderClose(OrderTicket(),OrderLots(),Ask,3,clrBlue); //SL - Commented out by Capella
                  closed = OrderClose(OrderTicket(),OrderLots(),Ask,3,clrBlue); //SL - Added by Capella
               }
            }   
         }
       }
     // Check stale order, delete untriggered Stop Order
     if ((PendingBuy+PendingSell==1)&&(MarketBuy+MarketSell==1)) DelAllStop();
     // Check if missing one of both Stop Order
     if ((PendingBuy+PendingSell==1)&&(MarketBuy+MarketSell==0)) theBrainz();
     // Send order per candle
     if (newTime()==true)
       {
        // Delete all untriggered Pending Orders
        if (DelPrevPO==true) DelAllStop();
        // Start trade if no order
         if (PendingBuy+PendingSell= StartTime && CurrentTime =MinCandleVolume) theBrainz();
               }
           }  
           if (EnableTime==false)
           {
              if (TradeAllCandle==true) theBrainz();
              if (TradeAllCandle==false && CSPip>=MinCandleVolume) theBrainz();
           }
         }
       }
     }
   else { Alert("Trade run on ",EnumToString(TimeFrame)," timeframe only."); }
   return(0);
  }
//+------------------------------------------------------------------+
// Trail specific order; check to see if StopLoss cand and should be changed
void CheckTrail( int par_ticket )
{
   double trstart = TrailStart * Point;
   double gap = TrailGap * Point;
   double step = TrailStep * Point;
   double spread = Ask - Bid;
   double openprice = 0;
   double oldSL = 0;
   double newSL = 0;
   bool wasmodified = FALSE;
   // If we can select the order that should be trailing
   if ( OrderSelect( par_ticket, SELECT_BY_TICKET, MODE_TRADES ) == TRUE  )
   {
      openprice = OrderOpenPrice();
      oldSL = OrderStopLoss();
           // Spread is higher than Trailing value, so set Trailing value to spread
      if ( spread > trstart )
         trstart = spread;
           // If the order does not have any SL yet
      if ( oldSL == 0 )
      {
         // If we have matching order type and symbol
         if ( OrderType() == OP_BUY )
         {
            // Calculate new SL
            newSL = ND ( MathMin(openprice + trstart,Bid-stoplevel*Point) );
            // If the current close price is larger than opening price + TrailingStart
            if ( Bid > newSL )
            {         
               // Modify the order with a new SL
               wasmodified = OrderModify( par_ticket, openprice, newSL, 0, 0, Blue );  
               // If the order could not be modified with a new SL then print out an error message         
               if ( wasmodified == FALSE )
                  Print ( "Attempt to trail SL for profitable order# ", par_ticket, " failed! Last error = ", GetLastError(),", OrderType = ", OrderType(),", Bid = ", Bid,", Open price = ", openprice,", OldSL = ", oldSL,", TrailSL = ", newSL );         
            }
         }
         // If we have matching order type
         if ( OrderType() == OP_SELL )
         {
            // Calculate new SL
            newSL = ND ( MathMax(openprice - trstart,Ask+stoplevel*Point) );
            // If the current close price is less than the opening price - TrailingStart - TrailingStep
            if ( Ask  0 )
         {
            // Print out message that says that the order is now trailing in profit
            Print ( "The order ", string ( par_ticket ), " is trailing SL in profit!" );
         }
*/         
      }
           // The order already has a SL, and a check to see if it can be changed         
      else
      {
         // If we have matching order type
         if ( OrderType() == OP_BUY )
         {
            // Calculate new SL as current closing price - gap
            newSL = ND ( MathMin(Bid - gap,Bid-stoplevel*Point) );
            // If the distance between the new SL and the old SL is more than the trailing step
            if ( ( newSL - oldSL ) > step )
            {
               // Modify the profit order with a new SL that is current close price less trailing,         
               wasmodified = OrderModify( par_ticket, openprice, newSL, 0, 0, Blue );
               // In case the order couiild not be modified then print out error message
               if ( wasmodified == FALSE )
                  Print ("Modify error. newSl = ", newSL,", oldSl = ", oldSL,", Bid = ", Bid);
            }
         }
         // If we have matching order type and symbol               
         if ( OrderType() == OP_SELL )
         {
            // Calculate a new SL
            newSL = ND ( MathMax(Ask + gap,Ask+stoplevel*Point) );
            // If the distance between the new SL and the old SL is more than the trailing step
            if ( ( oldSL - newSL ) > gap )
            {
               // Modify the profit order with a new SL that is current close price plus trailing
               wasmodified = OrderModify( par_ticket, openprice, newSL, 0, 0, Blue );
               // In case the order could not be modified then print out error message
               if ( wasmodified == FALSE )
                  Print ("Modify error. newSl = ", newSL,", oldSl = ", oldSL,", Ask = ", Ask );                  
            }
         }
      }   
   }
}
// Normalize decimals to number of decimals for this currency pair as defined by the broker. Added by Capella
double ND( double par_value )
{
   return ( NormalizeDouble ( par_value, Digits ) );
}
收藏
收藏0
转播
转播
分享
分享
分享
淘帖0

精彩评论5

跳转到指定楼层
沙发
lianww123 发表于 2016-8-18 21:37:00 | 只看该作者
确认模拟盘可以下单,实盘下不了单
板凳
 楼主| 一直糊涂 发表于 2016-8-18 21:37:45 | 只看该作者

模拟盘手动加载会下单的,实际需要的是次日开盘0点即下挂单。但模拟盘次日不自动下单。
地板
mgt1972 发表于 2016-8-17 17:13:00 | 只看该作者
设置找到extern ENUM_TIMEFRAMES TimeFrame = PERIOD_D1;
选择周期:M1,M5,M15,M30,H1,H4,D,W
5#
mgt1972 发表于 2016-8-21 03:39:07 | 只看该作者
是否缺Expert core function核心功能   
, Custom indicator deinitialization function客户指标初始化函数?
6#
justin123 发表于 2016-8-21 03:49:37 | 只看该作者
如何联系
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

发布主题
阅读排行更多+

Powered by 顺水鱼MT4外汇EA网! X3.2© 2001-2017 顺水MT4外汇EA公司.( 陕ICP备17014341号-1