Работа завершена
Техническое задание
Hello MQL5 freelancers, good day.
I’m just starting to learn MQL5 and I’ve started reading the MQL5 Docs and a couple tutorials for beginners but haven’t found a good tutorial on how to place pending orders using Buy Stop and Sell Stop. I would like to know if anybody could teach me how to do what I need with one or two freelance jobs, this way I could get the grasp of it a little bit faster and start learning by the example of an experienced MQL5 programmer.
Here are the basic rules for my first EA:
1. Check ADX Indicator and determine if +DI > -DI or +DI < -DI; assign this to variables used for next steps.
a) Buy_Condition = +DI > -DI;
b) Sell_Condition = +DI < -DI;
2. If +DI > -DI the order should be Buy Stop and if +DI < -DI the order should be Sell Stop.
a) If +DI > -DI look for black candles (bears) and if +DI < -DI look for white candles (bulls).
b) Once this conditions are met respectively, open a position to buy or sell (depending on the criteria) with Open price from previous bar/candle.
3. If the order is Buy Stop, Stop Loss should be equal to: Low price from previous bar/candle -0.1 cents; if the order is Sell Stop, Stop Loss should be equal to: High price from previous bar/candle +0.1 cents.
4. The timeframe/period for this should be daily (D1).
For this EA I think I won’t be needing Moving Average nor Take Profit.
Here is the code I have started to create but could't place any orders yet:
// Input variables input double Lots = 0.1; input int StopLoss = 1000; input int TakeProfit = 1000; input int MAPeriod = 10; input int ADXPeriod = 14; // Global variables bool glBuyPlaced, glSellPlaced; // OnInit() event handler int OnInit() { //--- //--- return(0); } // OnTick() event handler void OnTick() { // Trade structures MqlTradeRequest request; MqlTradeResult result; ZeroMemory(request); // Moving average double ma[]; ArraySetAsSeries(ma,true); int maHandle=iMA(_Symbol,0,MAPeriod,MODE_SMA,0,PRICE_CLOSE); CopyBuffer(maHandle,0,0,1,ma); // ADX Indicator values double ADX_Val[], Plus_DI[], Minus_DI[]; ArraySetAsSeries(ADX_Val,true); ArraySetAsSeries(Plus_DI,true); ArraySetAsSeries(Minus_DI,true); int ADX_Handle = iADX(NULL, 0, ADXPeriod); CopyBuffer(ADX_Handle, 0, 0, 3, ADX_Val); CopyBuffer(ADX_Handle, 1, 0, 3, Plus_DI); CopyBuffer(ADX_Handle, 2, 0, 3, Minus_DI); // Open price double open[]; ArraySetAsSeries(open,true); CopyOpen(_Symbol,0,0,1,open); // High price double high[]; ArraySetAsSeries(high,true); CopyHigh(_Symbol,0,0,1,high); // Low price double low[]; ArraySetAsSeries(low,true); CopyLow(_Symbol,0,0,1,low); // Close price double close[]; ArraySetAsSeries(close,true); CopyClose(_Symbol,0,0,1,close); // Current position information bool openPosition = PositionSelect(_Symbol); long positionType = PositionGetInteger(POSITION_TYPE); double currentVolume = 0; if ( openPosition == true ) currentVolume = PositionGetDouble(POSITION_VOLUME); // Open buy market order if ( Plus_DI[0] > Minus_DI[0] && glBuyPlaced == false && (positionType != POSITION_TYPE_BUY || openPosition == false) ) { request.action = TRADE_ACTION_PENDING; request.type = ORDER_TYPE_BUY_STOP; request.symbol = _Symbol; request.volume = Lots + currentVolume; request.type_filling = ORDER_FILLING_FOK; request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK); request.sl = 0; request.tp = 0; request.type_time = ORDER_TIME_SPECIFIED; request.deviation = 50; request.stoplimit = 0; OrderSend(request,result); // Modify SL/TP if ( result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE ) { request.action = TRADE_ACTION_SLTP; do Sleep(100); while(PositionSelect(_Symbol) == false); double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN); if ( StopLoss > 0 ) request.sl = positionOpenPrice - (StopLoss * _Point); if ( TakeProfit > 0 ) request.tp = positionOpenPrice + (TakeProfit * _Point); if ( request.sl > 0 && request.tp > 0 ) OrderSend(request,result); glBuyPlaced = true; glSellPlaced = false; } } // Open sell market order else if ( Minus_DI[0] < Plus_DI[0] && glSellPlaced == false && positionType != POSITION_TYPE_SELL ) { request.action = TRADE_ACTION_PENDING; request.type = ORDER_TYPE_SELL_STOP; request.symbol = _Symbol; request.volume = Lots + currentVolume; request.type_filling = ORDER_FILLING_FOK; request.price = SymbolInfoDouble(_Symbol,SYMBOL_BID); request.sl = 0; request.tp = 0; request.type_time = ORDER_TIME_SPECIFIED; request.deviation = 50; request.stoplimit = 0; OrderSend(request,result); // Modify SL/TP if ( (result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE) && (StopLoss > 0 || TakeProfit > 0) ) { request.action = TRADE_ACTION_SLTP; do Sleep(100); while(PositionSelect(_Symbol) == false); double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN); if ( StopLoss > 0 ) request.sl = positionOpenPrice + (StopLoss * _Point); if ( TakeProfit > 0 ) request.tp = positionOpenPrice - (TakeProfit * _Point); if ( request.sl > 0 && request.tp > 0 ) OrderSend(request,result); glBuyPlaced = false; glSellPlaced = true; } } }
I hope someone could point me in the right direction and help me to solve this EA.
Regards and thank you in advance,
codeMolecules