Algotrading:如何合并这种类型的追踪止损?

时间:2018-06-21 09:30:44

标签: mql4 algorithmic-trading forex

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

我目前正试图创建一个类似这样的追踪止损:

每隔一个报价框扫描所有未结订单并进行修改(如果尚未修改的话),例如:

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

  2. 获利5点,将止损转为入场价

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

这是我到目前为止所拥有的

//---- 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点移动止损4点至获利8点(当前价格)后

如果您可以共享可以集成到我的EA中或作为独立脚本/ EA运行的代码/思想,真的会帮上大忙。

谢谢

0 个答案:

没有答案