HELP Coding Trailing Stop and Trade Up.

 

Hello, so my target is to create Trade Up with trailing stop of all open order either it OP_BUY or OP_SELL after several "Distance" of pips going profit.

So here is my code:


void TradeUpTrailing()

{

      double   LastBuyOrderPrice = 0.0;

      double   LastSellOrderPrice = 0.0;

      for(int i = (OrdersTotal()-1); i >= 0; i --)

      {

         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

         {

         if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)

           {

            double CurBuyOrderPrice = OrderOpenPrice();

           {    

            if(CurBuyOrderPrice > LastBuyOrderPrice)

               {

                 CurBuyOrderPrice = LastBuyOrderPrice;

                 LastBuyOrderPrice = OrderOpenPrice();

                 {

                  if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)

                  {

                    if (OrderStopLoss() <= Bid - (Trailing_Step*PipValue*Point+Trailing_Stop*PipValue*Point))

                    {

                     bool ret1 = OrderModify(OrderTicket(), OrderOpenPrice(), (Bid - Trailing_Stop*PipValue*Point), (Bid + TrailTP*PipValue*Point), 0, clrNONE);

                     if (ret1 == false)

                     Print ("TrailingBuy() error - ", ErrorDescription (GetLastError()));

                     {

                        if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1 && Bid - LastBuyOrderPrice >= Distance*PipValue*Point)

                        {

                          bool ret3 = OrderSend(Symbol(), OP_BUY, TrailLots, Bid, 4, (Bid - SLoss*PipValue*Point), (Bid + TProfit*PipValue*Point), "Alpha Up Buy", 1, 0, Blue);

                          if (ret3 == false)

                          Print ("TradeUpBuy() error - ", ErrorDescription (GetLastError()));

                        }

                     }

                    }

                   }

                  }

                 }

                }

              }

             }

         {

         if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

         {

         if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == 2)

           {

            double CurSellOrderPrice = OrderOpenPrice();

           {    

            if(CurSellOrderPrice > LastSellOrderPrice)

               {

                 CurSellOrderPrice = LastSellOrderPrice;

                 LastSellOrderPrice = OrderOpenPrice();

                 {

                  if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == 2)

                  {

                    if (OrderStopLoss() >= Ask + (Trailing_Step*PipValue*Point+Trailing_Stop*PipValue*Point))

                    {

                     bool ret2 = OrderModify(OrderTicket(), OrderOpenPrice(), (Ask + Trailing_Stop*PipValue*Point), (Ask - TrailTP*PipValue*Point), 0, clrNONE);

                     if (ret2 == false)

                     Print ("TrailingSell() error - ", ErrorDescription (GetLastError()));

                     {

                        if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == 2 && LastSellOrderPrice >= Ask + Distance*PipValue*Point)

                        {

                          bool ret4 = OrderSend(Symbol(), OP_SELL, TrailLots, Bid, 4, (Ask + SLoss*PipValue*Point), (Ask - TProfit*PipValue*Point), "Alpha Up Sell", 2, 0, Red);

                          if (ret4 == false)

                          Print ("TradeUpSell() error - ", ErrorDescription (GetLastError()));

                        }

                     }

                    }

                   }

                  }

                 }

                }

              }

             }

           }

         }    


I Attach of my backtest report using this code.

As you can see it only open 0.05 Lots position (meaning its only open the first position)

My Settings are:

First Open Position Lot is 0.05

After certain "Distance" traveled into a profit, a new order will be open with 0.10 Lot along with trailing stop to all open trade position.

Example:

Starting Lot: 0.05

SL: 500

Trail Lot: 0.10

Distance: 500

Trailing_Stop: 500

Trailing Gap: 500


So if the first position opened as OP_BUY at Symbol(XAUUSD) at Price 1800.00

the SL would be at 1795.00

and due to distance set to 500 point meaning a new Buy order will be opened if the Ask reach 1805.00 with SL at 1800.00 and at the same time the trailing will move the SL of the first order to 1800.00

if the price going up again to 1810.00 then a new Buy Order with 0.10 lot will be opened again with SL at 1805.00 and at the same time all SL of previous order will be move to 1805.00 by the trailing feature.


Is there something wrong with my code? Plz help I'am novice at programming languange. Thanks a lot!


Files:
Trail_Error.jpg  260 kb
 
As a sugestion to trace your code… Print something After every line of code in the for loop… After first line Print(“1”)… second line Print(“2”)… and so on… for the Sell side you cam Prnt a b c…. After every OrderOpen or OrderModify you print something also… just to SEE Where your code stops and why… 

Also…. Looking at your log…. Maybe you should reverse conditions as every single one of your entries are Stopped
 
Daniel Cioca #:
As a sugestion to trace your code… Print something After every line of code in the for loop… After first line Print(“1”)… second line Print(“2”)… and so on… for the Sell side you cam Prnt a b c…. After every OrderOpen or OrderModify you print something also… just to SEE Where your code stops and why… 

Also…. Looking at your log…. Maybe you should reverse conditions as every single one of your entries are Stopped

Looking at the journal it says OrderSend Error 130, also a lot of OrderModify Error - 130, Trailing Buy Error - Invalids Stops.

Means the trailing didn't work and so does the New Open Order.


And also this is my First open position of Buy's code:


void BuyOrder1()

{

    double SL1 = Ask - SLoss*PipValue*Point;

    if (SLoss == 0) SL1 = 0;

    double TP1 = Ask + TProfit*PipValue*Point;

    if (TProfit == 0) TP1 = 0;

    int ticket = -1;

    if (true)

    ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 4, SL1, TP1, "Alpha Buy", 1, 0, Blue);

    else

    ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 4, SL1, TP1, "Alpha Buy", 1, 0, Blue);

    if (ticket > -1)

    {

        if (true)

        {

            bool sel = OrderSelect(ticket, SELECT_BY_TICKET);

            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL1, TP1, 0, Blue);

            if (ret == false)

            Print("OrderModify() error - ", ErrorDescription(GetLastError()));

        }

            

    }

    else

    {

        Print("OrderSendBuy() error - ", ErrorDescription(GetLastError()));

    }

}

 
Fajar Sentoso #:

Looking at the journal it says OrderSend Error 130, also a lot of OrderModify Error - 130, Trailing Buy Error - Invalids Stops.

Means the trailing didn't work and so does the New Open Order.


And also this is my First open position of Buy's code:


void BuyOrder1()

{

    double SL1 = Ask - SLoss*PipValue*Point;

    if (SLoss == 0) SL1 = 0;

    double TP1 = Ask + TProfit*PipValue*Point;

    if (TProfit == 0) TP1 = 0;

    int ticket = -1;

    if (true)

    ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 4, SL1, TP1, "Alpha Buy", 1, 0, Blue);

    else

    ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 4, SL1, TP1, "Alpha Buy", 1, 0, Blue);

    if (ticket > -1)

    {

        if (true)

        {

            bool sel = OrderSelect(ticket, SELECT_BY_TICKET);

            bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL1, TP1, 0, Blue);

            if (ret == false)

            Print("OrderModify() error - ", ErrorDescription(GetLastError()));

        }

            

    }

    else

    {

        Print("OrderSendBuy() error - ", ErrorDescription(GetLastError()));

    }

}

 
In OrderSend() , StopLoss should be a price … and Normalize… 
 
Daniel Cioca #:
In OrderSend() , StopLoss should be a price … and Normalize… 

Can u please type the code for me? I'm not familiar using NormalizeDouble


I put Normalize Double and I got new error in compiling the code it says Wrong Parameters Count

 
Fajar Sentoso #:

Can u please type the code for me? I'm not familiar using NormalizeDouble


I put Normalize Double and I got new error in compiling the code it says Wrong Parameters Count

What is PipValue? 
 
Daniel Cioca #:
What is PipValue? 

PipValue is 1... here is the code:


double PipValue=1;    // this variable is here to support 5-digit brokers


Btw this is the code I need to make....

After OP_BUY (MagicNumber is 1) or OP_SELL (MagicNumber is 2) is opened...


Then I need to Select ALL OP_BUY and modify all the SL by the TrailSL variable...

Also if the OP_SELL is opened... I need to select ALL OP_SELL and modify all the OP_SELL's SL by the TrailSL variable.

What I need for this to work is to SELECT ALL the position Opened either it Buy or Sell using their each magic number and modify ALL the opened to Trailing SL.


After that, In seperate command... I need a code to:

Select LAST OP_BUY and after the last OP_BUY go into profit (decided using the pips traveled in the variable i called distance) then it need to Open 1 more OP_BUY with the same MagicNumber...

also for the OP_SELL is the same...


so this is what I think....


Void TrailAllBuyOrder()

double SL1= Ask-SLoss*PipValue*Point;

double TP1= Ask+TProfit*PipValue*Point;

for (int i=OrdersTotal()-1; i>= 0; i--)

if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES))

{

if (OrderType () == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)

{

OrderModify(ALL _TICKET_FOR _OP_BUY, OrderOpenPrice(), SL1, TP1, 0, clrNONE);

}

else

{

Print("TrailBuyError() error --", ErrorDescription (GetLastError()));

}

}



void TradeUpBuy()

double SL1= Ask-SLoss*PipValue*Point;

double TP1= Ask+TProfit*PipValue*Point;

double   LastBuyOrderPrice = 0.0;

double   LastSellOrderPrice = 0.0;

for(int i = (OrdersTotal()-1); i >= 0; i --)

{

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

         {

         if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)

           {

            double CurBuyOrderPrice = OrderOpenPrice();

           {    

            if(CurBuyOrderPrice > LastBuyOrderPrice)

               {

                 CurBuyOrderPrice = LastBuyOrderPrice;

                 LastBuyOrderPrice = OrderOpenPrice();

{

if(OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1 && (LastBuyOrderPrice+Distance*PipValue*Point)<= Distance*PipValue*Point)

{

OrderSend(Symbol(), OP_BUY, TrailLots, Bid, 4, SL1, TP1, "Trail Up Buy", 1, 0, Blue);

}

else

{

Print ("TradeUpBuy() error -", Errordescription (GetLastError()));

}

}

}

}

}


Please correct my code... I am a novice in Programming, still learning... I hope u understand what I'm trying to code here...

 
  1. Fajar Sentoso #:. I hope u understand

    This is an international English forum; Post in English.
              Please don't write ur - it's "you are" or "your" - MQL4 programming forum (2014)

  2. Please edit your posts and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  3. void TradeUpBuy()
    double SL1= Ask-SLoss*PipValue*Point;

    That's not how you define a function. Do not post code that will not even compile.

    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.

  4. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

 
So let me understand in 2 lines… because posting your code like that I cannot follow…. You want to open a trade based on a indicator signal … and then if price goes in your direction add on trades…. Correct? And then trail all opened orders… correct?