stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Help adding trail only in profit
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4870
Page 1 of 1
Author:  steven [ Sat Aug 20, 2016 9:28 am ]
Post subject:  Help adding trail only in profit

I want to make a candle trailing stop ea , and i want to add one more option "Trail Only In Profit" .
For example if i set candle back 4, when current candle 5 have create then stop loss only start move to the candle i set high or low .
And one more problem , is the ea only trail on buy but not trail on sell .
I cant find the problem .
Any one can giving a helping hand ?
Thank You .

Code: Select all

  int BuyStopCandle  = iLowest(NULL,0,MODE_LOW,CandlesBack,0);
 int SellStopCandle = iHighest(NULL,0,MODE_HIGH,CandlesBack,0);
 int err;
 
      //
         
         int    digits     = MarketInfo(OrderSymbol(),MODE_DIGITS);
         double point      = MarketInfo(OrderSymbol(),MODE_POINT);
         double PointRatio = 1;
               if (digits==3 || digits==5) PointRatio = 10;
               
 for(int b = OrdersTotal()-1; b>=0; b--)
 {
  MyResult = OrderSelect(b,SELECT_BY_POS,MODE_TRADES);
    if(OrderMagicNumber() == Magic)
      if(OrderSymbol()!=Symbol())               continue;
        if(OrderType() == OP_BUY)
          RefreshRates();
          
          if(OrderStopLoss()<Low[BuyStopCandle]-PipAmountsForTrailing*point)
             MyResult = OrderModify(OrderTicket(),OrderOpenPrice(),Low[BuyStopCandle]-PipAmountsForTrailing*point,OrderTakeProfit(),0,CLR_NONE);
              err=GetLastError();
               if(err==4 || err==136 || err==137 || err==138 || err==146)
               {
                  RefreshRates();
                  continue;
               }
            break; 
         }                     
 
 for(int s = OrdersTotal()-1; s>=0; s--)
 {
  if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
    if(OrderMagicNumber()== Magic)
      if(OrderSymbol()!=Symbol())               continue;
        if(OrderType() == OP_SELL)
          RefreshRates();
          
          if(OrderStopLoss()>High[SellStopCandle]+PipAmountsForTrailing*point)
             MyResult = OrderModify(OrderTicket(),OrderOpenPrice(),High[SellStopCandle]+PipAmountsForTrailing*point,OrderTakeProfit(),0,CLR_NONE);         
              err=GetLastError();
               if(err==4 || err==136 || err==137 || err==138 || err==146)
               {
                  RefreshRates();
                  continue;
               }
   }
} 
Author:  ixbone [ Sat Sep 03, 2016 1:24 pm ]
Post subject:  Help adding trail only in profit

for my understanding, a trailingprofit should work always without any rules, if in profit, trail it, because trailing based on hard settings is diametral to an dynamic trailingprofit...dynamic market needs dynamic rules...
my 2c
Author:  steven [ Sun Oct 02, 2016 9:56 am ]
Post subject:  Help adding trail only in profit

I have rewrite and it only trail on buy and not for sell .
And it also get "OrderModify error 1" , any one have solution to solve it ?
I want to add it to my ea , or it there any ready make ea for study ?
I cant find any candle trailing stop really work .
Thank you .

Code: Select all

int GetHighLowClose()
{
   datetime startTime   = TimeCurrent(); 
   for (int i=OrdersTotal ()-1; i>=0; i--) 
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
if (OrderSymbol() != Symbol()) continue;
if ( (OrderMagicNumber() != MagicNumber ) continue;
    RefreshRates();
 
         int    digits     = MarketInfo(OrderSymbol(),MODE_DIGITS);
         double point      = MarketInfo(OrderSymbol(),MODE_POINT);
         double PointRatio = 1;
               if (digits==3 || digits==5) PointRatio = 10;
               
//=======================================TrailingBuyStop=====================================
if (OrderType() == OP_BUY)
 {
         double bid        = MarketInfo(OrderSymbol(),MODE_BID);
         double maxBuyStop = NormalizeDouble(bid-MarketInfo(OrderSymbol(),MODE_STOPLEVEL)*point,digits);
         for (i = 1; i <= CountBar; i ++ )
          {         
      if (i==1) new_Bar = iLow(NULL,TimeFrame,i);
         else 
      if (new_Bar>iLow(Symbol(),TimeFrame,i)) new_Bar = iLow(Symbol(),TimeFrame,i);
          }         
      if ((((new_Bar - (CountBar + MarketInfo(Symbol(),MODE_SPREAD))*point)>OrderStopLoss()) || (OrderStopLoss()==0)) && 
         ((new_Bar - (HighLowPipsGap + MarketInfo(Symbol(),MODE_SPREAD))*point)>OrderOpenPrice()) &&
         (new_Bar - (HighLowPipsGap + MarketInfo(Symbol(),MODE_SPREAD))*point<bid-MarketInfo(Symbol(),maxBuyStop)*point))
      if (OrderModify(OrderTicket(),OrderOpenPrice(),new_Bar - HighLowPipsGap*point,OrderTakeProfit(),OrderExpiration()))
              {
                  RefreshRates();
                  continue;
              }
            break; 
         }
       }                               
//======================================TrailingSellStop======================================
if (OrderType() == OP_SELL)
 {
         double ask         = MarketInfo(OrderSymbol(),MODE_ASK);
         double minSellStop = NormalizeDouble(ask+MarketInfo(OrderSymbol(),MODE_STOPLEVEL)*point,digits);
          {   
      if (i==1) new_Bar = iHigh(OrderSymbol(),TimeFrame,i);
         else 
      if (new_Bar<iHigh(OrderSymbol(),TimeFrame,i)) new_Bar = iHigh(OrderSymbol(),TimeFrame,i);
          }         
      if ((((new_Bar + (CountBar + MarketInfo(Symbol(),MODE_SPREAD))*point)<OrderStopLoss()) || (OrderStopLoss()==0)) && 
         ((new_Bar + (HighLowPipsGap + MarketInfo(Symbol(),MODE_SPREAD))*point)<OrderOpenPrice()) && 
         (new_Bar + (HighLowPipsGap + MarketInfo(Symbol(),MODE_SPREAD))*point>ask+MarketInfo(Symbol(),minSellStop)*point))
      if (OrderModify(OrderTicket(),OrderOpenPrice(),new_Bar + (HighLowPipsGap + MarketInfo(Symbol(),MODE_SPREAD))*point,OrderTakeProfit(),OrderExpiration()))
                {
                   RefreshRates();
                }
              }                
   return(0);      
}
All times are UTC Page 1 of 1