Closing Pending Order and Opening new OrderSend? - page 2

 
DomGilberto:

Lol fair enough.

I modify lots, take profit and stop loss? I delete and re-open an order until it is triggered or cancelled. I've tried this, but it doesn't seem to work? Not sure if it has something to do with comparing the doubles? (Not sure what you mean by that? NormalizeDouble?)

You don't have an OrderModify() in that code . . . . why are we discussing error 1 in relation to OrderModify() ?
 
Lol ok.
 
Yea, I don't quite know why I was posting that part of the code... (facepalm) I think I have managed to sort out the error 1 message now.
 
DomGilberto:
Yea, I don't quite know why I was posting that part of the code... (facepalm) I think I have managed to sort out the error 1 message now.

 
WHRoeder:
Call it once, at the beginning of start(). Return if false.

Would you mind elaborating on this? This is where I am stuck on at the moment. I have experimented for a long time on this now.

At the moment, I am calling "IsNewCandle()" from the int start() and then "CheckForMaTrade();" which if "CheckForMaTrade()" returns true, it will pass over control to an order send function...

When it gets passed over to this order send function from "CheckForMaTrade()" it is NOT checking every hour bar close to modify its order (which is what I am wanting it to do.) Instead, I am finding it hard to understand how, but it is doing it on random H1 bar closes.

When it does the modify, it is OrderDelete() the pending Order, and then re-opening a new order with the new stops, lots, and targets... (80-90% of the time it will need to recalibrate the order relative to the MA, as this is where the stop goes before the order is triggered.)


How do I use "IsNewCandle()" in conjunction with the following code to make sure that on every new candle, the order is deleted, and a new order is placed...?
//+------------------------------------------------------------------+
//| Order Enter Function                                             |
//+------------------------------------------------------------------+
void OrderEntry(int direction)
{
   //determine amount to risk on this trade based on RiskPercent Above.
   double LotSize=0;
   double Equity=AccountEquity();
   double RiskedAmount=Equity*RiskPercent*0.01;
  
   double ATR_Pad = iATR(NULL,60,14,1)/2; 
   double Buy_Pad = NormalizeDouble(ATR_Pad,Digits);
   double Sell_Pad = NormalizeDouble(ATR_Pad,Digits);
   
   //Get Highest Price in our lookback range and set buy price above it.
   int iTBT = iBarShift(NULL,60, triggerBarTime, true),
   iHH = iHighest(NULL,60, MODE_HIGH, iTBT + CandlesBeforeBiasObtained, 0);
   double Buy_Here = High[iHH] + Buy_Pad;
   double buyPrice= NormalizeDouble(Buy_Here,Digits);

   //Get Lowest Price in our lookback range and set sell price below it.
   int iTBT_1 = iBarShift(NULL, 60, triggerBarTime, true),
   iLL = iLowest(NULL, 60, MODE_LOW, iTBT_1 + CandlesBeforeBiasObtained, 0);
   double Sell_Here=Low[iLL] - Sell_Pad;
   double sellPrice= NormalizeDouble(Sell_Here,Digits);
   
   //Stop calculations.    
   double ATR = iATR(NULL,60,14,1);
   double MA = iMA(NULL,60,MA_Period,0,1,0,1);
   double BuyStopPriceMath = MA - ATR;
   double SellStopPriceMath = MA + ATR;
   double BuyStopPrice = NormalizeDouble(BuyStopPriceMath,Digits);
   double SellStopPrice = NormalizeDouble(SellStopPriceMath,Digits);


   //get our buystop price from below the ma and our takeprofit based on our r:r ratio.
   double pips_to_bsl=buyPrice-BuyStopPrice;
   double buy_tp_price=(pips_to_bsl*RewardRatio)+buyPrice;
   double buy_takeprofit_price= NormalizeDouble(buy_tp_price, Digits);

   //get our sellstop price from below the ma and our takeprofit based on our r:r ratio.
   double pips_to_ssl=SellStopPrice-sellPrice;
   double sell_tp_price=sellPrice-(pips_to_ssl*RewardRatio);
   double sell_takeprofit_price= NormalizeDouble(sell_tp_price, Digits);
   


//+-------------------------------------------------------------------------------------+
//| Order Buy Function                                                                  |
//+-------------------------------------------------------------------------------------+   

//Place a pending buystop if no orders exists.. pending or otherwise.
if(direction==0)
{ 
      double btp=buy_takeprofit_price;
      double Min_Lot = MarketInfo(Symbol(),MODE_MINLOT);
      double Lot_Step = MarketInfo(Symbol(),MODE_LOTSTEP); 
      double BuyLotSize =(RiskedAmount/(pips_to_bsl/pips))/10;
      double Lots = NormalizeDouble(BuyLotSize,2);
      LotSize = MathFloor(Lots/Lot_Step)*Lot_Step;


      static double Stored_BuyPrice;

      if(OpenOrdersThisPair(Symbol())==0)
         {
         int BuyTicketOrder= OrderSend(Symbol(),OP_BUYSTOP,LotSize,buyPrice,3,BuyStopPrice,btp,NULL,MagicNumber,0,Green); 
         } 

     for(int b=OrdersTotal()-1; b>=0; b--)
        {
        if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
          if(OrderType()==OP_BUYSTOP) 
            if(OrderMagicNumber()==MagicNumber)
               if(OrderSymbol() == Symbol())
                {  
                   if(BuyStopPrice - OrderStopLoss() > Point / 2. )
                        {
                        Stored_BuyPrice = OrderOpenPrice();
                        DeleteOrder = OrderDelete(OrderTicket());
                        if(DeleteOrder != TRUE) Print("Buy Delete Order Failed = ",GetLastError());
                        }

                   if(OpenOrdersThisPair(Symbol())==0 && DeleteOrder==True && H1_Buy_Touch == "H1 Buy Touch")// If there are no open orders = place a new order.
                        {
                        int NewBuyOrder = OrderSend(Symbol(),OP_BUYSTOP,LotSize,Stored_BuyPrice,3,BuyStopPrice,btp,NULL,MagicNumber,0,Green);

                        }   
                 }      
         }
}   // end of  if(direction==0)
 
Man this is frustrating me lol.

I've tried calling "Modify_Order();" which is a void with a for loop to get the open order AFTER "IfIsNewCandle()" within the "int start()" section, but I am having no luck with it - It does the job, but it's not as consistent with picking up the correct number of trades it should do through-out the back-test, and instead of being slightly better results (which it should most definitely be), it is dramatically worse?
 
DomGilberto:
Man this is frustrating me lol.

Tell me about it . . . I've just spent 2 hours tracking down a bug in my code which was as a result of TimeDayOfWeek(TimeCurrent()) returning the wrong day number . . . then I realised I took my test terminal offline . . . on Monday.

 
DomGilberto: How do I use "IsNewCandle()" in conjunction
int start(){
   if( !isNewCandle() ) return;
   :
 
WHRoeder:



...  

 // Check for Moving Averages Fanned down creating a DOWN bias.  
   if(D1_Bias=="Daily is Down" && H4_Bias=="4 Hour is Down" && H1_Bias=="None")
      if(CurrentSmallFish1<CurrentSmallFish2)
         if(CurrentSmallFish2<CurrentSmallFish3)
            if(CurrentSmallFish3<CurrentSmallFish4)
               if(CurrentSmallFish4<CurrentSmallFish5)
                  if(CurrentSmallFish5<CurrentSmallFish6)
                     if(CurrentSmallFish6<CurrentBigFish1)
                        if(CurrentBigFish1<CurrentBigFish2)
                           if(CurrentBigFish2<CurrentBigFish3)
                              if(CurrentBigFish3<CurrentBigFish4)
                                 if(CurrentBigFish4<CurrentBigFish5)
                                    if(CurrentBigFish5<CurrentBigFish6)
                                       {
                                       triggerBarTime=Time[1];
                                       H1_Bias="Down";
                                       Comment("Bias is: "+H1_Bias+" since: "+TimeToStr(triggerBarTime,TIME_DATE|TIME_MINUTES));
                                       H4_Bias="4 Hour is Down";
                                       Comment("Bias is: "+H4_Bias+" since: "+TimeToStr(H4_Bar,TIME_DATE|TIME_MINUTES));
                                       D1_Bias="Daily is Down";
                                       Comment("Bias is: "+D1_Bias+" since: "+TimeToStr(D1_Bar,TIME_DATE|TIME_MINUTES));
                                       }
   
   ///////////////////////////////////////////////////////////////////////////////////////

   H1_high  = iHigh(NULL, PERIOD_H1, 1);
   H1_close = iClose(NULL, PERIOD_H1, 1);
   if(H1_Bias=="Down" && H4_Bias=="4 Hour is Down" && D1_Bias=="Daily is Down" && H1_high >= ema21 && H1_close < CurrentBigFish6) // This is being called on every H1 close?
      {
      H1_Sell_Touch = "H1 Sell Touch";
         {
         OrderEntry(1); // Pending order Sell Stop function is called.
         }
      }
This being called on every "IsNewCandle()" - So when it is correct, it will send an "Order Send" function via "OrderEntry(1)". When it reaches OrderEntry(1) (which is the code I posted in previous post), it will push out a pending order, BUT it will NOT calibrate the order based upon EACH H1 bar close, because the OrderEntry(1) is a void.

So, forgive me if I am sounding stupid (as I have not quite got this in my own head yet!), but why when I try to call the OrderEntry(1) from Int Start(), If( IsNewCandle(), it still will not update or modify the current pending order?

If( !IsNewCandle() ) return; <<<< Does that mean, if it is NOT a new candle, right? Are you suggesting I put it beneath that and brace the OrderEntry(1) call from there? (sorry for being a little slow :s)
 
RaptorUK:

Tell me about it . . . I've just spent 2 hours tracking down a bug in my code which was as a result of TimeDayOfWeek(TimeCurrent()) returning the wrong day number . . . then I realised I took my test terminal offline . . . on Monday.


I've started to realise how it's not a case of just writing a system and happy days... I was never that naive anyway, but I am starting to realise how every little thing I do is incredibly important, how I write it, but not only that, remembering what I've changed (Hence why I am now saving Version_1, version_2 and so forth lol)