Virtual Stop Loss

 

Hi guys,

I'm trying to make an EA which I can use as a virtual stop loss.

I essentially want the EA to close any positions of the current symbol if it hits -1% without the use of conventional stop losses as my broker keeps giving bad slippage.

The code I wrote doesn't seem to calculate the current symbol profit correctly... any help?


Thanks

input bool UseEquityStop = true;
input double MaxLoss = 1; //Max Loss (%)

//+------------------------------------------------------------------------------------------------------------------------+

void OnTick()
  {
   //if (UseEquityStop==true &&  AccountEquity() < ((AccountBalance()*(1-(MaxLoss/100)))))
   if (UseEquityStop==true && (OrderProfit()+OrderCommission()+OrderSwap()) < -(AccountBalance()*(MaxLoss/100)) )
      {
       CloseAll();
      }
      
      
  }

//+-------------------------------------------------------------------------------------------------------------------------+

void CloseAll()
  {
  int totalorders = OrdersTotal();
  for(int i=totalorders-1;i>=0;i--)
     {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
       {
        if (OrderSymbol()==Symbol()) 
        
         {
          if (OrderType() == OP_BUY) OrderClose( OrderTicket(), OrderLots(), MarketInfo(NULL,MODE_BID), 5, Red );
          if (OrderType() == OP_SELL) OrderClose( OrderTicket(), OrderLots(), MarketInfo(NULL,MODE_ASK), 5, LimeGreen );
          }
        }
      }
     return;
  } 
 

Hi guys,


I found a solution if this is what anyone is looking for. It counts the current symbol profit and if the equity stop is hit for that symbol, it only closes open positions for that symbol.

input bool UseEquityStop = true;
input double MaxLoss = 1; //Max Loss (%)

//+------------------------------------------------------------------------------------------------------------------------+

void OnTick()
  {
   double FloatingProfit = NULL;
   int i = NULL;
   for(i=0;i<OrdersTotal();i++)
      {
       OrderSelect(i,SELECT_BY_POS);
       if(OrderSymbol() == Symbol())
         {
          if( OrderType() == OP_BUY || OrderType() == OP_SELL )
            {
             FloatingProfit = FloatingProfit + OrderProfit() + OrderSwap() + OrderCommission();
            }    
         }
      }
      
   if (UseEquityStop==true && (FloatingProfit < -(AccountBalance()*(MaxLoss/100)) ))
      {
       CloseAll();
      }
  }

//+-------------------------------------------------------------------------------------------------------------------------+

void CloseAll()
  {
  for(int i=OrdersTotal()-1;i>=0;i--)
     {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
       {
        if (OrderSymbol()==Symbol()) 
         {
          if (OrderType() == OP_BUY) OrderClose( OrderTicket(), OrderLots(), MarketInfo(NULL,MODE_BID), 5, Red );
          if (OrderType() == OP_SELL) OrderClose( OrderTicket(), OrderLots(), MarketInfo(NULL,MODE_ASK), 5, LimeGreen );
          }
        }
      }
     return;
  }