Tp and SL are changing Auto... !

 

Hi

I wrote this code. User input SL and TP and Volume..
The program with rand() function give us odd / even.
For example: if return Even ,open buy position....and if return Odd : open sell position.

After the price hit the TP/SL then open another position . (According to : odd/even)

I have a Problem(BIG PROBLEM):

when user set TP and SL and Volume... the problem appear (Mostly) for second position and rest of the positions.
the TP and SL changing automaticly!!!!!!

For example: User input TP:20.........SL : 10............ For first position it is ok (Mostly). For other position it changes to :

TP : 18 ...... SL : 12

Or 

TP : 16 ..... SL : 14

Or

TP : 13 ..... SL : 17

And i do not know WHY...

Thanks.

//+------------------------------------------------------------------+
//|                                                    flip coin.mq4 |
//|                        Copyright 2022, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//#property description
#property show_inputs
#define PMA
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
input int  TP=20;
input int SL=10;
input double Vol=0.01;
int OnInit()
  {
  
    



//--- create timer
   EventSetTimer(60);

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
 
      if(OrdersTotal()==0)
        {
         int y=rand();
         if(y%2==0)
           {
            OrderSend(Symbol(),OP_BUY,Vol,Ask,5,Ask-(SL*Point),Ask+(TP*Point),NULL,0,0,clrGreen);
            double curSpread = (Ask - Bid) / Point;
            Comment(" Buy Price Norm :",NormalizeDouble(Ask,Digits),"\n Stop Loss Norm :",NormalizeDouble(Ask-(SL*Point),Digits),"\n Take Profit Norm :",NormalizeDouble(Ask+(TP*Point),Digits),"\n spread :",curSpread,"\n Price:",Ask,"\n Stop Loss:" , Ask-(SL*Point),"\n Take Profit :",Ask+(TP*Point));

           }
         else
           {
            OrderSend(Symbol(),OP_SELL,Vol,Bid,5,Bid+(SL*Point),Bid-(TP*Point),NULL,0,0,clrGray);
            double curSpread = (Ask - Bid) / _Point;
            Comment(" Sell Price Norm :",NormalizeDouble(Bid,Digits),"\n Stop Loss Norm :",NormalizeDouble(Bid+(SL*Point),Digits),"\n Take Profit Norm :",NormalizeDouble(Bid-(TP*Point),Digits),"\n spread :",curSpread,"\n Price:",Bid,"\n Stop Loss:" , Bid+(SL*Point),"\n Take Profit :",Bid-(TP*Point));

           }
        }

  
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }
//+------------------------------------------------------------------+
 
zeusjoon:

Hi

I wrote this code. User input SL and TP and Volume..
The program with rand() function give us odd / even.
For example: if return Even ,open buy position....and if return Odd : open sell position.

After the price hit the TP/SL then open another position . (According to : odd/even)

I have a Problem(BIG PROBLEM):

when user set TP and SL and Volume... the problem appear (Mostly) for second position and rest of the positions.
the TP and SL changing automaticly!!!!!!

For example: User input TP:20.........SL : 10............ For first position it is ok (Mostly). For other position it changes to :

TP : 18 ...... SL : 12

Or 

TP : 16 ..... SL : 14

Or

TP : 13 ..... SL : 17

And i do not know WHY...

Thanks.

Hey, try static int instead. 
 

The problem is probably because the TP of a buy is triggered when price=BID, and than you issue say another buy with the ask, which is 2 pips above your exit price (the bid) and you get at the end result that your first TP exit is not the same as your new ask (because exit in bid and entry in ask).

Reason: