Someone please help me improve the functionality of my dynamic break even

 
So, I made a dynamic break even and it moves as the bid moves based on the difference in percentage and right now it is only for buy orders. Still implementing the sell one. However, it does not move as it is supposed to. If the stop loss is below the order open price, it will not move in increments, but when it finally modifies and moves in increments, it is above the order open price. 

Please tell me how to improve it and eliminate order 130. I have TradersWay for my broker, it is a 3/5 digit broker, there is no stop level. I had to put in a parameter for if the new stop loss is greater than old stop loss to prevent it to modify to lower value. I did forget to mention I have not run this on ticks in Strategy Tester, I do not have  the real tick data purchased from outside source yet, so I cannot conclude this to be a fully improper functional code but even with using control points or open prices, a program can still initiate to close to 70% efficiency.


I did update the code and clean it up a bit, I have not added the percentage part for sell yet.

enum Point_Type
  {
   Static_Points=1,//Points
   Percent=2 //Percent
  };

enum BE_Type
  {
   Regular =  1,//Regular Breakeven
   Dynamic = 2, //Dynamic Breakeven
  };
string Symb;
extern int Tester_Speed= 50;
extern int Magic_Number=051025;
extern int Slippage=3;

extern bool Use_Time_Filter = true;
extern int Begin_Trade_Hour = 1;
extern int End_Trade_Hour=23;

extern bool  Use_Spread_Filter=true;
extern int Spread=10;

extern double Lot_Size=0.01;
extern double Take_Profit=0.0;
extern double Stop_Loss=0.0;

extern bool Break_Even=true;
extern BE_Type Break_Even_Type=2;
extern int When_to_Move_to_Break_Even=10.0;
extern int Move_Break_Even=2;

int OnInit()
  {
//---
   if(Digits==3 || Digits==5)
     { vPoint=Point*10; vSlippage=Slippage*10; }
   else{ vPoint=Point; vSlippage=Slippage; }
   Symb=Symbol();
//---
   return(INIT_SUCCEEDED);
  }
void Move_to_Breakeven()
  {
   bool ans=false;
   int ticket=0;
   RefreshRates();

   double sl;

   if(OrdersTotal()>0)
     {
      for(int i=OrdersTotal() -1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
            if(OrderMagicNumber()==Magic_Number)
              {
               if(OrderSymbol()==Symb)
                 {
                  if(OrderType()==OP_BUY)
                    {
                     double stoploss=OrderStopLoss();
                     const double endpnt=OrderOpenPrice()+When_to_Move_to_Break_Even*vPoint;
                     if(Break_Even_Type==Dynamic)
                       {
                        sl=OrderStopLoss();
                        if(OrderStopLoss()!=0)
                          {
                           sl=OrderStopLoss();
                          }
                        if(sl==0.0)
                          {
                           sl=OrderOpenPrice()-10*vPoint;
                           ans=OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,clrNONE);
                          }
                        const double endpntBE=(OrderOpenPrice()+Move_Break_Even *vPoint);
                        if(Bid>OrderOpenPrice() && Bid<=endpnt)
                          {
                           const double priceDiff=NormalizeDouble((1.00 -((endpnt-Bid)/(When_to_Move_to_Break_Even*Point))),2);
                           //const double priceDiff=NormalizeDouble(((endpnt-Bid)/(When_to_Move_to_Break_Even*Point)),2);
                           const double priceDiff2=NormalizeDouble(((endpntBE-stoploss)*priceDiff),Digits);
                           sl=endpnt+priceDiff2;
                           // Comment(priceDiff, "\n", priceDiff2, "\n", sl);
                           //double trail_amount=(When_to_Move_Stop_Loss-Move_Break_Even)*vPoint;
                           //sl=Bid-trail_amount;
                           if(sl>stoploss)
                             {
                              //Comment(sl, "\n", stoploss);
                              //Print(sl);
                              //Print(stoploss);
                              ans=OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,clrNONE);
                             }
                          }
                       }
                     if(Bid>endpnt)
                       {
                        if(OrderStopLoss()==0.0 || OrderStopLoss()<OrderOpenPrice()+Move_Break_Even*vPoint)
                          {
                           sl = OrderOpenPrice() +(Move_Break_Even*vPoint);
                           ans=OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,clrNONE);
                          }
                       }
                    }
                 }
               if(OrderType()==OP_SELL)
                 {
                  const double endpnt=OrderOpenPrice()-When_to_Move_to_Break_Even*vPoint;
                  double stoploss=OrderStopLoss();
                  if(Break_Even_Type==Dynamic)
                    {
                     if(Ask<OrderOpenPrice() && Ask>endpnt)
                       {
                        double trail_amount=(When_to_Move_Stop_Loss-Move_Break_Even)*vPoint;
                        sl=Ask+trail_amount;
                        if(sl>stoploss)
                          {
                           ans=OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,clrNONE);
                          }
                       }
                    }
                  if(Ask<endpnt)
                    {
                     if(stoploss==0.0 || stoploss>endpnt)
                       {
                        sl=OrderOpenPrice()-(Move_Break_Even*vPoint);
                        ans=OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,clrNONE);
                       }
                    }
                 }
              }
           }
        }
     }
  }
 

 

Do not double post.

I have deleted your other topic.

 
senbonzuka:
So, I made a dynamic break even and it moves as the bid moves based on the difference in percentage and right now it is only for buy orders. Still implementing the sell one. However, it does not move as it is supposed to. If the stop loss is below the order open price, it will not move in increments, but when it finally modifies and moves in increments, it is above the order open price. 

Please tell me how to improve it and eliminate order 130. I have TradersWay for my broker, it is a 3/5 digit broker, there is no stop level. I had to put in a parameter for if the new stop loss is greater than old stop loss to prevent it to modify to lower value. I did forget to mention I have not run this on ticks in Strategy Tester, I do not have  the real tick data purchased from outside source yet, so I cannot conclude this to be a fully improper functional code but even with using control points or open prices, a program can still initiate to close to 70% efficiency.


I did update the code and clean it up a bit, I have not added the percentage part for sell yet.

 

error  order 130

Error 130 mean that your stop loss are too close of the Bid or Ask, if must be to a distance minimum of  MarketInfo(NULL,MODE_STOPLEVEL)*Point ;

 
ffoorr:

error  order 130

You put your code into en init() fonction, does it work in init() ?

You better put your code into a script, it would be read and execute only once

Error 130 mean that your stop loss are too close of the Bid or Ask, if must be to a distance minimum of STOP_LEVEL

Surprisingly, it does work from init function. I am still cleaning everything up. I am learning to not assign stoploss levels to a variable especially when comparing in a conditional parameter.

Examine things again but it tells me my stop level is always 0 
 
senbonzuka:
Surprisingly, it does work from init function. I am still cleaning everything up. I am learning to not assign stoploss levels to a variable especially when comparing in a conditional parameter.

Examine things again but it tells me my stop level is always 0 

No It was ok, it's not in the init();

you have to start whit something more simple and compréhensible, and clear the meaning ot

extern int When_to_Move_to_Break_Even=10.0;
extern int Move_Break_Even=2;


                  if(OrderType()==OP_BUY)
                    {
                     be_buy = OrderOpenPrice() + When_to_Move_to_Break_Even*vPoint; // 100 point
                     if(Bid > be_buy && (Bid - be_buy) > MarketInfo(NULL,MODE_STOPLEVEL)*Point )  
                       {
                        ans = false;
                        ans=OrderModify(OrderTicket(),OrderOpenPrice(),be_buy,OrderTakeProfit(),0,clrNONE);
                        if( ans == false ) Print("ERROR" );
                       }
                     }

I did not test this,

 
ffoorr:

No It was ok, it's not in the init();

you have to start whit something more simple and compréhensible, and clear the meaning ot


I did not test this,

So that's how I can compare the stop level. That's gives me something to work with and to be able to debug more.

Thank you very much.
 
So, I figured out a temporary solution. If it change the digit amount from  2 to 1 in my normalized double for the part that takes a percentage of the amount from priceDiff, it will modify completely as it is supposed to be and will produce less error 130. I guess the 2 digits is too much for it.