Download MetaTrader 5

Need help with Last Trade Open Price.

To add comments, please log in or register
Publish your articles and improve your rating!
ForexTrader17
1041
ForexTrader17 2016.05.04 17:23 

This is my function for the last trade opening price.

double LastTradePrice()

  {

   double result = 0;

   for(int i = OrdersTotal()-1; i >= 0; i--)

     {

      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;

      if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && (OrderType() == OP_BUY || OrderType() == OP_SELL))

        {

         result = OrderOpenPrice();

         break;

        }

     } 

   return(result);

  } 

 

Now I would like to compare it to the current price. I wrote this.

 

  Bid > LastTradePrice() +/- 100 * myPoint //Price > Last Open Trade Price + fixed value

 

How would I make sure this is a positive number always as the last trade could have opened on either side of the bid. I just need to make sure that number will always be positive. 

Thank you. 

Daniela Bluemel
586
Daniela Bluemel 2016.05.04 19:32  

You can check after that if the value is positive and only if positive go on with your code

Or use MathAbs 

ForexTrader17
1041
ForexTrader17 2016.05.04 19:57  

So is this correct? Will this work whether last open trade price was above or below the current bid? This will return a positive number?

 MathAbs( Bid - LastTradePrice() ) < 50 * myPoint

Thanks. 

Daniela Bluemel
586
Daniela Bluemel 2016.05.04 20:36  

It returns always a value >= 0

So if your value is -5.21 it returns 5.21, if your value is 5.21 it returns 5.21 

ForexTrader17
1041
ForexTrader17 2016.05.04 21:07  

Thanks. So will the above format work as I want it to?

Thanks. 

Daniela Bluemel
586
Daniela Bluemel 2016.05.04 21:14  
I don't know what you want ;)
Marco vd Heijden
Moderator
4681
Marco vd Heijden 2016.05.04 21:21  
How about you try it.
Osama Shaban
1444
Osama Shaban 2016.05.04 23:09  

It seems to me you are doing Grid or Martingale code ...

In your case, you must know that whenever price above last opened order by a certain number of pips (PipStep) , you have to open Sell order

and when price is down the last opened trade with a certain number of pips you have to open Buy order ... right? 

 Then, if you used the proper formula, you will never get negative numbers ...

Please check my own code I used to use ...

//In the start() function ...
      if(BuyOrders < MaxTradesPerPair && Bid <= (LastOpenBuyPrice(Symbol())-MathPow(PipStepExponent,NumberOfOpenBuyOrders())*PipStep*Point)){
         NewLots = NormalizeDouble(LastLotsUP(Symbol())*LotExponent,LotDigits);
         if(NewLots == LastLotsUP(Symbol())) NewLots = NormalizeDouble((NewLots+Lots),LotDigits);
         if(AllowBuyTrades) {OpenBuy(NewLots, MagicUP, TakeProfit, StopLoss, Expert_Name); return(0);}
      }      

                                                                            
      if(SellOrders < MaxTradesPerPair && Ask >= (LastOpenSellPrice(Symbol())+MathPow(PipStepExponent,NumberOfOpenSellOrders())*PipStep*Point)){
         NewLots = NormalizeDouble(LastLotsDN(Symbol())*LotExponent,LotDigits);
         if(NewLots == LastLotsDN(Symbol())) NewLots = NormalizeDouble((NewLots+Lots),LotDigits);
         if(AllowSellTrades) {OpenSell(NewLots, MagicDN, TakeProfit, StopLoss, Expert_Name); return(0);}
      }
   }  

...
...
...
// Out of the start() function ...

double LastOpenBuyPrice(string smbl){ 
   int cnt, total; 
   total = OrdersTotal(); 
   double oprice=0;
   
   for(cnt=0; cnt<total; cnt++){ 
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
   
      if(OrderSymbol()==smbl && OrderType()==OP_BUY && OrderMagicNumber()==MagicUP){
         oprice = OrderOpenPrice();      
      }
   }
   return(oprice);
}


double LastOpenSellPrice(string smbl){ 
   int cnt, total; 
   total = OrdersTotal(); 
   double oprice=0;
   
   for(cnt=0; cnt<total; cnt++){ 
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
   
      if(OrderSymbol()==smbl && OrderType()==OP_SELL && OrderMagicNumber()==MagicDN){
         oprice = OrderOpenPrice();      
      }
   }
   return(oprice);
}

 Note that I use separate functions for both Buy and Sell orders logic.

This makes things easier, I guess ... 

ForexTrader17
1041
ForexTrader17 2016.05.05 03:22  
Thanks. I am spacing trades out but not a grid strategy.
To add comments, please log in or register