I have an EA but the SL for the EA was set randomly. Can someone modified it to a fix 25 SL for me?
Hi,
I can give it a try. Please send the .mq4 file.
I get an error when trying to upload the file…
Hi,
my post with the email got removed. I am not sure if you are allowed to send me over PM… :S
too lazy to read the forum rules.
Post the code here. I will try to help.
extern string COM_TRADES_1 = “— Trade Settings”;
extern string COM_TRADES_2 = “SL;DEV_BUY;DEV_SELL|TP;TP_VAL|BE;BE_TRIG;BE_DEV|TS;TS_TRIG;TS_STEP|MM;MM_VAL”;
extern string Trade_Info = “0;30;30|1;200|0;0;0|0;0;0|0;0.1”;
extern int Max_SL = 250;
extern bool Close_Pending_After_TP = false;
extern int Friday_Close_Hour = 20;
This EA will auto set SL for me. And the SL is depends on the previous candle length. But I want to set it as fix SL which is 25 pips.
Hi,
Could you also post the code where the stop loss is being used?
These extern params code is not enough to understand how SL is used.
How many digits does your broker use?
//±-----------------------------------------------------------------+
//| Open new Orders |
//±-----------------------------------------------------------------+
void OpenNewOrders(double Ref_High,double Ref_Low) {
int i;
for (i=0;i<T_NUM;i++) {
//Take care of reentries
//if (Re_Num > 0 && T_RE_B[i] > Re_Num) continue;
T_RE_B[i]++;
RefreshRates();
double CurrentBid = MarketInfo(Symbol(), MODE_BID);
double CurrentAsk = MarketInfo(Symbol(), MODE_ASK);
int ticket,err;
//Put up long position
double EntryLong = Ref_High+(T_SL_BUY[i]*Point);
if (CurrentAsk >= EntryLong-StopLevel*Point) EntryLong = CurrentAsk + StopLevel*Point;
double StopLong = Ref_Low-(T_SL_SELL[i]*Point);
double ProfitLong = EntryLong+(T_TP_VAL[i]*Point);
//Check for MAX-SL
if ((EntryLong-StopLong) > (Max_SL*Point)) StopLong = EntryLong-(Max_SL*Point);
//Determine Lot size
double LotLong = T_MM_VAL[i];
if (T_MM[i] == 1) LotLong = CalculateLotByRisk(((EntryLong-StopLong)/Point),T_MM_VAL[i]);
if (T_MM[i] == 2) LotLong = NormalizeDouble(((AccountBalance()/MM_Mode_2_Value)*T_MM_VAL[i]),2);
while (IsTradeContextBusy()) Sleep(50);
int x,y;
for (x=0;x<Max_Retries;x++) {
ticket = 0;
if (!IsECN) {
ticket = OrderSend(Symbol(),OP_BUYSTOP,LotLong,EntryLong,Max_Slippage,StopLong,ProfitLong,"PCB_LONG_"+i,Magic_Number,0,CLR_NONE);
if (ticket > 0) {
break;
}
else {
err = GetLastError();
if (err==0 || err==4 || err==136 || err==137 || err==138 || err==146) {//Busy errors
Sleep(1500);
continue;
}
else {
Print("TRADE ERROR: Open Buy Stop Error Code: " + err);
break;
}
}
}
else {
ticket = OrderSend(Symbol(),OP_BUYSTOP,LotLong,EntryLong,Max_Slippage,0,0,"PCB_LONG_"+i,Magic_Number,0,CLR_NONE);
if (ticket > 0) {
for (y=0;y<Max_Retries;y++) {
OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
if (OrderModify(OrderTicket(),OrderOpenPrice(),StopLong,ProfitLong,0,Blue)) {
break;
}
else {
err = GetLastError();
if (err==0 || err==4 || err==136 || err==137 || err==138 || err==146) {//Busy errors
Sleep(1500);
continue;
}
else {
Print("TRADE ERROR: Modify Buy Stop Error Code: " + err);
break;
}
}
}
}
else {
err = GetLastError();
if (err==0 || err==4 || err==136 || err==137 || err==138 || err==146) {//Busy errors
Sleep(1500);
continue;
}
else {
Print("TRADE ERROR: Open Buy Stop Error Code: " + err);
break;
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Take care of reentries
//if (Re_Num > 0 && T_RE_S[i] > Re_Num) continue;
T_RE_S[i]++;
RefreshRates();
CurrentBid = MarketInfo(Symbol(), MODE_BID);
CurrentAsk = MarketInfo(Symbol(), MODE_ASK);
//Put up short position
double EntryShort = Ref_Low-(T_SL_SELL[i]*Point);
if (CurrentBid <= EntryShort+StopLevel*Point) EntryShort = CurrentBid - StopLevel*Point;
double StopShort = Ref_High+(T_SL_BUY[i]*Point);
double ProfitShort = EntryShort-(T_TP_VAL[i]*Point);
//Check for MAX-SL
if ((StopShort-EntryShort) > (Max_SL*Point)) StopShort = EntryShort+(Max_SL*Point);
//Determine Lot size
double LotShort = T_MM_VAL[i];
if (T_MM[i] == 1) LotShort = CalculateLotByRisk(((StopShort-EntryShort)/Point),T_MM_VAL[i]);
if (T_MM[i] == 2) LotShort = NormalizeDouble(((AccountBalance()/MM_Mode_2_Value)*T_MM_VAL[i]),2);
while (IsTradeContextBusy()) Sleep(50);
for (x=0;x<Max_Retries;x++) {
ticket = 0;
if (!IsECN) {
ticket = OrderSend(Symbol(),OP_SELLSTOP,LotShort,EntryShort,Max_Slippage,StopShort,ProfitShort,"PCB_SHORT_"+i,Magic_Number,0,CLR_NONE);
if (ticket > 0) {
break;
}
else {
err = GetLastError();
if (err==0 || err==4 || err==136 || err==137 || err==138 || err==146) {//Busy errors
Sleep(1500);
continue;
}
else {
Print("TRADE ERROR: Open Buy Stop Error Code: " + err);
break;
}
}
}
else {
ticket = OrderSend(Symbol(),OP_SELLSTOP,LotShort,EntryShort,Max_Slippage,0,0,"PCB_SHORT_"+i,Magic_Number,0,CLR_NONE);
if (ticket > 0) {
for (y=0;y<Max_Retries;y++) {
OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);
if (OrderModify(OrderTicket(),OrderOpenPrice(),StopShort,ProfitShort,0,Red)) {
break;
}
else {
err = GetLastError();
if (err==0 || err==4 || err==136 || err==137 || err==138 || err==146) {//Busy errors
Sleep(1500);
continue;
}
else {
Print("TRADE ERROR: Modify Buy Stop Error Code: " + err);
break;
}
}
}
}
else {
err = GetLastError();
if (err==0 || err==4 || err==136 || err==137 || err==138 || err==146) {//Busy errors
Sleep(1500);
continue;
}
else {
Print("TRADE ERROR: Open Buy Stop Error Code: " + err);
break;
}
}
}
}
}
}
Hi,
(In the future you can use code tags to better highlight the code. It is easier to read it.)
I have attached the modified code.
Can’t compile since I miss part of the code but it should be fine.
In order to produce only minor updates and to be able to go back to previous code easy, I have added a new external variable “StopLoss”.
extern int StopLoss = 200;
Please update your stop loss only from here from now on. The check against [B][I]Max_SL[/I][/B] is not being done any more, hence the usage of this parameter is not necessary (but I did not remove it from your code).
I have commented the 4 locations where the modification has been done.
Let me know if you have questions.
PS: This code (int to double conversion):
double StopLong = MathRound(StopLoss);
does the same (exact) thing as
double StopLong = StopLoss;
Both versions are correct. I prefer to use the first one.
Thanks for the help, I will try it when market open. So the previous mql4 and this mql4 should attach together to the same chart for it to perform well? So the pips of stoploss, I just have to change it from here right? “extern int StopLoss = 200”
I just uploaded the full EA, sorry for caused you a lot of troubles. Last time I just upload the mql4 and showed error. i just realized should be zip it. Now this is the full EA .
PC_Breakout_Alpha_51.zip (6.51 KB)
I modified by add in extern int StopLoss = 250; and change to double StopLong = MathRound(StopLoss); and double StopLong = StopLoss; . But got 2 more modification for the Max_SL , should I delete it or just let it be?
Hi,
I have attached the modified version of the EA. Stop loss is fixed now. I have checked and the variable name “StopLoss” was already use inside a function, so I have changed our new variable name to “customFixedStopLoss” (line 33).
Other changes:
- line 422: Comment existing line and added a new line
- line 428: Comment existing line
- line 507: Comment existing line and added a new line
- line 513: Comment existing line
This will always create new orders with fixed stop loss, determined by the “customFixedStopLoss” variable.
If you want to go back to the initial version, just invert the changes (remove comment where commented and comment where new line was added.)
These changes apply only for 5 digits broker.
Easiest fix if you switch to 4 digits broker is is add another “0” to “customFixedStopLoss” (e.g. 2000 instead of 200 - this would mean 20 pips stop loss).
Good luck.
Im sorry for disturbing you again. I still don’t understand, the modified code.zip is the same as the previous zip file that you sent me. But I still don’t understand how to change. Can you help me modified the mql4 that I sent on previous code and save change for me? Sorry for keep disturbing you
Hmmm, ok, maybe I have used the same name for the zip as before and sent you the same file…
I will make the changes again, shortly.
ok,
Now I had more time and did little more change, so it looks better.
Same as before, your new stop loss variable is: “customFixedStopLoss”.
Usage: if you want a fixed stop loss of [B][I]25 pips[/I][/B] set [B]customFixedStopLoss = 25[/B].
Other changes:
- line 433: Comment existing line and added a new line
- line 438: Comment existing line
- line 517: Comment existing line and added a new line
- line 522: Comment existing line
Test it and let me know.
Should I put 250 or 25 for 25 pips? Bcz my broker using 5 digits
For this modified version of mql4 , I failed to attach to the chart.
Use [B]25[/B]. I have added some code which identifies the number of digits used and adapts.
Can you give more details…
I doubt that it is because of the changes, but would you like me to check it as well?