Beginner Problems!

 

Firstly let me start by saying I have no coding background (bar a small bit of HTML and CSS, not much use here). I have been trying unsuccessfully to code an EA for this strategy, using online guides and taking "inspiration" from other EA's. Needless to say, it hasn't worked very well. Before I go into the code I have Frankensteined together, let me say this. I am not here to ask you to give up your time for free to write me an EA, I'm interested in learning how to myself. Before anyone links to the MQL4 book tutorial, I am already working my way through it (albeit slowly).

When compiling my EA I get the "value of OrderSend should be checked" warning. I have been working on this problem using this guide, however, it usually takes me a lot of trial and error to get anything to work so this is still in the process. As there were no errors highlighted in the code, I tried to run a Strategy Test on it to see if it actually worked (also never tried the Strategy Tester before, bet you wish you hadn't started reading now). It seems to start fine, opening a new visual chart, however, it tends to stop after a while. I have checked that I have the requisite data needed to run the test so that isn't the issue. When the test is stopped manually, the chart is populated with the required MA's and Stochastic, I'm going to take this as a small win :D On the report etc. no traded were generated so there are no test results. Also, I'm using a 5 digit broker FYI.

I feel like I'm probably missing a lot of code, I thought this would be the best place to ask. Basically what I need is someone to point me in the right direction as I feel like I have hit a wall. Any input or help is greatly appreciated. Also, I'm aware my code probably looks like shit.

//--- input parameters
input int      FastMA=100;
input int      SlowMA=200;
input int      TakeProfit=2000;
input int      StopLoss=500;
extern double   LotSize=0.04;

double PreviousSlowMA = iMA (NULL, 0, SlowMA, 0, MODE_SMA, PRICE_CLOSE, 2);
double CurrentSlowMA = iMA (NULL, 0, SlowMA, 0, MODE_SMA, PRICE_CLOSE, 1);
double PreviousFastMA = iMA (NULL, 0, FastMA, 0, MODE_SMA, PRICE_CLOSE, 2);
double CurrentFastMA = iMA (NULL, 0, FastMA, 0, MODE_SMA, PRICE_CLOSE, 1);
double PreviousKline = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_MAIN, 2);
double CurrentKline = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
double PreviousDline = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 2);
double CurrentDline = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1);  
//---
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
   {
   
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      if(PreviousFastMA < PreviousSlowMA && CurrentFastMA > CurrentSlowMA)
      {
      if(PreviousKline < 20 && CurrentKline > 20)
      {
      if(PreviousDline < 20 && CurrentKline > 20)
      OrderSend(Symbol (), OP_BUY, LotSize, Ask , 50, Ask - (StopLoss*Point), Ask + (TakeProfit*Point), NULL, 0, 0, clrNONE);
      }}
 
      if(PreviousFastMA > PreviousSlowMA && CurrentFastMA < CurrentSlowMA)
      {
      if(PreviousKline > 80 && CurrentKline < 80)
      {
      if(PreviousDline > 80 && CurrentDline < 80)
      OrderSend(Symbol(), OP_SELL, LotSize, Bid, 50, Bid + (StopLoss*Point), Bid - (TakeProfit*Point), NULL, 0, 0, clrNONE);
      }}
   
  } 
 
UYPTrade:

Firstly let me start by saying I have no coding background (bar a small bit of HTML and CSS, not much use here). I have been trying unsuccessfully to code an EA for this strategy, using online guides and taking "inspiration" from other EA's. Needless to say, it hasn't worked very well. Before I go into the code I have Frankensteined together, let me say this. I am not here to ask you to give up your time for free to write me an EA, I'm interested in learning how to myself. Before anyone links to the MQL4 book tutorial, I am already working my way through it (albeit slowly).

When compiling my EA I get the "value of OrderSend should be checked" warning. I have been working on this problem using this guide, however, it usually takes me a lot of trial and error to get anything to work so this is still in the process. As there were no errors highlighted in the code, I tried to run a Strategy Test on it to see if it actually worked (also never tried the Strategy Tester before, bet you wish you hadn't started reading now). It seems to start fine, opening a new visual chart, however, it tends to stop after a while. I have checked that I have the requisite data needed to run the test so that isn't the issue. When the test is stopped manually, the chart is populated with the required MA's and Stochastic, I'm going to take this as a small win :D On the report etc. no traded were generated so there are no test results. Also, I'm using a 5 digit broker FYI.

I feel like I'm probably missing a lot of code, I thought this would be the best place to ask. Basically what I need is someone to point me in the right direction as I feel like I have hit a wall. Any input or help is greatly appreciated. Also, I'm aware my code probably looks like shit.


You input params should all use the same convention ie. input, not input and/or extern. Also your indicator code has to be inside of the OnTick function and not in the global scope. Finally, the Ordersend func returns a ticket number if successful. You need to check it somehow. ie. int tkt = OrderSend() or if(OrderSend(..)<0) error.


Try this instead.

//--- input parameters
input int      FastMA=100;
input int      SlowMA=200;
input int      TakeProfit=2000;
input int      StopLoss=500;
input double   LotSize=0.04;
 
int OnInit()
{
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{ 
}
//+------------------------------------------------------------------+
void OnTick()
{
   double PreviousSlowMA   = iMA (NULL, 0, SlowMA, 0, MODE_SMA, PRICE_CLOSE, 2);
   double CurrentSlowMA    = iMA (NULL, 0, SlowMA, 0, MODE_SMA, PRICE_CLOSE, 1);
   double PreviousFastMA   = iMA (NULL, 0, FastMA, 0, MODE_SMA, PRICE_CLOSE, 2);
   double CurrentFastMA    = iMA (NULL, 0, FastMA, 0, MODE_SMA, PRICE_CLOSE, 1);
   double PreviousKline    = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_MAIN, 2);
   double CurrentKline     = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
   double PreviousDline    = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 2);
   double CurrentDline     = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1);  
   
   if(PreviousFastMA < PreviousSlowMA && CurrentFastMA > CurrentSlowMA)
      if(PreviousKline < 20 && CurrentKline > 20)
         if(PreviousDline < 20 && CurrentKline > 20)
            if(OrderSend(Symbol(),OP_BUY,LotSize,Ask,50,Ask-StopLoss*_Point,Ask + TakeProfit*_Point) < 0)
               Print(__FUNCTION__+" OrderSendError: ",GetLastError());
   else
   if(PreviousFastMA > PreviousSlowMA && CurrentFastMA < CurrentSlowMA)
      if(PreviousKline > 80 && CurrentKline < 80)
         if(PreviousDline > 80 && CurrentDline < 80)
            if(OrderSend(Symbol(),OP_SELL,LotSize,Bid,50,Bid+StopLoss*_Point,Bid-TakeProfit*_Point) < 0)
               Print(__FUNCTION__+" OrderSendError: ",GetLastError()); 
} 
 
nicholishen:

You input params should all use the same convention ie. input, not input and/or extern. Also your indicator code has to be inside of the OnTick function and not in the global scope. Finally, the Ordersend func returns a ticket number if successful. You need to check it somehow. ie. int tkt = OrderSend() or if(OrderSend(..)<0) error.


Try this instead.


Thanks for the quick response Nicholishen. That has sorted out the OrderSend check perfectly. I think my code logic may actually be off but that is something I will fix shortly. Thanks again for your input, I was half expecting to be ridiculed :D

 

Hi guys, just going to give a quick update on my progress (may be of interest to other newbies), along with a cheeky question. Below is the code as it stands, I have made a few small changes from Nicholsen's code above. Firstly I removed the "Else" operator as it was causing the EA to only take long positions and ignore the short parameters located after the "Else". Not 100% on this but I believe it may have something to do with the semicolon treating the second set of rules as a second set rather than a part of a larger "if-else" rule. I may be completely wrong but that's my best guess. As I only want the EA to trade the first signal after the crossover, I have added in the OrdersTotal section and this is where my question comes in. Does this section imply that the EA will only open an order if there are none open across the platform, or will it open an order once there is no active order opened previously by this EA? Gut feeling is that I probably need to be more specific (i.e using MagicNumbers etc.) but I'm unsure.

Overall I'm happy with how the EA is progressing, thanks mainly to information found on this forum. The next step for me is adding a trailing stop mechanism and after that, I may incorporate an exit rule based on the MA's crossing back over, but that is for another progress post!

//---Input Parameters
input int      FastMA=100;
input int      SlowMA=200;
input int      TakeProfit=2000;
input int      StopLoss=500;
input double   LotSize=0.04;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
  
   if (OrdersTotal() > 0){
      return;
  }
   double PreviousSlowMA   = iMA (NULL, 0, SlowMA, 0, MODE_SMA, PRICE_CLOSE, 2);
   double CurrentSlowMA    = iMA (NULL, 0, SlowMA, 0, MODE_SMA, PRICE_CLOSE, 1);
   double PreviousFastMA   = iMA (NULL, 0, FastMA, 0, MODE_SMA, PRICE_CLOSE, 2);
   double CurrentFastMA    = iMA (NULL, 0, FastMA, 0, MODE_SMA, PRICE_CLOSE, 1);
   double PreviousKline    = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_MAIN, 2);
   double CurrentKline     = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
   double PreviousDline    = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 2);
   double CurrentDline     = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1);  
   

    if(CurrentFastMA > CurrentSlowMA)
         if(CurrentDline < CurrentKline && CurrentKline > 20)
            if(PreviousDline < 20 && CurrentDline > 20)
               if(OrderSend(Symbol(),OP_BUY,LotSize,Ask,50,Ask-StopLoss*_Point,Ask+TakeProfit*_Point, NULL, 111222) < 0)
                  Print(__FUNCTION__+" OrderSendError: ",GetLastError());
  
    if(CurrentFastMA < CurrentSlowMA)
         if(CurrentDline > CurrentKline && CurrentKline < 80)
            if(PreviousDline > 80 && CurrentDline < 80)
               if(OrderSend(Symbol(),OP_SELL,LotSize,Bid,50,Bid+StopLoss*_Point,Bid-TakeProfit*_Point, NULL, 111222) < 0)
                  Print(__FUNCTION__+" OrderSendError: ",GetLastError());
                  

//---
   
  }
 
UYPTrade:

Hi guys, just going to give a quick update on my progress (may be of interest to other newbies), along with a cheeky question. Below is the code as it stands, I have made a few small changes from Nicholsen's code above. Firstly I removed the "Else" operator as it was causing the EA to only take long positions and ignore the short parameters located after the "Else". Not 100% on this but I believe it may have something to do with the semicolon treating the second set of rules as a second set rather than a part of a larger "if-else" rule. I may be completely wrong but that's my best guess. As I only want the EA to trade the first signal after the crossover, I have added in the OrdersTotal section and this is where my question comes in. Does this section imply that the EA will only open an order if there are none open across the platform, or will it open an order once there is no active order opened previously by this EA? Gut feeling is that I probably need to be more specific (i.e using MagicNumbers etc.) but I'm unsure.

Overall I'm happy with how the EA is progressing, thanks mainly to information found on this forum. The next step for me is adding a trailing stop mechanism and after that, I may incorporate an exit rule based on the MA's crossing back over, but that is for another progress post!


Yes, you'll need to add some more logic to narrow down orders placed by this EA. Also, make sure to keep consistent with a code-style, especially your indent style! Stuff like this will cause you a slow death by debugging as your codebase grows.

void OnTick()
  {
  
   if (OrdersTotal() > 0){
      return;
  }

...and even the indentation space can throw you off...

 if(CurrentFastMA > CurrentSlowMA)
         if(CurrentDline < CurrentKline && CurrentKline > 20)
            if(PreviousDline < 20 && CurrentDline > 20)
               if(OrderSend(Symbol(),OP_BUY,LotSize,Ask,50,Ask-StopLoss*_Point,Ask+TakeProfit*_Point, NULL, 111222) < 0)
                  Print(__FUNC

If you like the GNU style which MQL has embraced (I personally hate it) you can also auto-format your code using the built in styler by going to Tools>Styler

Indent style - Wikipedia
Indent style - Wikipedia
  • en.wikipedia.org
Indenting is not a requirement of most programming languages, where it is used as secondary notation. Rather, indenting helps better convey the structure of a program to human readers. Especially, it is used to clarify the link between control flow constructs such as conditions or loops, and code contained within and outside of them. However...
 
nicholishen:

Yes, you'll need to add some more logic to narrow down orders placed by this EA. Also, make sure to keep consistent with a code-style, especially your indent style! Stuff like this will cause you a slow death by debugging as your codebase grows.

...and even the indentation space can throw you off...

If you like the GNU style which MQL has embraced (I personally hate it) you can also auto-format your code using the built in styler by going to Tools>Styler


Yeah, I had a feeling, more fun in store :) . I never really considered the impact of code-styling, I'll be sure to give the auto-format a go to start. Hopefully, as I get more familiar I can make a better fist of it myself. 

 

Hi guys, hope you all had a good weekend. Coming back to my order problems highlighted above, I have been trying to incorporate code found here into my EA. I'm getting hung up on a few things which definitely fall into the "Beginner Problems" category. When compiling the code I'm getting Undeclared Identifier errors for "n" and "total". Now, these questions are probably simple in the extreme. Is the "total" identifier not defined as being equal to OrdersTotal? Similarly below that "n" = 0, is this identified in an incorrect position? LongCountNew and ShortCountNew are also undefined, I presume they should be defined as 0? 


Seeing as I want the EA to only look for signals when there is no active order from this EA, is it possible to leave out the last "if" and "if-else" statement and put a return; if the MagicNumber equals the EA's magic number? I'm sorry for all the questions, I'm finding the answers difficult to find/understand in the MQL4 book.

#define MAGICNUMBER 111222

//---Input Parameters
input int      FastMA=100;
input int      SlowMA=200;
input int      TakeProfit=2000;
input int      StopLoss=500;
input double   LotSize=0.04;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

   if(OrdersTotal()>0)
      int total=OrdersTotal();
   for(n=0; n<total; n++)
     {
      if(!OrderSelect(n,SELECT_BY_POS,MODE_TRADES))
         continue;

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

      if(OrderMagicNumber()!=MAGICNUMBER)
         continue;
         
      if(OrderType()==OP_BUY)
         longCountNEW++;
         
      else if(OrderType()==OP_SELL)
         shortCountNEW++;
     }
   double PreviousSlowMA   = iMA (NULL, 0, SlowMA, 0, MODE_SMA, PRICE_CLOSE, 2);
   double CurrentSlowMA    = iMA (NULL, 0, SlowMA, 0, MODE_SMA, PRICE_CLOSE, 1);
   double PreviousFastMA   = iMA (NULL, 0, FastMA, 0, MODE_SMA, PRICE_CLOSE, 2);
   double CurrentFastMA    = iMA (NULL, 0, FastMA, 0, MODE_SMA, PRICE_CLOSE, 1);
   double PreviousKline    = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_MAIN, 2);
   double CurrentKline     = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
   double PreviousDline    = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 2);
   double CurrentDline     = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1);


   if(CurrentFastMA>CurrentSlowMA)
      if(CurrentDline<CurrentKline && CurrentKline>20)
         if(PreviousDline<20 && CurrentDline>20)
            if(OrderSend(Symbol(),OP_BUY,LotSize,Ask,50,Ask-StopLoss*_Point,Ask+TakeProfit*_Point,NULL,111222)<0)
               Print(__FUNCTION__+" OrderSendError: ",GetLastError());

   if(CurrentFastMA<CurrentSlowMA)
      if(CurrentDline>CurrentKline && CurrentKline<80)
         if(PreviousDline>80 && CurrentDline<80)
            if(OrderSend(Symbol(),OP_SELL,LotSize,Bid,50,Bid+StopLoss*_Point,Bid-TakeProfit*_Point,NULL,111222)<0)
               Print(__FUNCTION__+" OrderSendError: ",GetLastError());

//---

  }
//+------------------------------------------------------------------+
 
UYPTrade: is it possible to leave out the last "if" and "if-else" statement and put a return; if the MagicNumber equals the EA's magic number?
Of course "it's possible." What the code does is count non-pending orders (with that MN.) Then you ignore the result. If you replace the last two ifs with a return you do nothing if a open or pending order exists on any chart.
 
whroeder1:
Of course "it's possible." What the code does is count non-pending orders (with that MN.) Then you ignore the result. If you replace the last two ifs with a return you do nothing if a open or pending order exists on any chart.

So what I was trying to get at with the removal of the final if's is that if the order matches the MN the order criteria for the EA would not be run. So I was thinking something along the lines of the below. For the moment I will only be running this EA on a single pair on a single timeframe. Again, apologies for the dumb questions.

if(OrderMagicNumber() == MAGICNUMBER)
      return;
 

For any other newbies still interested in this thread I will provide a quick update. From the discussion here I have implemented code that will stop the EA from opening multiple orders from the same signal. The only change I made to the code found on that thread was to add an extra check for a Magic Number. The code is error free, although there are a few warnings I will need to look at.

#define MAGICNUMBER 111222

//---Input Parameters
input int      FastMA=100;
input int      SlowMA=200;
input int      TakeProfit=2000;
input int      StopLoss=500;
input double   LotSize=0.04;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int total,ord,i;

   int mn=MAGICNUMBER;

   string symbol;

   total=OrdersTotal();

   for(i=0;i<total;i++)

     {

      OrderSelect(i,SELECT_BY_POS);

      if(OrderSymbol()==Symbol() && OrderMagicNumber()==mn)ord++;

     }

   if(ord>0)
      return;

   double PreviousSlowMA   = iMA (NULL, 0, SlowMA, 0, MODE_SMA, PRICE_CLOSE, 1);
   double CurrentSlowMA    = iMA (NULL, 0, SlowMA, 0, MODE_SMA, PRICE_CLOSE, 0);
   double PreviousFastMA   = iMA (NULL, 0, FastMA, 0, MODE_SMA, PRICE_CLOSE, 1);
   double CurrentFastMA    = iMA (NULL, 0, FastMA, 0, MODE_SMA, PRICE_CLOSE, 0);
   double PreviousKline    = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
   double CurrentKline     = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_MAIN, 0);
   double PreviousDline    = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1);
   double CurrentDline     = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0);


   if(CurrentFastMA>CurrentSlowMA)
      if(CurrentDline<CurrentKline && CurrentKline>20)
         if(PreviousDline<20 && CurrentDline>20)
            if(OrderSend(Symbol(),OP_BUY,LotSize,Ask,50,Ask-StopLoss*_Point,Ask+TakeProfit*_Point,NULL,111222)<0)
               Print(__FUNCTION__+" OrderSendError: ",GetLastError());

   if(CurrentFastMA<CurrentSlowMA)
      if(CurrentDline>CurrentKline && CurrentKline<80)
         if(PreviousDline>80 && CurrentDline<80)
            if(OrderSend(Symbol(),OP_SELL,LotSize,Bid,50,Bid+StopLoss*_Point,Bid-TakeProfit*_Point,NULL,111222)<0)
               Print(__FUNCTION__+" OrderSendError: ",GetLastError());

//---

  }
//+------------------------------------------------------------------+

Next on the agenda is to add in a trailing stop to lock in profits along the way, hopefully, I will get this done soon. Although I am only on the "Variables" section of the MQL4 book, I have found it immensely helpful in understanding the structure of the MQL language. It can be a bit of a slog but stick with it!

 

Hi, guys, it has been a while! Between holidays and other work commitments I haven't had much time to spend on the EA, however, I have gotten a small bit done on it this week. I have added in the trailing atop (thanks to Phoenix). My next problem is an interesting one (I hope, I know how much you guys love interesting problems!). The basic premise of this EA is to open a trade when the stochastic pulls up from oversold for a long trade after the 100 SMA has crossed above the 200 SMA and the opposite for short positions. The EA opens trades nicely according to this rule, however, if a trade is stopped out or closed it will go ahead and open a new trade on the next stochastic pull-up. I require it to only trade once per SMA crossover.


My idea was to check the last closed order, and if it was a buy not open a trade if the 100 SMA is above the 200 SMA. Thanks to Whoreder1's code in this thread, I thought I had a solution as per the code below. This seems to half work, it will only open one order of each type. It seems to scan the whole order history for a trade matching the symbol and magic number as opposed to just the last trade. I know there is some difficulty in determining the last closed trade due to the way the order history works, but my knowledge of MQL4 isn't good enough yet to fix this problem. Does anyone have any ideas or suggestions? 

#define MAGICNUMBER 111222

//--- input parameters
input int      FastMA=100;
input int      SlowMA=200;
input int      TakeProfit=3000;
input int      StopLoss=1500;
input int      TrailingStop=1500;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int i=0;
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderMagicNumber()==MAGICNUMBER && OrderSymbol()==Symbol())
         if(OrderType()==OP_BUY)
           {
            if(((Bid-OrderOpenPrice())>(Point*TrailingStop)) && (OrderStopLoss()<(Bid-Point*TrailingStop)))
               OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),NULL,NULL);
           }
      else
         Print("OrderSelect() returned error - ",GetLastError());
      if(OrderType()==OP_SELL)
        {
         if(((OrderOpenPrice()-Ask)>(Point*TrailingStop)) && (OrderStopLoss()>(Ask+Point*TrailingStop)))
            OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),NULL,NULL);
        }
     }



     {
      int total,ord=0,a;

      int mn=MAGICNUMBER;

      string symbol;

      total=OrdersTotal();

      for(a=0;a<total;a++)

        {

         OrderSelect(a,SELECT_BY_POS);

         if(OrderSymbol()==Symbol() && OrderMagicNumber()==mn)ord++;

        }

      if(ord>0)
         return;      
        

   double CurrentSlowMA    = iMA (NULL, 0, SlowMA, 0, MODE_SMA, PRICE_CLOSE, 1);
   double CurrentFastMA    = iMA (NULL, 0, FastMA, 0, MODE_SMA, PRICE_CLOSE, 1);
   double PreviousKline    = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_MAIN, 2);
   double CurrentKline     = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_MAIN, 1);
   double PreviousDline    = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 2);
   double CurrentDline     = iStochastic (NULL, 0, 14, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 1);
   double Aa               = StopLoss*.1;
   double Bb               = AccountBalance()*.01;
   double LotSize          = Bb/Aa;
      
   
   for(int pos=OrdersHistoryTotal();pos >=0; pos--)
      if(OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)
         && OrderMagicNumber() == MAGICNUMBER
         && OrderSymbol() == Symbol()
         && OrderType() == OP_BUY
         && CurrentFastMA>CurrentSlowMA)
         return;
         
   for(int pos=OrdersHistoryTotal();pos >=0; pos--)      
      if(OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)
         && OrderMagicNumber() == MAGICNUMBER
         && OrderSymbol() == Symbol()
         && OrderType() == OP_SELL
         && CurrentFastMA<CurrentSlowMA)
         return;      
      
      
      if(CurrentFastMA>CurrentSlowMA)
         if(CurrentDline<CurrentKline && CurrentKline>25)
            if(PreviousDline<25 && CurrentDline>25)
               if(OrderSend(Symbol(),OP_BUY,LotSize,Ask,50,Ask-StopLoss*_Point,Ask+TakeProfit*_Point,NULL,111222)<0)
                  Print(__FUNCTION__+" OrderSendError: ",GetLastError());

      if(CurrentFastMA<CurrentSlowMA)
         if(CurrentDline>CurrentKline && CurrentKline<75)
            if(PreviousDline>75 && CurrentDline<75)
               if(OrderSend(Symbol(),OP_SELL,LotSize,Bid,50,Bid+StopLoss*_Point,Bid-TakeProfit*_Point,NULL,111222)<0)
                  Print(__FUNCTION__+" OrderSendError: ",GetLastError());

      //---
     }
  }
//+------------------------------------------------------------------+

Also, before anyone notices the LotSize calculation, this EA will be used with a Spread Betting provider, not a traditional contract-based provider, and so the LotSize is the actual monetary amount per pip required not contract size.


Thanks as always guys and gals.

Reason: