Forex Zone | Forex Forum

EA Code

Discussion started on EA Modifications

  • Posts: 352
  • Points: 71
  • Likes Received: 144
  • Reputation: +11/-0
EA Code in EA Modifications_xx
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();
             }
I actually want to use it as a hidden SL and OrderClose if bid > SLSellDymanic , or on a buy when bid< SLBuyDynamic
#16 - November 18, 2018, 05:04:44 PM

  • Posts: 230
  • Points: 330
  • Likes Received: 124
  • Reputation: +16/-1
EA Code in EA Modifications_xx
In this case, you can create 2 more global variables. But they will be reset to zero after restarting MT4.



Code: [Select]
double PrevSLBuyDynamic=0,PrevSLSellDynamic=0; // Global variables 

void VirtualTrail(){
  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)));

  if(PrevSLBuyDynamic>SLBuyDynamic){
      SLBuyDynamic=PrevSLBuyDynamic;
    }
  else{
      PrevSLBuyDynamic=SLBuyDynamic;
    }

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

  if(PrevSLSellDynamic>0 && PrevSLSellDynamic<SLSellDynamic){
      SLSellDynamic=PrevSLSellDynamic;
    }
  else{
      PrevSLSellDynamic=SLSellDynamic;
    }
//...
// your code
//...
}




#17 - November 18, 2018, 05:36:54 PM

  • Posts: 352
  • Points: 71
  • Likes Received: 144
  • Reputation: +11/-0
EA Code in EA Modifications_xx
Awesome. 
Thanks
#18 - November 18, 2018, 06:09:35 PM

  • Posts: 352
  • Points: 71
  • Likes Received: 144
  • Reputation: +11/-0
EA Code in EA Modifications_xx
And I am back with another coding question.

I managed to write an EA which takes trades based on the Advanced Currency Meter.

The attached code is just a small script I wrote to try and exclude certain symbols when the EA runs. (The code will obviously be incorporated in the EA, I just wrote a script to test)


Code: [Select]
extern string Symbols_To_Exclude = "GBPUSD,AUDUSD,USDJPY,GBPCHF";
enum currencies {Top6=5,TopRow=13,All=27};
input currencies Symbols_To_Trade = Top6;
string ccy;
string dir;

void OnStart()
 {  
  for (int c=0;c<=Symbols_To_Trade;c++)
   {
    ccy=ObjectGetString(0,"currency"+IntegerToString(c,1),OBJPROP_TEXT);//Symbol

    int d = (c+40);

    dir=ObjectGetString(0,"currency"+IntegerToString(d,1),OBJPROP_TEXT);//Direction

    for (int i=0; i<=70; i+=7)
     {
      string exclude =StringSubstr(Symbols_To_Exclude,i,StringLen(ccy));

      if (ccy== exclude)break;

      MessageBox ("Not excluded " +ccy);
     }   
   }
 }
 //+------------------------------------------------------------------+


As you can see I have a drop down menu for which symbol groups to trade, now I want to exclude certain symbols too.





It looks as if it works, but I excluded GBPUSD, AUDUSD and USDJPY as a test, when the message box pops up, it doesn't show GBPUSD, but it still shows USDJPY once, and AUDUSD twice, I understand it is because the loop runs, but if it is in an EA, it will actually take a trade on the excluded pairs, (or am I wrong, because in my mind, the OrderSend func will be where the MessageBox func is now, (after all the other expressions it had to pass) so the OrderSend function will be executed?)

Thanks again.

#19 - November 28, 2018, 08:43:42 PM
« Last Edit: November 28, 2018, 09:51:45 PM by Admin »

  • EA Code in EA Modifications_index
  • Posts: 3373
  • Points: 34386
  • Likes Received: 3043
  • Reputation: +172/-13
EA Code in EA Modifications_xx
Could you attach the indicator you are using with this so I can do a quick test?
#20 - November 29, 2018, 07:09:38 PM

  • Posts: 352
  • Points: 71
  • Likes Received: 144
  • Reputation: +11/-0
EA Code in EA Modifications_clip
Here it is
#21 - November 29, 2018, 08:18:25 PM
Attachments:

  • EA Code in EA Modifications_index
  • Posts: 3373
  • Points: 34386
  • Likes Received: 3043
  • Reputation: +172/-13
EA Code in EA Modifications_clip
Unfortunately, this indicator doesn't load for me. I'll look at the code and see if I can give you any advice.
#22 - November 29, 2018, 09:04:17 PM
Attachments:

  • EA Code in EA Modifications_index
  • Posts: 3373
  • Points: 34386
  • Likes Received: 3043
  • Reputation: +172/-13
EA Code in EA Modifications_xx
I tested the script and it doesn't work for me as it did for you. It just alerts for EURUSD over and over many times. No other symbols are excluded. I cannot test the rest of the code since the indicator doesn't work for me. For example, I can't see if the Symbols and Directions are being recognized correctly.

One thing that stands out to me though is that I would probably change "if (ccy== exclude)break;" to "if (ccy== exclude)continue;". So if an excluded symbol is reached then it will ignore that symbol but not stop the process entirely. So it can go on to check other valid symbols.
#23 - November 29, 2018, 09:19:17 PM

  • Posts: 352
  • Points: 71
  • Likes Received: 144
  • Reputation: +11/-0
EA Code in EA Modifications_clip
Here is another version of the indi, maybe it works better.

I tried the continue; option, but the it cycles through all the symbols every time the loop runs. (So it basically shows every pair, wether excluded or not, for 12 times if I choose i<=70)
#24 - November 29, 2018, 09:39:06 PM
Attachments:

  • EA Code in EA Modifications_index
  • Posts: 3373
  • Points: 34386
  • Likes Received: 3043
  • Reputation: +172/-13
EA Code in EA Modifications_xx
The indicator you just attached looks exactly the same as the previous indicator. It also doesn't work just like the last one too. I tried 2 different platforms. Please double check you are uploading the same one you are using the script with.

I cannot see what you are saying without testing with your indicator because the script reads objects created by the indicator. All the script did for me was give be a message for EURUSD over and over.
#25 - November 30, 2018, 12:32:12 AM

  • Posts: 352
  • Points: 71
  • Likes Received: 144
  • Reputation: +11/-0
EA Code in EA Modifications_xx
It is the same indicator I use on the charts, I have 2 versions which do the same job.
I will try and sort it out thanks.
#26 - November 30, 2018, 03:32:21 PM

  • Posts: 352
  • Points: 71
  • Likes Received: 144
  • Reputation: +11/-0
EA Code in EA Modifications_xx
Why do I get an "improper enumerator cannot be used" error?

This is the enumerator code :


Code: [Select]
// Indi Inputs & variable
extern int FastMAperiod=20;

enum mamethod {Simple=0,Exponential=1,Smoothed=2,Linear=3};

input mamethod FastMA_method=Simple;

extern int MidMAperiod=40;

input mamethod MidMA_method=Simple;

extern int SlowMAperiod=80;

input mamethod SlowMA_method=Simple;



And this is where they are called:


Code: [Select]
void OnTick()
  {
//Indicators
   FastMA = iMA(NULL,0,FastMAperiod,0,FastMA_method,0,1);

   MidMA = iMA(NULL,0,MidMAperiod,0,MidMA_method,0,1);

   SlowMA = iMA(NULL,0,SlowMAperiod,0,SlowMA_method,0,1);



#27 - December 05, 2018, 01:09:59 PM
« Last Edit: December 05, 2018, 01:37:15 PM by Mikser »

  • Posts: 230
  • Points: 330
  • Likes Received: 124
  • Reputation: +16/-1
EA Code in EA Modifications_xx
Hi, this parameter must be strictly an integer.

I propose two solutions.

#1
Code: [Select]
void OnTick()
  {
//Indicators

 FastMA = iMA(NULL,0,FastMAperiod,0,(int)FastMA_method,0,1);

 MidMA = iMA(NULL,0,MidMAperiod,0,(int)MidMA_method,0,1);

 SlowMA = iMA(NULL,0,SlowMAperiod,0,(int)SlowMA_method,0,1);
// ....
  }

#2
Code: [Select]
// Indi Inputs & variable
extern int FastMAperiod=20;

// enum mamethod {Simple=0,Exponential=1,Smoothed=2,Linear=3};

input ENUM_MA_METHOD FastMA_method=MODE_SMA;

extern int MidMAperiod=40;

input ENUM_MA_METHOD MidMA_method=MODE_SMA;

extern int SlowMAperiod=80;

input ENUM_MA_METHOD SlowMA_method=MODE_SMA;
#28 - December 05, 2018, 02:55:54 PM
« Last Edit: December 05, 2018, 02:57:35 PM by CrazyProg »

  • Posts: 352
  • Points: 71
  • Likes Received: 144
  • Reputation: +11/-0
EA Code in EA Modifications_xx
Thanks
Slowly but surely I am learning
#29 - December 05, 2018, 08:42:38 PM

  • Posts: 113
  • Points: 0
  • Likes Received: 20
  • Reputation: +0/-0
EA Code in EA Modifications_xx
I really appreciate you because you have shared this EA code so that a beginner like me can learn it, and maybe I will also try the EA code from you, because I think this is very good and useful
#30 - February 05, 2019, 06:22:31 AM

Members:

0 Members and 1 Guest are viewing this topic.


* Share Topic

Share via delicious Share via digg Share via facebook Share via linkedin Share via pinterest Share via reddit Share via stumble Share via tumblr Share via twitter

* Similar Topics

xx
need code done mq4

Started by rgmorris on EA Modifications

13 Replies
619 Views
Last post June 26, 2019, 07:35:58 AM
by zahro
clip
NEED HELP TO CODE NEW INDICATOR

Started by Gutis on New Indicator Coding

2 Replies
476 Views
Last post July 05, 2019, 09:05:41 AM
by urip
xx
CODE EXISTING EA

Started by milo1969 on New EA Coding

19 Replies
876 Views
Last post July 25, 2019, 08:25:10 AM
by gawe
clip
Need to code an EA named MKM_Pro_v1.0

Started by Mohanan Mavilakandy on New EA Coding

9 Replies
674 Views
Last post July 05, 2019, 09:27:56 AM
by gawe