Einzelnen Beitrag anzeigen
  #135 (permalink)  
Alt 14.02.12
Mast83 Mast83 ist offline
Elite Mitglied
 
Registriert seit: Aug 2011
Ort: NRW
Beiträge: 764
Mast83 befindet sich auf einem aufstrebenden Ast
Mast83 eine Nachricht über ICQ schicken Mast83 eine Nachricht über Skype™ schicken
Standard

Für all jene die ihn noch mal haben wollen...

Code:
extern double    Lot              = 0.01;
extern bool      MM               = TRUE;
extern double    Risk             = 0.5; //%
 
extern int       StopLoss         = 5;
extern int       TakeProfit       = 15;

extern bool      UseTrailing      = false;
extern int       TrailingStopLoss = 5;
extern int       TrailingStep     = 2.5;

extern int       MagicNumber      = 8888;

bool initialized = false;
int MAX_TRAILING_STEP = 15;

int init() {
   if (Digits == 5 || Digits == 3)
  {
   StopLoss = StopLoss * 10;
   TakeProfit = TakeProfit * 10;
   TrailingStopLoss = TrailingStopLoss * 10;
   TrailingStep = TrailingStep * 10;
  }
   double stopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);
   if (StopLoss < stopLevel) {
      Alert("StopLoss", stopLevel);
      return(-1);
   }
   if (TakeProfit < stopLevel && !UseTrailing) {
      Alert("TakeProfit", stopLevel);
      return(-1);
   }
   double minLot = MarketInfo(Symbol(), MODE_MINLOT);
   if (Lot < minLot) {
      Alert("minLot", minLot);
      return(-1);
   }
   double maxLot = MarketInfo(Symbol(), MODE_MAXLOT);
   if (Lot > maxLot) {
      Alert("maxLot", maxLot);
      return(-1);
   }
   double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
   for (double i = minLot; i <= maxLot; i+=lotStep) {
      if (i == Lot) {
         break;
      }
      if (i + lotStep > maxLot) {
         Alert("LotStep", lotStep);
         return(-1);
      }
   }
   if (UseTrailing) {
      if (TrailingStopLoss < stopLevel) {
         Alert("TrailingStopLoss", stopLevel);
         return(-1);
      }
      if (TrailingStep < MAX_TRAILING_STEP) {
         Alert("TrailingStep", MAX_TRAILING_STEP);
         return(-1);
      }
   }
   initialized = true;
   return(0);
}

int deinit() {
   initialized = false;
   return(0);
}

int start() {
   if (!initialized) {
      return(0);
   }
   if (DayOfWeek() == 0 || DayOfWeek() == 6) {
      return(0);
   }
   if (!IsTradeAllowed()) {
      return(0);
   }
   
   int ticket;
   int q = 0;
 
   double L = iLow(NULL, NULL, 1);
   double H = iHigh(NULL, NULL, 1);
   double C = iClose(NULL, NULL, 0);
   double O = iOpen(NULL, NULL, 0);
   double spred = MarketInfo(Symbol(), MODE_SPREAD) * Point;
   double sl = StopLoss * Point;
   double tp = TakeProfit * Point;
   
   if (UseTrailing) {
      TrailingPositions(TrailingStopLoss, TrailingStep, MagicNumber);
   }
   
   //
   if  (O >= H || O <= L) {
      Comment("\\OP_BUYSTOP & OP_SELLSTOP\\");
      return (0);
   }
   
   
   if (MM) Lot = AutoLot(Risk);
   for (q = 0; q < OrdersTotal(); q++) {
      if (OrderSelect(q, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_BUYSTOP) {
            return(0);
         } else if (OrderType() == OP_SELLSTOP) {
            return(0);
         }
      }
   }
   
   double stopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL) * Point;
 
   //ORDER BUY
   if (H - C >= stopLevel && C - L >= stopLevel && O < H) {
      double tpReal = 0;
      if (!UseTrailing) {
         tpReal = H + tp + spred * 2;
      }
      ticket = OrderSend(Symbol(), OP_BUYSTOP, Lot, H + spred * 2, 0, H - sl + spred * 2, tpReal, NULL, MagicNumber, iTime(Symbol(), Period(), 0) + Period() * 60);
      ChechAndPrintError(ticket);
   } else {
      Comment("\Sleeping.... Will wake up again at 0:00");
   }     
  
   //ORDER SELL
   if (H - C >= stopLevel && C - L >= stopLevel && O > L) {
      tpReal = 0;
      if (!UseTrailing) {
         tpReal = L - spred - tp;
      }
      ticket = OrderSend(Symbol(), OP_SELLSTOP, Lot, L - spred, 0, L - spred + sl, tpReal, NULL, MagicNumber, iTime(Symbol(), Period(), 0) + Period() * 60);
      ChechAndPrintError(ticket);
   } else {
      Comment("\Sleeping.... Will wake up again at 0:00");
   }
   
}

void TrailingPositions(int trailingStopLoss, int trailingStep, int magicNumber) {
   double bid = 0;
   double ask = 0;
   for (int i = 0; i < OrdersTotal(); i++) {
      if (!(OrderSelect(i, SELECT_BY_POS)) || OrderSymbol() != Symbol() || OrderMagicNumber() != magicNumber) {
         continue;
      }
      
      bid = MarketInfo(OrderSymbol(), MODE_BID);
      ask = MarketInfo(OrderSymbol(), MODE_ASK);
      
      if (OrderType() == OP_BUY) {
         if (bid - OrderOpenPrice() > trailingStopLoss * Point) {
            if (OrderStopLoss() < bid - (trailingStopLoss + trailingStopLoss - 1) * Point || OrderStopLoss() == 0) {
               OrderModify(OrderTicket(), OrderOpenPrice(), bid - trailingStopLoss * Point, OrderTakeProfit(), OrderExpiration());
            }
         }
      } else if (OrderType() == OP_SELL) {
         if (OrderOpenPrice() - ask > trailingStopLoss * Point) {
            if (OrderStopLoss() > ask + (trailingStopLoss + trailingStep - 1) * Point || OrderStopLoss() == 0) {
               OrderModify(OrderTicket(), OrderOpenPrice(), ask + trailingStopLoss * Point, OrderTakeProfit(), OrderExpiration());
            }
         }
      }
   }
}

void ChechAndPrintError(int result) {
   if (result < 0) {
      int err = GetLastError();
      Print("Expert+10 >> error(", err, ")");
   }
}

double AutoLot(double ad_0)
 {
   double l_minlot = MarketInfo(Symbol(), MODE_MINLOT);
   double l_maxlot = MarketInfo(Symbol(), MODE_MAXLOT);
   double l_lotstep = MarketInfo(Symbol(), MODE_LOTSTEP);
   double LotSize = AccountBalance()/ MarketInfo(Symbol(), MODE_MARGINREQUIRED) * ad_0;
   LotSize = MathMin(l_maxlot, MathMax(l_minlot, LotSize));
   if (l_minlot >= 0.01) LotSize = NormalizeDouble(LotSize, 2);
   else {
      if (l_minlot >= 1.0) LotSize = NormalizeDouble(LotSize, 1);
      else LotSize = NormalizeDouble(LotSize, 0);
   }
   if (LotSize < l_minlot) LotSize = l_minlot;
   if (LotSize > l_maxlot) LotSize = l_maxlot;
   return (LotSize);
}
__________________
Heute Weizen, Abends Corn morgen fangen wir an von vorn...