Invalid Price using Stoplevels & FreezeLevels

 

Hi All 

I want to use trail my buy stop and sell stop orders but I encounter invalid price error when using the code below.

input double TakeProfit=5;
input double StopLoss=2;
input double PendingStopTrail=2;

int MagicNumber;
double Filter=0;
int Direction=0;
int StopLevel=(int)SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);
int FreezeLevel=(int)SymbolInfoInteger(Symbol(),SYMBOL_TRADE_FREEZE_LEVEL);
double Ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK); 
double Bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
 {
  ResetLastError();
  
  if(!m_symbol.Name(Symbol()))
  {
   return(INIT_FAILED);
  }
   
  trade.SetExpertMagicNumber(MagicNumber);
  trade.SetMarginMode();
  trade.SetTypeFillingBySymbol(m_symbol.Name());
  
  MagicNumber=(StringGetCharacter(Symbol(),0)*1
             +StringGetCharacter(Symbol(),2)*3
             +StringGetCharacter(Symbol(),4)*5
             +StringGetCharacter(Symbol(),6)*7
             +StringGetCharacter(Symbol(),8)*9
             +StringGetCharacter(Symbol(),10)*11
             +StringGetCharacter(Symbol(),12)*13
             +StringGetCharacter(Symbol(),14)*15
             +StringGetCharacter(Symbol(),16)*17);
             
  TesterHideIndicators(true);
  
  return(INIT_SUCCEEDED);
 }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int CountPending()
 {
  int Pending=0;
  for(int b=OrdersTotal()-1;b>=0;b--)
  {
   bool Select=OrderSelect(OrderGetTicket(b));
   string OrderSymbol=OrderGetString(ORDER_SYMBOL);
   if(OrderSymbol==Symbol())
   {
    ENUM_ORDER_TYPE Type=(ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE);
    if(Type==ORDER_TYPE_BUY_STOP||Type==ORDER_TYPE_SELL_STOP)
    Pending++;
   }
  }
  return(Pending);
 }
 
double Pips()
 {
  double PipPoint=0;
  int Digit=(int)SymbolInfoInteger(Symbol(),SYMBOL_DIGITS);
  if(Digit==2||Digit==3||Digit==5){PipPoint=Point()*10;}
  return(PipPoint);
 }
 
bool OrderLevels(double PriceStop,ENUM_ORDER_TYPE Type)
 {
  bool Levels=true;
  
  switch(Type)
  {
   case ORDER_TYPE_BUY_STOP:
   Levels=PriceStop-Bid>StopLevel*Pips();
   Levels=PriceStop-Bid>FreezeLevel*Pips();
   
   case ORDER_TYPE_SELL_STOP: 
   Levels=Bid-PriceStop>StopLevel*Pips();
   Levels=Bid-PriceStop>FreezeLevel*Pips();
  }
  
  return(Levels);
 } 
 
void BuyPending()
 {
  double TPBuy=Bid+TakeProfit*Pips();
  double SLBuy=Bid-StopLoss*Pips();
  
  double BuyPrice=Bid+PendingStopTrail*Pips();
  
  Filter=3*Pips();
  double open=iOpen(Symbol(),0,0);
  double Range=MathAbs(open-Bid);
  
  MqlRates Candle[];
  ArraySetAsSeries(Candle,true);
  int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);
  
  if(Candle[0].close>Candle[0].open)
  {
   if(Direction!=1)
   {
    Direction=1;
    trade.BuyStop(0.01,OrderLevels(BuyPrice,ORDER_TYPE_BUY_STOP),Symbol(),SLBuy,0,0,0,"Test");
   } 
  }
  
 }  
 
void SellPending()
 {
  double TPSell=Bid-TakeProfit*Pips();
  double SLSell=Bid+StopLoss*Pips();
  
  double SellPrice=Bid-PendingStopTrail*Pips();
  
  Filter=3*Pips();
  double open=iOpen(Symbol(),0,0);
  double Range=MathAbs(open-Bid);
  
  MqlRates Candle[];
  ArraySetAsSeries(Candle,true);
  int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);
  
  if(Candle[0].close<Candle[0].open)
  {
   if(Direction!=-1)
   {
    Direction=-1;
    trade.SellStop(0.01,OrderLevels(SellPrice,ORDER_TYPE_SELL_STOP),Symbol(),SLSell,0,0,0,"Test");
   } 
  }
  
 } 
 
void PriceTrail()
 {
  double MinStepToModify=PendingStopTrail;
  double Step=PendingStopTrail;
  double MinModify=MinStepToModify*PendingStopTrail*Pips();
  
  double BuyPrice=Bid+Step*Pips();
  double SellPrice=Bid-Step*Pips();
 
  double BuyTP=Bid+Step*Pips();
  double SellTP=Bid-Step*Pips();
  
  double BuySL=Bid-Step*Pips();
  double SellSL=Bid+Step*Pips();
  
  for(int k=OrdersTotal()-1;k>=0;k--)
  {
   if(m_order.SelectByIndex(k)) 
   if(m_order.Symbol()==Symbol())
   if(m_order.OrderType()==ORDER_TYPE_BUY_STOP)
   if(BuyPrice)
   if(m_order.PriceOpen()-Bid>Step*Pips()+MinModify)
   trade.OrderModify(m_order.Ticket(),OrderLevels(BuyPrice,ORDER_TYPE_BUY_STOP),BuySL,m_order.TakeProfit(),0,0,0);
  }
  
  for(int l=OrdersTotal()-1;l>=0;l--)
  {
   if(m_order.SelectByIndex(l))
   if(m_order.Symbol()==Symbol())
   if(m_order.OrderType()==ORDER_TYPE_SELL_STOP)
   if(SellPrice)
   if(Ask-m_order.PriceOpen()>Step*Pips()+MinModify)
   trade.OrderModify(m_order.Ticket(),OrderLevels(SellPrice,ORDER_TYPE_SELL_STOP),SellSL,m_order.TakeProfit(),0,0,0);
  }
  
 }      

void OnTick()
 {
  trade.SetExpertMagicNumber(MagicNumber);
  
  if(CountPending()<1){BuyPending();}
  
  if(CountPending()<1){SellPending();}
  
  PriceTrail();
   
 }

Plus I get failed to modify error for no changes 

 
Scalper8:

Hi All 

I want to use trail my buy stop and sell stop orders but I encounter invalid price error when using the code below.

Plus I get failed to modify error for no changes 

Why is it that when I've checked my freeze levels no orders don't open?

bool CheckFreeze(ulong Ticket)
 
 bool Freeze=false;
  double PriceOpen=OrderGetDouble(ORDER_PRICE_OPEN);
  ENUM_ORDER_TYPE Type=(ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE);
  
  switch(Type)
  {
   case ORDER_TYPE_BUY_STOP:Freeze=PriceOpen-Ask>FreezeLevel*Pips();break;
   
   case ORDER_TYPE_SELL_STOP:Freeze=Bid-PriceOpen>FreezeLevel*Pips();break;
  }
  
  return(false);
 } 
void OnTick()

 {
  trade.SetExpertMagicNumber(MagicNumber);

  if(CheckFreeze(m_order.Ticket()))

  if(CountPending()<1){BuyPending();}

  if(CheckFreeze(m_order.Ticket()))

  if(CountPending()<1){SellPending();}

 }
 
Scalper8 #:

Why is it that when I've checked my freeze levels no orders don't open?

Tried a different approach but I still get invalid price:

void BuyPending()
 {
  double TPBuy=Bid+TakeProfit*Pips();
  double SLBuy=Bid-StopLoss*Pips();

  double PriceOpen=OrderGetDouble(ORDER_PRICE_OPEN);
  
  double BuyPrice=Bid+PendingStopTrail*Pips();
  
  Filter=3*Pips();
  double open=iOpen(Symbol(),0,0);
  double Range=MathAbs(open-Bid);
  
  MqlRates Candle[];
  ArraySetAsSeries(Candle,true);
  int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);

  if(Candle[0].close>Candle[0].open)
  {
   if(Direction!=1)
   {
    Direction=1;
    if(PriceOpen+Ask>StopLevel||PriceOpen+Ask>FreezeLevel||Bid-StopLoss>StopLevel*Pips()||TakeProfit-Bid>StopLevel*Pips())
     {
     trade.BuyStop(0.01,BuyPrice,Symbol(),0,0,0,0,"Test");
    }
   } 
  }
  
 }

void SellPending()
 {
  double TPSell=Bid-TakeProfit*Pips();
  double SLSell=Bid+StopLoss*Pips();

  double PriceOpen=OrderGetDouble(ORDER_PRICE_OPEN);
  
  double SellPrice=Bid-PendingStopTrail*Pips();
  
  Filter=3*Pips();
  double open=iOpen(Symbol(),0,0);
  double Range=MathAbs(open-Bid);
  
  MqlRates Candle[];
  ArraySetAsSeries(Candle,true);
  int rates=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),Candle);
  
  if(Candle[0].close<Candle[0].open)
  {
   if(Direction!=-1)
   {
    Direction=-1;
    if(Bid-PriceOpen>StopLevel||Bid-PriceOpen>FreezeLevel||StopLoss-Ask>StopLevel*Pips()||Ask-TakeProfit>StopLevel*Pips())
    {
     trade.SellStop(0.01,SellPrice,Symbol(),0,0,0,0,"Test");
    }
   } 
  }
  
 } 
 
Scalper8:

Hi All 

I want to use trail my buy stop and sell stop orders but I encounter invalid price error when using the code below.

Plus I get failed to modify error for no changes 

i see you have used bid price when I would think you should be using Ask. but i only took quick look. i could be wrong.

 
Michael Charles Schefe #:

i see you have used bid price when I would think you should be using Ask. but i only took quick look. i could be wrong.

I just tested it out and I still get invalid prices error

 
Maybe your SL or TP is too close to the price. The broker is usually not accepting tight SLs and TPs. Try larger values, or a broker that accept scalping strategies.
 
Frederic Metraux #:
Maybe your SL or TP is too close to the price. The broker is usually not accepting tight SLs and TPs. Try larger values, or a broker that accept scalping strategies.

before posting that I need assistance with the invalid price error I disabled my TP'S and SL's but it's the still the same outcome, I will try a different broker 

 
input double TakeProfit=5;
input double StopLoss=2;
input double PendingStopTrail=2;
The values are too small in all cases. As soon as the trade is open, it will close instantly. And for the pending trade, it will not open. I think you have seen it already ;)
 
Frederic Metraux #:
The values are too small in all cases. As soon as the trade is open, it will close instantly. And for the pending trade, it will not open. I think you have seen it already ;)

You actually right I've noticed a difference no more invalid prices, I just wanted my EA to use a stop loss of 2 pips I'll change it though

Thanks for the help  :) 

 
Frederic Metraux #:
The values are too small in all cases. As soon as the trade is open, it will close instantly. And for the pending trade, it will not open. I think you have seen it already ;)

I've currently using MqlTick function for ask and bid I don't encounter invalid stops anymore even if I use the values that are too close to the price.

input double TakeProfit=5;
input double StopLoss=2;
input double PendingStopTrail=2;
MqlTick Tick;
SymbolInfoTick(Symbol(),Tick);
  
double NewAsk=Tick.ask;
double NewBid=Tick.bid;
double NewBuyTP=NewAsk+TakeProfit*Pips();
double NewBuySL=NewAsk-StopLoss*Pips();

double NewAsk=Tick.ask;
double NewBid=Tick.bid;
double NewSellTP=NewBid-TakeProfit*Pips();
double NewSellSL=NewBid+StopLoss*Pips();