Need Help to Customizing an Order

 

Hello all coders here! I'm new and inexperienced on MQL4+MQL5 code. I was read the MQL4 books, and too often try many expert advisor. Now, I has my own strategy and I want to develop it with myself (The public if it really works and not for sale! I really hate to those who sell their expert advisers!). I had very basic C++ experience. Now the question is:

How to make a decision to buy or sell with many Indicators? I can only use one indicator like this sample (x mean extern parameter):

  1. if(iStochastic(NULL, 0, 14, 3, 3, 0, PRICE_CLOSE, MODE_MAIN, 0) > iStochastic(NULL, 0, 14, 3, 3, 0, PRICE_CLOSE, MODE_MAIN, 2))
    {
    OrderSend(Symbol(), OP_BUY, xOrderLots, Ask, xSlippage, Ask - xStopLoss * Point, Ask + xTakeProfit * Point, 0, xMagicNumbers, 0, Green);
    }
    if(iStochastic(NULL, 0, 14, 3, 3, 0, PRICE_CLOSE, MODE_MAIN, 0) < iStochastic(NULL, 0, 14, 3, 3, 0, PRICE_CLOSE, MODE_MAIN, 2))
    {
    OrderSend(Symbol(), OP_SELL, xOrderLots, Bid, xSlippage, Bid + xStopLoss * Point, Bid - xTakeProfit * Point, 0, xMagicNumbers, 0, Red);
    }
  2. Other question later, one by one, i want it works fine in my and other. :) Hope coders helps me out :D
 
LibertyCity:


How to make a decision to buy or sell with many Indicators?

How do you make a decision using more than 2 variables ? what decision do you want to make ?

Buy if a>b and b>c and c>d ?

if( a > b && b > c && c > d) 
   {
    // code to place a buy order
   }
 
LibertyCity:
How to make a decision to buy or sell with many Indicators? I can only use one indicator like this sample (x mean extern parameter):
  1. Don't write impenetrable code like that. Make it self documenting, make your conditions readable (in English):
    double stochCur = iStochastic(NULL, 0, 14, 3, 3, 0, PRICE_CLOSE, MODE_MAIN, 0),
           stochPrv = iStochastic(NULL, 0, 14, 3, 3, 0, PRICE_CLOSE, MODE_MAIN, 2);
    :
    double indi2 ...
    :
    bool   isStochUp= (stochCur > stochPrv), isStochDn = (!isStochUp);
    bool   isIndi2Up= (...),                 isIndi2Dn = (!isIndi2Up);
    
    if (isStochUp && isIndi2Up){
       OrderSend(Symbol(), OP_BUY, ...
    }
    if(isStochDn && isIndi2Dn){
       OrderSend(Symbol(), OP_SELL, ...
    }

  2. Always test return codes
    int ticket = OrderSend(...);
    if (ticket < 0) Alert("OrderSend failed: ",GetLastError());

  3. EA's must adjust TP, SL, AND slippage, from pips.
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

  4. On ECN brokers you must open first and THEN set stops
 

RaptorUK:

Buy if a>b and b>c and c>d ?


Hemmp ... I think if stochastic, moving average and MACD give signal to buy, until now i just know the MA. Now I can only write code for the moving average in order to provide a signal sir. So, it may like this (but i didn't know well for other indicators)

bool open;

int start()
{
MA();
MACD();
stoch();
if(MA == MACD && MACD == stoch && stoch == 0)
{
//buy code palced here
}
else if(MA == MACD && MACD == stoch && stoch == 1)
{
//sell code palced here
}
return(0);
}

bool MA()
{
if(((/*slow moving average indicator*/) - (/*fast moving average indicator*/)) >= 2 * Point) open == 1;
else if(((/*fast moving average indicator*/) - (/*slow moving average indicator*/)) >= 2 * Point) open == 0;
return(open);
}

bool MACD()
{
if((/*MACD indicator shift 0*/) > (/*MACD indicator shift 1*/)) open == 0;
else if ((/*MACD indicator shift 0*/) < (/*MACD indicator shift 1*/)) open == 1;
return(open);
}

bool stoch()
{
if((/*stochastic indicator below level 20*/)) open == 0;
else if ((/*stochastic above level 80*/)) open == 1;
return(open);
}


okay, may code looks messy, correct me if I'm wrong (later I'll typed this as CMIIW, hehehe) and I need 3 indicators to confirm that sell or buy (sure, the decision to buy or sell). Unfortunately, I only know how to write code for the moving average indicator. Any ideas? :)


int fast, slow;

fast = iMA(Symbol(), 0, 5, 0, 0, PRICE_CLOSE, 0);
slow = iMA(Symbol(), 0, 10, 0, 0, PRICE_CLOSE, 0);


Is the code of moving average indicator that I write is correct sir?

 
LibertyCity:
Hemmp ... I think if stochastic, moving average and MACD give signal to buy,
No they all return a value. YOU decide what value(s) give signals. Which is why I posted
if (isStochUp && isIndi2Up){
 
WHRoeder:
  1. Don't write impenetrable code like that. Make it self documenting, make your conditions readable (in English):

  2. Always test return codes
  3. EA's must adjust TP, SL, AND slippage, from pips.

  4. On ECN brokers you must open first and THEN set stops

  1. Okay, I'll try to documenting all conditions later, I'm new here, so I'm still need to be guided. :)
  2. Well, I'll give it a try, but my own code now is very-very simple and not the same as the strategy I would expect. It's why I go here to learn more about MQL.
  3. Wow! From where you know that eventually I would like to ask these questions? Interesting, but later I will describe further the strategies that I expected. Do you also trade futures or fx?
  4. Same as point 3 :)

I want to create my own expert advisor because there are many expert advisers that be sold on the internet, and from the results that they offer really interesting and I had bought it once and did not produce any profit, that's why I want to make it with a strategy that I have. Thanks for the help


WHRoeder:
No they all return a value. YOU decide what value(s) give signals. Which is why I posted

Is my temporary code above is right to give a signal?

bool open;

int start()
{
MA();
MACD();
stoch();
...
 
LibertyCity:

  1. Okay, I'll try to documenting all conditions later, I'm new here, so I'm still need to be guided. :)


We can give you help with coding here . . . if you want help with method or strategy you will need to look somewhere else . .
 
If you walk in to a giant cave and don't mark your turns ("I'll try to documenting all conditions later") how long until you are completely lost.
 
WHRoeder:
If you walk in to a giant cave and don't mark your turns ("I'll try to documenting all conditions later") how long until you are completely lost.

haha, not long as my strategy works fine ... okay ... first, how i make a code that will send order than modify the TP and SL ...
RaptorUK:
We can give you help with coding here . . . if you want help with method or strategy you will need to look somewhere else . .
no, i had the strategy and method and i'm here to learn the code
 
LibertyCity:

haha, not long as my strategy works fine ... okay ... first, how i make a code that will send order than modify the TP and SL

SendOrder with SL and TP set to 0, check that the order has worked and then do a OrderModify to set the TP and SL . . . again check that that has worked . . . if it fails I retry it a number of times, if it still fails I try to close the order. I report all these errors to screen using Label objects (so they stay in the same place on) and also send them via email . . .
 
RaptorUK:
SendOrder with SL and TP set to 0, check that the order has worked and then do a OrderModify to set the TP and SL . . . again check that that has worked . . . if it fails I retry it a number of times, if it still fails I try to close the order. I report all these errors to screen using Label objects (so they stay in the same place on) and also send them via email . . .

OrderSend(Symbol(), OP_BUY, xOrderLots, Ask, xSlippage, 0, 0, 0, xMagicNumbers, 0, Green);

this for sending an order without tp ans sl, am i right sir? then ...

RaptorUK:
check that the order has worked and then do a OrderModify to set the TP and SL

how I can check it sir? as I know the code was blind and don't know the order numbers that they send before (except some memory was allocated to store order numbers for this reason) then I'll wrote this code below to modify

OrderModify(OrderTicket(), OrderOpenPrice(), Ask - xStopLoss, Ask + xTakeProfit, 0, 0);
is the code right sir? how the program know the order ticket on first parameter array sir?
Reason: