Need internal Trailing Stop function to bypass broken limitation... - page 2

 

If you had bothered to check your return codes What are Function return values ? How do I use them ? - MQL4 forum you would know that there is no position -1

Also always count down Loops and Closing or Deleting Orders - MQL4 forum



 

Job was officially added in the system... Apply and ear some money... You can use the EA for yourself too... I don't care about autors right...

GAB

 
WHRoeder:

If you had bothered to check your return codes What are Function return values ? How do I use them ? - MQL4 forum you would know that there is no position -1

Also always count down Loops and Closing or Deleting Orders - MQL4 forum







Everywhere it says that it's better to start from -1... For the cout down, look like results are exactly the same...


Anyway, I only have 1 position oppened everytime so, CloseOrder order is not important for the moment...


My problem is that for an unknown reason, internal stoploss is never updated and my positions finish with physicals SL & TP I've put in case my system shut down!

GAB

 
excalibur7:

Unfortunatly, it's not the problem...


Stoploss = 0 is only to reset the stoploss value in case of CloseOrder...

If I don't reset the stoploss value, after closeorder, the next order, Bid price is always lower than stoploss and order close right after open and it close/open orders every second...

Maybe that the solution is a refresh rate, an array value or maybe I can use a custom indicator... I will try everything to find a solution to my problem!!!

GAB



You didn't get the point

In your if/else, the else will never be carried out because the condition for the if will always be true

if (TrailLevel > StopLoss)        //TrailLevel will always be bigger than StopLoss
    StopLoss = TrailLevel;        //Now StopLoss has a value

                                  //Conditions for if are met, so the else will be ignored
   else if (Bid <= StopLoss)
    {OrderClose(OrderTicket(), OrderLots(), Bid, 0, White);
 

I see, my StopLoss don't have any starting value so, it cause problems with the output!

I will work on it... if you want to work on the problem, I can pay you... I really want this virtual trailing stop loss to work!

100$ by Paypal if you are able to code it... maybe more if you impress me...

Thanks!

GAB

 
excalibur7:

I see, my StopLoss don't have any starting value so, it cause problems with the output!

I will work on it... if you want to work on the problem, I can pay you... I really want this virtual trailing stop loss to work!

100$ by Paypal if you are able to code it... maybe more if you impress me...

Thanks!

GAB


I am not an expert coder and certainly not advanced enough to charge money.

Your code should be relatively easy to modify though and I see much more difficult requests in Jobs where they are offering 10 to 20 for the work. 10 or 20 what, I don't know, assume $ but I haven't actually seen the currency defined anywhere.

For $100 I think that you'd get plenty of responses.

But your code needs more work.

Seems to me that you want a trailing stop of 50 points plus spread, I assume that equates to 5 pips.

You calculate the value of the TS, but you don't compare it to anything.

TrailLevel is calculated to be Bid minus 50 points minus spread

If you remove the else,

as StopLoss == TrailLevel, and TrailLevel has been calculated to be lower than Bid, Bid can never be <= StopLoss.

So the orderclose will never happen.

You don't check to see if it is a buy or sell trade

 
excalibur7:

Here's my complete script ; it's a simple code that open long transaction when direction of last 15 minutes open are UP, current open (1H) is greather than last 8 open and that short MA is over the long MA 4-8/1H!

. . .

The problem I have is that the server don't accept TakeProfits or StopLoss under 200Points...

If I put 200points as trailing stop, the Expert is not working anymore so, I need an internal trailing stop that working with 50points...

I think the following function will work:

int DynamicStop() {
   GetLastError();
   double TrailLevel = 0, cSpread = 0; 
   static double vStoploss[][2];
   bool TicketFound = false; int ARange = 0;
   for (int I = OrdersTotal() - 1; I >= 0; I--)
      if (OrderSelect(I, SELECT_BY_POS) && OrderSymbol() == Symbol()) {
         for (int i = 0; i < ArrayRange(vStoploss, 0); i++)
            if (OrderTicket() == NormalizeDouble(vStoploss[i][0], 0))
               { TicketFound = true; break; }
         if (!TicketFound) {
            ArrayResize(vStoploss, ArrayRange(vStoploss, 0)+1);
            i = ArrayRange(vStoploss, 0) - 1;
            vStoploss[i][0] = OrderTicket();
            vStoploss[i][1] = 0;
         }   
         if (OrderType() == OP_BUY) {
            cSpread = MarketInfo(OrderSymbol(),MODE_SPREAD);
            TrailLevel = NormalizeDouble(Ask - ((50+cSpread)*Point), Digits);
            if (TrailLevel > vStoploss[i][1] || vStoploss[i][1] == 0)
               vStoploss[i][1] = TrailLevel;
            if (Bid <= vStoploss[i][1]) {
               if (!OrderClose(OrderTicket(), OrderLots(), Bid, 0, White))
                  Print ("Order ", OrderTicket(), " failed to close.  Error: ", GetLastError());
               else {
                  ARange = ArrayRange(vStoploss, 0);
                  if (ARange - 1 > i) {
                     ArrayCopy(vStoploss, vStoploss, i*2, (i+1)*2, ((ARange - (i + 1)) * 2));
                     ArrayResize(vStoploss, ARange - 1);
                  }
                  else if (ARange - 1 == i)
                     ArrayResize(vStoploss, ARange - 1);
               }
            }
         }
         if (OrderType() == OP_SELL) {
            cSpread  = MarketInfo(OrderSymbol(),MODE_SPREAD);
            TrailLevel = NormalizeDouble(Bid + ((50+cSpread)*Point), Digits);
            if (TrailLevel < vStoploss[i][1] || vStoploss[i][1] == 0)
               vStoploss[i][1] = TrailLevel;
            if (Ask >= vStoploss[i][1]) {
               if (!OrderClose(OrderTicket(), OrderLots(), Ask, 0, White))
                  Print ("Order ", OrderTicket(), " failed to close.  Error: ", GetLastError());
               else {
                  ARange = ArrayRange(vStoploss, 0);
                  if (ARange - 1 > i) {
                     ArrayCopy(vStoploss, vStoploss, i*2, (i+1)*2, ((ARange - (i + 1)) * 2));
                     ArrayResize(vStoploss, ARange - 1);
                  }
                  else if (ARange - 1 == i)
                     ArrayResize(vStoploss, ARange - 1);
               }
            }
         }
      }
   return (0);  
}

I recommend (1) that each EA control only those orders for the symbol of the chart it is on (which is how it is setup now) and (2) that you use a magic number, so each EA only controls its own orders (I didn't put a magic number in the function since you didn't have one in your original post). Also remember: the variables will reset if the EA is re-initialized. To get around this, one may need to save the info to a file.

 
Thirteen:

I think the following function will work:

I recommend (1) that each EA control only those orders for the symbol of the chart it is on (which is how it is setup now) and (2) that you use a magic number, so each EA only controls its own orders (I didn't put a magic number in the function since you didn't have one in your original post). Also remember: the variables will reset if the EA is re-initialized. To get around this, one may need to save the info to a file.


WOW... array was one of the solution I was working on...

I'm not an expert programmer and this kind of function is hard to setup to give the exact output we need!

If I'm able to use what you sent to me in my EA, you win 50$.

If you want to work on it and make my EA work with a 50pips trailing stop with only operation on BUY, you win between 100-200$, 200$ if you really impress me!

Thanks!

GAB

 
Thirteen:

I think the following function will work:

I recommend (1) that each EA control only those orders for the symbol of the chart it is on (which is how it is setup now) and (2) that you use a magic number, so each EA only controls its own orders (I didn't put a magic number in the function since you didn't have one in your original post). Also remember: the variables will reset if the EA is re-initialized. To get around this, one may need to save the info to a file.


Wow... you're a king!

I've added this function instead of my old one and were on the right way...

The graphic is not the same as I was able to see with my first version of this EA that was setup with a 50Points Physical Trailing stop...

Unfortunatly I can't use it as it cause in real market, AUDCAD SL/TP have to be at least 200points from openprice...

I give you the code of this one, if you can make the same result with AUD/CAD from september 2012 to september 2013 :

//Expert property
double StopLossLevel, Spread, Lots, MinLots, MaxLots, TPoint;
int Ticket, Total, SDigits, TDSession;
//--------------
 
//Expert Initialisation
int start()
 {Total   = OrdersTotal();
  Spread  = MarketInfo(Symbol(),MODE_SPREAD);
  SDigits = MarketInfo(Symbol(),MODE_DIGITS);
  MinLots = MarketInfo(Symbol(),MODE_MINLOT);
  MaxLots = MarketInfo(Symbol(),MODE_MAXLOT);
  if (DayOfWeek()==0 || DayOfWeek()==6 || Hour()<10 || Hour()>20 || (DayOfWeek()==5 && Hour()>16))
   {TDSession = 0;
    Print ("Not in trading session");
    Sleep(30000);}
   else
    {TDSession = 1;
     Print ("Expert on duty hours");
     Sleep(3000);}
  ExpAlloc();
  TrailingStop();
  if (AccountFreeMargin() > (1000 * Lots) && Total == 0 && TDSession == 1) 
   TradeCheck();
return(0);}
//--------------

//Exponential Lots Allocation
int ExpAlloc()
 {Lots = AccountFreeMargin() / 100000 * 10;
  if (Symbol() == "XAGUSD" || Symbol() == "XAUUSD")
   {MinLots = 1;
    Lots = AccountFreeMargin() / 1700 * 10;}
    Lots = MathMin(MaxLots,MathMax(MinLots,Lots));
  if(MinLots < 0.1)
   Lots = NormalizeDouble(Lots,2);
   else
    {if(MinLots < 1)
     Lots = NormalizeDouble(Lots,1);
     else Lots=NormalizeDouble(Lots,0);}
  if(Lots < MinLots)Lots = MinLots;
  if(Lots > MaxLots)Lots = MaxLots;
return(0);}
//--------------

//Check For Trade Function
int TradeCheck()
 {double EMASHORT,EMALONG,COPEN,OPEN5M,OPEN10M,CPRICE,PAST1,PAST2,PAST3,PAST4,PAST5,PAST6,PAST7,PAST8;
  EMASHORT = iMA(NULL,PERIOD_H1,4,0,MODE_SMA,1,0);
  EMALONG  = iMA(NULL,PERIOD_H1,8,0,MODE_SMA,1,0);
  COPEN    = iOpen(NULL,PERIOD_M5,0);
  OPEN5M   = iOpen(NULL,PERIOD_M5,1);
  OPEN10M  = iOpen(NULL,PERIOD_M5,2);
  CPRICE   = iOpen(NULL,PERIOD_H1,0);
  PAST1    = iOpen(NULL,PERIOD_H1,1);
  PAST2    = iOpen(NULL,PERIOD_H1,2);
  PAST3    = iOpen(NULL,PERIOD_H1,3);
  PAST4    = iOpen(NULL,PERIOD_H1,4);
  PAST5    = iOpen(NULL,PERIOD_H1,5);
  PAST6    = iOpen(NULL,PERIOD_H1,6);
  PAST7    = iOpen(NULL,PERIOD_H1,7);
  PAST8    = iOpen(NULL,PERIOD_H1,8);
  if (EMASHORT >= EMALONG && COPEN >= OPEN5M && COPEN >= OPEN10M && CPRICE >= PAST1 && 
      CPRICE >= PAST2 && CPRICE >= PAST3 && CPRICE >= PAST4 && CPRICE >= PAST5 && CPRICE >= PAST6 &&
      CPRICE >= PAST7 && CPRICE >= PAST8)
   {OpenBuy();
    Print ("Buy Signal for : " + Symbol() + " -- Time Frame : " + Period());}
   else if (EMASHORT <= EMALONG && COPEN <= OPEN5M && COPEN <= OPEN10M && CPRICE <= PAST1 && 
      CPRICE <= PAST2 && CPRICE <= PAST3 && CPRICE <= PAST4 && CPRICE <= PAST5 && CPRICE <= PAST6 &&
      CPRICE <= PAST7 && CPRICE <= PAST8)
   {OpenSell();
    Print ("Sell Signal for : " + Symbol() + " -- Time Frame : " + Period());}
return(0);}   
//----------------   

//BUY Function
int OpenBuy()
 {StopLossLevel = Ask-(Point*(200+Spread));
  Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 0, StopLossLevel, 0, 0, DodgerBlue);
  if(Ticket > 0)
   {if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES))
    Print("BUY order opened : ", OrderOpenPrice());
    SendMail("[OPEN BUY Transaction Alert!]", "[Long Transaction Oppened For Symbol : " + Symbol() + " with " + OrderLots() + "Lots");}
return(0);}
//---------------

//SELL Function
int OpenSell()
 {StopLossLevel = Bid+(Point*(200+Spread));
  Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 0, StopLossLevel, 0, 0, DeepPink);
  if(Ticket > 0)
   {if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES))
    Print("SELL order opened : ", OrderOpenPrice());
    SendMail("[OPEN SELL Transaction Alert!]", "[Short Transaction Oppened For Symbol : " + Symbol() + " with " + OrderLots() + "Lots");}
return(0);}
//---------------

//Trailing Stop   
int TrailingStop()
 {double Trailing;
  for (int C = 0; C < Total; C ++) 
  {OrderSelect(C, SELECT_BY_POS, MODE_TRADES);
  if (OrderType() == OP_BUY)
   {Trailing = NormalizeDouble((Bid-(Point*(50+Spread))),Digits);
   if (OrderStopLoss() < Trailing)
    OrderModify(OrderTicket(), OrderOpenPrice(), Trailing, OrderTakeProfit(), 0, Yellow);}
  if (OrderType() == OP_SELL)
   {Trailing = NormalizeDouble((Ask+(Point*(50+Spread))),Digits);
   if (OrderStopLoss() > Trailing)
    OrderModify(OrderTicket(), OrderOpenPrice(), Trailing, OrderTakeProfit(), 0, Yellow);}}
return (0);}
//----------------

 

You delivered the job and won 50$! Give me your Paypal email address and I'll send you this amount as first part of the job.

If you are able to help me get the same result as I got with my first version of my EA, a big surprise will come in your Paypal!

My first version was working perfectly with 50point Physical trailing stop in test mode but was impossible in real due to broker limitation!

Check what it give with data from september 2012 to september 2013, AUDUSD, 1H range, Long positions only!

http://youtu.be/kA35UHhI9Bs