mql4 - 算法交易 : How to incorporate this type of trailing stop?

标签 mql4 algorithmic-trading forex

我目前有一个几乎每 5 分钟就开始一次交易的系统。

我目前正在全神贯注地创建一个执行以下操作的追踪止损:

每个订单号扫描所有未结订单并修改它们,如果它们还没有被修改,像这样:

  1. 以 25 点的静态止损开始

  2. 在获利 5 点时将止损移至入场价

  3. 8 点利润移动止损 8 点利润(当前价格)后 4 点

这是我目前的情况

//---- input parameters
extern double     InitialStop     = 25;
extern double     BreakEven       = 20;    // Profit Lock in pips  
extern double     StepSize        =  5;
extern double     MinDistance     = 10;

int   k, digit=0;
bool BE = false;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

//----
   return(0);
  }

// ---- Stepped Stops
void StepStops()
{        
    double BuyStop, SellStop;
    int total=OrdersTotal();
    for (int cnt=0;cnt<total;cnt++)
    { 
     OrderSelect(cnt, SELECT_BY_POS);   
     int mode=OrderType();    
        if ( OrderSymbol()==Symbol() ) 
        {
            if ( mode==OP_BUY )
            {
               BuyStop = OrderStopLoss();
               if ( Bid-OrderOpenPrice()>0 || OrderStopLoss()==0) 
               {
               if ( Bid-OrderOpenPrice()>=Point*BreakEven && !BE) {BuyStop = OrderOpenPrice();BE = true;}

               if (OrderStopLoss()==0) {BuyStop = OrderOpenPrice() - InitialStop * Point; k=1; BE = false;}

               if ( Bid-OrderOpenPrice()>= k*StepSize*Point) 
               {
               BuyStop = OrderStopLoss()+ StepSize*Point; 
               if (Bid - BuyStop >= MinDistance*Point)
               { BuyStop = BuyStop; k=k+1;}
               else
               BuyStop = OrderStopLoss();
               }                              
               //Print( " k=",k ," del=", k*StepSize*Point, " BuyStop=", BuyStop," digit=", digit);
               OrderModify(OrderTicket(),OrderOpenPrice(),
                           NormalizeDouble(BuyStop, digit),
                           OrderTakeProfit(),0,LightGreen);
                  return(0);
                  }
               }
            if ( mode==OP_SELL )
            {
               SellStop = OrderStopLoss();
               if ( OrderOpenPrice()-Ask>0 || OrderStopLoss()==0) 
               {
               if ( OrderOpenPrice()-Ask>=Point*BreakEven && !BE) {SellStop = OrderOpenPrice(); BE = true;}

               if ( OrderStopLoss()==0 ) { SellStop = OrderOpenPrice() + InitialStop * Point; k=1; BE = false;}

               if ( OrderOpenPrice()-Ask>=k*StepSize*Point) 
               {
               SellStop = OrderStopLoss() - StepSize*Point; 
               if (SellStop - Ask >= MinDistance*Point)
               { SellStop = SellStop; k=k+1;}
               else
               SellStop = OrderStopLoss();
               }
               //Print( " k=",k," del=", k*StepSize*Point, " SellStop=",SellStop," digit=", digit);
               OrderModify(OrderTicket(),OrderOpenPrice(),
                          NormalizeDouble(SellStop, digit),
                          OrderTakeProfit(),0,Yellow);      
               return(0);
               }    
            }
         }   
      } 
}

// ---- Scan Trades
int ScanTrades()
{   
   int total = OrdersTotal();
   int numords = 0;

   for(int cnt=0; cnt<total; cnt++) 
   {        
   OrderSelect(cnt, SELECT_BY_POS);            
   if(OrderSymbol() == Symbol() && OrderType()<=OP_SELL) 
   numords++;
   }
   return(numords);
}


//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   digit  = MarketInfo(Symbol(),MODE_DIGITS);


   if (ScanTrades()<1) return(0);
   else
   if (BreakEven>0 || InitialStop>0 || StepSize>0) StepStops(); 

 return(0);
}//int start
//+------------------------------------------------------------------+

如您所见,此 EA 确实以 25 点的静态止损开始,但我无法让它执行最后两个步骤,它们是:

  1. 在获利 5 点时将止损移至入场价
  2. 8 点利润移动止损 8 点利润(当前价格)后 4 点

如果您可以分享我可以合并到我的 EA 中或作为独立脚本/EA 运行的代码/想法,那将真的很有帮助。

谢谢

最佳答案

建议不要在服务器端申请止损,只要满足条件就直接用自己的代码平仓。

关于mql4 - 算法交易 : How to incorporate this type of trailing stop?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50965143/

相关文章:

mql4 - 我如何编写 mql4 代码 (EA) 用矩形标记列出的蜡烛图案

python - pine 脚本如何使用 2 系列而不是 1 和一个周期来计算 RSI?

low-latency - 当今最先进的高频交易系统有多快?

python - 使用 MT5 在 python 中得到 retcode=10021

mql4 - 如何在 MQL4 Metatrader 4 中打开多个 OrderSend()?

c# - 无法使用 Visual Studio 2015、MetaTrader Terminal 4 运行带有 [ nquotes ] 的简单智能交易系统

algorithmic-trading - 如何在 MQL4 中计算(添加)日期时间值?

r - quantStrat 无法识别列名

python - Python 中的外汇历史数据

api - 自动外汇交易的最佳API是什么?