• Home
  • Forums
  • News
  • Calendar
  • Coins
  • Market
  • Login
  • Join
  • 10:44am
Menu
  • Forums
  • News
  • Calendar
  • Coins
  • Market
  • Login
  • Join
  • 10:44am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Forex Factory

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Script to automatically modify SL&TP for all open positions 1 reply

Open Manually, Close Automatically EA? 0 replies

Software that automatically closes all open trades in just one click 9 replies

When you open a live account, do you automatically retain a demo one as well? 7 replies

Please help! - How to automatically close open position after 1 hour? 5 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 6
Attachments: EA to Set SL & TP of Open Trades Automatically
Exit Attachments
Tags: EA to Set SL & TP of Open Trades Automatically
Cancel

EA to Set SL & TP of Open Trades Automatically

  • Post #1
  • Quote
  • First Post: Edited 12:38pm May 11, 2021 11:24am | Edited 12:38pm
  •  chimoong
  • | Joined May 2016 | Status: Member | 245 Posts
Hi Coders, I'm a beginner of coding and no programming background.

FYI, I manually open the trades for my own testing only.

I created an EA to set SL (60 pips) &TP (1,400 pips) of open trades automatically on 1-digit, 2-digit, 3-digit, 4-digit and 5-digit symbols (including crypto BTCUSD) but partially failed, don't understand why. Problems :-

1) Some of the trades are successfully and correctly assigned with SL & TP buy majority of the symbols are not assigned with SL & TP at all.

2) My settings for SL=60 pips and TP=1,400 pips, some of the pairs are correctly set with 60 / 1,400 pips but some of the pairs are set with only 0.6 / 140 pips.

Inserted Code
extern double TPpip=1400.0; //TP in Pips
extern double SLpip=60.0;   //SL in Pips
 
double cPoint=MarketInfo(Symbol(),MODE_POINT)*10;
 
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {
 
   for(int i=OrdersTotal()-1; i>=0; i--)
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
   if(OrderStopLoss()==0 || (OrderTakeProfit()==0 && OrderStopLoss()==0)) {
      if(OrderType()==OP_BUY) {
         if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-SLpip*cPoint,OrderOpenPrice()+TPpip*cPoint,0,clrNONE))
            Print("OrderModified BUY Error:",Symbol(),":",GetLastError());
      }
 
      if(OrderType()==OP_SELL) {
         if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+SLpip*cPoint,OrderOpenPrice()-TPpip*cPoint,0,clrNONE))
            Print("OrderModified SELL Error:",Symbol(),":",GetLastError());
      }
}
}

Attached for my EA, kindly run and have a look by manually open trades for 1-digit, 2-digit, 3-digit, 4-digit and 5-digit symbols.

FYI, my definition of pip and point levels are as follows :-
a) EU (5-digit) : 1.21689 >> 8=pip level and 9=point level
b) GJ (3-digit) : 153.489 >> 8=pip level and 9=point level
c) XAUUSD (2-digit) : 1735.89 >> 8=pip level and 9=point level
d) BTCUSD (2-digit) : 56143.89 >> 8=pip level and 9=point level

Hope someone can modify my EA and share to me on your mod, and hope can tell me where am I going wrong and with reasons so that I can learn. Thanks
Attached File(s)
File Type: mq4 SetSLTP EA.mq4   2 KB | 463 downloads
  • Post #2
  • Quote
  • May 11, 2021 1:06pm May 11, 2021 1:06pm
  •  kennyhubbard
  • Joined Sep 2008 | Status: Member | 354 Posts
Your EA is modifying all trades off all the charts you running it on, so if you run it on XAU, it is going to try and modify your EU trades as well, with the consequent incorrect point info.

After your OrderSelect add a line like this :-

Quote
Disliked
if(OrderSymbol()!=Symbol())continue;

Now you telling the EA to only modify trades of the symbol that matches the chart.
 
 
  • Post #3
  • Quote
  • May 11, 2021 1:17pm May 11, 2021 1:17pm
  •  chimoong
  • | Joined May 2016 | Status: Member | 245 Posts
Quoting kennyhubbard
Disliked
Your EA is modifying all trades off all the charts you running it on, so if you run it on XAU, it is going to try and modify your EU trades as well, with the consequent incorrect point info. After your OrderSelect add a line like this :- {quote} Now you telling the EA to only modify trades of the symbol that matches the chart.
Ignored
Hi Kenny, thank you for your reply. I added the following as follows, but it is not working, kindly advise, thanks.

Inserted Code
extern double TPpip=1400.0; //TP in Pips
extern double SLpip=60.0;   //SL in Pips
 
double cPoint=MarketInfo(Symbol(),MODE_POINT)*10;
 
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {
 
   for(int i=OrdersTotal()-1; i>=0; i--)
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
   if(OrderStopLoss()==0 || (OrderTakeProfit()==0 && OrderStopLoss()==0))
   if(OrderSymbol()!=Symbol())continue; {
 
      if(OrderType()==OP_BUY) {
         if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-SLpip*cPoint,OrderOpenPrice()+TPpip*cPoint,0,clrNONE))
            Print("OrderModified BUY Error:",Symbol(),":",GetLastError());
      }
 
      if(OrderType()==OP_SELL) {
         if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+SLpip*cPoint,OrderOpenPrice()-TPpip*cPoint,0,clrNONE))
            Print("OrderModified SELL Error:",Symbol(),":",GetLastError());
      }
}
}
 
 
  • Post #4
  • Quote
  • May 11, 2021 1:32pm May 11, 2021 1:32pm
  •  kennyhubbard
  • Joined Sep 2008 | Status: Member | 354 Posts
Yeah, just had a closer look,

try replacing your ONTick function with this :-

Inserted Code
void OnTick()
  {
   double sl = SLpip * cPoint;
   double tp = SLpip * cPoint;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()!=Symbol())
            continue;
      if(OrderStopLoss()==0 || (OrderTakeProfit()==0 && OrderStopLoss()==0))
        {
         if(OrderType()==OP_BUY)
           {
            sl = OrderOpenPrice()-sl;
            tp = OrderOpenPrice() + tp;
            if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderClosePrice()-SLpip*cPoint,Digits),NormalizeDouble(OrderClosePrice()+TPpip*cPoint,Digits),0,clrNONE))
               Print("OrderModified BUY Error:",Symbol(),":",GetLastError()," ",sl," ",tp);
           }
         if(OrderType()==OP_SELL)
           {
            if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderClosePrice()+SLpip*cPoint,Digits),NormalizeDouble(OrderClosePrice()-TPpip*cPoint,Digits),0,clrNONE))
               Print("OrderModified SELL Error:",Symbol(),":",GetLastError());
           }
        }
     }
  }

One of the issue you will find is that BTC has a massive spread and if your TP is not big enough, it lands within the spread and results in a invalid stops error.

To address this I have used OrderClosePrice() rather than OrderOpenPrice(). I also Normalised the doubles for you. There was also a set of brakets missing for the loop function, so the EA was only looping through the OrderSelect command and then carrying on with i = 0, so it will only modify 1 order.
 
 
  • Post #5
  • Quote
  • May 11, 2021 1:35pm May 11, 2021 1:35pm
  •  kennyhubbard
  • Joined Sep 2008 | Status: Member | 354 Posts
You can ignore these lines :-

Quote
Disliked
sl = OrderOpenPrice()-sl;
tp = OrderOpenPrice() + tp;

I put them in there for some troubleshooting.
 
 
  • Post #6
  • Quote
  • May 11, 2021 2:04pm May 11, 2021 2:04pm
  •  chimoong
  • | Joined May 2016 | Status: Member | 245 Posts
Quoting chimoong
Disliked
{quote} Hi Kenny, thank you for your reply. I added the following as follows, but it is not working, kindly advise, thanks. extern double TPpip=1400.0; //TP in Pips extern double SLpip=60.0; //SL in Pips double cPoint=MarketInfo(Symbol(),MODE_POINT)*10; //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { for(int i=OrdersTotal()-1; i>=0; i--) if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) if(OrderStopLoss()==0 || (OrderTakeProfit()==0...
Ignored
Hi Kenny, thanks. I followed your codes exactly, it modified the EU pair only but ignored the rest of the symbols (attachment).

Secondly, I would like to set the SL=60 pips and TP=1,400 pips, the EU trades shows the SL / TP as follows :-

1) BUY open trade : SL=60.7 pips and TP=1,399.3 pips

2) SELL open trade : SL=59.5 pips and TP=1,400.5 pips

The slight differences are due to "NormalizeDouble" problem ? Is there anyway to make get the exact figure of SL=60 pips and TP=1,400 pips for both BUY and SELL trades ? Kindly advise, thanks.
Attached Image (click to enlarge)
Click to Enlarge

Name: OpenTrades.png
Size: 29 KB
 
 
  • Post #7
  • Quote
  • May 11, 2021 2:46pm May 11, 2021 2:46pm
  •  kennyhubbard
  • Joined Sep 2008 | Status: Member | 354 Posts
Chimoong,

i will have to consider the rounding problem tomorrow as it’s past my bedtime. Isuspect it has more to do with your multiplying the point by 10. Try removing the multiplyer and manually increase your stops x 10.

I should also correct myself. You don’t have to use OrderClosePrice for calculating the stops. I just realised that I was getting an error with OrderOpenPrice because I was running the EA on a trade that was a day old and the price action had moved beyond the stops. If you only run on new trades you can use openprice but if you run on old trades then closeprice is better but your stops will not be accurate based on entry price.
 
 
  • Post #8
  • Quote
  • May 11, 2021 2:50pm May 11, 2021 2:50pm
  •  chimoong
  • | Joined May 2016 | Status: Member | 245 Posts
Quoting kennyhubbard
Disliked
Chimoong, i will have to consider the rounding problem tomorrow as it’s past my bedtime. Isuspect it has more to do with your multiplying the point by 10. Try removing the multiplyer and manually increase your stops x 10. I should also correct myself. You don’t have to use OrderClosePrice for calculating the stops. I just realised that I was getting an error with OrderOpenPrice because I was running the EA on a trade that was a day old and the price action had moved beyond the stops. If you only run on new trades you can use openprice but if you...
Ignored
Thanks Kenny, go ahead to go to bed, have a good rest. Thanks again for spending your time to help me, awaiting for tomorrow's answer from you.
 
 
  • Post #9
  • Quote
  • May 12, 2021 12:58am May 12, 2021 12:58am
  •  kennyhubbard
  • Joined Sep 2008 | Status: Member | 354 Posts
chimoong,

I think you will find the rounding issue is because I used OrderClosePrice, so the TP & SL is off by the value of the spread. I reckon if you change to OpenPrice, the problem will go away. Try it and let me know.
 
 
  • Post #10
  • Quote
  • May 12, 2021 1:42am May 12, 2021 1:42am
  •  chimoong
  • | Joined May 2016 | Status: Member | 245 Posts
Quoting kennyhubbard
Disliked
chimoong, I think you will find the rounding issue is because I used OrderClosePrice, so the TP & SL is off by the value of the spread. I reckon if you change to OpenPrice, the problem will go away. Try it and let me know.
Ignored
Hi Kenny, sorry, I really do not understand what you meant and Don't know what to do. The main issue is the EA only place SL TP on EU pair but not the rest of the pairs.

Could you please help just to mod the EA on my first post ? Highly appreciated.
 
 
  • Post #11
  • Quote
  • May 12, 2021 2:18am May 12, 2021 2:18am
  •  kennyhubbard
  • Joined Sep 2008 | Status: Member | 354 Posts
Ok, here you go. I haven't tested it extensively but seems to work.

It will only set tp & sl of the symbol of the chart it is running on. If you want to it set the stops of other symbols, you must run it on those chart as well, so you will be running it on multiple chart, ie the 4 charts you mentioned in your first post.
Attached File(s)
File Type: mq4 SetSLTP EA.mq4   3 KB | 334 downloads
 
 
  • Post #12
  • Quote
  • Edited 3:36am May 12, 2021 2:49am | Edited 3:36am
  •  chimoong
  • | Joined May 2016 | Status: Member | 245 Posts
Quoting kennyhubbard
Disliked
Ok, here you go. I haven't tested it extensively but seems to work. It will only set tp & sl of the symbol of the chart it is running on. If you want to it set the stops of other symbols, you must run it on those chart as well, so you will be running it on multiple chart, ie the 4 charts you mentioned in your first post. {file}
Ignored
Hi Kenny, thanks and tested. I amended this tp_pnts = TPpip * Point * 10; (originally using SLpip).

1) Yes, the SL and TP levels are exactly 200pips and 1400 pips.

2) But the loop only assigned SL & TP for EU but totally ignored the rest of the pairs such as EJ, XAUUSD, BTCUSD, XAUUSD, AUDCAD. What I want the EA is to assign SL & TP for all the pairs of BUY and SELL trades. Could you please look into the coding of this problem ? What I want is the EA to run only on one chart and oversees the open trades of all the symbols.

Thanks.
Attached Image (click to enlarge)
Click to Enlarge

Name: aaa.png
Size: 49 KB
 
 
  • Post #13
  • Quote
  • May 12, 2021 3:57am May 12, 2021 3:57am
  •  kennyhubbard
  • Joined Sep 2008 | Status: Member | 354 Posts
Ok, try this
Attached File(s)
File Type: mq4 SetSLTP EA.mq4   3 KB | 559 downloads
 
1
  • Post #14
  • Quote
  • May 12, 2021 4:29am May 12, 2021 4:29am
  •  chimoong
  • | Joined May 2016 | Status: Member | 245 Posts
Quoting kennyhubbard
Disliked
Ok, try this {file}
Ignored
Hi Kenny, yes, it is working now by removed the codes in OnInit. This is the secret of coding that no way a beginner able to troubleshoot the problem

Let me study the rest of the codes to learn.
Attached Image (click to enlarge)
Click to Enlarge

Name: aaa.png
Size: 36 KB
 
 
  • Post #15
  • Quote
  • Last Post: Dec 23, 2022 2:30pm Dec 23, 2022 2:30pm
  •  sathishkbala
  • | Joined May 2021 | Status: Member | 15 Posts
Hi @kennyhubbard,
Greetings mate.! looks like you are a coder with good knowledge.. I m trying to code an EA for my strategy. Will you be able to help me out. Let me know, i will share you the strategy & the raw coding which i did myself.
many thanks in advance for your reply.
 
 
  • Platform Tech
  • /
  • EA to Set SL & TP of Open Trades Automatically
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About CC
  • Mission
  • Products
  • User Guide
  • Blog
  • Contact
CC Products
  • Forums
  • Calendar
  • News
  • Coins
  • Market
CC Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow CC
  • Facebook
  • Twitter

CC Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Forex Factory

Crypto Craft® is a brand of Fair Economy, Inc.

Terms of Service / ©2023