Help with some code - Stuck in a loop

 

Hey guys first post so hello to everyone. Long time lurker.

Been trying to code my EA to place buystop and sellstop either side of the pivot when the price crosses it.

However it occasionally gets stuck in a loop at R2 not every-time though. Saying Ordersend Error 130.

As a sample the values it returned for me when it got stuck last time are.

Price = 1.58099

TP = 1.58169

SL = 1.58049

MarketInfo(Symbol(),MODE_STOPLEVEL) = 30

Thanks in advance for any help.

Gap = (MarketInfo(Symbol(),MODE_STOPLEVEL)+20)*Point;

{

if (Bid == R2)//-0.0001 && Bid < (R2+0.0001))
{
buyticket = 0;
while (buyticket <= 0)
{
buyticket = OrderSend(Symbol(),OP_BUYSTOP,lots,NormalizeDouble(R2+Gap,Digits),5,NormalizeDouble(R2-GapOff,Digits),BR3TP,"Adams Pivot EA R1 Up",MAGIC,0,Red);
Sleep (1000);
RefreshRates();
}
}
if (Bid == R1)//-0.0001 && Bid < (R1+0.0001))
{
buyticket = 0;
while (buyticket <= 0)
{
RefreshRates();
buyticket = OrderSend(Symbol(),OP_BUYSTOP,lots,NormalizeDouble(R1+Gap,Digits),5,NormalizeDouble(R1-GapOff,Digits),BR2TP,"Adams Pivot EA R1 Up",MAGIC,0,Red);
Sleep (1000);
}
}
if (Bid == Pivot)// - 0.0001 && Bid <= Pivot+0.0001)
{
buyticket = 0;
while (buyticket <= 0)
{
RefreshRates();
buyticket = OrderSend(Symbol(),OP_BUYSTOP,lots,NormalizeDouble(Pivot+Gap,Digits),5,NormalizeDouble(Pivot-GapOff,Digits),BR1TP,"Adams Pivot EA R1 Up",MAGIC,0,Red);
Sleep (1000);
}
}
if (Bid == S1)//-0.0001 && Bid < (S1+0.0001))
{
buyticket = 0;
while (buyticket <= 0)
{
RefreshRates();
buyticket = OrderSend(Symbol(),OP_BUYSTOP,lots,NormalizeDouble(S1+Gap,Digits),5,NormalizeDouble(S1-GapOff,Digits),BPivotTP,"Adams Pivot EA R1 Up",MAGIC,0,Red);
Sleep (1000);
}
}
if (Bid == S2)//-0.0001 && Bid < (S2+0.0001))
{
buyticket = 0;
while (buyticket <= 0)
{
RefreshRates();
Alert("Price =",DoubleToStr(S2,5)," Take Profit @ ",DoubleToStr(BS1TP,5)," Stop Loss @ ",DoubleToStr(S2-GapOff,5)," Buy Ticket ",buyticket,"Minimum Stop ",MarketInfo(Symbol(),MODE_STOPLEVEL));
buyticket = OrderSend(Symbol(),OP_BUYSTOP,lots,NormalizeDouble(S2+Gap,Digits),5,NormalizeDouble(S2-GapOff,Digits),BS1TP,"Adams Pivot EA R1 Up",MAGIC,0,Red);
Sleep (1000);
}
}
if (Bid == S3)//-0.0001 && Bid < (S3+0.0001))
{
buyticket = 0;
while (buyticket <= 0)
{
buyticket = OrderSend(Symbol(),OP_BUYSTOP,lots,NormalizeDouble(S3+Gap,Digits),5,NormalizeDouble(S3-GapOff,Digits),BS2TP,"Adams Pivot EA R1 Up",MAGIC,0,Red);
Sleep (1000);
RefreshRates();
}
}
}
 

Long time lurker should be familiar with this statement then

Please use this to post code . . . it makes it easier to read.

The answer to your question lies in the error code 130 invalid stops so the loop can never end as it can't place the order the stoploss takeprofit values given.

Your while loops should also end if an error occurs in trying to place an order. There is some good code in the book for dealing with the resubmission of orders.

 

oh great.

another sock puppet that came with his own ea that is almost finished, he just needs icky and whroeder to show him how to fix it.

oh my gawd.

if we could just get the rumpled ones over here, we'd have a full house.

this is gonna be good.

you dont get this much action at the rodeo.

zero/.

 

sock puppets cant read.

 
How do you think the origional poster will percieve your post smoknfx? How would you behave if someone post a message like this to you?
 
Ickyrus:
How do you think the origional poster will percieve your post smoknfx? How would you behave if someone post a message like this to you?


i am sure that whatever he thinks about my post, it wont even make him blink compared to when mr whroeder comes in with his rant about slaves and nobody works for free around here, lmfao...

what do you think that he will think about that?

i give up.

hint: i have been saving all of these pages, cuz i am meeting with some fairly phat vc's this week down in bodunk silicone valley and i wanna bring hard copies with me... when i tell em how the kids in this business operate, i wanna bring hard copies. omg.

zero/.

 

Thanks Ickyrus I'll have a look at that.

I have done as you said and put it in SRC below. Makes it a bit easier to read for anyone looking.

If I came here with no code asking for someone to write it for me then I agree that would be unfair. I wouldn't work for free and I wouldn't expect anyone else to either. Following Mr Whroeder's advice of either learning to code or paying someone to do it for me, I learnt to code. Fact is I knew nothing about coding before I started writing this.

I mearlly came for advice as to how to fix a problem with a loop.

That is all.

void buystops()
{
      if (Bid == R2)//-0.0001 && Bid < (R2+0.0001))
      {
         buyticket = 0;
         while (buyticket <= 0)
         {
         buyticket = OrderSend(Symbol(),OP_BUYSTOP,lots,NormalizeDouble(R2+Gap,Digits),5,NormalizeDouble(R2-GapOff,Digits),BR3TP,"Adams Pivot EA R1 Up",MAGIC,0,Red);
         Sleep (1000);
         RefreshRates();
         }
      }
      
      if (Bid == R1)//-0.0001 && Bid < (R1+0.0001))
      {
         buyticket = 0;
         while (buyticket <= 0)
         {
         RefreshRates();
         buyticket = OrderSend(Symbol(),OP_BUYSTOP,lots,NormalizeDouble(R1+Gap,Digits),5,NormalizeDouble(R1-GapOff,Digits),BR2TP,"Adams Pivot EA R1 Up",MAGIC,0,Red);
         Sleep (1000);
         }
      }
      
      if (Bid == Pivot)// - 0.0001 && Bid <= Pivot+0.0001)
      {
         buyticket = 0;
         while (buyticket <= 0)
         {
         RefreshRates();
         buyticket = OrderSend(Symbol(),OP_BUYSTOP,lots,NormalizeDouble(Pivot+Gap,Digits),5,NormalizeDouble(Pivot-GapOff,Digits),BR1TP,"Adams Pivot EA R1 Up",MAGIC,0,Red);
         Sleep (1000);
         }
      }
      
      if (Bid == S1)//-0.0001 && Bid < (S1+0.0001))
      {
         buyticket = 0;
         while (buyticket <= 0)
         {
         RefreshRates();
         buyticket = OrderSend(Symbol(),OP_BUYSTOP,lots,NormalizeDouble(S1+Gap,Digits),5,NormalizeDouble(S1-GapOff,Digits),BPivotTP,"Adams Pivot EA R1 Up",MAGIC,0,Red);
         Sleep (1000);
         }
      }
      
      if (Bid == S2)//-0.0001 && Bid < (S2+0.0001))
      {
         buyticket = 0;
         while (buyticket <= 0)
         {
         RefreshRates();
         Alert("Price =",DoubleToStr(S2,5)," Take Profit @ ",DoubleToStr(BS1TP,5)," Stop Loss @ ",DoubleToStr(S2-GapOff,5)," Buy Ticket ",buyticket,"Minimum Stop ",MarketInfo(Symbol(),MODE_STOPLEVEL));
         buyticket = OrderSend(Symbol(),OP_BUYSTOP,lots,NormalizeDouble(S2+Gap,Digits),5,NormalizeDouble(S2-GapOff,Digits),BS1TP,"Adams Pivot EA R1 Up",MAGIC,0,Red);
         Sleep (1000);
         }
      }
      
      if (Bid == S3)//-0.0001 && Bid < (S3+0.0001))
      {
         buyticket = 0;
         while (buyticket <= 0)
         {
         buyticket = OrderSend(Symbol(),OP_BUYSTOP,lots,NormalizeDouble(S3+Gap,Digits),5,NormalizeDouble(S3-GapOff,Digits),BS2TP,"Adams Pivot EA R1 Up",MAGIC,0,Red);
         Sleep (1000);
         RefreshRates();
         }      
      }
}
 
falsedave:

Thanks Ickyrus I'll have a look at that.

I have done as you said and put it in SRC below. Makes it a bit easier to read for anyone looking.

If I came here with no code asking for someone to write it for me then I agree that would be unfair. I wouldn't work for free and I wouldn't expect anyone else to either. Following Mr Whroeder's advice of either learning to code or paying someone to do it for me, I learnt to code. Fact is I knew nothing about coding before I started writing this.

I mearlly came for advice as to how to fix a problem with a loop.

That is all.

You should check what the Error is if OrderSend() returns a value less than zero, i.e. -1 you will find some help here: What are Function return values ? How do I use them ?

What is the value of Gap ? how is it derived ? you most likely do not need the NormalizeDoubles. Is your Broker an ECN Broker ? ECN

You also need to consider the StopLevel when placing Orders: https://book.mql4.com/appendix/limits

 

Sorry that your thread is bing hijacked with sour grapes and the misuderstanding of how a forum works.

You may have missed my post edit repeated here.

The answer to your question lies in the error code 130 which means invalid stops so the loop can never end as it can't place the order with the stoploss takeprofit values you give.

Your while loops should also end if an error occurs while in the process of trying to place an order.

There is some good code in the book for dealing with the resubmission of orders.

You should read this

 
if (Bid == S3)
Doubles rairly compare equal. See Can price != price ? - MQL4 forum