Download MetaTrader 5

Capture a indicator value at open position

To add comments, please log in or register
Guilherme Moniz Caixeta
144
Guilherme Moniz Caixeta  
I have a doubt to resolve a problem, and if anyone could help me I would be very grateful.

I am trying to create a fix stoploss, using the value of indicator at the bar that gives the buy signal.

For example:



In this case, the position will be open at the new bar open.

I am trying to capture the value of indicator at the bar that gives the buy signal.


double StopLoss=0;

if(newBar == true)
        {
 for(int i=0;i<30;i++)
   {
   
    if(openPosition == false && close[barShift]  > ma[barShift] && low[barShift]  < ma[barShift])  // check if not in a trade and valid entry trigger
    {                           
                
            StopLoss=STL[i+1];  // Capture Stoploss Threshold at entry
      
    }   
   
   
   ....
   ....


                // Modify SL/TP
                if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
                {
                        request.action = TRADE_ACTION_SLTP;

                        do Sleep(100); while(PositionSelect(_Symbol) == false);
                        double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);

                        if(StopLoss > 0) request.sl = StopLoss;

                        if(request.sl > 0 ) OrderSend(request,result);

                        
                }
                break; 
   }



When backtest starts, the first position always works, adjusting the stoploss value, and closing the position.





The Error message starts to appear, after finished the first position and trying to place stop on second condition...





The whole code:


#include <Mql5x\Timer2.mqh>
CTimer Timer;
CNewBar NewBar;

// Input variables
input double TradeVolume=10;

input int TakeProfit=1000;
input int MAPeriod=10;

input ulong Slippage = 3;
input bool TradeOnNewBar = true;

// Global variables



// OnTick() event handler
void OnTick()
{

        // Trade structures
   MqlTradeRequest request;

   MqlTradeResult result;

   ZeroMemory(request);

   ZeroMemory(result);

   // Check for new bar
        bool newBar = true;
        int barShift = 0;
        
        if(TradeOnNewBar == true) 
        {
                newBar = NewBar.CheckNewBar(_Symbol,_Period);
                barShift = 1;
        }
        
        
        // Moving average
        double ma[];
        ArraySetAsSeries(ma,true);
        
        double STL[];
        ArraySetAsSeries(STL,true);

        int STLH=iMA(_Symbol,0,3,MODE_SMA,0,PRICE_LOW);
        CopyBuffer(STLH,0,0,3,STL);

        int maHandle=iMA(_Symbol,0,MAPeriod,MODE_SMA,0,PRICE_CLOSE);
   CopyBuffer(maHandle,0,0,3,ma);
   
   double StopLoss=0;

   // Close price
   double close[];
   ArraySetAsSeries(close,true);
   CopyClose(_Symbol,0,0,3,close);
   
   // Close price
   double high[];
   ArraySetAsSeries(high,true);
   CopyHigh(_Symbol,0,0,3,high);
   
   // Close price
   double low[];
   ArraySetAsSeries(low,true);
   CopyLow(_Symbol,0,0,3,low);

 
   // Current position information
   bool openPosition = PositionSelect(_Symbol);
   long positionType = PositionGetInteger(POSITION_TYPE);

   double currentVolume = 0;
   if(openPosition == true) currentVolume = PositionGetDouble(POSITION_VOLUME);

   
   // Order placement
        if(newBar == true)
        {

   for(int i=0;i<30;i++)
   {
   
    if(openPosition == false && close[barShift]  > ma[barShift] && low[barShift]  < ma[barShift])  // check if not in a trade and valid entry trigger
    {                           
                
            StopLoss=STL[i+1];  // Capture Stoploss Threshold at entry
      
    }   
   
   
   // Open buy market order
   if(close[barShift]  > ma[barShift] && low[barShift]  < ma[barShift] &&  openPosition == false)
   {
        request.action = TRADE_ACTION_DEAL;
                request.type = ORDER_TYPE_BUY;
                request.symbol = _Symbol;
                request.volume = TradeVolume + currentVolume;
                request.type_filling = ORDER_FILLING_FOK;


                OrderSend(request,result);

                // Modify SL/TP
                if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
                {
                        request.action = TRADE_ACTION_SLTP;

                        do Sleep(100); while(PositionSelect(_Symbol) == false);
                        double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);

                        if(StopLoss > 0) request.sl = StopLoss;

                        if(request.sl > 0 ) OrderSend(request,result);

                        
                }
                break; 
   }
 } 
}
 
}
Alain Verleyen
Moderator
32025
Alain Verleyen  

There is so many problems with you code that it's surprising it place a trade

See your issue about stoploss. Why are using this loop ?

   for(int i=0;i<30;i++)

Inside this loop you set :

StopLoss=STL[i+1]; 

But your STL array can only have a size of 3.

CopyBuffer(STLH,0,0,3,STL);

Print your stoploss value, and the market price at the time you got the error.

I strongly suggest you to check the returned value of any function you are using, they can produce an error.

Also you have to normalize your price.

Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
  • www.mql5.com
Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
To add comments, please log in or register