微信扫码
在线客服
返回顶部
首页 > 程序化语言学习

SureForexHedging解析

分享到:
点击次数:183 更新时间:2021年10月31日20:45:40 打印此页 关闭
突破型EA,避开了凌晨时段,对冲保护,简单策略
#property copyright "Copyright 2018, sure fire strategy"#property link      "sure fire forex strategy"extern double lots = 0.02;extern double dollars=3;extern double incr = 0.02;extern double multiplier=2;double sellorderprofit,buyorderprofit;extern int magic=1;extern double startHour = 2;extern double endHour = 22;extern double hedge = 300;string pending="null";double a,b;int xTime = 0;bool basketdollars=true;extern string C0 = "#Do not touch values under this!!#";extern string C99 = "Logics";bool runnable=true;bool init=true;bool result;double pBid, pAsk, pp;int i=0;datetime timeprev=0;
double b_close;
double init_lot;
double LastBuy;
  double LastSell;
 double s_close;int init() {
 init_lot=lots;
 return (0);}int deinit() {
 return (0);}int start() {
    double lda_0[][6];
    ArrayCopyRates(lda_0, Symbol(), PERIOD_D1);  //日线数据
    if (xTime != TimeDay(lda_0[0][0]) && DayOfWeek() != 1 )
    {
           xTime = TimeDay(lda_0[0][0]);
           a=iLow(Symbol(), PERIOD_D1, 1);//前日线最低
   }
  Strategy_1();
  if(basketdollars)
  {
    sellorderprofit=0;
    buyorderprofit=0;
  }  

  if(OrdersTotal()>0)
  {
    for(i=0;i<=OrdersTotal();i++)
    {
          OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
          if(OrderType()==OP_SELL && OrderMagicNumber()== magic)
          {
             if(basketdollars) sellorderprofit=sellorderprofit+ OrderProfit()+ OrderSwap() + OrderCommission();
          }
          if(OrderType()==OP_BUY && OrderMagicNumber()== magic)
          {
             if(basketdollars) buyorderprofit=buyorderprofit+ OrderProfit()+ OrderSwap() + OrderCommission();
          }  
    }
  }

  if(basketdollars)
  {
    if ((sellorderprofit + buyorderprofit) > dollars)
    {
       CloseAllTrades();
       lots = init_lot;
       sellorderprofit=0;
       buyorderprofit=0;
       pending="null";
    }
  }
  return(0);}          
        int Strategy_1() {
 string l_dbl2str_12="Scalp Sure";
  b=iLow(Symbol(), PERIOD_M1, 1);//上一分钟最低
  if (b < a) a=b;//新低

 if (lots > init_lot && (CountOrdersThisPairBUY() == 0 && CountOrdersThisPairSELL() == 0)   )
 { lots =init_lot;}

  if (CountOrdersThisPairBUY() == 0 && CountOrdersThisPairSELL() == 0  
&& Hour() >=startHour  && Hour() < endHour  // 2-22点,避开凌晨头皮时段&& Ask > iHigh(Symbol(), PERIOD_D1, 0)  //日内突破新高&& AccountFreeMargin() > 0 )
{
     OrderSend(Symbol(), OP_BUY,  lots , Ask, 3, 0,0, l_dbl2str_12, magic, 0, RoyalBlue);
     lots=lots + (incr*multiplier);
     if ( CountOrdersThisPairBUY() > 0 )
     {
        OrderSend(Symbol(),OP_SELLSTOP, lots ,Bid-hedge*Point,3,0,0,l_dbl2str_12,magic,0,Red);//下方保护
        pending="SELLSTOP";
     }
  }
  if (CountOrdersThisPairBUY() == 0 && CountOrdersThisPairSELL() == 0  && Hour() >=startHour  
&& Hour() < endHour  
&& Bid < a  && AccountFreeMargin() > 0   ) {
    OrderSend(Symbol(), OP_SELL,  lots , Bid, 3, 0, 0, l_dbl2str_12, magic, 0, Red);
            lots=lots + (incr*multiplier);
            if ( CountOrdersThisPairSELL() > 0    ){
      OrderSend(Symbol(),OP_BUYSTOP, lots ,Ask+hedge*Point,3,0,0,l_dbl2str_12,magic,0,RoyalBlue);
      pending="BUYSTOP";
    }
 }
 if ((CountOrdersThisPairBUYSTOP() == CountOrdersThisPairSELLSTOP()) && pending=="SELLSTOP"  && AccountFreeMargin() > 0
&& (CountOrdersThisPairBUY() > 0 || CountOrdersThisPairSELL() > 0    ) ) {
   lots=lots * multiplier;
           OrderSend(Symbol(),OP_BUYSTOP, lots ,Ask+hedge*Point,3,0,0,l_dbl2str_12,magic,0,RoyalBlue);
   pending="BUYSTOP";
  }
  if ((CountOrdersThisPairBUYSTOP() == CountOrdersThisPairSELLSTOP()) && pending=="BUYSTOP"  && AccountFreeMargin() > 0 && (CountOrdersThisPairBUY() > 0 || CountOrdersThisPairSELL() > 0    ) ) {
       lots=lots * multiplier;
    OrderSend(Symbol(),OP_SELLSTOP, lots ,Bid-hedge*Point,3,0,0,l_dbl2str_12,magic,0,Red);
    pending="SELLSTOP" ;
  }
  return (0);}int CountOrdersThisPairBUY() {
 int l_count_0;
 for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++)
 {
    OrderSelect(l_pos_4, SELECT_BY_POS);
    if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == magic)
    l_count_0++;
 }
 return (l_count_0);}int CountOrdersThisPairSELL() {
 int l_count_0 = 0;
 for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++)
 {
    OrderSelect(l_pos_4, SELECT_BY_POS);
    if (OrderSymbol() == Symbol() && OrderType() == OP_SELL  && OrderMagicNumber() == magic)
    l_count_0++;
 }
 return (l_count_0);}int CountOrdersThisPairBUYSTOP() {
 int l_count_0;
 for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
    OrderSelect(l_pos_4, SELECT_BY_POS);
    if (OrderSymbol() == Symbol() && OrderType() == OP_BUYSTOP && OrderMagicNumber() == magic) l_count_0++;
 }
 return (l_count_0);}int CountOrdersThisPairSELLSTOP() {
 int l_count_0 = 0;
 for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
    OrderSelect(l_pos_4, SELECT_BY_POS);
    if (OrderSymbol() == Symbol() && OrderType() == OP_SELLSTOP  && OrderMagicNumber() == magic) l_count_0++;
 }
 return (l_count_0);}// Close all open trades when in profitvoid CloseAllTrades(){
 for (i=OrdersTotal()-1;i>=0;i--){
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
           if (OrderType()==OP_SELL && OrderMagicNumber() == magic)result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,CLR_NONE);
           if(result!=TRUE) Print("LastError = ", GetLastError());
           if (OrderType()==OP_BUY && OrderMagicNumber() == magic)result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,CLR_NONE);
           if(result!=TRUE) Print("LastError = ", GetLastError());
           if (OrderType()==OP_BUYSTOP && OrderMagicNumber() == magic)result=OrderDelete(OrderTicket());
           if(result!=TRUE) Print("LastError = ", GetLastError());
           if (OrderType()==OP_SELLSTOP && OrderMagicNumber() == magic)result=OrderDelete(OrderTicket());
           if(result!=TRUE) Print("LastError = ", GetLastError());
       }
    else Print( "Error when order select ", GetLastError());
    }

下一条:Flows EA