Just started learning how to program, very easy question - page 2

 
OK, it's time for you to get to grips with doubles and their precision . . . have a read of this thread, carefully: https://www.mql5.com/en/forum/136997
 

Something more to trie did an update of my previous post while you were answering

If it all fails and it still not printing 123 Then you have to show more of your code

and take a notice to the link in the post of RaptorUK

 
RaptorUK:
OK, it's time for you to get to grips with doubles and their precision . . . have a read of this thread, carefully: https://www.mql5.com/en/forum/136997


Raptor i think the error is not there, the IF doesn't work even if i don't use NormalizeDouble and i used >= too so it's not a matter of precision, the ASK price obviously surpassed the OrderTakeProfit()-Value1*4 price

still doesn't print 123

 

here's more of my code:

double LotSizeBuy(){
DollarRisk = AccountBalance() * risk * 0.01;
Value1=Ask-StopLossBuy();
Lot1= (DollarRisk/(Value1*10000))/10;
return(Lot1);

}



double StopLossBuy(){
return(Low[iLowest(NULL,0,1,10,0)]-10*Point);
}



void buy(){
            ticket=OrderSend(Symbol(),OP_BUY,LotSizeBuy(),Ask,0,0,0,"ciao",123581321,0,Blue);
            OrderModify(ticket,Ask,StopLossBuy(),Ask+Value1*5,0,Blue);
         //   if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)==true){
            Print("blabla");
            if(Ask>=NormalizeDouble(OrderTakeProfit()-Value1*4,5)){Print("123"); OrderModify(ticket,Ask,StopLossBuy()+Value1,Ask+Value1*4,0,Blue);}
            if(NormalizeDouble(Ask,4)==NormalizeDouble(OrderTakeProfit()-Value1*3,4)){ OrderModify(ticket,Ask,StopLossBuy()+Value1*2,Ask+Value1*3,0,Blue);}
            if(NormalizeDouble(Ask,4)==NormalizeDouble(OrderTakeProfit()-Value1*2,4)){ OrderModify(ticket,Ask,StopLossBuy()+Value1*3,Ask+Value1*2,0,Blue); }
            if(NormalizeDouble(Ask,4)==NormalizeDouble(OrderTakeProfit()-Value1,4)){ OrderModify(ticket,Ask,StopLossBuy()+Value1*4,Ask+Value1*1,0,Blue); }
        //    }
            
            
            if(ticket>0){
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            Print("Buy order opened", OrderOpenPrice());
            }
            else
            Print("Error", GetLastError());
            return(0);   
                        

}
 
FLCL:

here's more of my code: [...]

If you don't do an OrderSelect(), then the value of OrderTakeProfit() is unpredictable. It may return 0, or it may return the t/p on a different order to the one you intend. Calling OrderSend() does not automatically select the order which gets created.

However, the bit which remains puzzling is that if OrderTakeProfit() is zero because no order has been selected, then the condition should be true; it should evaluate to "is Ask larger than a negative number ?"
 
jjc:
If you don't do an OrderSelect(), then the value of OrderTakeProfit() is unpredictable. It may return 0, or it may return the t/p on a different order to the one you intend. Calling OrderSend() does not automatically select the order which has been created.

However, the bit which remains puzzling is that if OrderTakeProfit() is zero because no order has been selected, then the condition should be true; it should evaluate to "is Ask larger than a negative number ?"

Well it also wouldn't explain why the comment on page 1 shows the correct value( the one i wanted) plus as you said the condition should be true in either case
 
    ticket=OrderSend(Symbol(),OP_BUY,LotSizeBuy(),Ask,0,0,0,"ciao",123581321,0,Blue);
    OrderModify(ticket,Ask,StopLossBuy(),Ask+Value1*5,0,Blue);
//   if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)==true){
        Print("blabla");
        if(Ask>=NormalizeDouble(OrderTakeProfit()-Value1
  1. You CAN NOT use OrderTakeProfit() before using OrderSelect().
  2. Always test return codes
        int ticket = OrderSend(..., 0,0,...)
        if (ticket < 0)
           Alert("OrderSend failed: ", GetLastError());
        else if (!OrderSelect(ticket, SELECT_BY_TICKET))
           Alert("OrderSelect failed: ", GetLastError());
        else if (!OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0)
           Alert("OrderModify failed: ", GetLastError());
    

  3. If you use Ask (unless it happens to be exactly the OrderOpenPrice()) then the modify will always fail (you can't modify the open price of a opened order.) You can NOT use previous Ask because of slippage and can't use the current Ask because 1) irrelevant, 2) it's out of date (no RefreshRate() after server call)

  4. Don't use NormalizeDouble, ever. Don't compare doubles with equality. See the link Raptor mentioned.
 
FLCL:

Well it also wouldn't explain why the comment on page 1 shows the correct value( the one i wanted) plus as you said the condition should be true in either case

The code is more than a little opaque, but what seems to be happening is as follows:

* Value1 holds the distance from the entry price to the (prospective) stoploss, e.g. 0.0020

* Immediately after placing an order you set the s/l and t/p. Your screenshot shows that this is working; the s/l line is visible.

* The t/p is set to 5 times the s/l (and is therefore off the top of the screenshot).

* Immediately after placing the order, the line of code in question is saying "is the ask price less than 4 times the s/l distance from the t/p?"

* I can't see how this can be true immediately after order placement. By definition, the ask price is 5 times the s/l away from the t/p

* These conditions apparently only get called immediately after order placement, in the buy() function. They are apparently not called on subsequent ticks. Therefore, the condition is going to be evaluated once only, at a time when it can't be true.

 
jjc:

The code is more than a little opaque, but what seems to be happening is as follows:

* Value1 holds the distance from the entry price to the (prospective) stoploss, e.g. 0.0020

* Immediately after placing an order you set the s/l and t/p. Your screenshot shows that this is working; the s/l line is visible.

* The t/p is set to 5 times the s/l (and is therefore off the top of the screenshot).

* Immediately after placing the order, the line of code in question is saying "is the ask price less than 4 times the s/l distance from the t/p?"

* I can't see how this can be true immediately after order placement. By definition, the ask price is 5 times the s/l away from the t/p

* These conditions apparently only get called immediately after order placement, in the buy() function. They are apparently not called on subsequent ticks. Therefore, the condition is going to be evaluated once only, at a time when it can't be true.


This is exactly the problem.

Thank you guys for your time.

My brain hurts too much for today, i can't seem to trail my stops as i want, but when i'll be feeling better i'll try to fix it.

Reason: