Comparing between Loss and given Loss amount.

 

Good day.

I just would like to ask if the formula that I've written below can close a trade with a given example loss of -15.00.


   for(ctr=0;ctr<OrdersTotal();ctr++)
   {
      if(OrderSelect(ctr,SELECT_BY_POS)==true)
      {
         if(OrderProfit()-(-15.00)>Point/2.00)
         {
            if(OrderType()==0)
            {
               OrderClose(OrderTicket(),OrderLots(),Ask,0,CLR_NONE); 
            }
            else if(OrderType()==1)
            {
               OrderClose(OrderTicket(),OrderLots(),Bid,0,CLR_NONE); 
            }   
         }
      }
   }

The logic is that if the open trades reached a loss of -15.00, it will close the existing trade.

Thank you very much...

 
raven_chrono:

Good day.

I just would like to ask if the formula that I've written below can close a trade with a given example loss of -15.00.


The logic is that if the open trades reached a loss of -15.00, it will close the existing trade.

Thank you very much...

Don't count up, if you do you will miss trades, this explains why: Loops and Closing or Deleting Orders

Don't use 0 and 1 keep your code readable by using OP_BUY and OP_SELL, an OP_BUY is closed with a sell and a sell happens at Bid not Ask visa versa bor a OP_SELL . . .

You don't need to distinguish between Buy and Sell trades, instead of Bid and Ask use OrderClosePrice()

 
RaptorUK:

Don't count up, if you do you will miss trades, this explains why: Loops and Closing or Deleting Orders

Don't use 0 and 1 keep your code readable by using OP_BUY and OP_SELL, an OP_BUY is closed with a sell and a sell happens at Bid not Ask visa versa bor a OP_SELL . . .

You don't need to distinguish between Buy and Sell trades, instead of Bid and Ask use OrderClosePrice()


Thank you for the comment and for the insights... and i have seen your point. i should have use decrementing loop rather than incrementing loop. But the question remains if the code that i have posted above regarding the LOSS is appropriate or accurate...

Thanks...

 
raven_chrono:


Thank you for the comment and for the insights... and i have seen your point. i should have use decrementing loop rather than incrementing loop. But the question remains if the code that i have posted above regarding the LOSS is appropriate or accurate...

Yes I think it is. What about OrderCommission() would you count that as part of the profit/loss?
 
RaptorUK:
Yes I think it is. What about OrderCommission() would you count that as part of the profit/loss?


Thanks for the help and i will look through that for OrderCommission...
 
RaptorUK:
Yes I think it is. What about OrderCommission() would you count that as part of the profit/loss?

Hey, Raptor

I'm not quite sure about the OP's conditional logic. I don't really understand what dividing Point by 2 is going to accomplish. It would seem to me that it would be cutting the close threshold in half. The OrderCommission is something I hadn't thought about. From watching the charts and looking at the Profit column in the trade terminal I've only considered profit and loss in terms of the value in the Profit column, DUH! Of course you need to consider the trade cost in your profit and loss considerations. Yep, I've been a dummy. :-)

However, if you're looking strictly at the number of pips your trade is running in the red, wouldn't the conditional be more on target if it was coded like this:

If (MathAbs(OrderOpenPrice() - OrderClosePrice() >= dblPipSize*15)

Provided, of course, that the pip size(in decimal format) was already determined according to the currency pair?

If I'm all wet here I know you'll feel free to bop me on the head and say so. Bop away - I'm learning, too, so if I'm completely off base I'll be appreciative of being pointed in the right direction. BTW, thanks for the link to the material you posted about closing and deleting orders. At first, it didn't really make sense judging by what I know of C#, C++, etc. programming but on reflection it became apparent to me that you're not iterating through a standard array in the manner that I'm used to. It's not an array that you've created, it's an array managed by MT4 and its size changes with each insertion OR deletion. Correct?

 
raven_chrono:

Good day.

I just would like to ask if the formula that I've written below can close a trade with a given example loss of -15.00.


The logic is that if the open trades reached a loss of -15.00, it will close the existing trade.

Thank you very much...


It is not only problem of counting down and check error returns

it is also not selecting on symbol

if you close at Bid or Ask it will be the Bid or Ask price of the symbol your chart has

while the selected trade can be another symbol....

and it also not selecting magicnumber

so it will close all trades when the condition has come true

running on an account it can mess other strategies from other EA's

Profit of a trade is

OrderProfit() + OrderCommission() + OrderSwap()

and why not do..... try out

if(Profit <= -15.00)OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,CLR_NONE);

 
ProfessorMetal:

Hey, Raptor

I'm not quite sure about the OP's conditional logic. I don't really understand what dividing Point by 2 is going to accomplish. It would seem to me that it would be cutting the close threshold in half.

It's to handle double comparison issues I think, read this: Can price != price ?
 
string   PriceToStr(double p){   return( DoubleToStr(p, Digits) );            }

         //{Print(PriceToStr(Bid),"<",PriceToStr(ca.below),"=",Bid<ca.below,
         // Printed: 1.25741<1.25741=1
         //}"<" returned true but Prices were equal. Thus I need the Point_2

The == operand. - MQL4 forum

The smallest possible change in value is a point. So any difference less than point/2 are the same value. Any difference greater are different values.