Download MetaTrader 5
To add comments, please log in or register
Deposit to your account using bank cards via Gate2Shop
Dominic Gilbert
1632
Dominic Gilbert 2014.09.19 13:27 
//+------------------------------------------------------------------+
//|Calibrate Open trade setups if they require it                    |
//+------------------------------------------------------------------+
void Trade_Calibration()      
{      
      
   //Padding for the stop and padding for the entry too. 
    ATR_Pad = iATR(NULL,low,ATR_Period,1)/2; 
    Buy_Pad = NormalizeDouble(ATR_Pad,Digits);
    Sell_Pad = NormalizeDouble(ATR_Pad,Digits);
   
   //Stop calculations.    
    ATR = iATR(NULL,low,ATR_Period,1);
    MA = iMA(NULL,low,MA_Period,0,1,0,1);
  
   //Risk per trade places  
    risk_amount = AccountBalance( )*RiskPercent/100;
    risk_amount2 = AccountBalance( )*RiskPercent2/100;
    risk_amount3 = AccountBalance( )*RiskPercent3/100;
    risk_amount4 = AccountBalance( )*RiskPercent4/100;
   
   //Market information for the lot and risk calculations
    Lot_Step = MarketInfo(Symbol(), MODE_LOTSTEP);
    ts = MarketInfo(Symbol(), MODE_TICKSIZE);
    tv = MarketInfo(Symbol(), MODE_TICKVALUE);
    minlot = MarketInfo(Symbol(), MODE_MINLOT);

//+------------------------------------------------------------------+


   int PositionIndex; 
   int TotalNumberOfOrders;  
   TotalNumberOfOrders = OrdersTotal(); 
       
   for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)  
     {
       if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   
        if( OrderMagicNumber() == MagicNumber1 )
 
          if( OrderSymbol() == Symbol())            
            {
                  
            if ( OrderType() == OP_SELLSTOP )
              {
                  
               Stored_SellPrice = OrderOpenPrice();
                  
               double SellStopPriceMath = MA + ATR, SellStopPrice = NormalizeDouble(SellStopPriceMath,Digits);
   
               //Get Take Profit price for FIRST TARGET
               double pips_to_ssl = SellStopPrice - Stored_SellPrice , sell_tp_price1 = Stored_SellPrice-(pips_to_ssl),
                      sell_takeprofit_price1 = NormalizeDouble(sell_tp_price1, Digits) , stp1 = sell_takeprofit_price1;
      
               //Get Take Profit price for Second TARGET
               double sell_tp_price2 = Stored_SellPrice -(pips_to_ssl*Second_Target_Ratio) , 
                      sell_takeprofit_price2 = NormalizeDouble(sell_tp_price2, Digits) , stp2 = sell_takeprofit_price2;
      
               //Get Take Profit price for Third TARGET
               double sell_tp_price3 = Stored_SellPrice -(pips_to_ssl*Third_Target_Ratio) ,
                      sell_takeprofit_price3 = NormalizeDouble(sell_tp_price3, Digits) , stp3 = sell_takeprofit_price3;
   
               //Get Take Profit price for Fourth TARGET
               double sell_tp_price4 = Stored_SellPrice -(pips_to_ssl*Fourth_Target_Ratio) , 
                      sell_takeprofit_price4 = NormalizeDouble(sell_tp_price4, Digits) , stp4 = sell_takeprofit_price4;
      
               //Get Lot Size calculations for all FOUR Trades
               double loss_for_1_lot1 = pips_to_ssl / ts * tv ;
               double LotSize_Sell =  MathFloor( risk_amount / loss_for_1_lot1/ Lot_Step) * Lot_Step ,LotSize_Sell2 = MathFloor( risk_amount2 / loss_for_1_lot1/ Lot_Step) * Lot_Step , 
                      LotSize_Sell3 = MathFloor( risk_amount3 / loss_for_1_lot1/ Lot_Step) * Lot_Step ,LotSize_Sell4 = MathFloor( risk_amount4 / loss_for_1_lot1/ Lot_Step) * Lot_Step ;  
                   
                   

               if( SellStopPrice - OrderStopLoss() > Point / 2. )continue;
                if( OrderStopLoss() > SellStopPrice - Point && H1_Sell_Touch == "H1 Sell Touch" ) 
                    {
                     
                     if( OpenOrdersThisPair(Symbol()) < 4 )
                       {
                        SellTicketOrder1 = OrderSend(Symbol(),OP_SELLSTOP,LotSize_Sell,Stored_SellPrice,3,SellStopPrice,stp1,NULL,MagicNumber1,0,Red);
                        SellTicketOrder2 = OrderSend(Symbol(),OP_SELLSTOP,LotSize_Sell2,Stored_SellPrice,3,SellStopPrice,stp2,NULL,MagicNumber1,0,Red);
                        SellTicketOrder3 = OrderSend(Symbol(),OP_SELLSTOP,LotSize_Sell3,Stored_SellPrice,3,SellStopPrice,stp3,NULL,MagicNumber1,0,Red);
                        SellTicketOrder4 = OrderSend(Symbol(),OP_SELLSTOP,LotSize_Sell4,Stored_SellPrice,3,SellStopPrice,stp4,NULL,MagicNumber1,0,Red);
                           
                        if(SellTicketOrder1 == -1)Print("1st CALIBRATED Sell Order FAILED: ",GetLastError(), " On: ", Symbol());
                        if(SellTicketOrder1 > 0)Print("1st CALIBRATED Sell Order Placed: ", Symbol(), " LotSize_Sell is: ", LotSize_Sell, " - Take Profit is: ", stp1);
         
                        if(SellTicketOrder2 == -1)Print("2nd CALIBRATED Sell Order FAILED: ",GetLastError(), " On: ", Symbol());
                        if(SellTicketOrder2 > 0)Print("2nd CALIBRATED Sell Order Placed: ", Symbol(), " LotSize_Sell is: ", LotSize_Sell2, " - Take Profit is: ", stp2);
         
                        if(SellTicketOrder3 == -1)Print("3rd CALIBRATED Sell Order FAILED: ",GetLastError(), " On: ", Symbol());
                        if(SellTicketOrder3 > 0)Print("3rd CALIBRATED Sell Order Placed: ", Symbol(), " LotSize_Sell is: ", LotSize_Sell3, " - Take Profit is: ", stp3);
                           
                        if(SellTicketOrder4 == -1)Print("4th CALIBRATED Sell Order FAILED: ",GetLastError(), " On: ", Symbol());
                        if(SellTicketOrder4 > 0)Print("4th CALIBRATED Sell Placed: ", Symbol(), " LotSize_Sell is: ", LotSize_Sell4, " - Take Profit is: ", stp4);
                       }
                           
                     
                     DeleteOrder1 = OrderDelete(OrderTicket()); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                    
                     if(DeleteOrder1!=TRUE)Print("Sell Delete Order Failed = ",GetLastError(), " On: ", OrderSymbol());
                     if(DeleteOrder1==True)Print("Sell Order Deleted = ", OrderTicket(), " On: ", OrderSymbol());continue;
                     
                     
                    } 
               }

...

I don't think I've got the logic nailed just yet in a live environment, but in demo this works as there are no pairs to contest with. What I want this to do is if the trades need "calibrating" (i.e. stops, lots and targets) it deletes all 4 orders and places the new ones in...

When it gets to "DeleteOrder1" it doesn't necessarily have the right OrderTicket() to work with? 

Can anyone help?

(I apologize if it's hard to read!)
/
To add comments, please log in or register