Here much work is needed on this one to turn it into an EA. Programming exact intersections can be tricky (at least for me). Does anyone have a better solution?
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //--TradeFrequency-----minimum time between trades in minutes //--TimeStamp----------usually this is the next ?, int StopLoss=20, TakeProfit=40, magicnumber=777, iMinute=60, TimeStamp, TradeFrequency=15; bool BuySignal, SelSignal; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ int start(){ //~~~~~~~~~~ double Avg_Current= iMA(NULL,0,30,0,MODE_SMMA,PRICE_CLOSE,1); double Avg_Previus= iMA(NULL,0,30,0,MODE_SMMA,PRICE_CLOSE,2);//--Added because if you're new it helps with indicators-crossing logics //~~~~~~~~~~ if(iOpen(NULL,PERIOD_M1,1)<Avg_Current //--Instead of asking for exact intersections use this instead. && iClose(NULL,PERIOD_M1,1)>Avg_Current){//--exect price match is unreliable .. in tester or real life BuySignal=true; SelSignal=false; //--Global bool will keep the value so EA remembers direction price is comming from } //~~~~~~~~~~ if(iOpen(NULL,PERIOD_M1,1)>Avg_Current && iClose(NULL,PERIOD_M1,1)<Avg_Current){ SelSignal=true; BuySignal=false; } //~~~~~~~~~~ if(OrdersTotal()==0){ if(TimeCurrent()>TimeStamp+TradeFrequency*iMinute){//--Recommend reading the mql4.com book for stuff u dont understand in here if(BuySignal==true && Ask<Avg_Current){ OrderSend(Symbol(),OP_BUY,1,Ask,3,Bid-StopLoss*Point,//--Buy at A ... Close at B ... thats how I remember Bid+TakeProfit*Point,"BUY",magicnumber,0,Blue); BuySignal=false; TimeStamp=TimeCurrent();//--Remember to reset static variables } } //~~~~~~~~~~ if(TimeCurrent()>TimeStamp+TradeFrequency*iMinute){ if(SelSignal==true && Bid>Avg_Current){ OrderSend(Symbol(),OP_SELL,1,Bid,3,Ask+StopLoss*Point,//--Reverse logic Ask-TakeProfit*Point,"SELL",magicnumber,0,Red); SelSignal=false; TimeStamp=TimeCurrent(); } } } //~~~~~~~~~~ Comment("BuySignal=",BuySignal," SelSignal=",SelSignal,"\n"); //~~~~~~~~~~ return(0);} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hey, thanks alot for helping me out guys,
One thing though, ubzen, I tested your code but it only opens sell positions, not sure why..
Also, I understand the exact price is not reliable, but is there a way i can open the buy/sell position when the price touches the average? (I was asked to do so hehe)
ask/bid==average doesn't seem to work.. it does nothing..
again, thanks alot for the help!!
One thing though, ubzen, I tested your code but it only opens sell positions, not sure why..
It works buy and sell for me in back-tester. Are u using backtester or live? If back-tester then maybe check your test settings. Could someone else run this and verify.
Also, I understand the exact price is not reliable, but is there a way i can open the buy/sell position when the price touches the average? (I was asked to do so hehe)
Yea, change the code to if(BuySignal==true && Ask==Avg_Current){, it might work (better) in live trading. However using equalities with double variables is a very very bad idea. Also, if price gaps/bounce over the value of average then the user would be left scratching their heads. With in back-tester, its even worse, I only have 1-minute data like most ppl. My guess is one way or another you'll have to choose a range of values you can live with..that or get really creative and that part is beyond my pay rate here. :)
ask/bid==average doesn't seem to work.. it does nothing.
I'm not sure why you're doing that. Are u trying to do ask+bid/2 ... either way don't matter. If you want something to trigger at any price example= 1.54321 .. lets call this the value of Average. It'll be next to imposable to guarantee. All u can do is > or <. Or as a compromise >1.54320 and <1.54322 the bigger the range the more reliable.
- erow1d:of course it doesn't. Doubles rarely are equal. Working with Doubles in MQL4 - MQL4 Articles
ask/bid==average doesn't seem to work.. it does nothing..
Try
if ( MathAbs(ask-average) < pip2dbl ) // close.
- You probably don't want to test for closeness but instead test for it to cross.
static double prevBid; if ( (prevBid-average)*(Bid-average) ){ // Bid just crossed average ... } prevBid = Bid;
If the EA only trades at the start of a bar, replace prevBid and bid with Close[2], close[1] if(OrdersTotal()==0){
This makes the EA incompatible with any other EA (including itself) and manual trading, One chart Only.int count=0; for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if ( OrderSelect(pos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == magic.number // my magic number && OrderSymbol() == Symbol() ){ // and my pair. count++; } if (count == 0){
sorry guys, when i said "ask/bid==average" I had a different meaning. I meant if I try ask==average or bid==average, nothing happens.
WHRoeder:
I'm trying to use your method but Im getting an error 'comparison expression expected'. Also, this is what Im trying:
if (Ask >= average) { //price is higher than the average BuySignal=true; SellSignal=false; } if (BuySignal==true) //if its higher then { if ( (prevBid-average)*(Bid-average) ){ // when bid crosses average: buy it. OrderSend(Symbol(),OP_BUY,1,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point, "BUY",NumeroMagico,0,Blue); return; } } prevBid = Bid; //not really sure where I put this
Thanks for the inputs WHRoeder.
Also @ erow1d, you could be having some problems because of differences in broker digits. The EA does not take that into account.
what i do then? haha
=/
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi guys, I'm new to this mql4 expert programming.
I need some help.
I'm trying to do an expert like this:
When the Ask price is higher than the smooth average, it waits until it goes down equal to the average and open a buy operation.
or when the Bid price is lower than the smooth average, it waits until it goes up equal to the average and open a sell operation.
this is what i have so far, but it's not working.
any tips?
thanks!!