expert advisor - miscellaneous questions - page 8

 

Okay so if you use

int stoploss=50;
double stop;

stop=stoploss*Point();

Print(DoubleToString(stoploss));

By using the Point() function this should get you your value of stoploss and then you have to either add or subtract it from respectively Bid and Ask to give you the price level of your stop.

The same works for take profit but of course in reverse.

You can check some of the EA's in code base there are a lot of examples and variations on how coders do these things in their EA's.

For example heres one called Trailing Master https://www.mql5.com/en/code/16136

//-------------------------------------------------------------------------
// 1. Main function
//-------------------------------------------------------------------------
void OnTick(void)
  {
   Comment("Copyright © 2016, Il Anokhin\n"+TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));

//--- 1.1. Define pip -----------------------------------------------------
   if(Digits==4 || Digits<=2) pip=Point;
   if(Digits==5 || Digits==3) pip=Point*10;

//--- 1.2. Trailing -------------------------------------------------------
   for(i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderSymbol()==Symbol() && TS>0 && OrderProfit()>0)
           {
            if(UC==true && OrderComment()==Comm && UM==true && OrderMagicNumber()==Magic && OrderType()==OP_BUY && OrderOpenPrice()+TS*pip<=Bid && OrderStopLoss()<Bid-TS*pip) w=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TS*pip,OrderTakeProfit(),0);
            if(UC==true && OrderComment()==Comm && UM==true && OrderMagicNumber()==Magic && OrderType()==OP_SELL && OrderOpenPrice()-TS*pip>=Ask && (OrderStopLoss()>Ask+TS*pip || OrderStopLoss()==0)) w=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TS*pip,OrderTakeProfit(),0);
            if(UC==true && OrderComment()==Comm && UM==false && OrderType()==OP_BUY && OrderOpenPrice()+TS*pip<=Bid && OrderStopLoss()<Bid-TS*pip) w=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TS*pip,OrderTakeProfit(),0);
            if(UC==true && OrderComment()==Comm && UM==false && OrderType()==OP_SELL && OrderOpenPrice()-TS*pip>=Ask && (OrderStopLoss()>Ask+TS*pip || OrderStopLoss()==0)) w=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TS*pip,OrderTakeProfit(),0);
            if(UC==false && UM==true && OrderMagicNumber()==Magic && OrderType()==OP_BUY && OrderOpenPrice()+TS*pip<=Bid && OrderStopLoss()<Bid-TS*pip) w=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TS*pip,OrderTakeProfit(),0);
            if(UC==false && UM==true && OrderMagicNumber()==Magic && OrderType()==OP_SELL && OrderOpenPrice()-TS*pip>=Ask && (OrderStopLoss()>Ask+TS*pip || OrderStopLoss()==0)) w=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TS*pip,OrderTakeProfit(),0);
            if(UC==false && UM==false && OrderType()==OP_BUY && OrderOpenPrice()+TS*pip<=Bid && OrderStopLoss()<Bid-TS*pip) w=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TS*pip,OrderTakeProfit(),0);
            if(UC==false && UM==false && OrderType()==OP_SELL && OrderOpenPrice()-TS*pip>=Ask && (OrderStopLoss()>Ask+TS*pip || OrderStopLoss()==0)) w=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TS*pip,OrderTakeProfit(),0);
           }
        }
     }

//--- 1.3. End of main function -------------------------------------------

Where you can see the coder Il Anokhin fist defines pip.

But here is another example called E-smart trailing from Дима https://www.mql5.com/en/code/8674


//+------------------------------------------------------------------+

void ModifyStopLossInPoint(int stoploss)
{
bool   result;
double sl = 0;
double point = MarketInfo(OrderSymbol(),MODE_POINT);

if (OrderType() == OP_BUY) sl = OrderOpenPrice() + stoploss * point;
if (OrderType() == OP_SELL) sl = OrderOpenPrice() - stoploss * point;

result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);
if (result && UseSound) PlaySound(var_132);
}

//+------------------------------------------------------------------+

Notice the subtle differences in doing things.

I always try various things until i find something that works best.

Trailing Master
Trailing Master
  • votes: 11
  • 2016.10.27
  • Il Anokhin
  • www.mql5.com
The Expert Advisor that performs order trailing after reaching certain profit.
 
Marco vd Heijden:

Okay so if you use...

Omg, just quick check out amazing thanks for everything before I started reading and trying something. I will try that soon.

One more time huge thanks @Marco vd Heijden 

 
Marco vd Heijden:

The objects are stacked on top of each other so its important to let chartevent () know which clicks on what button or objects have the highest priority over other objects that might overlap.

Huge thanks Man, I liked your comments which one that are more clearly and more useful.

All the best to you.

 
Marco vd Heijden:

Okay so if you use

int stoploss=50;

I always try various things until i find something that works best.

I am just trying to understand your good comment and then write some good codes for my Orders, Stop Loss and Take Profit functions - thanks for that.

So, I already tried something like below code, and I need to be sure are both EURUSD and FB Stop Loss Value right, for 10 pips? ( if you want to know what I am thinking - just I think yes below logs / results both are right value for 10 pips. )
e.g: stoploss value = 10; Position - BUY
log: EURUSD | price (ask): 1.05633 / Stop Loss: 1.05533
log: FB         | price (ask): 119.69 / Stop Loss: 119.59

( I call below function by void OnChartevent() )

// this part of code from OnChartEvent()
//---
if ( sparam == _buy_name_btn )
{
    ObjectSetInteger( 0, sparam, OBJPROP_STATE, false );
    _Buy_calc_SL_TP();
    
    Print( " | Click Verify /   ");
}
//---
return;
void _Buy_calc_SL_TP()
{
    //---
    if ( _StopLoss_ON == true )
    {
      //_Sl = OrderOpenPrice() - _StopLoss *  _Pip;
        _Sl = Ask - _StopLoss *  _Pip;
        Print( " | Buy Calc SL TP Function / StopLoss: ", _Sl );
    }
    //---
    return;
}
 

Seems to be working.

 

I will try to involve Mr. William's comment to head of EA's code - thanks once again William.  Great Pip's Calculation MQL4
( please do not blame me because this code industry looks like very widely for me, and sometimes I loss my mind - anyway I hope I will finish my Trade Panel EA's codes soon )

And I will share my test EA's code after I finished it for is that code good / right or what? ( because I am not professional coder / programmer - I am working on it - )

 

Keith Watford:

Ok, fair enough. I can't be bothered to read some of your posts because it hurts my eyes.
If others feel the same, then you miss out, not me.

I did not know your this name. I just know you like a @Gumrai, so sorry I was little angry against your comment. So you helped me with this comment when my code knowledge under zero. There is no problem ignore all my comments, needs help or something else. But I just need to sorry for my angry answer to you.
 

Market is closed, and I am not sure anything else. But I tried something like below code, I feel I missed something but I can't find it.
( I would like to mention, my Trade Panel EA's have code's for Open Buy, and Open Sell positions, but I am trying to write it with my hand, that I am sure how this EA's works )

Thanks in advance for your help and advice.

#define CHANGE   double      ///< Difference of two prices.
#define POINT    int         ///< `CHANGE / _Point`.
#define PIP      double      ///< `POINT / PipsPerPOINT`.
        CHANGE   points_to_change(POINT  n){ return n * _Point                         ;}
        POINT    change_to_points(CHANGE c){ return POINT(c / _Point + 0.5)            ;}
        CHANGE   pips_to_change  (PIP    n){ return points_to_change(pips_to_points(n));}
        PIP      change_to_pips  (CHANGE c){ return points_to_pips(change_to_points(c));}
        POINT    pips_to_points  (PIP    n){ if( (_Digits&1) == 1)   n *= 10.0; return POINT(n);}
        PIP      points_to_pips  (POINT  n){ PIP p = n; if( (_Digits&1) == 1) p /= 10.0; return p;}

string prfx       = "test"        ;
string sellbutton = prfx + "sell" ;
string sounduse   = "tick.wav"    ;

int    stoploss   = 10    ;
int    takeprofit = 10    ;
int    magic      = 12345 ;
int    ticket     = 0     ;
int    slippage   = 3     ;

double lot = 0.01         ;
double sl                 ;

bool   stoploss_on        = true ;
bool   result                    ;
bool   sound_use          = true ;

//---
//---
int OnInit()
{
    ObjectCreate     ( 0, sellbutton, OBJ_BUTTON        , 0, 0, 0           );
    ObjectSetString  ( 0, sellbutton, OBJPROP_TEXT      , "Sell"            );
    ObjectSetInteger ( 0, sellbutton, OBJPROP_XDISTANCE , 20                );
    ObjectSetInteger ( 0, sellbutton, OBJPROP_YDISTANCE , 20                );
    ObjectSetInteger ( 0, sellbutton, OBJPROP_XSIZE     , 80                );
    ObjectSetInteger ( 0, sellbutton, OBJPROP_YSIZE     , 40                );
    ObjectSetInteger ( 0, sellbutton, OBJPROP_CORNER    , CORNER_LEFT_UPPER );
    ObjectSetInteger ( 0, sellbutton, OBJPROP_COLOR     , clrWhite          );
    ObjectSetInteger ( 0, sellbutton, OBJPROP_BGCOLOR   , clrMaroon         );
    ObjectSetInteger ( 0, sellbutton, OBJPROP_STATE     , false             );
    ObjectSetInteger ( 0, sellbutton, OBJPROP_ZORDER    , 1                 );

    Print( "ObjectsTotal: ", ObjectsTotal() );
    return(0);
}

//---
//---
void OnTick()
{
    //---

    //---
    return;
}

//---
//---
void OnDeinit(const int reason)
{
    //---
    ObjectsDeleteAll( 0, prfx );
    //---
    return;
}

//---
//---
void OnChartEvent(const int     id     ,
                  const long&   lparam ,
                  const double& dparam ,
                  const string& sparam
                  )
{
    // Sell
    if ( sparam == sellbutton )
    {
        ObjectSetInteger( 0, sparam, OBJPROP_STATE, false );
        ticket = Orderfuntions( sellbutton );
    }
    return;
}

//---
//---
void SellCalcSLTP()
{
    //---
    if ( stoploss_on == true )
    {
        if ( OrderType() == OP_SELL ) sl = OrderOpenPrice() - pips_to_change( stoploss );
        if ( OrderType() == OP_BUY  ) sl = OrderOpenPrice() + pips_to_change( stoploss );

        result = OrderModify( OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0, CLR_NONE );
        if ( result && sound_use ) PlaySound( sounduse );
    }
    //---
    return;
}

//---
//---
int Orderfuntions( string _Order_selector )
{
    // Sell Order
    SellCalcSLTP();
    if ( _Order_selector == sellbutton )
    {
        ticket = OrderSend( _Symbol, OP_SELL, lot, Bid, slippage, sl, takeprofit, "comment", magic, 0, clrMaroon );
    }
    return(ticket);
}
 
It looks okay, but you can not use ordermodify like that, you first have to select an order with OrderSelect() function.
 
Marco vd Heijden:
It looks okay, but you can not use ordermodify like that, you first have to select an order with OrderSelect() function.
Nice, I will try it soon. Thanks!
Reason: