how should I code so that the first order has tp 40 and if it doesn't close it and open another order so that the second order has tp 30 and closes the first one too??
how should I code so that the first order has tp 40 and if it doesn't close it and open another order so that the second order has tp 30 and closes the first one too??
Matej Cehelsky:
how should I code so that the first order has tp 40 and if it doesn't close it and open another order so that the second order has tp 30 and closes the first one too??
how should I code so that the first order has tp 40 and if it doesn't close it and open another order so that the second order has tp 30 and closes the first one too??
Inside a grid system?
Or close all previous similar open orders when the tp of the latest one hits ?
You will need to retain the latest order ticket (or deal ticket) in memory .
Then you will check on tick if that order has closed in profit , if it has you will call your closer and close all the orders with that magic number
Note the following does not assign a ticket # to the ticket retention variable which you should do everytime you open a new trade
#property version "1.00" #property strict //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ bool checkContextBusy=false; int latestOrderTicket=-1;//you store the latest opened order ticket here int magicNR=316; int OnInit() { //looks for the latest opened order in case of reinit latestOrderTicket=find_latest_open_order(_Symbol,magicNR); checkContextBusy=false; //--- return(INIT_SUCCEEDED); } int find_latest_open_order(string _symbol,int _magic_nr){ int found=-1; datetime latest=0; for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){ if(OrderMagicNumber()==_magic_nr&&OrderSymbol()==_symbol){ if(OrderOpenTime()>latest){ latest=OrderOpenTime();found=i; }}}} return(found); } void OnTick() { //if check context not busy if(!checkContextBusy){ //set busy checkContextBusy=true; check_latest(latestOrderTicket); //unset checkContextBusy=false; } } void check_latest(int &_ticket){ //if theres a ticket if(_ticket!=-1){ //select the order if(OrderSelect(_ticket,SELECT_BY_TICKET)){ //if it is no longer open if(OrderCloseTime()!=0){ //and it closed in profit if((OrderProfit()+OrderSwap()+OrderCommission())>0.0){ //then dump the ticket _ticket=-1; //and close all order //call closer here }}}} } void OnDeinit(const int reason) { }
This code will flood the CPU and will result in delays or freezing of the platform it’s a redundant way of coding besides such a strategy is based on a grid system I am sure you need to go through examples here on mql5
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register