PSAR to closing trades - page 2

 
Luandre Ezra:

Yes it is.

Then check that it actually contains the correct ticket #

 

Keith Watford
:

Then check that it actually contains the correct ticket #

I think the problem is in the incorrect ticket. Here's the log from the journal (partially).

2020.06.25 21:33:59.339 2020.06.16 19:20:00  ScalpingStrategy NZDUSD-e,M5: open #1 buy 0.02 NZDUSD-e at 0.64509 sl: 0.64409 ok

2020.06.25 21:33:59.339 2020.06.16 19:20:00  ScalpingStrategy NZDUSD-e,M5: Order ticket: 18

2020.06.25 21:33:59.412 2020.06.16 21:23:13  Tester: stop loss #1 at 0.64409 (0.64409 / 0.64423)

2020.06.25 21:33:59.532 2020.06.16 23:00:00  ScalpingStrategy NZDUSD-e,M5: open #2 buy 0.02 NZDUSD-e at 0.64523 sl: 0.64423 ok

2020.06.25 21:33:59.532 2020.06.16 23:00:00  ScalpingStrategy NZDUSD-e,M5: Order ticket: 1

2020.06.25 21:33:59.551 2020.06.17 00:08:20  Tester: stop loss #2 at 0.64423 (0.64408 / 0.64422)

2020.06.25 21:33:59.585 2020.06.17 03:20:00  ScalpingStrategy NZDUSD-e,M5: open #3 sell 0.02 NZDUSD-e at 0.64404 sl: 0.64504 ok

2020.06.25 21:33:59.585 2020.06.17 03:20:00  ScalpingStrategy NZDUSD-e,M5: Order ticket: 2

2020.06.25 21:33:59.585 2020.06.17 03:20:01  ScalpingStrategy NZDUSD-e,M5: close #3 sell 0.02 NZDUSD-e at 0.64404 sl: 0.64504 at price 0.64419

2020.06.25 21:33:59.585 2020.06.17 03:20:01  ScalpingStrategy NZDUSD-e,M5: Order closed successfully for ticket: 3

2020.06.25 21:33:59.639 2020.06.17 04:00:00  ScalpingStrategy NZDUSD-e,M5: open #4 sell 0.02 NZDUSD-e at 0.64477 sl: 0.64577 ok

2020.06.25 21:33:59.639 2020.06.17 04:00:00  ScalpingStrategy NZDUSD-e,M5: Order ticket: 3

2020.06.25 21:33:59.639 2020.06.17 04:00:02  ScalpingStrategy NZDUSD-e,M5: close #4 sell 0.02 NZDUSD-e at 0.64477 sl: 0.64577 at price 0.64492

2020.06.25 21:33:59.639 2020.06.17 04:00:02  ScalpingStrategy NZDUSD-e,M5: Order closed successfully for ticket: 4

2020.06.25 21:33:59.640 2020.06.17 04:05:00  ScalpingStrategy NZDUSD-e,M5: open #5 sell 0.02 NZDUSD-e at 0.64492 sl: 0.64592 ok

2020.06.25 21:33:59.640 2020.06.17 04:05:00  ScalpingStrategy NZDUSD-e,M5: Order ticket: 4

2020.06.25 21:33:59.640 2020.06.17 04:05:07  ScalpingStrategy NZDUSD-e,M5: close #5 sell 0.02 NZDUSD-e at 0.64492 sl: 0.64592 at price 0.64507

2020.06.25 21:33:59.640 2020.06.17 04:05:07  ScalpingStrategy NZDUSD-e,M5: Order closed successfully for ticket: 5

for the order no. 1 and 2, I don't know how to get the order ticket because it is hit the stoploss. But for order no 3 to 5 (and the rest of the open order) the ticket for open order and the ticket for the close order is different by 1 number higher. My code for checking the open order is like here.

bool CheckIfOpenOrdersByMagicNumber(int magicNumber){

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

      if(OrderSelect(i,SELECT_BY_POS)==true){

         if(OrderMagicNumber() == magicNumber){

            return true;

         }

      }

   }

   return false;

}

my entire code looks like this:

void OnTick()

  {

   if(!CheckIfOpenOrdersByMagicNumber(magicNumber1))
{
//buy and sell order
}

   else
     if(CheckIfOpenOrdersByMagicNumber(magicNumber1))
{
//previous code for closing the open order
}

I don't know if the CheckIfOpenOrdersByMagicNumber() function is the problem or my entire code structure that make it like that.

 
Keith Watford:

Please do not give wrong information.
I have already explained why NOT to use ask.

Sorry for the wrong information, I was given wrong information.

However, using the Bid prices actually fixed my problem as well so thanks!!

 
Aidan Stcyr:

Sorry for the wrong information, I was given wrong information.

However, using the Bid prices actually fixed my problem as well so thanks!

Hi Aidan, could you elaborate your code?

if(PSARValue > Bid)
     {
     bool res = OrderClose(orderId,OrderLots(),Bid,0);
     if(!res)
        Print("Error in OrderModify. Error code=",GetLastError());
     else
        Print("Order closed successfully for ticket: ", OrderTicket());
     }

This is my code do you use Bid when you selling like Keith suggest?

Reason: