Forex Zone - Forex Forum

EA Code

Discussion started on EA Modifications

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
I don't which section to post this in. Hopefully this is the right one.
I am busy coding an EA (with my limited knowledge ;))
Here is one issue I can't seem to solve, obviously it works perfectly with the EA running on only 1 currency pair, but as soon as I drop it on another pair, I run into the following problems, I know it's just a stupid coding mistake on my side:
-------------------
I have set the maximum trades to 1, so it doesn't open
more than 1 buy or 1 sell.
This works fine, but if I drop the EA on a 2nd pair, it doesn't take trades, unless I change the 2nd MagicNumber to something different.
This is not a real issue, as I can just assign a new magic number to every currency pair. It would just be nice if I don't need 5 different magic numbers for the same EA on 5 pairs

The big issue I have is partial take profit.
It works fine on 1 pair, closes perfectly, BUT if I drop on pair number 2, it uses the 1st next tick to calculate the value the 2nd pair's value, and obviously it uses the wrong pair's value to partly close the other pair.


Code: [Select]
//  add your notes here ...
if ( ( Use_Partial_TP == true ))
{
SellPTP=OrderOpenPrice()-(Partial_TP* Point);
if ( ( OrderLots() == Lots ) || ( OrderLots() == _MMLots ))
{
if ( ( Ask <= SellPTP ))
{
for( int pos6030=0;pos6030<OrdersTotal();pos6030++)
{
if(OrderSelect(pos6030,SELECT_BY_POS)==false) continue;
if( ( OrderTicket() == _OrderTicketEA) )
{
double price6198=0;

price6198 = MarketInfo(OrderSymbol(), MODE_ASK);

_fnCloseSellPTP = OrderClose(  OrderTicket(), (OrderLots()/2), price6198, Slippage, White );
if( _fnCloseSellPTP == false )
Print("OrderClose error=", GetLastError());


Coding for buy is basically the same except Add to the open price, and close price is Bid, etc etc.
And it does this besides the fact that the same EA runs on both pairs, but different magic numbers, which is why I am totally stumped, I was under the impression if I assign different magics, it will solve this problem too.

Thanks for the help again.
Francois




Linkback: https://www.forex.zone/ea-modifications/17/ea-code/332/
#1 - October 22, 2018, 02:38:20 AM
« Last Edit: October 22, 2018, 01:45:21 PM by Mikser »

  • Full Member
  • Posts: 244
  • Points: 363
  • Likes Received: 140
  • Reputation: +16/-1
Hi, about

Code: [Select]
if(OrderSelect(pos6030,SELECT_BY_POS)==false) continue;
if( ( OrderTicket() == _OrderTicketEA) )


If you have already known the ticket of an order, then it makes no sense to seek the order by positions. Just use

Code: [Select]
if(!OrderSelect(_OrderTicketEA,SELECT_BY_TICKET)) continue;
if(OrderCloseTime()>0)continue;//if order is already closed.


If not then:

Code: [Select]
if(!OrderSelect(pos6030,SELECT_BY_POS)) continue;
  if(OrderSymbol()!=Symbol()) continue;
  if(OrderMagicNumber()!=YourMagic) continue;


Please try and let me know. Thanks
#2 - October 22, 2018, 02:01:02 PM

Administrator
  • Hero Member
  • Posts: 4990
  • Points: 29077
  • Likes Received: 5083
  • Reputation: +220/-14
I am sure both problems are related. If you haven't solved this yet, I would want to see the entire code. For example, I do not see how you are determining the value for the '_OrderTicketEA' variable. If you are not considering order symbol correctly then the wrong ticket could be assigned to this variable. 

Also, just a tip, but don't cycle through orders the way you do. It's better to do it like this:
Code: [Select]
for(int pos6030=OrdersTotal()-1;pos6030>=0;pos6030--)
#3 - October 22, 2018, 02:44:10 PM
EA Code in EA Modifications_FEN-Indicator-728x90

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
Thank you to both.
I will try all the options
#4 - October 22, 2018, 02:59:59 PM

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
I think it's better to post the code.
You will very likely not be happy with the way it is coded, as most of the coding is done by the wysiwyg software.
This is a template I am created to use the most rules I apply to a trade, ie, Use MoneyManagement (bool), Use Partial Take Profit (The BuyPTP and SellPTP), Use TradeTime(As you can see I only used the Hour()),
New Candle only trading.
The signal to enter a trade is just a plain 0==0, to just enter trades to see if the functions work.
Like I said, on 1 pair, it works fine, but add another, and all confusion starts.
 
#5 - October 22, 2018, 03:14:30 PM
Attachments:

Administrator
  • Hero Member
  • Posts: 4990
  • Points: 29077
  • Likes Received: 5083
  • Reputation: +220/-14
The reason it doesn't make any trades if you attach it to multiple charts unless you use a different magic number is because it is counting ALL trades with the same magic number in it's total counts.

Look at line 217:
Code: [Select]
if( OrderMagicNumber() == MagicNumber )

Replace this with the below and it should work:
Code: [Select]
if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
#6 - October 22, 2018, 08:38:01 PM

Administrator
  • Hero Member
  • Posts: 4990
  • Points: 29077
  • Likes Received: 5083
  • Reputation: +220/-14
Regarding the Partial TP problem, the code you posted before does not match the code in the EA you attached afterwards.

Here is the code from the EA you attached from line 1406:
Code: [Select]
if( ( OrderType() == OP_SELL) && ( OrderMagicNumber() == MagicNumber) && ( OrderSymbol() == Symbol()) )

In the code you posted before, it just has:
Code: [Select]
if( ( OrderTicket() == _OrderTicketEA) )

 There are other mismatches in the code, but this is the most important which is related to the problem. I think if you test the code in the EA attached, it will work better than the code that you posted before.
#7 - October 22, 2018, 08:55:42 PM

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
Thank you ONCE AGAIN for all the trouble.

I think it is sorted now.
The magic nr issue is definitely resolved.
The PTP, I think it is sorted, the market is just a bit slow now to know for sure, but it doesn't close "randomly" anymore, it is an issue of my OrderSelect function
#8 - October 22, 2018, 11:17:39 PM

  • Full Member
  • Posts: 244
  • Points: 363
  • Likes Received: 140
  • Reputation: +16/-1
I think if you test the code in the EA attached, it will work better than the code that you posted before.
Hi, I can't find it, please re-attach your code. Thanks
#9 - October 23, 2018, 04:05:59 AM

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
One more question please.
Do I need to assign a Partial TP (BuyPTP and SellPTP) Variabale to each pair?
I only use BuyPTP and SellPTP now, and  the EA assigns a value to them based on the first opened trade.  So if it opens a EURUSD trade, it writes the value, if it opens a GBPUSD trade after that, it doesn't get a new value for GBPUSD and assigns EURUSD's BuyPTP to GBPUSD as well.

#10 - October 23, 2018, 12:19:20 PM

Administrator
  • Hero Member
  • Posts: 4990
  • Points: 29077
  • Likes Received: 5083
  • Reputation: +220/-14
Do I need to assign a Partial TP (BuyPTP and SellPTP) Variabale to each pair?
No, I wouldn't do this.





I only use BuyPTP and SellPTP now, and  the EA assigns a value to them based on the first opened trade.  So if it opens a EURUSD trade, it writes the value, if it opens a GBPUSD trade after that, it doesn't get a new value for GBPUSD and assigns EURUSD's BuyPTP to GBPUSD as well.
This is the problem. The EA was not coded to recognize trades properly.

This is the code from your EA from lines 1353-1355:
Code: [Select]
_OrderSelectEA = OrderSelect(  0, _OrderTicketEA, 0 );
//  Returns ticket number for the currently selected order.//                          Note: The order must be previously selected by the OrderSelect() function.//                      //  Sample//  //                          if(OrderSelect(12, SELECT_BY_POS)==true)//                          order=OrderTicket();//                          else//                          Print("OrderSelect failed error code is",GetLastError());
_OrderTicketEA = OrderTicket();


I'm sorry to say, but this is totally not done right. First, you have a '0' for the first parameter in this function. Which means the selected order will be taken from the position in the order pool, but not by a ticket number. Yet, the 2nd parameter is supposed to be a ticket number based on the name. Also, the 3rd parameter is a '0' which means you will be selecting currently open orders, but this value is useless when selecting by ticket number. 

Also, you have '_OrderTicketEA' for the 2nd parameter, but the next line sets this variable with the ticket number from the order just selected. So you can basically just input a 0 for the 2nd parameter and it will accomplish the same thing. Actually, you can just have the EA pick any trade randomly and it will work just as well, because your EA is not filtering out any trades at all.

The reason I am mentioning the above lines of code is because later in the EA for the 'BuyPTP', it needs the correct information. 

From line 1564:
Code: [Select]
BuyPTP=_OrderOpenPriceEA+(Partial_TP* Point);

The root of your BuyPTP problem is that the EA is not getting the correct information for the open price to start with. The EA needs to select the correct order. So it needs to filter out trades that it shouldn't be considering. Once it selects the correct order then it will have the accurate information it needs for the rest of the code.
#11 - November 12, 2018, 01:58:29 PM

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
Thanks
I will add with the OrderSelect
if (OrderSymbol()==Symbol() && (OrderMagicNumber == Magic),
maybe that will solve the problem.
I do have issues with OrderSelect tho, maybe I don't understand it .
I attach a simple EA which I am testing the OrderSelect and OrderSend functions.
Here is what I want to do.
If there is a signal to trade (In this case I made a simple if 0==0) and there are no open orders, a trade must be placed. 
Now, if there is already a trade on the symbol from the EA, (so it must ignore manual trades or trades placed by another EA on the same symbol) 
(thus the OrderSelect fn, and the following if(!(OrderSymbol==Symbol && (OrderType == (OP_BUY) && (OrderMagicNumber==Magic) 
(In my small mind, it plays out, if there is not an open buy placed by this EA) it should not place an order, else it should place a trade(buy in this case)

Where am I missing what?
If I only let it Alert, it alerts fine, if there is an open order with this Magic number, it doesn't alert anything, and alerts when there is no open order from this EA, but if I unhash the OrderSend function, it opens a trade on every tick.
#12 - November 14, 2018, 02:48:28 PM
Attachments:

Administrator
  • Hero Member
  • Posts: 4990
  • Points: 29077
  • Likes Received: 5083
  • Reputation: +220/-14
There are a couple things I would do differently.

First, in line 47 you should have a '{' after the if statement. And then a matching close '}' later.

Also, you have this code:
Code: [Select]
if (!(OrderType()==OP_BUY) && (OrderSymbol()== Symbol())&& (OrderMagicNumber()==Magic))

If you want to alert if there is no trade from this EA open, it would be better to do it like this:
Code: [Select]
if (!(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic))
That way, all 3 conditions need to be false in order to alert and not just the 1st condition.

#13 - November 14, 2018, 08:18:55 PM

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
Thanks for that. It still opened trades for all the orders it selected which were not in the scope, so I created a for loop  to count open trades for the pair with the same direction and magic nr, and gave it a total order total, now I can actually choose how many trades I want open on that pair.

Now for the new question ;D

How do I make a variable static?

Code: [Select]
   double low2= iLow(NULL,0,iLowest(NULL,0,MODE_LOW,Nr_of_Bars_for_HiLo,0));

   double hi = iHigh(NULL,0,iHighest(NULL,0,MODE_HIGH,Nr_of_Bars_for_HiLo,0));

   double SLBuyDynamic = (low2-(MarketInfo(NULL,MODE_STOPLEVEL))*(MarketInfo(NULL,MODE_POINT)));

   double SLSellDynamic = (hi+(MarketInfo(NULL,MODE_STOPLEVEL))*(MarketInfo(NULL,MODE_POINT)));

   double TPBuy=((MarketInfo(NULL,MODE_BID)+(TakeProfit*(MarketInfo(NULL,MODE_POINT)))));

   double TPSell=((MarketInfo(NULL,MODE_BID)-(TakeProfit*(MarketInfo(NULL,MODE_POINT)))));

I use this to determine a "dynamic" SL.

As you can see, it takes the low of the last 10 bars (or whatever nr of bars one chooses in the inputs).
Now let's say it opens a buy trade, then the SL will be SLBuyDynamic, which obviously changes as the market goes up, now how do I set it to not go down again, but keep the highest price it had. (I am using this then as a kind of trailing stop)
Thanks again for the help
#14 - November 18, 2018, 02:21:56 AM
« Last Edit: November 18, 2018, 07:24:05 AM by Mikser »

  • Full Member
  • Posts: 244
  • Points: 363
  • Likes Received: 140
  • Reputation: +16/-1
If you use a not hidden SL then you can use the following in the TrailingStop function:


Code: [Select]
double SLBuyDynamic = (low2-(MarketInfo(NULL,MODE_STOPLEVEL))*(MarketInfo(NULL,MODE_POINT)));
           
            if(OrderType()==OP_BUY && OrderStopLoss()>SLBuyDynamic){
                 SLBuyDynamic = OrderStopLoss();
            }

 double SLSellDynamic = (hi+(MarketInfo(NULL,MODE_STOPLEVEL))*(MarketInfo(NULL,MODE_POINT)));
           
            if(OrderType()==OP_SELL && OrderStopLoss()>0 && OrderStopLoss()<SLSellDynamic){
                  SLSellDynamic = OrderStopLoss();
             }

#15 - November 18, 2018, 02:13:10 PM
« Last Edit: November 18, 2018, 02:16:06 PM by CrazyProg »

Members:

0 Members and 1 Guest are viewing this topic.