SetUpProfitAndLoss

Author: TheLiteShadow
Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
0 Views
0 Downloads
0 Favorites
SetUpProfitAndLoss
//+------------------------------------------------------------------+
//|                                   Copyright © 2012,TheLiteShadow |
//+------------------------------------------------------------------+
#property copyright "TheLiteShadow"
extern double TakeProfit=20;
extern double StopLoss=20;
extern double TrailingStop=-10;//îòðèöàòåëüíûå çíà÷åíèÿ äëÿ ïåðåâîäà â îðäåðà ÁÓ ïî äîñòèæåíèè
extern bool   SetOnlyZeroValues=true; //Ïðèçíàê èçìåíåíèÿ òîëüêî íóëåâûõ çíà÷åíèé
extern color  BuyOrderColor=Green;
extern color  SellOrderColor=Red;
//+------------------------------------------------------------------+
int start()
  { int cnt, ticket, total=OrdersTotal(); double kd;
    if (Digits==5) kd=10; else kd=1;
    double TP=MathAbs(TakeProfit)*kd;
    double SL=MathAbs(StopLoss)*kd;
    double TS=TrailingStop*kd;
    if (TP<MarketInfo(Symbol(),MODE_STOPLEVEL) && TP!=0)
      { Comment("TakeProfit value too small, must be >= "+DoubleToStr(MarketInfo(Symbol(),MODE_STOPLEVEL)/kd,0));
       return(0);
      }
    if (SL<MarketInfo(Symbol(),MODE_STOPLEVEL) && SL!=0)
      { Comment("StopLoss value too small, must be >= "+DoubleToStr(MarketInfo(Symbol(),MODE_STOPLEVEL)/kd,0));
       return(0);
      }
 // Óñòàíîâêà îãðàíè÷åíèÿ ïðèáûëè è óáûòêà
   for(cnt=0;cnt<total;cnt++)
     { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        { if(OrderType()==OP_BUY)
           { if(((OrderTakeProfit()==0 && TP!=0) || (OrderStopLoss()==0 && SL!=0)) && !SetOnlyZeroValues)
              { OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-Point*SL,OrderOpenPrice()+Point*TP,0,BuyOrderColor);
               return(0);
              }
             if(OrderTakeProfit()==0 && SetOnlyZeroValues && TP!=0)
              { OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()+Point*TP,0,BuyOrderColor);
               return(0);
              }
             if(OrderStopLoss()==0 && SetOnlyZeroValues && SL!=0)
              { OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-Point*SL,OrderTakeProfit(),0,BuyOrderColor);
               return(0);
              } 
           }
         else
           { if(((OrderTakeProfit()==0 && TP!=0) || (OrderStopLoss()==0 && SL!=0)) && !SetOnlyZeroValues)
              { OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+Point*SL,OrderOpenPrice()-Point*TP,0,SellOrderColor);
               return(0);
              }
             if(OrderTakeProfit()==0 && SetOnlyZeroValues && TP!=0)
              { OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),OrderOpenPrice()-Point*TP,0,SellOrderColor);
               return(0);
              }
             if(OrderStopLoss()==0 && SetOnlyZeroValues && SL!=0)
              { OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+Point*SL,OrderTakeProfit(),0,SellOrderColor);
               return(0);
              }
           }
        }
     }
 if (MathAbs(TS)<MarketInfo(Symbol(),MODE_STOPLEVEL) && TS!=0)
   { Comment("Traling stop value too small, must be >= "+DoubleToStr(MarketInfo(Symbol(),MODE_STOPLEVEL)/kd,0));
    return(0);
   }
//Trailing
 if (TS>0) { 
   for(cnt=0;cnt<total;cnt++)
     { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        { if(OrderType()==OP_BUY)
          { if(Bid-OrderOpenPrice()>Point*TS && OrderStopLoss()<Bid-Point*TS)
            { OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TS,OrderTakeProfit(),0,BuyOrderColor);
              return(0);
            }
          }
         else
          { if(OrderOpenPrice()-Ask>Point*TS && OrderStopLoss()>Ask+Point*TS)
            { OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TS,OrderTakeProfit(),0,SellOrderColor);
              return(0);
            }
          }
        }
     }}
//ZeroTrailing     
 if (TS<0) { 
   for(cnt=0;cnt<total;cnt++)
     { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
        { if(OrderType()==OP_BUY)
          { if(Bid-OrderOpenPrice()>-Point*TS && OrderStopLoss()!=OrderOpenPrice() && OrderStopLoss()<Bid+Point*TS)
            { OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,BuyOrderColor);
              return(0);
            }
          }
         else
          { if(OrderOpenPrice()-Ask>-Point*TS && OrderStopLoss()!=OrderOpenPrice() && OrderStopLoss()>Ask-Point*TS)
            { OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,SellOrderColor);
              return(0);
            }
          }
        }
     }}
   return(0);
 }

Comments