moving average trailing stoploss

 

hi,

just wondering if anyone can point me in the direction of how to go about making the trailing stoploss of my EA equal to a moving average value.

cheers

geoff

 


double TS_Factor = 2;
double TS_MA = iMA(NULL, 0, 3, 0, MODE_EMA, PRICE_CLOSE, 0);

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

if(TP_B == 0 && MS_B == false && OrderMagicNumber() == 1)
{
if(OrderStopLoss() < TS_MA - ATR * TS_Factor)
{
OrderModify(OrderTicket(), OrderOpenPrice(), TS_MA - ATR * TS_Factor, OrderTakeProfit(), 0, Green);
}
}

if(TP_S == 0 && MS_S == false && OrderMagicNumber() == 2)
{
if(OrderStopLoss() > TS_MA + ATR * TS_Factor)
{
OrderModify(OrderTicket(), OrderOpenPrice(), TS_MA + ATR * TS_Factor, OrderTakeProfit(), 0, Green);
}
}
}

u can modify

 
doshur wrote >>


double TS_Factor = 2;
double TS_MA = iMA(NULL, 0, 3, 0, MODE_EMA, PRICE_CLOSE, 0);

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

if(TP_B == 0 && MS_B == false && OrderMagicNumber() == 1)
{
if(OrderStopLoss() < TS_MA - ATR * TS_Factor)
{
OrderModify(OrderTicket(), OrderOpenPrice(), TS_MA - ATR * TS_Factor, OrderTakeProfit(), 0, Green);
}
}

if(TP_S == 0 && MS_S == false && OrderMagicNumber() == 2)
{
if(OrderStopLoss() > TS_MA + ATR * TS_Factor)
{
OrderModify(OrderTicket(), OrderOpenPrice(), TS_MA + ATR * TS_Factor, OrderTakeProfit(), 0, Green);
}
}
}

u can modify

hey thanks for that

cheers

 
Dua Yong Rew:


double TS_Factor = 2;
double TS_MA = iMA(NULL, 0, 3, 0, MODE_EMA, PRICE_CLOSE, 0);

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

if(TP_B == 0 && MS_B == false && OrderMagicNumber() == 1)
{
if(OrderStopLoss() < TS_MA - ATR * TS_Factor)
{
OrderModify(OrderTicket(), OrderOpenPrice(), TS_MA - ATR * TS_Factor, OrderTakeProfit(), 0, Green);
}
}

if(TP_S == 0 && MS_S == false && OrderMagicNumber() == 2)
{
if(OrderStopLoss() > TS_MA + ATR * TS_Factor)
{
OrderModify(OrderTicket(), OrderOpenPrice(), TS_MA + ATR * TS_Factor, OrderTakeProfit(), 0, Green);
}
}
}

u can modify

Hey, mind if you explain on this? i am struggling to understand this and adapt it into my program. Thank you so much in advance :)

 
Alster Low: explain on this? i am struggling to understand this
TS_MA - ATR * TS_Factor,

What needs explaining? TS = MA ± n × ATR

MT4: Learn to code it.
MT5: Begin learning to code it.
If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

Reason: