StopLoss Condition based on Previous Bar

 

 I am new to coding but I am learning about it. 

I have been trying to set my stoploss to 3 different condition as follow ( Open_Buy Case):

1. My Stoploss = 50 pips if the previous close is not greater than 50 pips compare to previous bar open .

2. My Stoploss = previous bar open if the different between previous close and open is greater than 50pips and smaller than 150pips.

3. My Stoploss = 150 pips if the different between previous bar close and open is greater than 150 pips.

 I have put the stoploss part only to ask for help.

 I have tried for a week but i cant get it write.

here some code I have tried.

 Please give me some advice.

#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict



extern double Lots = 1;
extern double TakeProfit = 500;
extern double MinStopLoss = 50;
extern double MaxStopLoss = 150;
extern bool EnterOpenBar = true;
extern bool OppositeClose = true;
extern double MagicNumber = 59483;
extern int Slippage = 10;

//Global Variables
int Counter, vSlippage;
double ticket, number, vPoint;


  
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      
     
      int OType=-1;
      int Ticket;
      double Price, MinSL, TP, Lot, LOpen, LClose,BarUp,MaxSL,SLMode1,SLMode2;
      
      for(int Counter=1; Counter<=OrdersTotal(); Counter++)
      {
      if (OrderSelect(Counter-1,SELECT_BY_POS)==true)

      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

      {
      Ticket=OrderTicket();
      OType =OrderType();
      Price =OrderOpenPrice();
      MinSL =OrderStopLoss();
      MaxSL = MaxStopLoss;
      TP =OrderTakeProfit();
      Lot =OrderLots();
      LOpen = iOpen(NULL,0,1);
      LClose = iClose(NULL,0,1);
      BarUp = LClose-LClose;
      SLMode1 = Bid -LOpen*Point;
      SLMode2 = Bid -MaxSL*Point;
           
   }
   }
     
        
    while(true)

{
if (OrdersTotalMagicOpen()==0 && OpenBuy==true && (BarUp*Point)< MinSL)
{

if(MinStopLoss>0)  
{MinSL=Bid - MinStopLoss*Point;}
ticket=0;number=0;
while(ticket<=0 && number<100)
{
RefreshRates();
ticket = OrderSend(Symbol(),OP_BUY,NormalizeDouble(Lots), Ask,vSlippage,MinSL,TP, MagicNumber, 0, Green);
}

}
else if (OrdersTotalMagicOpen()==0 && OpenBuy==true && BarUp > MinSL)

{
  if(MinStopLoss>0)  
{MinSL=Bid - MinStopLoss*Point;}
ticket=0;number=0;
while(ticket<=0 && number<100)
{
RefreshRates();
ticket = OrderSend(Symbol(),OP_BUY,Lots, Ask,vSlippage,LOpen,TP,MagicNumber, 0, Green);
}

}

if (OrdersTotalMagicOpen()==0 && OpenSell==true)
{
if(MinStopLoss>0){MinSL=Ask + MinStopLoss*Point;}else{MinSL=0;} if(TakeProfit>0){TP=Ask-TakeProfit*vPoint;}else{TP=0;}
ticket=0;number=0;
while(ticket<=0 && number<100){
RefreshRates();
ticket= OrderSend(Symbol(),OP_SELL, Lots, Bid,vSlippage,MinSL,TP, MagicNumber, 0, Red);
}
       
} 
    
} 
 
Sokphal: I have been trying to set my stoploss to 3 different condition as follow ...
  1. You have? Where is your code that looks at the previous candle and sets the SL.
  2. learn to code it, or pay someone. We're not going to code it FOR you.
    We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.
 
WHRoeder:
Sokphal: I have been trying to set my stoploss to 3 different condition as follow ...
  1. You have? Where is your code that looks at the previous candle and sets the SL.
  2. learn to code it, or pay someone. We're not going to code it FOR you.
    We are willing to HELP you when you post your attempt (using SRC)and the nature of your problem.

 I just want to know what function, operator, or any related needed codes for this kind of stop loss condition.

you just give me some advice on those thing. I don't need you to write it for me. 

 
Sokphal:

 I just want to know what function, operator, or any related needed codes for this kind of stop loss condition.

you just give me some advice on those thing. I don't need you to write it for me. 

You will need most of the functions from this section of the documentation: "Trade Functions" 

Read that. Most of the functions come with sample code. 

Also, read the book: "Mql4 Book"  

 
Sokphal:

I just want to know what function, operator, or any related needed codes for this kind of stop loss condition.

you just give me some advice on those thing. I don't need you to write it for me. 

learn to code it, or pay someone. We're not going to code it FOR you.
We are willing to HELP you when you post your attempt (using SRC) and the nature of your problem.

Sokphal:

1. My Stoploss = 50 pips if the previous close is not greater than 50 pips compare to previous bar open .

2. My Stoploss = previous bar open if the different between previous close and open is greater than 50pips and smaller than 150pips.

3. My Stoploss = 150 pips if the different between previous bar close and open is greater than 150 pips.

  1. Multiplication (pips to points and points to a change in price.) Addition change to a price.
  2. Conditional Operator if-else - Operators - Language Basics - MQL4 Reference
  3. Timeseries and Indicators Access - MQL4 Reference





 
drazen64:

You will need most of the functions from this section of the documentation: "Trade Functions" 

Read that. Most of the functions come with sample code. 

Also, read the book: "Mql4 Book"  

Thank you, I just did yesterday. now moved to another step 
 
WHRoeder:
learn to code it, or pay someone. We're not going to code it FOR you.
We are willing to HELP you when you post your attempt (using SRC)and the nature of your problem.

Sokphal:

1. My Stoploss = 50 pips if the previous close is not greater than 50 pips compare to previous bar open .

2. My Stoploss = previous bar open if the different between previous close and open is greater than 50pips and smaller than 150pips.

3. My Stoploss = 150 pips if the different between previous bar close and open is greater than 150 pips.

  1. Multiplication (pips to points and points to a change in price.) Addition change to a price.
  2. Conditional Operator if-else - Operators - Language Basics - MQL4 Reference
  3. Timeseries and Indicators Access - MQL4 Reference





Thank you.