Opening new buystop after killing the previous, solution?

 

bugHi, i am trying here to build a martingale sequence by using buy stops for the buy cycle. Basically, if price goes down, i open buy stops so when price goes up , the pending will be market. However, i need only one buy stop to be active. My logic is as follows, if signal is valid and there is no previous buy stop , open buy stop. If the signal is valid and there is previous buystop, kill the previous then open new one. 

The problem, On testing, the new buystop is killed once published.

    if (ValidNextBuyPrice () & IsPrevBuyStop()== true)// if signal is valid to open buystop AND also there is another buystop there
     {
     KillPreviousBuyStop();// so i need first to kill the previous buy stop THEN open a new buy stop
     int  ticketNumberSBS = OrderSend(Symbol(),OP_BUYSTOP,starting_lot_size*2,NextBuyPrice (),slippage,0,0,"",((magic*10)+3),0,clrLawnGreen);
           if (ticketNumberSBS > 0 )Print("Order buyfirst placed # ", ticketNumberSBS);   
           else                    Print("Order buyfirst Send failed, error # ", GetLastError() );
     }
     
     else if (ValidNextBuyPrice () & IsPrevBuyStop()== false)//if signal is valid but there is no previous buystop so just open a buystop order
     {
     
     int  ticketNumberSBS = OrderSend(Symbol(),OP_BUYSTOP,starting_lot_size*2,NextBuyPrice (),slippage,0,0,"",((magic*10)+3),0,clrLawnGreen);
           if (ticketNumberSBS > 0 )Print("Order buyfirst placed # ", ticketNumberSBS);   
           else                    Print("Order buyfirst Send failed, error # ", GetLastError() );
     }
     
 
EgyAlgoTrader:

 here to build a martingale sequence

If the signal is valid and there is previous buystop, kill the previous then open new one.  The problem, On testing, the new buystop is killed once published.


  1. Martingale, guaranteed to kill your account.
  2. Of course it is killed because now the new one is the previous.
  3. A) Don't test for a signal, test for a change in signal.
    static bool currValidBuyPrice=false;
    bool prevValidBuyPrice = currValidBuyPrice;
    currValidBuyPrice=ValidNextBuyPrice ();
    bool newSignal = currValidBuyPrice && !prevValidBuyPrice;
        if (newSignal & IsPrevBuyStop()== true)// if signal is valid to open buystop AND also there is another buystop there
    
  4. B) or don't kill it, just move it, like a trailing stoploss.
  5. if (newSignal & IsPrevBuyStop()== true
    This doesn't do what you think it does. You want "&&"
  6. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
 

WH is right in this.  Programming using variables that make sense is very helpful, even if you are the only one reading it, because say 3 or 4 years down the road, you crack this program open again, you might not have such an easy time following the money trail (pun intended).  Like Long_Entry is what you want to use to enter the market, something like Long_Entry_Target would be more appropriate.  Also something that makes your code read easier, for you now and later, and if you are asking others to help with your code, keep your indents to a certain spacing, and keep your code aligned with those, using brackets, parenthesis, whatever is needed at that particular point in your code.


 if (ValidNextBuyPrice () & IsPrevBuyStop()== true)// if signal is valid to open buystop AND also there is another buystop there
 {
     KillPreviousBuyStop();// so i need first to kill the previous buy stop THEN open a new buy stop
     int  ticketNumberSBS = OrderSend(Symbol(),OP_BUYSTOP,starting_lot_size*2,NextBuyPrice (),slippage,0,0,"",((magic*10)+3),0,clrLawnGreen);
     if (ticketNumberSBS > 0 )Print("Order buyfirst placed # ", ticketNumberSBS);             // these 2 should be aligned with the above because they are 
     else                     Print("Order buyfirst Send failed, error # ", GetLastError() ); // at the same level as the above int line
 }
     
 else if (ValidNextBuyPrice () & IsPrevBuyStop()== false)//if signal is valid but there is no previous buystop so just open a buystop order
 {
     int  ticketNumberSBS = OrderSend(Symbol(),OP_BUYSTOP,starting_lot_size*2,NextBuyPrice (),slippage,0,0,"",((magic*10)+3),0,clrLawnGreen);
     if (ticketNumberSBS > 0 )Print("Order buyfirst placed # ", ticketNumberSBS);   // same issue here as the set above
     else                    Print("Order buyfirst Send failed, error # ", GetLastError() );
 }
     

Now this particular layout is more my personal preference than some other people's, but putting the brackets in the same indent as the "header line" for the section of code they are with makes it easier visually to see that this bracket set is with that section of code.  Also, one thing I have seen recommended, and again, it is personal preference, is on an if/else structure, even if each only has one line, is put brackets around them as well.  This adds nothing functionally to the program, but in the future, if lines have to be added to one or the other or both, it is a little easier to see where they need to go, and it doesn't add any processing time with the brackets there, it is strictly visual for the coder.

 
WHRoeder:

  1. Martingale, guaranteed to kill your account.
  2. Of course it is killed because now the new one is the previous.
  3. A) Don't test for a signal, test for a change in signal.
  4. B) or don't kill it, just move it, like a trailing stoploss.
  5. This doesn't do what you think it does. You want "&&"
  6. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

Thank you for your quick help. 

 1- I agree but here martingale is part of my educational process not for a trading purpose

 2-Thanks, i got it

3- Would you please add some comment to your code lines so i understand the purpose and where it should go. I tried some combinations but did not work out so it seems i am missing something.

static bool currValidBuyPrice=false;// where this line should go? I am new to this "static" concept.
bool prevValidBuyPrice = currValidBuyPrice;
currValidBuyPrice=ValidNextBuyPrice ();
bool newSignal = currValidBuyPrice && !prevValidBuyPrice;
    if (newSignal && IsPrevBuyStop())// I added && and fixed this redundant check expression.Thanks for educating me about it

 4-This is a good idea. I will try it once i figure out how i could delete and repost

5-Got it, Thanks. However, Do you have better suggestions for those variable names  i used. My usage is still primitive,i know. 

 
salute to Pavel and his EA Truly excellent work , I have been examining the EA since few months and now I give him what he deserves (My honest review) But I have some advises for you , follow his instructions The larger deposits the better , don't use a greedy settings. Yes it can be risky system but if you used safe settings you will be on the safe side.
 

From my reading, i can see that:

1-static variable are initialized once when EA loaded

2-If static variable declared within a function, it will be available only within this function, and its value will be changed only by this function 

3-If static variable declared on global space, this will not add any thing because global are static in nature but available for all parts of EA 

4-Only could be initialized by constant or constant expression. 

 

What i am missing here is the logic of  usage of this static  expression in WHRoeder  code and how to use static within a function. I am still in my beginning so please take me slow. :))

 
static bool currValidBuyPrice=false;
bool prevValidBuyPrice = currValidBuyPrice;
currValidBuyPrice=ValidNextBuyPrice ();
bool newSignal = currValidBuyPrice && !prevValidBuyPrice;
Tick
ValidNextBuyPrice ()
currValidBuyPrice
prevValidBuyPrice
newSignal
Before first tick
N/AFalseN/A
N/A
First tick
FalseFalseFalseFalse
First time ValidNext is true.
TrueTrue
False
True (change)
Next ticks ValidNext is still true
TrueTrue
True
False
Next time ValidNext returns false.
FalseFalseTrueFalse
 
EgyAlgoTrader:

Thanks for enlightening me about these tips:

1- Long_Entry_Target should be better than ValidNextBuyPrice,  right?. However, i understand the importance of adding comments and using appropriate meaningful naming. As i said before, it is only usage is still at not that good. 

2-I got this indentation point. Thanks 

 

Either LET or VNBP would work, but you understand that the point is that both are a meaningful name and it is easy to tell from the name alone what their purpose is.  You don't have to search through the code to see what is being calculated and placed in that variable.  And as far as "not that good", everyone was there at some point or another.  You are learning and open to new ideas, which is a huge help on you getting better and better at it. :)
 
WHRoeder:
Tick
Before first tick
N/AFalseN/A
N/A
First tick
FalseFalseFalseFalse
First time ValidNext is true.
TrueTrue
False
True (change)
Next ticks ValidNext is still true
TrueTrue
True
False
Next time ValidNext returns false.
FalseFalseTrueFalse

Thanks so much. I am modifying my code but because i am testing with broker platform which huge stoplevel (20pips), it makes testing more complicated. I got error 130 when EA trying to add new buystop. And getting error 1 when i changed from killing to modify buystop as trailing stops. 

 

Is there a way to change this stoplevel on testing mode? or i have to go to other broker MT4 for testing.? On note, i added some UDFs to check if NextBuy is away enough from Ask by at least stoplevel or not. I think because of the large stoplevel, i can not test properly on low time frame. 

Also, i still have a question regarding the table above.

How the  prevValidBuyPrice return false back again (after raw 4) so i could get a valid signal on change. 

 
i moved to test on Pepperstone MT4 with a very tight StopLevel.
 

While i am testing, i have a question regarding tracking the closing event of a  specific order. Here, i am trying to code a simple martingale EA that will open a cycle of orders. Let me explain more,

Suppose,

1-EA open buy order ( i call it firstbuy) and i gave it unique magic number in the cycle ((magic*10)+1) to easily select later. This firstbuy has a fixed tp 

2-Now, price goes against firstbuy, EA starts to open buystops.

3-some of those buystops will be triggered and after sometime, i might have ( firstbuy + some triggered buystops (becomes now buyseconds) + an active buystop)

4- suppose now the price goes up again in favor and firstbuy closed in profit

 

Now, i need to code a function that connect all cycle orders and check if firstbuy is closed and then EA will close all cycle orders ( triggered buyseconds and active buystops) before it starts new cycle.

The problem is: When i check history pool, i use OrderMagicNumber() and OrderType() but i don't knew if i can select the right " last closed buyfirst" or it might select any buyfirst orders closed from previous cycles. All will have the same magic and same type. I feel even the last closed label is not enough. 

The code i might use is :

for(int i=OrdersHistoryTotal()-1; i>=0; i--)// start the loop through history orders in reverse order
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
      if(!OrderSymbol()==Symbol()) continue;
      if (OrderType () == OP_BUY ) 
      { 
         if(OrderMagicNumber()== (magic*10)+1) // i might have many orders in the historybool having same magic and type from previous cycles,right?
         
         {
 
         }
 
      } 


make sense? 

Reason: