//+------------------------------------------------------------------+ //| Trailing_EA.mq4 | //| Copyright © 2007 | //| | //| Written by Robert Hill aka MrPip for Forex-tsd group | //| | //+------------------------------------------------------------------+ #property copyright "Copyright © 2007, Robert Hill aka MrPip" #include #include extern bool UseMagicNumbers = true; extern int MagicNumber1 = 12345; extern int MagicNumber2 = 67890; extern string Expert_Name = "TrailingBEpoint"; extern int Slippage=3; extern double BreakEvenPoints = 300; extern int LockInPoints = 10; // Profit Lock in pips //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- HandleOpenPositions(); //---- return(0); } bool CheckMagicNumbers(int Magic) { if (!UseMagicNumbers) return(true); if (Magic == MagicNumber1 || Magic == MagicNumber2) return(true); return(false); } //+------------------------------------------------------------------+ //| Handle Open Positions | //| Check if any open positions need to be closed or modified | //+------------------------------------------------------------------+ void HandleOpenPositions() { int cnt = OrdersTotal(); if (cnt == 0) return; for(cnt=OrdersTotal()-1;cnt>=0;cnt--) { OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES); if ( !CheckMagicNumbers(OrderMagicNumber())) continue; if(OrderType() == OP_BUY) { BreakEven_TrailingStop(OP_BUY,OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderTakeProfit()); } if(OrderType() == OP_SELL) { BreakEven_TrailingStop(OP_SELL,OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderTakeProfit()); } } } int ModifyOrder(int ord_ticket,double op, double price,double tp, color mColor) { int CloseCnt, err; CloseCnt=0; while (CloseCnt < 3) { if (OrderModify(ord_ticket,op,price,tp,0,mColor)) { CloseCnt = 3; } else { err=GetLastError(); Print(CloseCnt," Error modifying order : (", err , ") " + ErrorDescription(err)); if (err>0) CloseCnt++; } } } //+------------------------------------------------------------------+ //| BreakEvenExpert_v1.mq4 | //| Copyright © 2006, Forex-TSD.com | //| Written by IgorAD,igorad2003@yahoo.co.uk | //| http://finance.groups.yahoo.com/group/TrendLaboratory | //+------------------------------------------------------------------+ void BreakEven_TrailingStop(int type, int ticket, double op, double os, double tp) { int digits; double pBid, pAsk, BuyStop, SellStop; digits = MarketInfo(Symbol(), MODE_DIGITS); if (type==OP_BUY) { pBid = MarketInfo(Symbol(), MODE_BID); if ( pBid-op > BreakEvenPoints ) { BuyStop = op + LockInPoints; if (digits > 0) BuyStop = NormalizeDouble( BuyStop, digits); if (os < BuyStop) ModifyOrder(ticket,op,BuyStop,tp,LightGreen); return; } } if (type==OP_SELL) { pAsk = MarketInfo(Symbol(), MODE_ASK); if ( op - pAsk > BreakEvenPoints ) { SellStop = op - LockInPoints; if (digits > 0) SellStop = NormalizeDouble( SellStop, digits); if (os > SellStop) ModifyOrder(ticket,op,SellStop,tp,DarkOrange); return; } } }