Try to figure out how to put my stop loss below my setup candle

 

Heyeveryone, i've been at this one for a couple days (im really new to coding) so long story short, bought a book, they give you a simple ea to work with, i've been tweaking and trying to code my own

strategy into it (my manual one) and i cannot for the life of me get it to work lol.. all im trying to do is get the stop loss to show up above or below the setup candle which is just 1 pip off of the close[1] , i've tried so many different combinations of this and that

if someone could have a peek at the code i have and steer me in the right direction that would be greatly appreciated!

the two different stop loss variables im trying to implement are below, one for long positions one for short positions.

double SLShort = (iHigh(NULL,0,1)+10*Point);
   double SLLong = (iLow(NULL,0,1)-10*Point);

thanks everyone!


#property strict


// Input variables
input int MagicNumber = 101;
input int Slippage = 10;

input double LotSize = 0.1;
input int StopLoss = 50;
input int TakeProfit = 100;

input int MaPeriod = 20;
input ENUM_MA_METHOD MaMethod = MODE_EMA;
input ENUM_APPLIED_PRICE MaPrice = PRICE_CLOSE;

// Trailing stop 
input int TrailingStop = 150;
input int MinimumProfit = 100;
input int Step = 10;

// Global variables
int gBuyTicket, gSellTicket;


// OnTick() event handler
void OnTick()
{
   
   // Current order counts
   int buyCount = 0, sellCount = 0;
   
   for(int order = 0; order <= OrdersTotal() - 1; order++)
   {
      bool select = OrderSelect(order,SELECT_BY_POS);
      
      if(OrderMagicNumber() == MagicNumber && select == true)
      {
         if(OrderType() == OP_BUY) buyCount++;
         else if(OrderType() == OP_SELL) sellCount++;
      }   
   }
 // ==================================================================
   //Indicators
   //==================================================================
   
   double bbLowerEntry = iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_LOWER,0);
   double bbUpperEntry = iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_UPPER,0);
   double bbMid = iBands(NULL,0,20,2,0,PRICE_CLOSE,MODE_MAIN,0);
   double SLShort = (iHigh(NULL,0,1)+10*Point);
   double SLLong = (iLow(NULL,0,1)-10*Point);
   
   double close = Close[0];
   double close1 = Close[1];
   double open = Open[0];
  
   

   //===================================================
   // Buy order condition
   //===================================================

   if(close1 <=bbLowerEntry+10*Point && open >=bbLowerEntry-5*Point && open >=bbLowerEntry+5*Point && buyCount == 0 && gBuyTicket == 0)
   {
      // Close sell order
     /* for(int order = 0; order <= OrdersTotal() - 1; order++)
      {
         bool select = OrderSelect(order,SELECT_BY_POS);
         
         if(OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber && select == true)
         {
            // Close order
            bool closed = OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrRed);
            if(closed == true) order--;
         }
      }*/
      
      // Open buy order
      gBuyTicket = OrderSend(_Symbol,OP_BUY,LotSize,Ask,Slippage,0,0,"Buy order",MagicNumber,0,clrGreen);
      gSellTicket = 0;
      
      // Add stop loss & take profit to order
      if(gBuyTicket > 0 && (StopLoss > 0 || TakeProfit > 0))
      {
         bool select = OrderSelect(gBuyTicket,SELECT_BY_TICKET);
         
         // Calculate stop loss & take profit
         double stopLoss = 0, takeProfit = 0;
         if(StopLoss > 0) stopLoss = OrderOpenPrice() - (StopLoss * _Point);
         if(TakeProfit > 0) takeProfit = OrderOpenPrice() + (TakeProfit * _Point);
         
         // Verify stop loss & take profit
         double stopLevel = MarketInfo(_Symbol,MODE_STOPLEVEL) * _Point;
         // try to make stop level above wick of setup candle
         RefreshRates();
         double upperStopLevel = Ask + stopLevel;
         double lowerStopLevel = Bid - stopLevel;
         
         if(takeProfit <= upperStopLevel && takeProfit != 0) takeProfit = upperStopLevel + _Point;
         if(stopLoss >= lowerStopLevel && stopLoss  != 0) stopLoss = lowerStopLevel - _Point; 
         
         // Modify order
         bool modify = OrderModify(gBuyTicket,0,stopLoss,takeProfit,0);
      }
   }
   
   //====================================================
   // Short order condition
   //====================================================
   
  if(close1 >=bbUpperEntry-10*Point && open <=bbUpperEntry-5*Point && open <=bbUpperEntry+5*Point && sellCount == 0 && gSellTicket == 0)
   {
      // Close buy order
     /* for(int order = 0; order <= OrdersTotal() - 1; order++)
      {
         bool select = OrderSelect(order,SELECT_BY_POS);
         
         if(OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber && select == true)
         {
            // Close order
            bool closed = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrRed);
            
            if(closed == true)
            {
               gBuyTicket = 0;
               order--;
            }
         }
      }*/
      
      // Open sell order
      gSellTicket = OrderSend(_Symbol,OP_SELL,LotSize,Bid,Slippage,0,0,"Sell order",MagicNumber,0,clrRed);
      gBuyTicket = 0;
      
      // Add stop loss & take profit to order
      if(gSellTicket > 0 && (StopLoss > 0 || TakeProfit > 0))
      {
         bool select = OrderSelect(gSellTicket,SELECT_BY_TICKET);
         
         // Calculate stop loss & take profit
         double stopLoss = 0, takeProfit = 0;
         if(StopLoss > 0) stopLoss = OrderOpenPrice() + (StopLoss * _Point);
         if(TakeProfit > 0) takeProfit = OrderOpenPrice() - (TakeProfit * _Point);
         
         // Verify stop loss & take profit
         double stopLevel = MarketInfo(_Symbol,MODE_STOPLEVEL) * _Point;
         
         RefreshRates();
         double upperStopLevel = Ask + stopLevel;
         double lowerStopLevel = Bid - stopLevel;
         
         if(takeProfit >= lowerStopLevel && takeProfit != 0) takeProfit = lowerStopLevel - _Point;
         if(stopLoss <= upperStopLevel && stopLoss != 0) stopLoss = upperStopLevel + _Point; 
         
         // Modify order
         bool modify = OrderModify(gSellTicket,0,stopLoss,takeProfit,0);
      }
   }
   
   
   // Trailing stop (Chapter 20)
   for(int order = 0; order <= OrdersTotal() - 1; order++)
   {
      bool select = OrderSelect(order,SELECT_BY_POS);
      
      if(TrailingStop > 0 && OrderMagicNumber() == MagicNumber && select == true)
      {
         RefreshRates();
         
         // Check buy order trailing stops
         if(OrderType() == OP_BUY)
         {
            double trailStopPrice = Bid - (TrailingStop * _Point);
            trailStopPrice = NormalizeDouble(trailStopPrice,_Digits);
            
            double currentStopLoss = NormalizeDouble(OrderStopLoss(),_Digits);
            double currentProfit = Bid - OrderOpenPrice();
            double minProfit = MinimumProfit * _Point;
            
            int getStep = 0;
            if(Step < 10) getStep = 10;
                 double step = getStep * _Point;
            
            if(trailStopPrice > currentStopLoss + step && currentProfit >= minProfit)
            {
               bool modify = OrderModify(OrderTicket(),OrderOpenPrice(),trailStopPrice,OrderTakeProfit(),0);
            }
         }
         
         // Check sell order trailing stops
         else if(OrderType() == OP_SELL && TrailingStop > 0)
         {
            double trailStopPrice = Ask + (TrailingStop * _Point);
            trailStopPrice = NormalizeDouble(trailStopPrice,_Digits);
            
            double currentStopLoss = NormalizeDouble(OrderStopLoss(),_Digits);
            double currentProfit = OrderOpenPrice() - Ask;
            double minProfit = MinimumProfit * _Point;
            
            int getStep = 0;
            if(Step < 10) getStep = 10;
                 double step = getStep * _Point;
            
            if((trailStopPrice < currentStopLoss - step || currentStopLoss == 0) && currentProfit >= minProfit)
            {
               bool modify = OrderModify(OrderTicket(),OrderOpenPrice(),trailStopPrice,OrderTakeProfit(),0);
            }
         }
      }
   }
   
}
Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
Before you proceed to study the trade functions of the platform, you must have a clear understanding of the basic terms: order, deal and position...