Then if you have the Trailing Stop EA, then you can modify it to place the order.
Then if you have the Trailing Stop EA, then you can modify it to place the order.
Thanks Fernando for your quick reply,
As I said I am beginner, I tried to edit however it does not work or it creates multi new orders. That is why I ask help here.
I appreciate if you can help me to add some additional lines to make it works.
Here is EA trailing stop
#property copyright "Forexia" #property link "https://www.mql5.com/en/users/scuxia" /* Kicks in when position reaches at least TrailingStop pips of profit. */ extern double TrailingStop = 5; // Set it to some value above 0 to activate stop-loss extern double StopLoss = 0; int init() { return(0); } int deinit() { return(0); } int start() { double PointValue; for (int i = 0; i < OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true) if (OrderSymbol() != Symbol()) continue; // Skipping positions in other currency pairs //Calculate the point value in case there are extra digits in the quotes if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) PointValue = 0.0001; else if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) PointValue = 0.01; else PointValue = MarketInfo(OrderSymbol(), MODE_POINT); //Normalize trailing stop value to the point value double TSTP = TrailingStop * PointValue; if (OrderType() == OP_BUY) { if (Bid - OrderOpenPrice() > TSTP) { if (OrderStopLoss() < Bid - TSTP) { if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP, OrderTakeProfit(), Red)) Print("Error setting Buy trailing stop: ", GetLastError()); OrderSend(Symbol(),OP_BUY,OrderLots(),OrderClosePrice(),0,NULL,NULL,"VNCIS",301278,0,CLR_NONE); } } else if ((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0) && (OrderStopLoss() == 0)) if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * PointValue, OrderTakeProfit(), Red)) Print("Error setting Buy stop-loss: ", GetLastError()); OrderSend(Symbol(),OP_BUY,OrderLots(),OrderClosePrice(),0,NULL,NULL,"VNCIS",301278,0,CLR_NONE); } else if (OrderType() == OP_SELL) { if (OrderOpenPrice() - Ask > TSTP) { if ((OrderStopLoss() > Ask + TSTP) || (OrderStopLoss() == 0)) { if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP, OrderTakeProfit(), Red)) Print("Error setting Sell trailing stop: ", GetLastError()); OrderSend(Symbol(),OP_SELL,OrderLots(),OrderClosePrice(),0,NULL,NULL,"VNCIS",301278,0,CLR_NONE); } } else if ((OrderStopLoss() != Ask + StopLoss * PointValue) && (StopLoss != 0) && (OrderStopLoss() == 0)) if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * PointValue, OrderTakeProfit(), Red)) Print("Error setting Sell stop-loss: ", GetLastError()); OrderSend(Symbol(),OP_SELL,OrderLots(),OrderClosePrice(),0,NULL,NULL,"VNCIS",301278,0,CLR_NONE); } } return(0); }
Please edit your post (don't create a new one) and place your code properly with the "</>" icon or Alt-S.
Don't just paste code as plain text. It makes it unreadable.
It seems your code is from the following CodeBase publication: TrailingStop
However, I don't see any changes you have made at your attempt at placing the order after the trailing stop is hit.
We are not going to code it for you. The forum is not a free coding service. So please make your own attempt at coding what you need, before we can provide help.
If however you do not know how to code, then consider placing a job request in the Freelance section.
The forum is for aiding those that have a specific issue and require some guidance in resolving it.
- www.mql5.com
It seems your code is from the following CodeBase publication: TrailingStop
However, I don't see any changes you have made at your attempt at placing the order after the trailing stop is hit.
We are not going to code it for you. The forum is not a free coding service. So please make your own attempt at coding what you need, before we can provide help.
If however you do not know how to code, then consider placing a job request in the Freelance section.
The forum is for aiding those that have a specific issue and require some guidance in resolving it.
Thanks for you comments,
I already added these lines, please see the codes
OrderSend(Symbol(),OP_BUY,OrderLots(),OrderClosePrice(),0,NULL,NULL,"VNCIS",301278,0,CLR_NONE);
OrderSend(Symbol(),OP_SELL,OrderLots(),OrderClosePrice(),0,NULL,NULL,"VNCIS",301278,0,CLR_NONE);
OrderSend(Symbol(),OP_BUY,OrderLots(),OrderClosePrice(),0,NULL,NULL,"VNCIS",301278,0,CLR_NONE);
OrderSend(Symbol(),OP_SELL,OrderLots(),OrderClosePrice(),0,NULL,NULL,"VNCIS",301278,0,CLR_NONE);
it will create many new orders but I need open only one order at previous close price. If I move these lines outside of loop "for" it will not open new order.
Regards,
vncis
Try this , the comments have explainers .
#property copyright "Forexia" #property link "https://www.mql5.com/en/users/scuxia" #property strict /* Kicks in when position reaches at least TrailingStop pips of profit. */ input double TrailingStop = 5;// Trailing Distance // Set it to some value above 0 to activate stop-loss input double StopLoss = 0;// Apply This Stop Loss (or 0 for not) input bool EnterOpposite=true;// Enter The Opposite Direction input bool AggregateTrades=false;// Aggregate Trades /* first we will need a log of the trailing stop the system has applied so we create that log */ struct trailer_log{ //our log has trading tickets int tickets[]; //thats all trailer_log(void){reset();} ~trailer_log(void){reset();} void reset(){ ArrayFree(tickets); } /* we need to be able to add to the log */ int add_to_log(int _new_ticket){ //calculate the new size of the tickets list int ns=ArraySize(tickets)+1;//existing size +1 //change the size of your tickets list ArrayResize(tickets,ns,0); //add the new ticket in position ns-1 because counting starts from 0 tickets[ns-1]=_new_ticket; //return the location of the new ticket in the log return(ns-1); } /* we may also need to search in the list */ int find_in_log(int _this_ticket){ //loop in the list , if you see the ticket return the position for(int i=0;i<ArraySize(tickets);i++){ if(tickets[i]==_this_ticket){return(i);} } //or return -1 if it does not exist return(-1); } /* But hold on , we may just want to know if the ticket is in the log right ? i mean we already are going to know what the ticket is so */ bool is_ticket_in_log(int _this_ticket){ //we utilize the finder int found_ix=find_in_log(_this_ticket); //and if the finder does not find anything we return false if(found_ix!=-1){return(true);} return(false); } /* we will also need to remove from the log */ void remove_from_log(int _ix){ //reduce the number of items in the log int ns=ArraySize(tickets)-1; //move the last item (which happens to be ns too) to the position we want to remove tickets[_ix]=tickets[ns]; //if the new size is >0 we resize the list if(ns>0){ArrayResize(tickets,ns,0);} //if the new size is 0 we free the list else{ArrayFree(tickets);} } /* lastly we will need a monitoring function which counts all closed by trail trades and tries to reopen those that closed */ void monitor_trailer(int attempts,//trading entry attemts uint timeout,//wait between attempts int slippage,//slippage string comment,//comment of trades int magic,//magic number of trades double override_lot,//if not 0.0 a single lot size will be used for reentry instead of the one in the orders bool opposite,//if true enter in the opposite direction of the original bool aggregate)//if true and we have to enter both ways and override is off we enter on the aggregate of the two sides { /* this function is accessed at a point in time within which more than one orders may have hit trail since we are only concerned with this symbol we will sum all that up in one buy that needs to open , maybe, or one sell that needs to open . We will add lots as we find orders that closed */ double reBuyLot=0.0,reSellLot=0.0; //now we access our tickets log upside down (because we may remove from it) int from=ArraySize(tickets)-1; for(int i=from;i>=0;i--) { //select the order if(OrderSelect(tickets[i],SELECT_BY_TICKET)){ //the order is in the log so we know it was trailed but was it closed ? if(OrderCloseTime()!=0){ //that means its closed so //we get the direction and add to our reentry lots accordingly if(OrderType()==OP_BUY){ //if the opposite is off if(!opposite){reBuyLot+=OrderLots();} //if the opposite is on else{reSellLot+=OrderLots();} }else if(OrderType()==OP_SELL){ if(!opposite){reSellLot+=OrderLots();} else{reBuyLot+=OrderLots();} } //and then we throw it in the garbage remove_from_log(i); } } } //the check ends here , now we need to reenter //if aggregate lots is on if(aggregate){ double temp=reBuyLot; reBuyLot-=reSellLot; reSellLot-=temp; if(reBuyLot<0.0){reBuyLot=0.0;} if(reSellLot<0.0){reSellLot=0.0;} } //do we have a buy order ? if(reBuyLot>0.0){ //do we override the lot size if(override_lot>0.0){reBuyLot=override_lot;} //try to open the buy order , it might fail , if it does we lost the oppurtunity //because we have also deleted the trailed ticket bool open=trade(OP_BUY,reBuyLot,attempts,timeout,slippage,comment,magic); } //do we have a sell order ? if(reSellLot>0.0){ //do we override the lot size if(override_lot>0.0){reSellLot=override_lot;} //try to open the buy order , it might fail , if it does we lost the oppurtunity //because we have also deleted the trailed ticket bool open=trade(OP_SELL,reSellLot,attempts,timeout,slippage,comment,magic); } } }; /* We have the monitoring structure with its functions , now what ? Now we declare a structure on the global scope and we use it . Keep in mind this version of the structure has no save/load capacity so if we switch timeframes it will "forget" everything. */ //declaration trailer_log LOG_OF_TRAILED_ORDERS;//can name it anything //simple trade function - used above , you can skip that bool trade(ENUM_ORDER_TYPE type, double lots, int attempts, uint timeout, int slippage, string comment, int magic){ int errors=0;ResetLastError(); double minlot=(double)SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);errors+=GetLastError();ResetLastError(); double maxlot=(double)SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);errors+=GetLastError();ResetLastError(); if(errors==0&&IsConnected()&&IsTradeAllowed()&&IsTradeAllowed(_Symbol,TimeCurrent())){ if(lots<minlot){lots=minlot;}else if(lots>maxlot){lots=maxlot;} double margin_remain=AccountFreeMarginCheck(_Symbol,type,lots); if(margin_remain>=(AccountBalance()*0.4)){ int attempted=0; int ticket=-1; while(ticket==-1&&attempted<attempts&&!IsTradeContextBusy()) { attempted++; double op=Ask; if(type==OP_SELL){op=Bid;} RefreshRates(); ticket=OrderSend(_Symbol,type,lots,op,slippage,0.0,0.0,comment,magic,0,clrWhite); Sleep(timeout); } if(ticket!=-1){return(true);} }} return(false); } int OnInit() { //We reset the trailer log LOG_OF_TRAILED_ORDERS.reset(); //opened a trade for the tester here ignore it //trade(OP_BUY,0.01,10,333,1000,"",888); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { } void OnTick() { //so within the functions we had there are changes expecially when we apply trailing double PointValue; for(int i = 0; i < OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true) if(OrderSymbol() != Symbol()) continue; // Skipping positions in other currency pairs //Calculate the point value in case there are extra digits in the quotes if(MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) PointValue = 0.0001; else if(MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) PointValue = 0.01; else PointValue = MarketInfo(OrderSymbol(), MODE_POINT); //Normalize trailing stop value to the point value double TSTP = TrailingStop * PointValue; if(OrderType() == OP_BUY) { if(Bid - OrderOpenPrice() > TSTP) { if(OrderStopLoss() < Bid - TSTP) { if(!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TSTP, OrderTakeProfit(), Red)){ Print("Error setting Buy trailing stop: ", GetLastError()); }else{//i.e if it trailed //we log this trade if its not in the log if(!LOG_OF_TRAILED_ORDERS.is_ticket_in_log(OrderTicket())){ LOG_OF_TRAILED_ORDERS.add_to_log(OrderTicket());//simple } } } } else if((OrderStopLoss() != Bid - StopLoss * PointValue) && (StopLoss != 0) && (OrderStopLoss() == 0)) if(!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - StopLoss * PointValue, OrderTakeProfit(), Red)) Print("Error setting Buy stop-loss: ", GetLastError()); } else if(OrderType() == OP_SELL) { if(OrderOpenPrice() - Ask > TSTP) { if((OrderStopLoss() > Ask + TSTP) || (OrderStopLoss() == 0)) { if(!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TSTP, OrderTakeProfit(), Red)){ Print("Error setting Sell trailing stop: ", GetLastError()); }else{//i.e. if we trailed it //we log this trade if its not in the log if(!LOG_OF_TRAILED_ORDERS.is_ticket_in_log(OrderTicket())){ LOG_OF_TRAILED_ORDERS.add_to_log(OrderTicket());//simple } } } } else if((OrderStopLoss() != Ask + StopLoss * PointValue) && (StopLoss != 0) && (OrderStopLoss() == 0)) if(!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopLoss * PointValue, OrderTakeProfit(), Red)) Print("Error setting Sell stop-loss: ", GetLastError()); } } //finally we monitor the log /* the settings have the reentry attemtps , timeout between attempts and basic settings */ LOG_OF_TRAILED_ORDERS.monitor_trailer(13,333,1000,"",888,0.0,EnterOpposite,AggregateTrades); } //+------------------------------------------------------------------+
OrderSend(Symbol(),OP_BUY,OrderLots(),OrderClosePrice(),0,NULL,NULL,"VNCIS",301278,0,CLR_NONE); OrderSend(Symbol(),OP_SELL,OrderLots(),OrderClosePrice(),0,NULL,NULL,"VNCIS",301278,0,CLR_NONE);
- You can not use any Trade Functions until you first select an order.
- You can't open orders at any price, only at the Bid or Ask.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I am beginner of EA. I would like to write simple EA to open new buy/sell market order right after the trailing stop loss is closed.
I already have free trailing stop EA.
I appreciate if someone can help me.
Regards,
VNCIS