Can anyone help me with code on how to have only one open position with magic no. in mql5.
Similar questions have been asked - have a look at these threads to get an idea how to loop through positions and query their properties:
https://www.mql5.com/en/forum/429908#comment_41190517- 2022.08.02
- www.mql5.com
Similar questions have been asked - have a look at these threads to get an idea how to loop through positions and query their properties:
https://www.mql5.com/en/forum/429908#comment_41190517Thank You so much but my code is not working properly. Can you help me plz. I want to have only one open position at one time.
#include <Trade/Trade.mqh> input ENUM_TIMEFRAMES TimeFrames = PERIOD_CURRENT; input int MaPeriod = 200; input double Lots = 0.1; input double slValue = 0.0050; input double tpValue = 0.0050; int handleMa; int handleMacd; int barsTotal; CTrade trade ; double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID); double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK); int buy_count = 0; int sell_count = 0; int OnInit() { handleMa = iMA(_Symbol,TimeFrames,MaPeriod,0,MODE_SMA,PRICE_CLOSE); handleMacd = iMACD(_Symbol,TimeFrames,12,26,9,PRICE_CLOSE); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { } void OnTick() { buy_count = 0; sell_count = 0; //#1 initialize counts for(int i = PositionsTotal() - 1 ; i >= 0 ; i--) //#2 not i <= 0 { PositionGetTicket(i); //#3 Missing PositionGetTicket(i); if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) //#4 not PositionSelect(_Symbol) { buy_count++; } if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) //#4 not PositionSelect(_Symbol) { sell_count++; } double Ma []; CopyBuffer(handleMa,BASE_LINE,0,1,Ma); double MacdMain[], MacdSignal[]; CopyBuffer(handleMacd,MAIN_LINE,0,2,MacdMain); CopyBuffer(handleMacd,SIGNAL_LINE,0,2,MacdSignal); bid = NormalizeDouble(bid,_Digits); ask = NormalizeDouble(ask,_Digits); if(bid < Ma[0] && MacdMain[0] > 0 && MacdMain[0] < MacdSignal[0] && MacdMain[1] > MacdSignal[1]){ Print("Sell Entry is triggering..."); double sl= bid+ slValue; sl = NormalizeDouble(sl,_Digits); double tp= bid- tpValue; tp = NormalizeDouble(tp,_Digits); trade.Sell(Lots,_Symbol,bid,sl,tp); } else if(ask > Ma[0] && MacdMain[0] < 0 && MacdMain[0] > MacdSignal[0] && MacdMain[1] < MacdSignal[1]){ Print("Buy Entry is triggering..."); double sl= ask- slValue; sl = NormalizeDouble(sl,_Digits); double tp= ask+ tpValue; tp = NormalizeDouble(tp,_Digits); trade.Buy(Lots,_Symbol,ask,sl,tp); } } }
Thank You so much but my code is not working properly. Can you help me plz. I want to have only one open position at one time.
You seem to have copied the code exactly - it was intended as an example to get you started.
Change the loop so that your read the magic number - you can then compare it with the magic number of your EA and decide whether to open or not.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use