Hi!
I tried as you say but now in the back test the EA doesn't take any trade. No error reported, just never open a trade.
#include <Trade\Trade.mqh> #include <Trade\PositionInfo.mqh> #define EXPERT_MAGIC 1 CTrade myTradingControlPanel; double currentAsk; double StopLose, TakeProfit, Size; //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK); for (int i = 0; i < PositionsTotal(); i++) { if(PositionSelectByTicket(PositionGetTicket(i)) && PositionGetInteger(POSITION_MAGIC) == EXPERT_MAGIC) { } else { StopLose = xxxx; TakeProfit = xxxx; Size = xxxx; MqlTradeRequest request; request.action = TRADE_ACTION_DEAL; request.magic = EXPERT_MAGIC; request.symbol = _Symbol; request.volume = Size; request.type = ORDER_TYPE_BUY; request.type_filling = ORDER_FILLING_FOK; request.price = currentAsk; request.sl = StopLose; request.tp = TakeProfit; MqlTradeResult result; myTradingControlPanel.OrderSend(request,result); } } //+------------------------------------------------------------------+
Excuse me, but I did not understand, what do you want to do by your code.
My EA can open a position in different scenarii.
I allow my EA to open only one position at a time per scenarii. So for example if I have 5 scenarii then the EA can send 5 different OrderSend() for open a position each one on top of each other.
So that's means : if the conditions are meet for open a position in one of this scenarii BUT there is already a position opened before because this scenarii and this position is still open : then I not allow my EA to send the order for add to this position.
Not sure I'm really clear in my explanation... Tell me if it's the case I will try to explain differently.
My EA can open a position in different scenarii.
I allow my EA to open only one position at a time per scenarii. So for example if I have 5 scenarii then the EA can send 5 different OrderSend() for open a position each one on top of each other.
So that's means : if the conditions are meet for open a position in one of this scenarii BUT there is already a position opened before because this scenarii and this position is still open : then I not allow my EA to send the order for add to this position.
Not sure I'm really clear in my explanation... Tell me if it's the case I will try to explain differently.
Ok.
Yes, the code I posted here is simplified to show only one scenarii, but I have defined several Magic numbers for each scenarii that are implemented during the Mqltraderequest orderSend() of each scenarii.
#include <Trade\Trade.mqh> #define EXPERT_MAGICM1 1 #define EXPERT_MAGICM2 2 CTrade myTradingControlPanel; double currentAsk; double StopLose, TakeProfit, Size; //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK); // Get latest Ask Price if(bunch of condition and then the last one is : no position already opened with this specific magic number) { for (int i = 0; i < PositionsTotal(); i++) { if(PositionSelectByTicket(PositionGetTicket(i)) && PositionGetInteger(POSITION_MAGIC) == EXPERT_MAGICM1) { } else { StopLose = xxx; TakeProfit = xxx; Size = xxx; MqlTradeRequest request; request.action = TRADE_ACTION_DEAL; request.magic = EXPERT_MAGICM1; request.symbol = _Symbol; request.volume = Size; request.type = ORDER_TYPE_BUY; request.type_filling = ORDER_FILLING_FOK; request.price = currentAsk; request.sl = StopLose; request.tp = TakeProfit; MqlTradeResult result; myTradingControlPanel.OrderSend(request,result); } } if(another bunch of condition and then the last one is : no position already opened with this specific magic number) { for (int i = 0; i < PositionsTotal(); i++) { if(PositionSelectByTicket(PositionGetTicket(i)) && PositionGetInteger(POSITION_MAGIC) == EXPERT_MAGICM2) { } else { StopLose = xxx; TakeProfit = xxx; Size = xxx; MqlTradeRequest request; request.action = TRADE_ACTION_DEAL; request.magic = EXPERT_MAGICM2; request.symbol = _Symbol; request.volume = Size; request.type = ORDER_TYPE_BUY; request.type_filling = ORDER_FILLING_FOK; request.price = currentAsk; request.sl = StopLose; request.tp = TakeProfit; MqlTradeResult result; myTradingControlPanel.OrderSend(request,result); } } } //+------------------------------------------------------------------+
I don't understand how to " have method of checking for positions absence by certain magic number. If such method returns true, then you can allow EA to open position."
Yes, the code I posted here is simplified to show only one scenarii, but I have defined several Magic numbers for each scenarii that are implemented during the Mqltraderequest orderSend() of each scenarii.
I don't understand how to " have method of checking for positions absence by certain magic number. If such method returns true, then you can allow EA to open position."
Try to use one loop for identifying and after loop ending open or not open position.
Okay, so I try to code something as you said. No error on the compilation, but when I run it in the strategy tester, I get an "invalid request" error. But I didn't change anything on my OrderSend() request, and it was working perfectly fine before.
Obviously I'm still missing something, what is it?
#include <Trade\Trade.mqh> #define EXPERT_MAGICM1 1 #define EXPERT_MAGICM2 2 CTrade myTradingControlPanel; double currentAsk; double StopLose, TakeProfit, Size; bool HavePosition; //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { currentAsk = SymbolInfoDouble(_Symbol,SYMBOL_ASK); if (bunch of condition) { for(int v = PositionsTotal() - 1;v >= 0 || HavePosition == true ; v--) { ulong positionticket = PositionGetTicket(v); if(PositionSelectByTicket(positionticket)) { if(PositionGetInteger(POSITION_MAGIC) == EXPERT_MAGICM1) { HavePosition = true; } else { HavePosition = false; } } } if(HavePosition == false) { StopLose = xxx; TakeProfit = xxx; SizeB = xxx; MqlTradeRequest request; request.action = TRADE_ACTION_DEAL; request.magic = EXPERT_MAGICM1; request.symbol = _Symbol; request.volume = Size; request.type = ORDER_TYPE_BUY; request.type_filling = ORDER_FILLING_FOK; request.price = currentAsk; request.sl = StopLose; request.tp = TakeProfit; MqlTradeResult result; myTradingControlPanel.OrderSend(request,result); } } if (bunch of condition) { for(int v = PositionsTotal() - 1;v >= 0 || HavePosition == true; v--) { ulong positionticket = PositionGetTicket(v); if(PositionSelectByTicket(positionticket)) { if(PositionGetInteger(POSITION_MAGIC) == EXPERT_MAGICM2) { HavePosition = true; } else { HavePosition = false; } } } if(HavePosition == false) { StopLose = xxx; TakeProfit = xxx; SizeB = xxx; MqlTradeRequest request; request.action = TRADE_ACTION_DEAL; request.magic = EXPERT_MAGICM2; request.symbol = _Symbol; request.volume = Size; request.type = ORDER_TYPE_SELL; request.type_filling = ORDER_FILLING_FOK; request.price = currentAsk; request.sl = StopLose; request.tp = TakeProfit; MqlTradeResult result; myTradingControlPanel.OrderSend(request,result); } } } //+------------------------------------------------------------------+
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
I'm stuck with what should be an easy problem to solve, apparently, but I'm going in circle without finding the answer (read a lot of thread and MQL documentation).
I want to check if a specific position is currently opened by using either the magic number or the order ticket of the OrderSend() request that potentially opened this position.
Here is the code :
I tried something with the magic number and the ticker number like this, no error is report in the code but it's doesn't work as wished :
Can you explain me like I'm 5 year old how to code this thing?
Thank you.