Trying to place a grid (MQL5)

 

Hi there fellow traders,

I am trying to place a grid when I open my initial position. The orders in my grid are pending orders and are increased in lot size when they get closer to my stoploss. However, the initial stop loss and take profit remain the same. I think I'm on the right path however the behaviour seems different when I'm using this system live. 

   double currentPrice = Close(0);

   double stopLoss, takeProfit;
   double grid;

   if(canOpenLong)
     {
      stopLoss = Low(1);
      takeProfit = currentPrice + ((currentPrice - stopLoss) * riskReward);
      grid = (currentPrice - stopLoss) / gridSize + 1;
      trade.PositionOpen(_Symbol, ORDER_TYPE_BUY, 0.01, SymbolInfoDouble(Symbol(),SYMBOL_ASK), stopLoss, takeProfit);
      for(int i = 1; i < gridSize; i++)
        {
         trade.OrderOpen(_Symbol, ORDER_TYPE_BUY_LIMIT, 0.01 * (i + 1), NULL, SymbolInfoDouble(Symbol(),SYMBOL_ASK) - (grid * i), stopLoss, takeProfit);
        }
     }

I have experience in coding, however MQL5 is totally new to me. 


2024.01.31 14:14:18.745 Trades  '4832071': market buy 0.01 XAUUSD sl: 2036.62 tp: 2038.31 placed for execution
2024.01.31 14:14:18.941 Trades  '4832071': order #153067811 buy 0.01 / 0.01 XAUUSD at market done in 296.990 ms
2024.01.31 14:14:18.941 Trades  '4832071': failed buy limit 0.02 XAUUSD at 2036.15 sl: 2036.62 tp: 2038.31 [Invalid stops]
2024.01.31 14:14:18.941 Trades  '4832071': failed buy limit 0.03 XAUUSD at 2035.12 sl: 2036.62 tp: 2038.31 [Invalid stops]
2024.01.31 14:14:18.941 Trades  '4832071': failed buy limit 0.04 XAUUSD at 2034.08 sl: 2036.62 tp: 2038.31 [Invalid stops]
2024.01.31 14:14:18.941 Trades  '4832071': failed buy limit 0.05 XAUUSD at 2033.04 sl: 2036.62 tp: 2038.31 [Invalid stops]
2024.01.31 14:14:18.941 Trades  '4832071': failed buy limit 0.06 XAUUSD at 2032.00 sl: 2036.62 tp: 2038.31 [Invalid stops]
2024.01.31 14:14:18.941 Trades  '4832071': failed buy limit 0.07 XAUUSD at 2030.97 sl: 2036.62 tp: 2038.31 [Invalid stops]
2024.01.31 14:14:18.941 Trades  '4832071': failed buy limit 0.08 XAUUSD at 2029.93 sl: 2036.62 tp: 2038.31 [Invalid stops]
2024.01.31 14:14:18.941 Trades  '4832071': failed buy limit 0.09 XAUUSD at 2028.89 sl: 2036.62 tp: 2038.31 [Invalid stops]
2024.01.31 14:14:18.941 Trades  '4832071': failed buy limit 0.1 XAUUSD at 2027.85 sl: 2036.62 tp: 2038.31 [Invalid stops]
2024.01.31 14:14:18.941 Trades  '4832071': failed buy limit 0.11 XAUUSD at 2026.82 sl: 2036.62 tp: 2038.31 [Invalid stops]
2024.01.31 14:14:18.941 Trades  '4832071': failed buy limit 0.12 XAUUSD at 2025.78 sl: 2036.62 tp: 2038.31 [Invalid stops]
2024.01.31 14:14:18.945 Trades  '4832071': deal #143480535 buy 0.01 XAUUSD at 2037.17 done (based on order #153067811)




Extract profit down to the last pip
Extract profit down to the last pip
  • www.mql5.com
The article describes an attempt to combine theory with practice in the algorithmic trading field. Most of discussions concerning the creation of Trading Systems is connected with the use of historic bars and various indicators applied thereon. This is the most well covered field and thus we will not consider it. Bars represent a very artificial entity; therefore we will work with something closer to proto-data, namely the price ticks.
 
boritopalito:

Hi there fellow traders,

I am trying to place a grid when I open my initial position. The orders in my grid are pending orders and are increased in lot size when they get closer to my stoploss. However, the initial stop loss and take profit remain the same. I think I'm on the right path however the behaviour seems different when I'm using this system live. 

I have experience in coding, however MQL5 is totally new to me. 





search the site for Invalid Stops.

And since that there is no definition of riskReward, how can we help?