• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 6:08pm
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 6:08pm
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Some help on debugging my code please :) 8 replies

Need help debugging SFX TOR indicator 2 replies

EA debugging help Please - SevenChi 7 replies

Need help debugging 0 replies

Help debugging my EA I feel like the solution is too simple. 1 reply

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 1
Attachments: Need help debugging EA to move SL twice
Exit Attachments
Tags: Need help debugging EA to move SL twice
Cancel

Need help debugging EA to move SL twice

  • Post #1
  • Quote
  • First Post: Edited 2:16pm Oct 3, 2014 2:15pm | Edited 2:16pm
  •  tigretoncio
  • | Joined Oct 2007 | Status: Member | 36 Posts
Hello,

My MQL4 skills are not particularly good, and I am unable to see what is the issue with this EA. I would like my SL to move to BE + some pips (given by variable LockInPips) when price is at "LockInPipsAt". This part works.

However I would like to move the SL again to protect more profit when price goes in my favor by "LockInPipsAt2", and change the stop to BE + "LockInPips2". (That does not work).

In the numerical example below, SL would be to BE+10 when prices goes in my favour 40 pips, (this works), and then SL should be moved to BE+50 when price goes in my favor by 100 pips.

I would appreciate any help given to fix the EA.


string str1 = "BreakEven";
extern int LockInPipsAt = 40;
extern int LockInPips = 10;
extern int LockInPipsAt2 = 100;
extern int LockInPips2 = 50;
extern bool ModifyTrades = TRUE;
extern bool ModifyTrade3 = TRUE;
extern int FontSize = 8;
extern color FontColour = White;
bool TP2achieved = FALSE;
bool TP1achieved = FALSE;
double punto;
double modpoint;
double merc;
string sonido = "expert.wav";
int slip1 = 3;
int slip2 = 0;
int unod = 0;
int init() {
modpoint = Point;
punto = Digits;
slip2 = slip1;
if (Digits == 5 || Digits == 3) {
punto = Digits - 1;
modpoint = 10.0 * Point;
slip2 = 10 * slip1;
}
if (NormalizeDouble(MarketInfo(Symbol(), MODE_LOTSTEP), 2) == 0.01) unod = 2;
else unod = 1;


ObjectCreate("EA_Version", OBJ_LABEL, 0, 0, 0);
ObjectSetText("EA_Version", str1 + " - Lock in " + LockInPips + " pips after " + LockInPipsAt + " pips profit", FontSize, "Arial Bold", FontColour);
ObjectSet("EA_Version", OBJPROP_XDISTANCE, 240);
ObjectSet("EA_Version", OBJPROP_YDISTANCE, 0);

return (0);
}
int deinit() {
ObjectDelete("EA_Version");
ObjectDelete("Monitor1");
ObjectDelete("Monitor2");
ObjectDelete("Monitor3");
return (0);
}
int start() {
if (Digits == 5 || Digits == 3) merc = MarketInfo(Symbol(), MODE_SPREAD) / 10.0;
else merc = MarketInfo(Symbol(), MODE_SPREAD);
if (LockInPipsAt > 0) {
for (int hh = 0; hh < OrdersTotal(); hh++) {
OrderSelect(hh, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol()) {
switch (OrderType()) {
case OP_BUY:
if (iClose(OrderSymbol(), 0, 0) - OrderOpenPrice() >= LockInPipsAt * modpoint && TP1achieved == FALSE) {
if (OrderStopLoss() < OrderOpenPrice()) {
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + LockInPips * modpoint, OrderTakeProfit(), 0, CLR_NONE);
Print("Stop Loss adjusted to ", LockInPips, " pips");
PlaySound(sonido);
TP1achieved = TRUE;

}
}
if (iClose(OrderSymbol(),0, 0) - OrderOpenPrice() >= LockInPipsAt2 * modpoint && TP2achieved == FALSE){
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + LockInPips2 * modpoint, OrderTakeProfit(), 0, CLR_NONE);
Print("Stop Loss 2 adjusted to ", LockInPips2, " pips");
PlaySound(sonido);
TP2achieved = TRUE;
}
break;
case OP_SELL:
if (OrderOpenPrice() - iClose(OrderSymbol(), 0, 0) >= LockInPipsAt * modpoint && TP1achieved == FALSE) {
if (OrderStopLoss() > OrderOpenPrice()) {
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - LockInPips * modpoint, OrderTakeProfit(), 0, Yellow);
Print("Stop Loss adjusted to ", LockInPips, " pips");
PlaySound(sonido);
TP1achieved = TRUE;
}
}
if (OrderOpenPrice() - iClose(OrderSymbol(),0, 0) >= LockInPipsAt2 * modpoint && TP2achieved == FALSE){
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + LockInPips2 * modpoint, OrderTakeProfit(), 0, CLR_NONE);
Print("Stop Loss 2 adjusted to ", LockInPips2, " pips");
PlaySound(sonido);
TP2achieved = TRUE;
}


}
}
}
}
return (0);
}
  • Post #2
  • Quote
  • Oct 5, 2014 10:26am Oct 5, 2014 10:26am
  •  cyber1
  • Joined Jan 2011 | Status: Member | 1,299 Posts
tigretoncio-

On the Sell side you added the trailing stop instead of subtracting it(on stage 2). I did not test the Buy side but looks okay to me.

I've attached the file with some coding modifications that I would suggest. In addition I would suggest using Print() statements at each stage to see where things go wrong.

You can run this EA in the Strategy Tester and it will place one Sell order. A hash mark will be left when the stop gets moved.
Attached File(s)
File Type: mq4 tigretoncio.mq4   6 KB | 230 downloads
 
 
  • Post #3
  • Quote
  • Oct 6, 2014 6:45am Oct 6, 2014 6:45am
  •  tigretoncio
  • | Joined Oct 2007 | Status: Member | 36 Posts
Quoting cyber1
Disliked
tigretoncio- On the Sell side you added the trailing stop instead of subtracting it(on stage 2). I did not test the Buy side but looks okay to me. I've attached the file with some coding modifications that I would suggest. In addition I would suggest using Print() statements at each stage to see where things go wrong. You can run this EA in the Strategy Tester and it will place one Sell order. A hash mark will be left when the stop gets moved. {file}
Ignored
Cyber1,

It works like a charm, and I have learned a lot with your code, thank you very much and kudos to you!
 
 
  • Post #4
  • Quote
  • Oct 6, 2014 8:30pm Oct 6, 2014 8:30pm
  •  scalpz
  • | Joined Oct 2008 | Status: Target 1: SL in the green | 513 Posts
Quoting tigretoncio
Disliked
{quote} Cyber1, ... and I have learned a lot with your code, thank you very much and kudos to you!
Ignored

Me too.

Nice stuff by both of you

cheers scalpz
Target 2 : Stoploss in the green
 
 
  • Post #5
  • Quote
  • Last Post: Edited 9:37pm Nov 11, 2020 8:11pm | Edited 9:37pm
  •  Sorobanista
  • Joined Jan 2017 | Status: Member | 471 Posts
delete
Caution, patience and balance.
 
 
  • Platform Tech
  • /
  • Need help debugging EA to move SL twice
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023