Change SL to breakeven

Luandre Ezra  

Hi,

I'd like to change my SL to breakeven when certain logic triggered. 

string GetTradeManagementStatus(int SymbolLoop)
  {
   string CurrentSymbol = SymbolArray[SymbolLoop];

//Need to copy values from indicator buffers to local buffers
   int    numValuesNeeded = 3;
   double bufferIndi[];
   
   //copy value to local buffer
   bool fillSuccessMiddle = tlamCopyBuffer(handle_DonchianChannel[SymbolLoop], 2, bufferMid, numValuesNeeded, CurrentSymbol, "Donchian Channel");

   double CurrentIndi= bufferIndi[0];

   double CurrentClose = iClose(CurrentSymbol, Period(), 0);
   double CurrentAsk   = SymbolInfoDouble(CurrentSymbol,SYMBOL_ASK);
   double CurrentBid   = SymbolInfoDouble(CurrentSymbol,SYMBOL_BID);

//INSERT YOUR OWN LOGIC MANAGEMENT HERE
   if(logic here)
      return("CHANGE BUY SL");
   else
      if(logic here)
         return("CHANGE SELL SL");
    else
      return("NO CHANGE IN SL");
  }


void ProcessTradeMangement(int SymbolLoop, string TradeManagementDirection)
  {
   string CurrentSymbol = SymbolArray[SymbolLoop];
   int    buyCount      = 0;
   int    sellCount     = 0;
   bool   changeSL      = false;

   //SETUP CTrade tradeObject HERE
   CTrade tradeObject;
   //SETUP CPositionInfo Pinfo HERE
   CPositionInfo  position;
   
   //looked for open position direction
   for(int i=PositionsTotal()-1; i>=0; i--)
      if(position.SelectByIndex(i))
         if(position.Symbol() == CurrentSymbol && position.Magic() == MagicNumber)
           {   
            if(position.PositionType() == POSITION_TYPE_BUY)
               buyCount++;
            if(position.PositionType() == POSITION_TYPE_SELL)
               sellCount++;
           }

   if(TradeManagementDirection == "CHANGE BUY SL" && buyCount>0)
      changeSL = true;

   if(TradeManagementDirection == "CHANGE SELL SL" && sellCount>0)
      changeSL = true;
   
   if(changeSL == true)
     {
      double openPrice = position.PriceOpen();
      double TP        = position.TakeProfit();
      bool bTMCheck = tradeObject.PositionModify(CurrentSymbol, openPrice, TP);
     }
  }

I used code above to change SL to Breakeven when logic returns true. I'd like to ask if there's anything that can be optimize or corrected.

Thank you

William Roeder  
Luandre Ezra: if there's anything that can be optimize or corrected
  1. Do not use strings or ints
          return("CHANGE BUY SL");
       ⋮
             return("CHANGE SELL SL");
        else
          return("NO CHANGE IN SL");
    ⋮
    void ProcessTradeMangement(int SymbolLoop, string TradeManagementDirection)
       bool   changeSL      = false;    
       if(TradeManagementDirection == "CHANGE BUY SL" && buyCount>0)
           changeSL = true;
    when you mean an enumeration:
    enumeration ChangeSL{ CSL_BUY, CSL_SELL, CSL_IDLE};
       ⋮
          return(CSL_BUY);
       ⋮
    void ProcessTradeMangement(int SymbolLoop, ChangeSL TradeManagementDirection)
       ⋮
       if(TradeManagementDirection == CSL_BUY && buyCount>0)
  2. Simplify
    bool   changeSL      = false;    
    if(TradeManagementDirection == "CHANGE BUY SL" && buyCount>0)
        changeSL = true;
    your booleans
    bool   changeSL      = TradeManagementDirection == "CHANGE BUY SL" && buyCount>0;

Reason: