Stuck trying to create first EA - SMA crossover
Ive been trying to code a simple SMA crossover where positions are opened and closed based on the MAs crossing over. Ive found a helpful youtube video which i copied some code from which develops and EA that is indeed an SMA crossover. Unfortunately it does not open or close positions. How can I rectify this problem? The code is as follows:
1. Please use the SRC button in the toolbar when entering source code. It makes it much easier to read.
2. Look at the OrderSend() function.
https://docs.mql4.com/trading/ordersend
Will keep that in mind. Thanks for the link, will look into it.
Cheers
Hello all,
Ive been trying to code a simple SMA crossover where positions are opened and closed based on the MAs crossing over. Ive found a helpful youtube video which i copied some code from which develops and EA that is indeed an SMA crossover. Unfortunately it does not open or close positions. How can I rectify this problem? The code is as follows:
extern double Lots = 0.1;
extern int MagicNumber = 12345;
void OnTick()
{
// current chart, current period, 20 candles, no shift, simple, close price
double SlowMovingAverage = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,0);
// current chart, current period, 20 candles, no shift, simple, close price
double LastSlowMovingAverage = iMA(NULL,0,20,0,MODE_SMA,PRICE_CLOSE,1);
// current chart, current period, 10 candles, no shift, simple, close price
double FastMovingAverage = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0);
// current chart, current period, 10 candles, no shift, simple, close price
double LastFastMovingAverage = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE, 1);
// if the fast sma is now above
if ((LastFastMovingAverage < LastSlowMovingAverage)
&&(FastMovingAverage > SlowMovingAverage))
// Chart output for Buy Signal
Comment ("BUY");
// if the fast sma is now above
if ((LastFastMovingAverage > LastSlowMovingAverage)
&&(FastMovingAverage < SlowMovingAverage))
// Chart output for Buy Signal
Comment ("SELL");
}
Any advice would be appreciated, thanks all.