诚信为本
量力而为
当前位置:峰汇在线 > MetaTrader 4 > MT4 EA > 正文

网格马丁EA Level Scalper V4.mq4

Level Scalper V4 EA用在一小时图表周期交易。属于网格马丁类型。

源码:

//+------------------------------------------------------------------+
//|                                             Level Scalper V4.mq4 |
//|                                           djindyfx#sbcglobal.net |
//+------------------------------------------------------------------+
#property copyright "djindyfx#sbcglobal.net"
#property link      "http://www.125808047.com"

extern int        pips=10; 
extern double     lots=0.1;
extern int        NbLevels=5;
extern bool       ContinueTrading=false;
extern bool   EntryTime       = true;
extern bool   UseEntryTime_1  = true;
extern int    EntryTime_1     = 8;
extern bool   UseEntryTime_2  = true;
extern int    EntryTime_2     = 13;
extern bool   indicator_1     = true;
extern int    period          = 100;
extern bool   indicator_2     = true;
extern bool   more            = true;
extern bool       CloseAllOrders=false;
bool closing = true;

  extern int      WhatCorner=1;
  extern color    FontColor = LawnGreen;
  extern int      FontSize=10;
  extern string   FontType="Calibri";
  extern int      MoveUpDown = 15 ;
  extern int      SideDistance = 2;

bool Enter = false;
int Magic = 20100825;
int ProfitTarget=0; 
int bar;
bool cg = false;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{ 
   bar = Bars;
   Comment("www.125808047.com");
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
   void OnDeinit(const int reason)
  {
   ObjectDelete("ObjLots_Label");
   ObjectDelete("ObjBuyGoal_Label");
   ObjectDelete("ObjBuyGoalProfit_Label");
   ObjectDelete("ObjSellGoal_Label");
   ObjectDelete("ObjSellGoalProfit_Label");
   ObjectDelete("ObjLevels_Label");
   ObjectDelete("ObjPairTotal_Label");
   ObjectDelete("ObjInitialprice_Label");
   ObjectDelete("ObjSellGoalStop_Label");
   ObjectDelete("ObjBuyGoalStop_Label");
   ObjectDelete("ObjContinueTrading_Label");
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
void OnTick()
  {
  if (Period() != PERIOD_H1) 
  {
   Alert ("user PERIOD_H1");
   return;
  }
  
   int ticket=0, cpt=0, profit=0, total=0, pairtotal=0, BuyGoalProfit=0, SellGoalProfit=0;
   double BuyGoal=0, BuyGoalStop=0, SellGoal=0, SellGoalStop=0, InitialPrice=0;

   Comment(Enter);

   if(pips*10<MarketInfo(Symbol(),MODE_STOPLEVEL)+MarketInfo(Symbol(),MODE_SPREAD)) pips=1+MarketInfo(Symbol(),MODE_STOPLEVEL)+MarketInfo(Symbol(),MODE_SPREAD);
   if(lots<MarketInfo(Symbol(),MODE_MINLOT)) lots=MarketInfo(Symbol(),MODE_MINLOT);
   
   for(cpt=0;cpt<OrdersTotal();cpt++)
   {
      cg = OrderSelect(cpt,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol()&& OrderMagicNumber()==Magic)
      {
         pairtotal++;
         if(!InitialPrice) InitialPrice=StrToDouble(OrderComment());
      }
   }

if (pairtotal < 1)
{
      if (ContinueTrading && EntryTime)
      {
         Alert ("ContinueTrading == EntryTime");
         return;
      }
      
      if (ContinueTrading && !EntryTime) Enter = true;
   
      if (!ContinueTrading && Indicator_Enter(indicator_1,period,indicator_2)) 
      {
         if ( (EntryTime && UseEntryTime_1 && Hour() == EntryTime_1) || (EntryTime && UseEntryTime_2 && Hour() == EntryTime_2) )
         {
            if (bar != Bars)
            {
               bar = Bars;
               Enter = true;
            }     
         }   
      }
      
}   

   if(CloseAllOrders == true) {KillSession();}
   
   if(closing == true && pairtotal>=1) {EndSession();}

   if(pairtotal==0 && Enter) 
   {
      closing = false;
      InitialPrice=MarketInfo(Symbol(),MODE_ASK);
      
      BuyGoal=NormalizeDouble(InitialPrice,Digits)+(NbLevels+1)*pips*10*MarketInfo(Symbol(),MODE_POINT);
      SellGoal=NormalizeDouble(InitialPrice,Digits)-(NbLevels+1)*pips*10*MarketInfo(Symbol(),MODE_POINT);
      
      BuyGoalStop = SellGoal-(MarketInfo(Symbol(),MODE_SPREAD)*MarketInfo(Symbol(),MODE_POINT));
      SellGoalStop = BuyGoal+(MarketInfo(Symbol(),MODE_SPREAD)*MarketInfo(Symbol(),MODE_POINT));

      for(cpt=1;cpt<=NbLevels;cpt++)
      {
         cg = OrderSend(Symbol(),OP_BUYSTOP,lots,NormalizeDouble(InitialPrice+(cpt*pips*10*MarketInfo(Symbol(),MODE_POINT)),Digits),30,NormalizeDouble(BuyGoalStop,Digits),NormalizeDouble(BuyGoal,Digits),DoubleToStr(InitialPrice,MarketInfo(Symbol(),MODE_DIGITS)),Magic,0);
         cg = OrderSend(Symbol(),OP_SELLSTOP,lots,NormalizeDouble(InitialPrice-(cpt*pips*10*MarketInfo(Symbol(),MODE_POINT)),Digits),30,NormalizeDouble(SellGoalStop,Digits),NormalizeDouble(SellGoal,Digits),DoubleToStr(InitialPrice,MarketInfo(Symbol(),MODE_DIGITS)),Magic,0);
      }
    }
    else 
    {
      BuyGoal= InitialPrice + ( NbLevels+1 )*pips*10*MarketInfo(Symbol(),MODE_POINT);
      SellGoal=(InitialPrice-MarketInfo(Symbol(),MODE_SPREAD)*MarketInfo(Symbol(),MODE_POINT)) -( NbLevels+1 )*pips*10*MarketInfo(Symbol(),MODE_POINT);
      BuyGoalStop = SellGoal+MarketInfo(Symbol(),MODE_SPREAD)*MarketInfo(Symbol(),MODE_POINT);
      SellGoalStop = BuyGoal-MarketInfo(Symbol(),MODE_SPREAD)*MarketInfo(Symbol(),MODE_POINT);
      
      if(MarketInfo(Symbol(),MODE_BID) >= BuyGoal ){ closing = true; EndSession();}
      if(MarketInfo(Symbol(),MODE_ASK) <= SellGoal ){ closing = true; EndSession();}

      if(closing==true) {EndSession();}

      BuyGoalProfit=CheckProfits(lots,OP_BUY,false,InitialPrice);
      SellGoalProfit=CheckProfits(lots,OP_SELL,false,InitialPrice);

      if(BuyGoalProfit<ProfitTarget && more)
      {
      Print ("==1== BuyGoalProfit  ProfitTarget "," BuyGoalProfit = ",BuyGoalProfit,"ProfitTarget = ",ProfitTarget);
         for(cpt=NbLevels;cpt>=1; cpt--)
         {
            if(MarketInfo(Symbol(),MODE_ASK)<=(InitialPrice+(cpt*pips*10-MarketInfo(Symbol(),MODE_STOPLEVEL))*MarketInfo(Symbol(),MODE_POINT)))
            {
               ticket=OrderSend(Symbol(),OP_BUYSTOP,cpt*lots,InitialPrice+cpt*pips*10*MarketInfo(Symbol(),MODE_POINT),30,BuyGoalStop,BuyGoal,DoubleToStr(InitialPrice,MarketInfo(Symbol(),MODE_DIGITS)),Magic,0);
            }
            if(ticket>0) BuyGoalProfit+=lots*(BuyGoal-InitialPrice-cpt*pips*10*MarketInfo(Symbol(),MODE_POINT))/MarketInfo(Symbol(),MODE_POINT);
         }
      }
      if(SellGoalProfit<ProfitTarget && more)
      {
      Print ("==4== SellGoalProfit  ProfitTarget "," SellGoalProfit = ",SellGoalProfit,"ProfitTarget = ",ProfitTarget);
         for(cpt=NbLevels;cpt>=1; cpt--)
         {
            if(MarketInfo(Symbol(),MODE_BID)>=(InitialPrice-(cpt*pips*10-MarketInfo(Symbol(),MODE_STOPLEVEL))*MarketInfo(Symbol(),MODE_POINT)))
            {
               ticket=OrderSend(Symbol(),OP_SELLSTOP,cpt*lots,InitialPrice-cpt*pips*10*MarketInfo(Symbol(),MODE_POINT),30,SellGoalStop,SellGoal,DoubleToStr(InitialPrice,MarketInfo(Symbol(),MODE_DIGITS)),Magic,0);
            }
            if(ticket>0) SellGoalProfit+=lots*(InitialPrice-SellGoal-cpt*pips*10*MarketInfo(Symbol(),MODE_POINT))/MarketInfo(Symbol(),MODE_POINT);
         }
      }
   }

   string ObjLots= DoubleToStr(lots,2);
   string ObjPips = pips*10;
   string ObjLevels = NbLevels;
   string ObjBuyGoal = DoubleToStr(BuyGoal,Digits);
   string ObjBuyGoalProfit = BuyGoalProfit;
   string ObjSellGoal = DoubleToStr(SellGoal,Digits);
   string ObjSellGoalProfit = SellGoalProfit;
   string ObjPairTotal = pairtotal;
   string ObjInitialprice = DoubleToStr(InitialPrice,Digits);
   string ObjBuyGoalStop = DoubleToStr(BuyGoalStop,Digits);
   string ObjSellGoalStop = DoubleToStr(SellGoalStop,Digits);
   string ObjContinueTrading = ContinueTrading;

   int YInitialDistance, Ydist01, Ydist02, Ydist03, Ydist04, Ydist05, Ydist06, Ydist07, Ydist08, Ydist09, DistFromSide, YDistInc;
   DistFromSide = SideDistance;
   
   YInitialDistance = MoveUpDown;
   YDistInc = 15;
   Ydist01 = YInitialDistance + YDistInc;
   Ydist02 = Ydist01 + YDistInc +10;
   Ydist03 = Ydist02 + YDistInc;
   Ydist04 = Ydist03 + YDistInc;
   Ydist05 = Ydist04 + YDistInc +10;
   Ydist06 = Ydist05 + YDistInc;
   Ydist07 = Ydist06 + YDistInc+10;
   Ydist08 = Ydist07 + YDistInc;
   Ydist09 = Ydist08 + YDistInc;

  ObjectCreate("ObjContinueTrading_Label", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjContinueTrading_Label", "Continuous Trade " + ObjContinueTrading, FontSize, FontType, FontColor);
  ObjectSet("ObjContinueTrading_Label", OBJPROP_CORNER, WhatCorner);
  ObjectSet("ObjContinueTrading_Label", OBJPROP_XDISTANCE, DistFromSide);
  ObjectSet("ObjContinueTrading_Label", OBJPROP_YDISTANCE, YInitialDistance);
  
  ObjectCreate("ObjLots_Label", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjLots_Label", "Lots " + ObjLots, FontSize, FontType, FontColor);
  ObjectSet("ObjLots_Label", OBJPROP_CORNER, WhatCorner);
  ObjectSet("ObjLots_Label", OBJPROP_XDISTANCE, DistFromSide);
  ObjectSet("ObjLots_Label", OBJPROP_YDISTANCE, Ydist01);
  
  ObjectCreate("ObjLevels_Label", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjLevels_Label", "Levels " + ObjLevels, FontSize, FontType, FontColor);
  ObjectSet("ObjLevels_Label", OBJPROP_CORNER, WhatCorner);
  ObjectSet("ObjLevels_Label", OBJPROP_XDISTANCE, DistFromSide+60);
  ObjectSet("ObjLevels_Label", OBJPROP_YDISTANCE, Ydist01);
  
  ObjectCreate("ObjInitialprice_Label", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjInitialprice_Label", "Start Price " + ObjInitialprice, FontSize, FontType, FontColor);
  ObjectSet("ObjInitialprice_Label", OBJPROP_CORNER, WhatCorner);
  ObjectSet("ObjInitialprice_Label", OBJPROP_XDISTANCE, DistFromSide);
  ObjectSet("ObjInitialprice_Label", OBJPROP_YDISTANCE, Ydist02);
  
  ObjectCreate("ObjBuyGoal_Label", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjBuyGoal_Label", "Buy TP " + ObjBuyGoal, FontSize, FontType, FontColor);
  ObjectSet("ObjBuyGoal_Label", OBJPROP_CORNER, WhatCorner);
  ObjectSet("ObjBuyGoal_Label", OBJPROP_XDISTANCE, DistFromSide);
  ObjectSet("ObjBuyGoal_Label", OBJPROP_YDISTANCE, Ydist03);

  ObjectCreate("ObjSellGoal_Label", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjSellGoal_Label", "Sell TP " + ObjSellGoal, FontSize, FontType, FontColor);
  ObjectSet("ObjSellGoal_Label", OBJPROP_CORNER, WhatCorner);
  ObjectSet("ObjSellGoal_Label", OBJPROP_XDISTANCE, DistFromSide);
  ObjectSet("ObjSellGoal_Label", OBJPROP_YDISTANCE, Ydist04);

  ObjectCreate("ObjBuyGoalProfit_Label", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjBuyGoalProfit_Label", "Buy Pips  " + ObjBuyGoalProfit, FontSize, FontType, FontColor);
  ObjectSet("ObjBuyGoalProfit_Label", OBJPROP_CORNER, WhatCorner);
  ObjectSet("ObjBuyGoalProfit_Label", OBJPROP_XDISTANCE, DistFromSide);
  ObjectSet("ObjBuyGoalProfit_Label", OBJPROP_YDISTANCE, Ydist05);

  ObjectCreate("ObjSellGoalProfit_Label", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjSellGoalProfit_Label", "Sell Pips  " + ObjSellGoalProfit, FontSize, FontType, FontColor);
  ObjectSet("ObjSellGoalProfit_Label", OBJPROP_CORNER, WhatCorner);
  ObjectSet("ObjSellGoalProfit_Label", OBJPROP_XDISTANCE, DistFromSide);
  ObjectSet("ObjSellGoalProfit_Label", OBJPROP_YDISTANCE, Ydist06);

  ObjectCreate("ObjPairTotal_Label", OBJ_LABEL, 0, 0, 0);
  ObjectSetText("ObjPairTotal_Label", "Total Trades  " + ObjPairTotal, FontSize, FontType, FontColor);
  ObjectSet("ObjPairTotal_Label", OBJPROP_CORNER, WhatCorner);
  ObjectSet("ObjPairTotal_Label", OBJPROP_XDISTANCE, DistFromSide);
  ObjectSet("ObjPairTotal_Label", OBJPROP_YDISTANCE, Ydist07);
  }
  
int CheckProfits(double lots_1, int Goal, bool Current, double InitialPrice)
{
   int profit=0, cpt;
   if(Current)//return current profit
   {
      for(cpt=0;cpt<OrdersTotal();cpt++)
      {
         cg = OrderSelect(cpt, SELECT_BY_POS, MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderMagicNumber() == Magic && StrToDouble(OrderComment())==InitialPrice)
         {
            if(OrderType()==OP_BUY) profit+=(MarketInfo(Symbol(),MODE_BID)-OrderOpenPrice())/MarketInfo(Symbol(),MODE_POINT)*OrderLots()/lots_1;
            if(OrderType()==OP_SELL) profit+=(OrderOpenPrice()-MarketInfo(Symbol(),MODE_ASK))/MarketInfo(Symbol(),MODE_POINT)*OrderLots()/lots_1;
         }
      }
      return(profit);
   }
   else
   {
      if(Goal==OP_BUY)
      {
         for(cpt=0;cpt<OrdersTotal();cpt++)
         {
            cg = OrderSelect(cpt, SELECT_BY_POS, MODE_TRADES);
            if(OrderSymbol()==Symbol() && OrderMagicNumber() == Magic && StrToDouble(OrderComment())==InitialPrice)
            {
               if(OrderType()==OP_BUY) profit+=(OrderTakeProfit()-OrderOpenPrice())/MarketInfo(Symbol(),MODE_POINT)*OrderLots()/lots_1;
               if(OrderType()==OP_SELL) profit-=(OrderStopLoss()-OrderOpenPrice())/MarketInfo(Symbol(),MODE_POINT)*OrderLots()/lots_1;
               if(OrderType()==OP_BUYSTOP) profit+=(OrderTakeProfit()-OrderOpenPrice())/MarketInfo(Symbol(),MODE_POINT)*OrderLots()/lots_1;
            }
         }
         return(profit);
      }
      else
      {
         for(cpt=0;cpt<OrdersTotal();cpt++)
         {
            cg = OrderSelect(cpt, SELECT_BY_POS, MODE_TRADES);
            if(OrderSymbol()==Symbol() && OrderMagicNumber() == Magic && StrToDouble(OrderComment())==InitialPrice)
            {
               if(OrderType()==OP_BUY) profit-=(OrderOpenPrice()-OrderStopLoss())/MarketInfo(Symbol(),MODE_POINT)*OrderLots()/lots_1;
               if(OrderType()==OP_SELL) profit+=(OrderOpenPrice()-OrderTakeProfit())/MarketInfo(Symbol(),MODE_POINT)*OrderLots()/lots_1;
               if(OrderType()==OP_SELLSTOP) profit+=(OrderOpenPrice()-OrderTakeProfit())/MarketInfo(Symbol(),MODE_POINT)*OrderLots()/lots_1;
               
            }
         }
         return(profit);
      }
   }
}

bool EndSession()
{
   int OrderCnt, total=OrdersTotal();
   for(OrderCnt=0;OrderCnt<=total;OrderCnt++)
   {
      cg = OrderSelect(OrderCnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol() && OrderMagicNumber()== Magic)
      {
         if (OrderType() == OP_SELLSTOP || OrderType() == OP_BUYSTOP) cg = OrderDelete(OrderTicket());
         if (OrderType() == OP_SELL || OrderType() == OP_BUY) cg = OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_BID),30);
      } 
   }
      if(!ContinueTrading)  Enter=false;
      return(true);
}


bool KillSession()
{
   int OrderCnt, total=OrdersTotal();
   for(OrderCnt=0;OrderCnt<=total;OrderCnt++)
   {
      cg = OrderSelect(OrderCnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol() && OrderMagicNumber()== Magic && OrderType()== OP_BUYSTOP) cg = OrderDelete(OrderTicket());
      else if(OrderSymbol()==Symbol() && OrderMagicNumber() == Magic && OrderType() == OP_SELLSTOP) cg = OrderDelete(OrderTicket());      
      else if(OrderSymbol()==Symbol() && OrderMagicNumber() == Magic && OrderType() ==OP_BUY) cg = OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_BID),30);
      else if(OrderSymbol()==Symbol() && OrderMagicNumber() == Magic && OrderType() ==OP_SELL) cg = OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symbol(),MODE_ASK),30);
       
    }
      if(!ContinueTrading)  Enter=false;
      return(true);
}
bool Indicator_Enter(bool indicator_1_, int period_, bool indicator_2_)
{
      bool ans = false;
      if (indicator_1_)
      {
      double high = High[iHighest(NULL,0,MODE_HIGH,period_,1)];
      double low = Low[iLowest(NULL,0,MODE_LOW,period_,1)];
      if ( NormalizeDouble(Bid,Digits) <= NormalizeDouble(low,Digits) ) ans = true;
      if ( NormalizeDouble(Bid,Digits) >= NormalizeDouble(high,Digits)) ans = true;
      }
      if (indicator_2_)
      {
      double day_high = iHigh(Symbol(),PERIOD_D1,1); 
      double day_low  = iLow(Symbol(),PERIOD_D1,1); 
      if ( NormalizeDouble(Bid,Digits) <= NormalizeDouble(day_low,Digits) ) ans = true;
      if ( NormalizeDouble(Bid,Digits) >= NormalizeDouble(day_high,Digits)) ans = true;
      }
      return (ans);
}
打赏
版权所有转载请注明标题及链接:峰汇在线 » 网格马丁EA Level Scalper V4.mq4
上一篇:
下一篇:
分享到: 更多 (0)

如您觉得此文不错请扫码打赏!

支付宝扫一扫打赏

微信扫一扫打赏