Download MetaTrader 5
To add comments, please log in or register
Having troubles understanding the code? Perhaps, you can find an article on that topic!
jonjonfx
10
jonjonfx 2014.05.29 04:00 
Hi Guys,

As new in learning mql4, i have manage to throw some basic codes and get started with my EA for a little while. Unfortunately, with limited knowledge of the available functions to use, stalled on some part of the codes logic. By the way, the part that i'm having difficulties with is the scaling/pyramiding logic/part of the strategy. Any help or inputs there on how to achieve the missing part of the logic would be greatly appreciated .

This is how it should work.

1. Open an initial trade when the condition is true , -lets focus on Long position for the meantime.
Condition: H4_SMA6 > H4_SMA25 && LongCount() ==0 ...no current open long position.

2. After opening an initial long position, check the profitability of that last and currently open position (Long).
If it gained a certain amount of pips, or a certain percentage of ATR (ex. 15% of ATR )
then open another Long Position, "adjust the lotsize by dividing by 2 and adjust stoploss too to minimize risk".

this will keep on iterating referencing the last opened Long position thus opening multiple Long positions without a take profit. It will only stop on Step 3.

3. When the last of the multiple long position has been stop via StopLoss (S/L). This will trigger to close all opened long position triggered on the step2 of the
strategy and doing a Take Profit.

Below is the code i've made. Please ask if need extra info and feel free to modify, comment, add etc. to the code. Hope to find answers from you. Thanks

//------------------------------------ --------------------------------+
#property copyright "EA"
#property link "https://www.mql5.com"
#property version "1.00"
#property strict

//+------------------------------------ ------------------------------+
//| External Parameter |
//+------------------------------------ ------------------------------+

extern double lotSize=0.08; //Lotsize
extern int Slippage=3; //Slippage
extern double sLoss=1000; // StopLoss
extern string Sym="EURUSD";
int result;

double H4_SMA6=iMA(Symbol(),240,6,0,0,0,0);
double H4_SMA25=iMA(Symbol(),240,25,0,0,0,0);
//+------------------------------------ ------------------------------+
//| Epert Advisor Start |
//+------------------------------------ ------------------------------+


int start()
  {

//Initial Long Entry
   if(H4_SMA6>H4_SMA25 && LongCount()==0)
      long_Entry();

// else if Call-> Profit_Check();

//else Call->Close_Position();

   return (0);
  }
//+------------------------------------ ------------------------------+
//| LONG_ENTRY |
//+------------------------------------ ------------------------------+

void long_Entry()
  {

   result=OrderSend(Sym,OP_BUY,lotSize,Ask,Sl ippage,0,0,"TR4 Profitability LONG",MagicNum,0,Green);

  }
//+------------------------------------ ------------------------------+
//| LAST LONG PROFITABILITY CHECK |
//+------------------------------------ ------------------------------+

//Profit_Check()

// Check Profitability of the last (LONG) currently open order vs ATR (need to get current profit of the order vs the openprice and compare it with ATR)

// If Last Long Order gained is > ATR*15%
//then Call Long Entry() ... but set lotSize/2 if it is not 0.01
//(so lotsize would be 0.08, then 0.04, then 0.02, then 0.01, then 0.01, then 0.01 etc...


//+------------------------------------ ------------------------------+
//| LONG POSITION COUNT |
//+------------------------------------ ------------------------------+

int LongCount()
  {

   int Long_Opened=0;
   bool oSelect;

   if(OrdersTotal()>0) 
     {

      for(int i=0; i<OrdersTotal(); i++)
        {
         oSelect=OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Sym) 
           {

            if(OrderType()==OP_BUY)
               Long_Opened+=1;

           }
        }

     }
   return(Long_Opened);
  }
//+------------------------------------ ------------------------------+
//| POSITION CLOSE FUNCTION |
//+------------------------------------ ------------------------------+
void Close_Position()
  {

//If the last order was stopped by StopLoss, the this function must be called to close and trigger all other opened Long order to close at once. thus taking profit, or maybe taking a loss .

   for(int i=1; i<=OrdersTotal(); i++) 
     {

      if(OrderSelect(i-1,SELECT_BY_POS)==TRUE) 
        {

         result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,Red);

        }
     }
  }
//+------------------------------------------------------------------+
ffoorr
983
ffoorr 2014.05.29 11:33  

Edit your post using SRC button please

Alain Verleyen
Moderator
28550
Alain Verleyen 2014.05.29 16:09  
jonjon:
Hi Guys,

As new in learning mql4, i have manage to throw some basic codes and get started with my EA for a little while. Unfortunately, with limited knowledge of the available functions to use, stalled on some part of the codes logic. By the way, the part that i'm having difficulties with is the scaling/pyramiding logic/part of the strategy. Any help or inputs there on how to achieve the missing part of the logic would be greatly appreciated .

This is how it should work.

1. Open an initial trade when the condition is true , -lets focus on Long position for the meantime.
Condition: H4_SMA6 > H4_SMA25 && LongCount() ==0 ...no current open long position.

2. After opening an initial long position, check the profitability of that last and currently open position (Long).
If it gained a certain amount of pips, or a certain percentage of ATR (ex. 15% of ATR )
then open another Long Position, "adjust the lotsize by dividing by 2 and adjust stoploss too to minimize risk".

this will keep on iterating referencing the last opened Long position thus opening multiple Long positions without a take profit. It will only stop on Step 3.

3. When the last of the multiple long position has been stop via StopLoss (S/L). This will trigger to close all opened long position triggered on the step2 of the
strategy and doing a Take Profit.

Below is the code i've made. Please ask if need extra info and feel free to modify, comment, add etc. to the code. Hope to find answers from you. Thanks



Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.

whroeder1
13609
whroeder1 2014.05.29 16:41  
Don't paste code
Play video
Please edit your post.
For large amounts of code, attach it.
/
To add comments, please log in or register