Writing own trailing stop & Stop Loss script

 

I found a few good "script order" that are helpful but none them had auto-trailing stop, and 0 in stop loss is the price of entry. What up with that. Null does not work. 

I post this topic in hope that someone guide me or send me to an article on how to create an auto-trailing & set stop loss to "null", onto an existing script, for my own personal use only. 

 

I use an ATR trailing stop. here's my code, enjoy!


I call this function via:

void onTick(){

ATRTrailingStop(ATRValue,AskStopLoss,_MagicNumber);

}


and pass on ATR value etc..

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#include "..\Functions\ClosedPrice.mq5"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void ATRTrailingStop(double ATRValue,double AskStopLoss,ulong _MagicNumber)
  {

   int NumberOfPositionsForThisPair="";
   ulong order_magic="";
   int CurrentPositionType="";
   double CurrentPositionTP = "";
   double CurrentPositionSL = "";
   double PositionOpenPrice = "";
   double PositionCurrentPrice="";

   int ticket="";
   ulong PositionTicket="";

   double StopLossGAP,PointToIncrease,TotalGAP,PriceWithAddedGAP="";
   double ATRRiskAmountForTrailingStop=1.5; // 3 x the ATR Value

   double ClosedPrice=ClosedPrice();

   double AmountPipsForATRTrailingStop=NormalizeDouble(ATRValue*ATRRiskAmountForTrailingStop,_Digits);

   for(int i=0;i<PositionsTotal(); i++)
     {
      if((PositionGetTicket(i))>0 && PositionGetString(POSITION_SYMBOL)==_Symbol)
        {

         order_magic=PositionGetInteger(POSITION_MAGIC);
         PositionTicket=PositionGetInteger(POSITION_TICKET);
         CurrentPositionType=PositionGetInteger(POSITION_TYPE);
         CurrentPositionTP = PositionGetDouble(POSITION_TP);
         CurrentPositionSL = PositionGetDouble(POSITION_SL);
         PositionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
         PositionCurrentPrice=PositionGetDouble(POSITION_PRICE_CURRENT);
         ticket=PositionGetTicket(i);
         NumberOfPositionsForThisPair++;

        }
     }

// ---------------------Check to see if the current price is higher than current SL -- // Break even for BUY/BID = 0
   if((NumberOfPositionsForThisPair==1) && (CurrentPositionType==0) && (ClosedPrice>CurrentPositionSL))
     {
      //  BreakEvenStopLoss(CurrentPositionType, Ask, Bid, PositionOpenPrice, CurrentPositionSL, order_magic);
      // Print("YES, PositionCurrentPrice is higher than CurrentPositionSL");
      // This will give us GAP between current ATR stoploss and GAP between the current Stoploss
      StopLossGAP=NormalizeDouble(( ClosedPrice-CurrentPositionSL),_Digits);

      TotalGAP=(StopLossGAP-AmountPipsForATRTrailingStop);

      // For added SL. We calculate current SL + GAP
      PriceWithAddedGAP=(CurrentPositionSL+TotalGAP);

      if((StopLossGAP>=AmountPipsForATRTrailingStop) && (TotalGAP!=0))
        {
         Print("BUY-BID - ATR Trailing STOP");
         Print("SL GAP: "+StopLossGAP+" ATRStop: "+AmountPipsForATRTrailingStop+" Total GAP:"+TotalGAP+" Price with Added GAP: "+PriceWithAddedGAP);
         Print("Closed Price: "+ClosedPrice);

         trade.PositionModify(PositionTicket,PriceWithAddedGAP,0);

        }

     }

// -----------------------Check to see if the current price is higher than current SL -- // Break even for SELL/ASK = 1
   if((NumberOfPositionsForThisPair==1) && (CurrentPositionType==1) && (ClosedPrice<CurrentPositionSL))
     {
      //  BreakEvenStopLoss(CurrentPositionType, Ask, Bid, PositionOpenPrice, CurrentPositionSL, order_magic);
      // Print("YES, PositionCurrentPrice is higher than CurrentPositionSL");
      // This will give us GAP between current ATR stoploss and GAP between the current Stoploss
      StopLossGAP=NormalizeDouble(( CurrentPositionSL-ClosedPrice),_Digits);

      TotalGAP=(StopLossGAP-AmountPipsForATRTrailingStop);

      // For added SL. We calculate current SL + GAP
      PriceWithAddedGAP=(CurrentPositionSL-TotalGAP);

      if((StopLossGAP>=AmountPipsForATRTrailingStop) && (TotalGAP!=0))
        {
         Print("SELL-ASK - ATR Trailing STOP");
         Print("SL GAP: "+StopLossGAP+" ATRStop: "+AmountPipsForATRTrailingStop+" Total GAP:"+TotalGAP+" Price with Added GAP: "+PriceWithAddedGAP);
         Print("Closed Price: "+ClosedPrice);

         trade.PositionModify(PositionTicket,PriceWithAddedGAP,0);

        }

     }





  }
//+------------------------------------------------------------------+
 
mrwick:

I use an ATR trailing stop. here's my code, enjoy!


I call this function via:


and pass on ATR value etc..

Interesting, using ATR too. Thank.