Help with Trailing Stop EA

 

So sorry to bother you.

I have never used an EA before.

I am trying to run a stop loss EA that I found on forex-tsd after being directed there from one of the posts on this site.

I "compiled" the program successfully and there appear to be no errors.

I dragged it onto my chart and the EA name and the "smiley face" appear in the top right corner of the chart.

The journal tab indicates that the EA was loaded successfully.

I tried to run the EA in demo and executed a trade while the EA appeared to be running. The trade went through but no stop loss line appeared.

Should a stop loss line appear on the chart if the EA is running after I enter an order? The order appears but no stop loss. I think that I must be doing something wrong. I have probably missed a step.

Would someone be willing to help me get the EA started?

This is where I found the program:

https://www.forex-tsd.com/expert-advisors-metatrader-4/4844-help-needed-making-perfect-trailing-stop-ea-2.html

Here is what it looks like:

//+------------------------------------------------------------------+
//| TrailingStop.mq4 |
//| Copyright © 2006, https://www.forex-tsd.com |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright © Steve Wilson - Forex TSD Forum"
#property link "https://www.forex-tsd.com"

//+--------------------------------------------------------------------+
// This trailing stoploss is made of features found in other Experts. |
// 1. Trailing Stop Loss which is chart specific |
// 2. AllPositions - Trailing Stop Loss which is account specific |
// Attach to any chart and it will modify stop loss on all trades. |
// 3. ProfitTrailing - Modify only trades that are in profit |
// 4. Hedge Option - Does not open positions but will close all trades |
// if a specified target is reached on your trading account. |
// Activate it by giving a value to ProfitTarget. Disables stop |
// loss by setting value to 5000 |
//+--------------------------------------------------------------------+

extern bool AllPositions = false; // (True To modify all positions ) False(modify chart positions)
extern bool ProfitTrailing = True; //(To Trail only postions on profit not loss )
extern int TrailingStop=5; // Minimum is 5
extern int ProfitTarget=0; // Setting this value will calculate all open trades and close if profit reached.
extern int TrailingStep = 1;
extern bool UseSound = True;
extern string NameFileSound = "expert.wav";

bool runnable=true;
bool init=true;
bool result;
double pBid, pAsk, pp;
int i=0;

datetime timeprev=0;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{

if (ProfitTarget>0)
{

TrailingStop=0;
ChartComment();
ClearStops();
CloseAllTrades();

}

if (AllPositions == false){
//+------------------------------------------------------------------+
//| Expert start function - Chart Specific |
//+------------------------------------------------------------------+

// Clean the chart
if (ProfitTarget==0)
CleanChart();


//Trailing Stop
TrailingAlls(TrailingStop);

//Close/Open
if(timeprev==Time[0])
return(0);
timeprev=Time[0];

return(0);
}
else
{
//+------------------------------------------------------------------+
//| Expert Start Function - All Positions |
//+------------------------------------------------------------------+

// Clean the chart
if (ProfitTarget==0)
CleanChart();

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

void TrailingAlls(int trail)
{
if (trail==0)
return;

double stopcrnt;
double stopcal;

int trade;
int trades=OrdersTotal();

for(trade=0;trade<trades;trade++)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()!=Symbol())
continue;

pp = MarketInfo(OrderSymbol(), MODE_POINT);


//Long

if(OrderType()==OP_BUY)
{

pBid = MarketInfo(OrderSymbol(), MODE_BID);
if (!ProfitTrailing || (pBid-OrderOpenPrice())>TrailingStop*pp) {
if (OrderStopLoss()<pBid-(TrailingStop+TrailingStep-1)*pp) {

stopcrnt=OrderStopLoss();
stopcal=Bid-(trail*Point);
if(stopcrnt==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);
}
else
{
if(stopcal>stopcrnt)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);
}
}
}

return;
}
}//Long

//Shrt
if(OrderType()==OP_SELL)
{


pAsk = MarketInfo(OrderSymbol(), MODE_ASK);
if (!ProfitTrailing || OrderOpenPrice()-pAsk>TrailingStop*pp) {
if (OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0) {

stopcrnt=OrderStopLoss();
stopcal=Ask+(trail*Point);
if(stopcrnt==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);
}
else
{
if(stopcal<stopcrnt)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);
}
}

}
return;
}
}//Shrt

}//for

}


//+------------------------------------------------------------------+
//| Functions for All Positions |
//+------------------------------------------------------------------+
void TrailingPositions() {
// double pBid, pAsk, pp;

pp = MarketInfo(OrderSymbol(), MODE_POINT);
if (OrderType()==OP_BUY) {
pBid = MarketInfo(OrderSymbol(), MODE_BID);
if (!ProfitTrailing || (pBid-OrderOpenPrice())>TrailingStop*pp) {
if (OrderStopLoss()<pBid-(TrailingStop+TrailingStep-1)*pp) {
ModifyStopLoss(pBid-TrailingStop*pp);
return;
}
}
}
if (OrderType()==OP_SELL) {
pAsk = MarketInfo(OrderSymbol(), MODE_ASK);
if (!ProfitTrailing || OrderOpenPrice()-pAsk>TrailingStop*pp) {
if (OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0) {
ModifyStopLoss(pAsk+TrailingStop*pp);
return;
}
}
}
}

//+------------------------------------------------------------------+
//| Modify StopLoss |
//+------------------------------------------------------------------+
void ModifyStopLoss(double ldStopLoss) {
bool fm;

fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
if (fm && UseSound) PlaySound(NameFileSound);
}
//+------------------------------------------------------------------+


void ChartComment()
{
string sComment = "";
string sp = "****************************\n";
string NL = "\n";

sComment = NL + sp;
sComment = sComment + "Current Profits (USD) = $" + DoubleToStr(AccountProfit(),2) + NL;
sComment = sComment + "Profit Target (USD) = $" + ProfitTarget + NL;
sComment = sComment + "Open Trades = " + OrdersTotal() + NL;
sComment = sComment + "Stop Loss Disabled = ("+ TrailingStop+")" + NL;
sComment = sComment + NL + sp;

Comment(sComment);
}

void CleanChart()
{
string sComment = "";
string sp = "****************************\n";
string NL = "\n";

sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
Comment(sComment);
}

// Close all open trades when in profit
void CloseAllTrades(){
if (AccountProfit() >= ProfitTarget){
for (i=OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if (OrderType()==OP_SELL)result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,CLR_NONE);
if(result!=TRUE) Print("LastError = ", GetLastError());
if (OrderType()==OP_BUY)result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,CLR_NONE);
if(result!=TRUE) Print("LastError = ", GetLastError());
if (OrderType()==OP_BUYSTOP)result=OrderDelete(OrderTicket());
if(result!=TRUE) Print("LastError = ", GetLastError());
if (OrderType()==OP_SELLSTOP)result=OrderDelete(OrderTicket());
if(result!=TRUE) Print("LastError = ", GetLastError());

}
else Print( "Error when order select ", GetLastError());
}
}
}

// Function to clear all existing Stop Losses
void ClearStops(){
for (int i=0;i<OrdersTotal();i++){
if (OrderSelect(i,SELECT_BY_POS)){
OrderSelect(i,SELECT_BY_POS);
OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderTakeProfit(),OrderExpiration(),Brown);
}
else Print( "Error when clearing Stops ", GetLastError());
}
return(0);

}

 

Hi,

I can't help you fix yours but I found a great little very reasonably priced and flexible and effective commercial utility that has a 2 stage Trailing Stop Loss that you can use either on its own or in conjunction with other AEs (it overrides the TSL in other AEs). It is from PipBoxer.Com, look for PBTS.

Good luck with it.

…………………(8 >) Prosperous Trading (< 8)

DougRH4x

.

PS: I’m looking for someone to splice an adjustable (Trailing &) Stop Loss into the Martingale based program: PipMaker. Overcoming this one downfall of this program will make it VERY profitable!

 

hi breman,


Just go to Terminal window, right click on your order, select Trailing Stop in the context menu, then you can choose 15 pips as minimum of forex or 200 points for Gold.


Hope it help

 
breman wrote >>

So sorry to bother you.

I have never used an EA before.

I am trying to run a stop loss EA that I found on forex-tsd after being directed there from one of the posts on this site.

I "compiled" the program successfully and there appear to be no errors.

I dragged it onto my chart and the EA name and the "smiley face" appear in the top right corner of the chart.

The journal tab indicates that the EA was loaded successfully.

I tried to run the EA in demo and executed a trade while the EA appeared to be running. The trade went through but no stop loss line appeared.

Should a stop loss line appear on the chart if the EA is running after I enter an order? The order appears but no stop loss. I think that I must be doing something wrong. I have probably missed a step.

Would someone be willing to help me get the EA started?

Hi

if you using Trailing stop only, you will see trailing stop line only after the price goes into your favor.

if in case BUY, the trailing stop line appears after the price goes up over the open price, if the price not go over opened price, you will not get the trailing stop in action.

and vice versa for Sell orders.

 
samirgham wrote >>

Hi

if you using Trailing stop only, you will see trailing stop line only after the price goes into your favor.

if in case BUY, the trailing stop line appears after the price goes up over the open price, if the price not go over opened price, you will not get the trailing stop in action.

and vice versa for Sell orders.

to add something

if you use stop loss with trailing stop, you should see the stoploss line, but will not trail if price moved against your order.

Reason: