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

HELP WITH ERROR 4107
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=2914
Page 2 of 18
Author:  fx800 [ Tue Aug 06, 2013 11:32 am ]
Post subject:  Re: HELP WITH ERROR 4107

phil_trade wrote:
fx8000 wrote:I am often puzzled why using the following BuyStop/SellStop code doesn't work. Can someone please explain.

Thanks.

Code: Select all

       stype = " BuyStop ";
      type = OP_BUYSTOP;
      
      if (!BuyOpen)
      {
         price = NormalizeDouble(High[1] + (Buffer / factor), Digits);
      } 

Code: Select all

      stype = " SellStop ";
      type = OP_SELLSTOP;
      
      if (!SellOpen)
      {
         price = NormalizeDouble(Low[1] - (Buffer / factor), Digits);
      } 
what do you mean by "doesn't work" ?
It means the ea does not enter by buystop/sellstop, instead it enters by buyopen/sellopen.

The example below is the buy trade taken for CHFJPY. Completely wrong entry.

That is why I had to resort to Has Buy Filled/ Has Sell Filled routine.

Can you suggest a BuyStop/SellStop code ?
Author:  fx800 [ Tue Aug 06, 2013 2:21 pm ]
Post subject:  Re: HELP WITH ERROR 4107

Has anyone got a BuyStop/SellStop code snippet to share.

I would be immensely grateful.

Thanks in advance.

peter
Author:  fx800 [ Tue Aug 06, 2013 11:22 pm ]
Post subject:  Re: HELP WITH ERROR 4107

I am trying out the following pending order code and see what happens.

Code: Select all

    //Long 
   
   if (SendLong)
   {
      stype = " BuyStop ";
      type = OP_BUYSTOP;
      
      price = NormalizeDouble(iHigh(NULL, 240, 1)  + (Buffer / factor), Digits);
                            
      if (!SendAlertNotTrade)
      {
         
         stop = CalculateStopLoss(OP_BUY, price);
         
         take = CalculateTakeProfit(OP_BUY, price);
         
         
         //Lot size calculated by risk
         if (RiskPercent > 0) SendLots = CalculateLotSize(price, NormalizeDouble(stop + (HiddenPips / factor), Digits) );

         type = OP_BUY;
         
      }//if (!SendAlertNotTrade)
      
      SendTrade = true;
      
   }//if (SendLong)
   
   //Short
   
   if (SendShort)
   {
      stype = " SellStop ";
      type = OP_SELLSTOP;
      
      price = NormalizeDouble(iLow(NULL, 240, 1) - (Buffer / factor), Digits);
           
      if (!SendAlertNotTrade)
      {
         
         stop = CalculateStopLoss(OP_SELL, price);
         
         take = CalculateTakeProfit(OP_SELL, price);
         
         
         //Lot size calculated by risk
         if (RiskPercent > 0) SendLots = CalculateLotSize(price, NormalizeDouble(stop - (HiddenPips / factor), Digits) );

         type = OP_SELL;
      }//if (!SendAlertNotTrade)
         
      SendTrade = true;      
   
      
   }//if (SendShort)
   

   if (SendTrade)
   {
      if (!SendAlertNotTrade) 
      { 
         result = SendSingleTrade(Symbol(), type, TradeComment, SendLots, price, stop, take);
         if (result) 
         {
            if (EmailTradeNotification) SendMail("Trade sent ", Symbol() + stype + "trade at " + TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES));
            if (AlertPush) AlertNow(WindowExpertName() + " " + Symbol() + " " + stype + " " + DoubleToStr(price, Digits) );
            OrderSelect(TicketNo, SELECT_BY_TICKET, MODE_TRADES);
            CheckTpSlAreCorrect(type);
         }//if (result)          
      }//if (!SendAlertNotTrade) 
      
      if (SendAlertNotTrade && !AlertSent)
      {
         Alert(WindowExpertName(), " ", Symbol(), " ", stype, "trade has triggered. ",  TimeToStr(TimeLocal(), TIME_DATE|TIME_MINUTES|TIME_SECONDS) );
         SendMail("Trade alert. ", Symbol() + " " + stype + " trade has triggered. " +  TimeToStr(TimeLocal(), TIME_DATE|TIME_MINUTES|TIME_SECONDS ));
         if (AlertPush) AlertNow(WindowExpertName() + " " + Symbol() + " " + stype + " " + DoubleToStr(price, Digits) );         
         AlertSent=true;
       }//if (SendAlertNotTrade && !AlertSent)
   }//if (SendTrade)
   
   //Actions when trade send succeeds
   if (SendTrade && result)
   {      
      if (!SendAlertNotTrade && !CloseEnough(HiddenPips, 0) ) ReplaceMissingSlTpLines();
   }//if (result)
   
   //Actions when trade send fails
   if (SendTrade && !result)
   {
      OldBarsTime = 0;
   }//if (!result)
The same code has been used in some old ea's and theoretically should work.
Author:  fx800 [ Fri Aug 09, 2013 3:01 pm ]
Post subject:  Re: HELP WITH ERROR 4107

Despite using a sell_stop code, the ea still sends a sell open order.

I wonder why ? :?:

Can someone please explain ?
Author:  ewinner [ Fri Aug 09, 2013 3:40 pm ]
Post subject:  Re: HELP WITH ERROR 4107

Hi,

You still have reference to type = OP_BUY 9 lines further down in the code, change it to OP_BUYSTOP

Same with type = OP_SELL further down, change it to OP_SELLSTOP

See if that works :-)

Mark



Code: Select all


   //Long 
   if (SendLong)
   {
      stype = " BuyStop ";
      type = OP_BUYSTOP;
            
      price = NormalizeDouble(iHigh(NULL, PERIOD_H4, 1) + (Buffer / factor), Digits); 
         
      if (!SendAlertNotTrade)
      {
         
         stop = CalculateStopLoss(OP_BUY, price);
                  
         take = CalculateTakeProfit(OP_BUY, price);
         
         
         //Lot size calculated by risk
         if (RiskPercent > 0) SendLots = CalculateLotSize(price, NormalizeDouble(stop + (HiddenPips / factor), Digits) );

         type = OP_BUY;
         
      }//if (!SendAlertNotTrade)
      
      SendTrade = true;
      
   }//if (SendLong)
   
   //Short
   if (SendShort)
   {
      stype = " SellStop ";
      type = OP_SELLSTOP;
      
      price = NormalizeDouble(iLow(NULL, PERIOD_H4, 1) - (Buffer / factor), Digits);

      if (!SendAlertNotTrade)
      {
         
         stop = CalculateStopLoss(OP_SELL, price);
         
         take = CalculateTakeProfit(OP_SELL, price);
         
         
         //Lot size calculated by risk
         if (RiskPercent > 0) SendLots = CalculateLotSize(price, NormalizeDouble(stop - (HiddenPips / factor), Digits) );

         type = OP_SELL;
      }//if (!SendAlertNotTrade)
         
      SendTrade = true;      
   
      
   }//if (SendShort)




Author:  fx800 [ Fri Aug 09, 2013 3:49 pm ]
Post subject:  Re: HELP WITH ERROR 4107

ewinner wrote:Hi,

You still have reference to type = OP_BUY 9 lines further down in the code, change it to OP_BUYSTOP

Same with type = OP_SELL further down, change it to OP_SELLSTOP

See if that works :-)

Mark



Code: Select all


   //Long 
   if (SendLong)
   {
      stype = " BuyStop ";
      type = OP_BUYSTOP;
            
      price = NormalizeDouble(iHigh(NULL, PERIOD_H4, 1) + (Buffer / factor), Digits); 
         
      if (!SendAlertNotTrade)
      {
         
         stop = CalculateStopLoss(OP_BUY, price);
                  
         take = CalculateTakeProfit(OP_BUY, price);
         
         
         //Lot size calculated by risk
         if (RiskPercent > 0) SendLots = CalculateLotSize(price, NormalizeDouble(stop + (HiddenPips / factor), Digits) );

         type = OP_BUY;
         
      }//if (!SendAlertNotTrade)
      
      SendTrade = true;
      
   }//if (SendLong)
   
   //Short
   if (SendShort)
   {
      stype = " SellStop ";
      type = OP_SELLSTOP;
      
      price = NormalizeDouble(iLow(NULL, PERIOD_H4, 1) - (Buffer / factor), Digits);

      if (!SendAlertNotTrade)
      {
         
         stop = CalculateStopLoss(OP_SELL, price);
         
         take = CalculateTakeProfit(OP_SELL, price);
         
         
         //Lot size calculated by risk
         if (RiskPercent > 0) SendLots = CalculateLotSize(price, NormalizeDouble(stop - (HiddenPips / factor), Digits) );

         type = OP_SELL;
      }//if (!SendAlertNotTrade)
         
      SendTrade = true;      
   
      
   }//if (SendShort)




Thanks Mark.

Do you mean this one

Code: Select all

         //Lot size calculated by risk
         if (RiskPercent > 0) SendLots = CalculateLotSize(price, NormalizeDouble(stop + (HiddenPips / factor), Digits) );

         type = OP_BUY;


and the same for sell ?

peter
Author:  ewinner [ Fri Aug 09, 2013 3:51 pm ]
Post subject:  Re: HELP WITH ERROR 4107

Yep, that should do it :-)
Author:  fx800 [ Fri Aug 09, 2013 3:56 pm ]
Post subject:  Re: HELP WITH ERROR 4107

ewinner wrote:Yep, that should do it :-)

Thank you very much. Will report back when the ea sends the next order.

peter
Author:  ewinner [ Fri Aug 09, 2013 3:58 pm ]
Post subject:  Re: HELP WITH ERROR 4107

Ok, if not ill take a closer look, cheers

Mark
Author:  fx800 [ Fri Aug 09, 2013 5:05 pm ]
Post subject:  Re: HELP WITH ERROR 4107

Another premature entry that would have been saved by a pending order a few pips above the bull candle.

Certainly worth investigating to see if using pending orders will make this ea even better.

It is logical to expect that if a move has momentum, it has first to break the previous bar high/low.

In the present market, it is difficult to see long trending moves. I am using Steve's mptm to manage the trades, BE30+2, JS30 SL ATR X2, TP ATR X 0.8.

I have added a maximum candle range filter. Do not trade if the trigger candle is > xxx pips.
All times are UTC Page 2 of 18