Download MetaTrader 5

Trailing Stop

To add comments, please log in or register
Hennie Reyneke
1399
Hennie Reyneke  

Can someone please give me an example of how to use the MQL5 built-in class TrailingFixedPips ?

I tried the following:

#include <Trade\Trade.mqh>
#include <Expert\Expert.mqh>
#include <Expert\Trailing\TrailingFixedPips.mqh>

input string Expert_Title = "toets8";
ulong Expert_MagicNumber = 55555;
bool Expert_EveryTick = false;

input double Vol = 0.1;
//input double Stop = 300;
//input int Profit = 600;
input int Trailing_FixedPips_StopLevel = 50;
input int Trailing_FixedPips_ProfitLevel = 50;

// Global expert object
CExpert ExtExpert;

int OnInit() {

    // Initializing expert
    if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber)) {
        // failed
        printf(__FUNCTION__+": error initializing expert");
        ExtExpert.Deinit();
        return(INIT_FAILED);
    }
     
    // Creation of trailing object
    CTrailingFixedPips *trailing = new CTrailingFixedPips;
    if(trailing == NULL) {
        printf(__FUNCTION__ + ": error creating trailing");
        ExtExpert.Deinit();
        return(INIT_FAILED);
    }
    
    // Add trailing to expert (will be deleted automatically))
    if(!ExtExpert.InitTrailing(trailing)) {
        printf(__FUNCTION__ + ": error initializing trailing");
        ExtExpert.Deinit();
        return(INIT_FAILED);
    }
    
    // Set trailing parameters
    trailing.StopLevel(Trailing_FixedPips_StopLevel);
    trailing.ProfitLevel(Trailing_FixedPips_ProfitLevel);   
    
    return(INIT_SUCCEEDED);
}

void OnTick() {
    ExtExpert.OnTick();
        
    MqlTradeRequest myrequest = {0};
    MqlTradeResult myresult = {0};
    ZeroMemory(myrequest);
    myrequest.action = TRADE_ACTION_DEAL;
    myrequest.symbol = _Symbol;
    myrequest.volume = Vol;
    myrequest.type_filling = ORDER_FILLING_FOK;
    myrequest.deviation = 0;
    
    myrequest.type = ORDER_TYPE_BUY;
    myrequest.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
    //myrequest.sl = 0;
    //myrequest.tp = 0;
    OrderSend(myrequest, myresult);
}
efexbroker
6
efexbroker  
RoboSpider:

Can someone please give me an example of how to use the MQL5 built-in class TrailingFixedPips ?

I tried the following:

I would like to have an example too. A short and simple way of adding a trailing stop is needed. I use hedge-accounts so I want to also add the trailing stop to all the positions I open.


It seems to me that doing trailing.StopLevel(tsl_pips); is not enough.

So, how to properly put the trailing stop loss with this built in class?

To add comments, please log in or register