Seeking help with my (allegedly) simple ea...

cordite

Newbie
Messages
3
Likes
0
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 (a 60 on the 5m, a 60 on the 1h, and 60 on the 4h), 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 better 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 -

EDIT:

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. |
//| Forex Trading Software: Forex Trading Platform MetaTrader 4 |
//+------------------------------------------------------------------+
#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);
}
//+------------------------------------------------------------------+
 
sma = iMA(NULL,0,60,0,MODE_SMA,PRICE_CLOSE,a);
why not just say "CLOSE,1)"? doesnt the "a" loop round over past 100 bars?

if(Close[1]>sma && Close[1]>mma && Close[1]>lma &&ready == 0)

if(Close[1]>sma && Close[1]>mma &&Close[1]<lma && ready ==0)

should this be ">"?

EDIT: sorry!!
if(Close[1]<sma && Close[1]<mma &&Close[1]<lma && ready ==0)
 
Last edited:
sma = iMA(NULL,0,60,0,MODE_SMA,PRICE_CLOSE,a);
why not just say "CLOSE,1)"? doesnt the "a" loop round over past 100 bars?

if(Close[1]>sma && Close[1]>mma && Close[1]>lma &&ready == 0)

if(Close[1]>sma && Close[1]>mma &&Close[1]<lma && ready ==0)

should this be ">"?

EDIT: sorry!!
if(Close[1]<sma && Close[1]<mma &&Close[1]<lma && ready ==0)

Ahh, that < and > error was the result of my testing various small changes to try and find the source of the problem, heh, but I forgot to reset it before posting the code.

The loop - I took that form a sample website that illustrated something vaguely similar to what I'm looking for.

My trouble is, I even took away everything but 1 SMA and a simple BUY if CLOSE[1]>SMA, and it still carried on buying no matter where the price was in relation to the SMA.

I just don't understand how to relate the Buy order to the SMA value of the close of the last bar, as no matter what I do it seems to buy independently.

Thanks a lot for your ideas so far though, any other points would be very gratefully received! I'm completely stuck!
 
have you tried putting in some debug messages?


{Alert(txt1 + ": CCI: 200-OS-Hook " + txt2 + DoubleToStr(acurrcci,2) );}


for example, replace the "CCI-hook" (ignore txt1, txt2) etc with the sma[1], mma[1] [blah, blah, blah] and Close[a] value, and see if you can see visually the values that are triggering the buys and not sells.
hope that helps somewhat.

EDIT: dont forget to display value "a" !
 
Last edited:
Heh, thanks for the very swift reply...

I'll be honest though, I didn't understand any of that :D

I only started looking at coding yesterday; before that I knew some html and that was about it! So, I'm plodding through...

I'm just very stuck now, as by logic the code I've written, I think, should work. I just can't see where I've gone wrong.

I tell the platform that when the close of the last bar is above all three SMA, then it should buy, and if it's below all three, it should sell. I don't understand why it does extra buying, and no selling...

I'm being blind I'm sure, but I've downloaded many other user made codes to look at and try to understand and I just cannot see where I've gone wrong, especially as much of what people have written is virtually in a foreign language to me, heh...
 
Top