請問可以設定自動時間平倉與查詢淨部位EA嗎?

 

請問可以設定自動時間平倉嗎?

例如固定每天3~5點不交易

因此3點就自動把所有部位平倉


我的系統是多空會並存

能查詢淨部位並每小時回報目前淨部位嗎?

或是能設定多空互抵平倉的AE嗎?

 

把文档看一遍,掌握 时间相关的几个函数后, 你问的这些都不是问题。

datetime  TimeCurrent();
datetime  TimeCurrent( 
   MqlDateTime&  dt_struct      // structure type variable 
   );
bool  TimeToStruct( 
   datetime      dt,            // date and time 
   MqlDateTime&  dt_struct      // structure for the adoption of values 
   );
datetime  StructToTime( 
   MqlDateTime$  dt_struct      // structure of the date and time 
   );
 
Ziheng Zhuang:

把文档看一遍,掌握 时间相关的几个函数后, 你问的这些都不是问题。

謝謝大大 請問 多空並存的平倉 EA能夠自動平調嗎?
 
陳柏昌:
謝謝大大 請問 多空並存的平倉 EA能夠自動平調嗎?


可以

 

是使用 PositionClose(Symbol())嗎?

但他沒有平倉 指是建立另外一個多空並存的部位

 
陳柏昌:

是使用 PositionClose(Symbol())嗎?

但他沒有平倉 指是建立另外一個多空並存的部位


具体情况,具体分析,没有统一的答案。

建议参考code base 其他人的EA

 
 //+----------------------------------------------- -------------------+
 //| 版權資訊
 //+----------------------------------------------- -------------------+
 #property copyright "Copyright 2012, MetaQuotes Software Corp."
 #property link "https://www.mql5.com"
 #property version "1.00"
 //+----------------------------------------------- -------------------+
 //| EA初始化1
 //+----------------------------------------------- -------------------+
 #include <Trade\Trade.mqh> //包含執行的交易庫
 #include <Trade\PositionInfo.mqh> //包含部位的資訊庫
 CTrade m_Trade; //執行交易的結構體
 CPositionInfo m_Position; //獲取持倉信息的結構體

 bool  SendNotification; 

 input int UpThreshold = 2;
 input int DownThreshold = 2;
 
 input double 下單量 = 0.1;
  
 int Upcount ; int Downcount;
  
   int CheckBar;   int Kcount;   int iKcount;
 
 double Close_buf[];   //存儲動態數組
 double Open_buf[];    //存儲動態數組

   datetime CTime;
   string CCTime ;
   string CCCTime ;
   string iCCCTime ;
   
   input int 可交易時間 = 16 ; //24代表全時段 0代表 0~1可交易
   bool 可交易嗎;

 //ENUM_TIMEFRAMES my_timeframe; //存儲時間框架的變量

 input int 已有部位 = 0;
 int iMyPositions = 已有部位;
 int pMyPositions = 已有部位;
 //+----------------------------------------------- -------------------+
 //| EA初始化2
 //+----------------------------------------------- -------------------+
 int OnInit ()  {    
 ArraySetAsSeries (Close_buf, true ); //將buf數組的索引設置為時間序列
 ArraySetAsSeries (Open_buf, true );  //將buf數組的索引設置為時間序列
 return ( 0 ); 
 }
 //+----------------------------------------------- -------------------+
 //| EA去初始化函數|
 //+----------------------------------------------- -------------------+
 void OnDeinit ( const int reason) { 
 ArrayFree (Open_buf);  //釋放動態數組
 ArrayFree (Close_buf); //釋放動態數組
 }
 //+----------------------------------------------- -------------------+
 //| EA的tick函數|
 //+----------------------------------------------- -------------------+
 void OnTick ()
 {  int err1= 0 ;  int err2= 0 ; //用於存儲價格圖表處理結果的變量 
 err1= CopyOpen (Symbol(),0, 0 ,2 ,Open_buf );   //將價格數據拷貝到動態數組,以進一步處理
 err2= CopyClose(Symbol(),0, 0 ,2 ,Close_buf);   //將價格數據拷貝到動態數組,以進一步處理
 if (err1!= 2 || err2!= 2  )  { Print ( "Failed to copy data " );  }//如果出錯退出
 
  iKcount = SeriesInfoInteger (Symbol(), Period(), SERIES_BARS_COUNT);   
   if (Kcount == 0)  Kcount = iKcount;
   if (Kcount == iKcount)  CheckBar = 0 ;
   if (iKcount != Kcount ) CheckBar = 1 ;  
   
   
CTime = SeriesInfoInteger (Symbol(), 0, SERIES_LASTBAR_DATE);
CCTime = TimeToString(CTime,TIME_MINUTES);
CCCTime =  StringSubstr(CCTime,0,2);
//Print("CCTime → " +CCTime + "  CCCTime → " +CCCTime);
if( CCCTime <10 ) {CCCTime =  StringSubstr(CCTime,1,1);}
   
   
   if(可交易時間!=CCCTime                   ) { 可交易嗎 = false;}
   if(可交易時間==CCCTime || 可交易時間== 24) { 可交易嗎 = true; }
   
   
  if (CheckBar == 1 ) //////////////超大if
 { 
  
  if (可交易嗎)
  {
  if (Close_buf[ 1 ] > Open_buf[ 1 ]) Upcount   = Upcount+1   ; //上漲實體 加積分
  if (Close_buf[ 1 ] < Open_buf[ 1 ]) Downcount = Downcount+1 ; //上漲實體 加積分
  }
 
  //做多
 if (iMyPositions  < 0 && Upcount == UpThreshold  && 可交易嗎)   { m_Trade.Buy( 下單量 ,Symbol());  iMyPositions=0; Downcount=0;}
 if (iMyPositions == 0 && Upcount == UpThreshold  && 可交易嗎)   { m_Trade.Buy( 下單量 ,Symbol());  iMyPositions=1; Downcount=0;}
 if( iMyPositions > pMyPositions  ) {
  SendNotification( 
     Symbol() + "\n" +  "已出現 " +Upcount + " 次實體上漲"  +"\n"   + "請務必評估自身風險承受度與資金控管,善設停損。" 
   );                 }
   
   //做空
 if (iMyPositions  > 0 && Downcount == DownThreshold && 可交易嗎 )   { m_Trade.Sell( 下單量 ,Symbol());  iMyPositions=0 ; Upcount=0;}
 if (iMyPositions == 0 && Downcount == DownThreshold && 可交易嗎 )   { m_Trade.Sell( 下單量 ,Symbol());  iMyPositions=-1; Upcount=0;}
 if( iMyPositions < pMyPositions  ) {
  SendNotification( 
     Symbol() + "\n" +  "已出現 " +Downcount + " 次實體下跌"  +"\n"   + "請務必評估自身風險承受度與資金控管,善設停損。" 
   );                  }


 //強平 休息囉  亦可以用在期貨沒開盤
 if (可交易嗎 == false)
 { 
   if (iMyPositions != 0 )  
  {
      if (iMyPositions ==  1 )   { m_Trade.Sell( 下單量 ,Symbol()); }
      if (iMyPositions == -1 )   { m_Trade.Buy ( 下單量 ,Symbol()); }
    SendNotification( 
     Symbol() + "\n" +  "強平!休息啦!" 
                    );  

   }
iMyPositions=0; Upcount=0;Downcount=0;
}

  Kcount = iKcount ;

  
  
}//超大if
  } //             Symbol() +  "\n" 
   //  "Upcount"      "\n    " +  Upcount+ "\n" 
 //+----------------------------------------------- -------------------+ 
 

時間部分搞定了

如何直接平倉 不要多空並存呢?


可以在哪裡加入相關程式碼呢?

 

找到一個程式碼

看起來可以用

但是下單都被拒絕? 該怎麼改呢?


2019.05.31 10:59:16.731 testClose (EURUSD_,H1) CTrade::OrderSend: instant buy 0.01 position #49458 EURUSD_ at 1.11356 [rejected]



//+------------------------------------------------------------------+
//|                                            CloseAllPositions.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of current position
      if(m_position.SelectByIndex(i))     // selects the position by index for further access to its properties
         if(m_position.Symbol()==Symbol())
            m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
  }
//+------------------------------------------------------------------+
 

以上是用限價單

如何改市價單平倉呢?

每筆報價後台回覆都差0.1

 

或是允許誤差

要怎麼加進去??


例如 deviation = 50