A Simple Stochastic indicator based MQL4 Forex Robot for Buy and Sell trades - GoTrade4me


Here is an example of a simple Forex robot in MQL4 that uses a stochastic indicator to determine buy and sell signals, with money management inputs for controlling the trade size.

First, we will need to include the necessary libraries and define some global variables:

#include <StdLib.mqh>

input double Risk = 2;
input double StopLoss = 50;
input double TakeProfit = 100;

double lots = 0.01;

int init()
{
  // Initialize the robot
  return(0);
}

void deinit()
{
  // Deinitialize the robot
}

int start()
{
  // Calculate the current stochastic value
  double stochastic = iStochastic(NULL, 0, 14, 3, 3, MODE_SMA, PRICE_CLOSE, 0);

  // Check if the stochastic value is above 80, indicating an overbought market
  if (stochastic > 80)
  {
    // Place a sell trade with the calculated lot size and specified stop loss and take profit values
    OrderSend(Symbol(), OP_SELL, lots, Ask, 3, 0, StopLoss, TakeProfit, "Sell", MagicNumber, 0, Red);
  }

  // Check if the stochastic value is below 20, indicating an oversold market
  if (stochastic < 20)
  {
    // Place a buy trade with the calculated lot size and specified stop loss and take profit values
    OrderSend(Symbol(), OP_BUY, lots, Bid, 3, 0, StopLoss, TakeProfit, "Buy", MagicNumber, 0, Blue);
  }

  return(0);
}

This robot uses the iStochastic function to calculate the current stochastic value for the specified symbol, period, and settings. The Risk input is used to calculate the lot size for the trades based on the account balance and stop loss value. The StopLoss and TakeProfit inputs are used to specify the stop loss and take profit values for the trades.

The robot checks if the stochastic value is above 80 or below 20, and if so, it places a sell or buy trade using the OrderSend function. The MagicNumber variable is used to identify the trades placed by this robot, so that it can manage them correctly.

Leave a Reply