Ok. Then could you guide me where to find the place to learn how to code, the BUY/SELL action, under 3 conditions I explained before? The main problem for me are the "conditions"
Jorgehk:
Ok. Then could you guide me where to find the place to learn how to code, the BUY/SELL action, under 3 conditions I explained before? The main problem for me are the "conditions"
Ok. Then could you guide me where to find the place to learn how to code, the BUY/SELL action, under 3 conditions I explained before? The main problem for me are the "conditions"
1) In the reference of the editor are code snippets examples.
2) GOOGLE e.g. for "EA template mt4"
3) Use the EA that exits in the MQL\expert folder and modify it
...
Hi everybody. I tried to code my strategy, but still has some mistakes. Could you give me some advices?
// HEM system EA //Heiken AShi + EMA + MACD// //------------------------------------- #property indicator_chart_window #property indicator_buffers 4 #property indicator_color1 Red #property indicator_color2 Green #property indicator_color3 Red #property indicator_color4 Green //---- buffers double ExtMapBuffer1[]; double ExtMapBuffer2[]; double ExtMapBuffer3[]; double ExtMapBuffer4[]; //---- int ExtCountedBars=0; //---- double Lots= 0.01; int EMA_Period= 63; double MacdCurrent,MacdPrevious,SignalCurrent; double EMACurrent,EMAPrevious,EMASignal,Price_open; double Heiken_AshiPreviousBar = Open[pos -1]; double Heiken_AshiCurrentBar = Open[pos]; double PriceAsk, PriceBid, Price, TakeProfit; int Break_Even_After_X_Pips = 15; bool useMagicNumber = false; int Magic = 1234567; int init() { SetIndexStyle(0,DRAW_HISTOGRAM, 0, 1, Red); //Heiken Ashi SetIndexBuffer(0, ExtMapBuffer1); SetIndexStyle(1,DRAW_HISTOGRAM, 0, 1, Green); SetIndexBuffer(1, ExtMapBuffer2); SetIndexStyle(2,DRAW_HISTOGRAM, 0, 3, Red); SetIndexBuffer(2, ExtMapBuffer3); SetIndexStyle(3,DRAW_HISTOGRAM, 0, 3, Green); SetIndexBuffer(3, ExtMapBuffer4); //---- SetIndexDrawBegin(0,10); SetIndexDrawBegin(1,10); SetIndexDrawBegin(2,10); SetIndexDrawBegin(3,10); //---- indicator buffers mapping SetIndexBuffer(0,ExtMapBuffer1); SetIndexBuffer(1,ExtMapBuffer2); SetIndexBuffer(2,ExtMapBuffer3); SetIndexBuffer(3,ExtMapBuffer4); if(Digits == 5) //Break Even { Break_Even_After_X_Pips = Break_Even_After_X_Pips * 10; } return(0); } int deinit() { return(0); } int start() { double haOpen, haHigh, haLow, haClose; if(Bars<=10) return(0); ExtCountedBars=IndicatorCounted(); //---- check for possible errors if (ExtCountedBars<0) return(-1); //---- last counted bar will be recounted if (ExtCountedBars>0) ExtCountedBars--; int pos=Bars-ExtCountedBars-1; while(pos>=0) { haOpen=(ExtMapBuffer3[pos+1]+ExtMapBuffer4[pos+1])/2; haClose=(Open[pos]+High[pos]+Low[pos]+Close[pos])/4; haHigh=MathMax(High[pos], MathMax(haOpen, haClose)); haLow=MathMin(Low[pos], MathMin(haOpen, haClose)); if (haOpen<haClose) { ExtMapBuffer1[pos]=haLow; ExtMapBuffer2[pos]=haHigh; } else { ExtMapBuffer1[pos]=haHigh; ExtMapBuffer2[pos]=haLow; } ExtMapBuffer3[pos]=haOpen; ExtMapBuffer4[pos]=haClose; pos--; } //---- MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0); MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1); SignalCurrent=MacdCurrent-MacdPrevious; EMACurrent=iMA(NULL,0,Lots,0,MODE_EMA,PRICE_CLOSE,0); EMAPrevious=iMA(NULL,0,Lots,0,MODE_EMA,PRICE_CLOSE,1); EMASignal=EMACurrent-EMAPrevious; TakeProfit=200; PriceAsk= Ask ; PriceBid= Bid ; //+CONDITIONS+ if (SignalCurrent > 0 && Price > EMACurrent && EMASignal > 0 && Open[pos] > Open[pos -1] && Open[pos] > EMACurrent) //Long position orders { int ticket=OrderSend(Symbol(),OP_BUY,Lots,PriceBid,3,EMACurrent -10,TakeProfit,0,1111,0,Green); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) return (0); } } OrderClose;if {(SignalCurrent < 0 && Price < EMACurrent -10 && EMASignal < 0 && POpen[pos] < Open[pos -1] && Open[pos] < EMACurrent) return; } if (SignalCurrent < 0 && Price < EMACurrent && EMASignal < 0 && Open[pos] < Open[pos -1] && Open[pos] < EMACurrent) //Short position orders { int ticket=OrderSend (Symbol(),OP_SELL,Lots,PriceAsk,3,EMACurrent +10,TakeProfit,0,2222,0, Red); return; if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) return (0); } } OrderClose if { (SignalCurrent > 0 && Price > EMACurrent +10 && EMASignal > 0 && Open[pos] > Open[pos -1] && Open[pos] > EMACurrent) return; } { AdjustStopLoss(); return(0); } //+------------------------------------------------------------------+ void AdjustStopLoss() { for(int cnt=OrdersTotal()-1;cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if(useMagicNumber == true) { if (OrderMagicNumber()==Magic && OrderStopLoss() != OrderOpenPrice()) { if(( OrderProfit() - OrderCommission() ) / OrderLots() / MarketInfo( OrderSymbol(), MODE_TICKVALUE ) >= Break_Even_After_X_Pips) { if(OrderType()==OP_SELL){ OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red); } if(OrderType()==OP_BUY){ OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue); } } } } else if(useMagicNumber == false) { if(OrderStopLoss() != OrderOpenPrice()) { if(( OrderProfit() - OrderCommission() ) / OrderLots() / MarketInfo( OrderSymbol(), MODE_TICKVALUE ) >= Break_Even_After_X_Pips) { if(OrderType()==OP_SELL){ OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red); } if(OrderType()==OP_BUY){ OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue); } } } } }
Jorgehk: but still has some mistakes. Could you give me some advices?
- You coded an indicator. Indicator's can not sleep, therefor they can not trade (server calls take time.) Make it an EA.
if(Digits == 5) //Break Even { Break_Even_After_X_Pips = Break_Even_After_X_Pips * 10;
Every time you change pair or TF you go through a deinit/init cyccle. Therefor you variable becomes 10x 100x 1000x. Never modify your externals.ExtCountedBars=IndicatorCounted();
Only in indicators
if (ExtCountedBars>0) ExtCountedBars--; int pos=Bars-ExtCountedBars-1; while(pos>=0) { haOpen=(ExtMapBuffer3[pos+1]+ExtMapBuffer4[pos+1])/2;
No need for the decrement. Contradictory information on IndicatorCounted() - MQL4 forum
Code your lookback (1) properly.int counted = IndicatorCounted(); int lookback = ... // iMA(period) has look back of period. // buffer[i+2] has look back of 2 // use maximum of all. for(int iBar = Bars - 1 - MathMax(lookback, counted); iBar >= 0; --iBar) ...
int ticket=OrderSend(Symbol(),OP_BUY,Lots,PriceBid,3,EMACurrent -10,TakeProfit,0,1111,0,Green); if(ticket>0)
- Check your return codes
What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
- EMAcurrent is a price (1.2345) minus 10 is a negative number. You need ten pips.
- Check your return codes
What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) return (0);
Why are you selecting an order only to return. Are selections kept between calls? Have you tested it?OrderClose;if {(SignalCurrent < 0 && Price < EMACurrent -10 && EMASignal < 0 && POpen[pos] < Open[pos -1] && Open[pos] < EMACurrent) return; }
- Why are you posting code that doesn't even compile?
- You've exited the while loop so pos is -1 here. There is no X[-2]
if(( OrderProfit() - OrderCommission() ) / OrderLots() / MarketInfo( OrderSymbol(), MODE_TICKVALUE ) >= Break_Even_After_X_Pips)
- Don't use TickValue by itself. Only as a ratio with TickSize. See DeltaValuePerLot()
- Some brokers (e.g. FXCM) do not use OrderCommission, they create a history entry (type=6)
Thanks a lot! Let me resolve it and learn.
Jorgehk:
I haven't understood something. Does that mean inside bar look change look-like:- BUY if : ...+ [Heiken Ashi Smoothed (on 2nd Green bar) after changing from red bars, ...
bool lastTicks_HA_notUP = true; if ( lastTicks_HA_notUP ) { /* means small swing? */ }
than what, before green comes is signal, or wait for Green confirmation again??
if ( true ) TicksLast_HA_Up = false; /* global bool */ if ( TicksLast_HA_Up ) { /* means no need bar down/swing? */ if ( true ) BarLast_HA_Up = true; /* means current -1 bar doesn't matter, entry buy? */ }
(I take in account that you will fix the compiling errors on your own). The other things are working?
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi everybody. I would like to develop an EA, but unfortunately I'm horrible managing "code", specially when I have to add conditions.
The system is easy to understand. And should work for all pairs, and all time frames. And also only 1 opened trade allowed by pair, at the same time)
* options : ( window when you add EA on chart)
- Lots = A
- Stoploss= B ( pips against your trade )
- Break even= C ( after C pips on profit way, add break even and delete stoploss )
- EMA = D ( D=Period)
- MACD= E, F, G ( E=Fast EMA value, F= slow EMA value, G= MACD SMA value)
* Action:
- BUY if : MACD has uptrend movement + Price is above EMA + [Heiken Ashi Smoothed (on 2nd Green bar) after changing from red bars, and above EMA]
Close trade when Heiken Ashi Smoothed bar closes red. (Take Profit)
- SELL if: MACD has downtrend movement + Price is below EMA + [Heiken Ashi Smoothed (on 2nd red bar) after changing from Green bars, and below EMA]
Close trade when Heiken Ashi Smoothed bar closes Green. (Take Profit)
Thanks for your help!