Ok, I have tried (I am very new to coding, started yesterday) to refine the code so it's smoother and less chunky. It still just places buy orders though without following the rules correctly...
//+------------------------------------------------------------------+
//| Trend system.mq4 |
//| Copyright © 2009, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
extern double StopLoss = 150.0;
extern double TakeProfit = 300.0;
extern double LotSize = 0.01;
int start()
{
//----
int ticket;
int ready, sar, osc;
int sma, mma, lma; //---- Create the integers for the 3 SMA
for(int a=0;a<100;a++) //---- Loop
{
sma = iMA(NULL,0,60,0,MODE_SMA,PRICE_CLOSE,a); //---- Short sma, value of 60
mma = iMA(60,0,60,0,MODE_SMA,PRICE_CLOSE,a); //---- Medium sma, value of 720
lma = iMA(240,0,60,0,MODE_SMA,PRICE_CLOSE,a); //---- Long sma, value of 2880
ready = OrdersTotal();
if(Volume[0]==1) //---- assess current ticks
{
if(Close[1]>sma && Close[1]>mma && Close[1]>lma &&ready == 0) //---- establish if last candle closed above the three sma
{
ticket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,0,0,0,Green);
return(0);
}
else
if(Close[1]>sma && Close[1]>mma &&Close[1]<lma && ready ==0) //---- establish if last candle closed below the three sma
{
ticket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,0,0,0,Red);
return(0);
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Ok, I have tried (I am very new to coding, started yesterday) to refine the code so it's smoother and less chunky. It still just places buy orders though without following the rules correctly...
I'm not too sure why you are looping through the values for shift "a", maybe try leaving it at zero.
Other than that looks ok to me(nother beginer!)
You will likely need to add a print statement to get the values of sma,mma,lma and close[1] to see if they are what you think they are.
HTH
Keith
Your 100 loop... Basically, you're saying that if anywhere within the last 100 bars your condition exists, then open an order right now. You're comparing the moving average of up to 100 bars ago to the current closing price, Get rid of the loop.
Your open buy logic is if >, >, > and your open sell logic is >, >, <. I'm guessing you wanted those to all be <. And, in your second example you don't have the params right for the mma and lma.
I rewrote the code form scratch, this time omitting the loop. I also noticed that I assigned the SMA as an integer rather than a double.
Anyhow, the code now works fine.
Thanks for your time everyone helping me out :)
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hey there,
I'm having real problems trying to root out the cause of my codes failures.
I'm trying to simply create an ea that places a buy order if it is above three SMA, or places a sell order if it is below. This is not the full ea I'll be using, this is the starting process for a richer plan. But I can't get past this first road block, as whenever I run my script it doesn't seem to take notice of my buy/sell conditions, and simply places buy orders throughout no matter where the current price is (above or below any of the SMA). It also never generates sell orders.
I hope I explained this correctly, and any help will be greatly appreciated!
Many thanks!
Cord
The code -
//+------------------------------------------------------------------+
//| Trend system.mq4 |
//| Copyright © 2009, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
extern double StopLoss = 150.0;
extern double TakeProfit = 300.0;
extern double LotSize = 0.01;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int ticket;
int ready,smhma;
int sma, mma, lma; //---- Create the integers for the 3 SMA
if (Bars<2880) // Conditions of history check, to value of long sma
{
Alert("Not enough history to analyze");
return(0);
}
for(int a=0;a<100;a++) //---- Loop
{
sma = iMA(NULL,0,60,0,MODE_SMA,PRICE_CLOSE,a); //---- Short sma, value of 60
mma = iMA(NULL,0,720,0,MODE_SMA,PRICE_CLOSE,a); //---- Medium sma, value of 720
lma = iMA(NULL,0,2880,0,MODE_SMA,PRICE_CLOSE,a); //---- Long sma, value of 2880
ready = OrdersTotal();
if(Volume[0]==1) //---- assess current ticks
{
if(Close[1]>sma && Close[1]>mma && Close[1]>lma &&ready == 0) //---- establish if last candle closed above the three sma
{
ticket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,0,0,0,Green);
return(0);
}
else
if(Close[1]>sma && Close[1]>mma &&Close[1]<lma && ready ==0) //---- establish if last candle closed below the three sma
{
ticket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,0,0,0,Red);
return(0);
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+