//+------------------------------------------------------------------+ //| _simpleMA_dmy.mq4 | //| TSferro | //| | //+------------------------------------------------------------------+ #property copyright "TSferro" #property link "" #property version "1.00" #property strict #define SIGNAL_NONE 0 #define SIGNAL_BUY 1 #define SIGNAL_SELL 2 #define SIGNAL_CLOSEBUY 3 #define SIGNAL_CLOSESELL 4 //--- input parameters extern int MAper=150; extern int BBper=20; extern double BBsds=2.0; extern double StpPct=0.015; extern double Lots = 1.0; extern int Slippage = 3; extern int MagicNumber = 10101; int P = 1; int Order = SIGNAL_NONE; int Total, Ticket, Ticket2; double StopLossLevel, TakeProfitLevel, StopLevel; double LatestClose = iClose(NULL,0,1); double PriorClose = iClose(NULL,0,2); bool MAxCrossUp = False; bool MAxCrossDn = False; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- double MyMA = iMA(NULL,0,MAper,0, MODE_SMA, PRICE_CLOSE,1); MAxCrossUp = LatestClose > iMA(NULL,0,MAper,0, MODE_SMA, PRICE_CLOSE,1) && PriorClose < iMA(NULL,0,MAper,0, MODE_SMA, PRICE_CLOSE,2); MAxCrossDn = LatestClose < iMA(NULL,0,MAper,0, MODE_SMA, PRICE_CLOSE,1) && PriorClose > iMA(NULL,0,MAper,0, MODE_SMA, PRICE_CLOSE,2); //+------------------------------------------------------------------+ //| EXIT LOGIC | //+------------------------------------------------------------------+ //Here is where I am trying to identify if I already have a position open.......if so and then if MA cross, then exit.... if(MAxCrossUp) Order = SIGNAL_CLOSEBUY; // Rule to EXIT a Long trade if (SIGNAL_CLOSEBUY) OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, DarkOrange); //+------------------------------------------------------------------+ if (MAxCrossDn) Order = SIGNAL_CLOSESELL; // Rule to EXIT a Short trade if (SIGNAL_CLOSESELL) OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange); //+------------------------------------------------------------------+ //| ENTRY LOGIC | //+------------------------------------------------------------------+ if (LatestClose < PriorClose && LatestClose < MyMA) Order = SIGNAL_BUY; if (LatestClose > PriorClose && LatestClose > MyMA) Order = SIGNAL_SELL; if (SIGNAL_BUY) OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Buy",MagicNumber,0,DodgerBlue); if (SIGNAL_SELL) OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Short",MagicNumber,0,DodgerBlue); } //+------------------------------------------------------------------+
int totalOpenBuys(){ int total=0; for(int i=OrdersTotal()-1; i >= 0; i--){ if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){ if(OrderMagicNumber()== MagicNumber){ if(OrderType() == OP_BUY) total++; } } else Print("Failed to select order",GetLastError()); } return total; } int totalOpenSells(){ int total=0; for(int i=OrdersTotal()-1; i >= 0; i--){ if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){ if(OrderMagicNumber()== MagicNumber){ if(OrderType() == OP_SELL) total++; } } else Print("Failed to select order",GetLastError()); } return total; }
double LatestClose = iClose(NULL,0,1); double PriorClose = iClose(NULL,0,2);
These should be updated every new bar. They are given values globalscope so do not change.
#define SIGNAL_NONE 0 #define SIGNAL_BUY 1 #define SIGNAL_SELL 2 #define SIGNAL_CLOSEBUY 3 #define SIGNAL_CLOSESELL 4
These are not bools and should not be treated as bools
ie
if (SIGNAL_BUY)
should be-
if (Order = SIGNAL_BUY)
You use OrderTicket() without selecting an order first
You don't reset the value of Order, so next tick, it may have the same value even though conditions may have changed.
@GumRai - thanks for the extra help - I've gone ahead and moved the 2 iClose variables into the "void OnTick()" portion, and also updated my if statements as per your explanation.
I am however a little confused as to the "OrderTicket()" issue you are talking about....perhaps you would be so kind as to point me towards the process of how I should go about selecting an order and resetting it on each tick? apologies, this is all quite challenging for me =\
Don't use defines | When you mean an enumeration |
---|---|
#define SIGNAL_NONE 0 #define SIGNAL_BUY 1 #define SIGNAL_SELL 2 #define SIGNAL_CLOSEBUY 3 #define SIGNAL_CLOSESELL 4 | enum Signals{ SIGNAL_NONE, SIGNAL_BUY, SIGNAL_SELL, SIGNAL_CLOSEBUY, SIGNAL_CLOSESELL }; |
@GumRai - thanks for the extra help - I've gone ahead and moved the 2 iClose variables into the "void OnTick()" portion, and also updated my if statements as per your explanation.
I am however a little confused as to the "OrderTicket()" issue you are talking about....perhaps you would be so kind as to point me towards the process of how I should go about selecting an order and resetting it on each tick? apologies, this is all quite challenging for me =\
Janek 6191 has already given you an example of an OrderSelect() which allows access to OrderTicket()
The Order that I referred to is the globally declared variable, not a selected order.
Your problem seems to stem from the fact that you have not made any plan for this EA. You need to write down the sequence of events as if you were explaining what to do to a very stupid person. Without a plan, you cannot tell the EA to do exactly what you want. An EA cannot make assumptions.
As your code only checks bar index 1 and 2 for a signal, there is no need to check every tick, so it should be checked when a new bar opens.
Use enums as WHRoeder has pointed out.
enum Signals{ SIGNAL_NONE, SIGNAL_BUY, SIGNAL_SELL, SIGNAL_CLOSEBUY, SIGNAL_CLOSESELL };
Actually, I would use 2 enums
study this - Not tested
#property strict enum Signals{ SIGNAL_NONE,SIGNAL_BUY,SIGNAL_SELL}; enum CloseAction{ CLOSE_NONE,CLOSE_BUY,CLOSE_SELL }; //--- input parameters extern int MAper=150; extern int BBper=20; extern double BBsds=2.0; extern double StpPct=0.015; extern double Lots=1.0; extern int Slippage=3; extern int MagicNumber=10101; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { static datetime bar_time=0; bool new_bar=false; if(Time[0]!=bar_time) { new_bar=true; bar_time=Time[0]; } if(new_bar) { Signals order=SIGNAL_NONE; CloseAction exit=CLOSE_NONE; int buy_ticket=0; int sell_ticket=0; int ticket; for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol()) { if(OrderType()== OP_BUY) buy_ticket=OrderTicket(); else if(OrderType() == OP_SELL) sell_ticket=OrderTicket(); } } double latestClose= iClose(NULL,0,1); double priorClose = iClose(NULL,0,2); double MyMA_last = iMA(NULL,0,MAper,0, MODE_SMA, PRICE_CLOSE,1); double MyMA_prev = iMA(NULL,0,MAper,0, MODE_SMA, PRICE_CLOSE,2); if(latestClose<priorClose && latestClose<MyMA_last) order=SIGNAL_BUY; else if(latestClose>priorClose && latestClose>MyMA_last) order=SIGNAL_SELL; if(latestClose>MyMA_last && priorClose<MyMA_prev) exit=CLOSE_BUY; else if(latestClose<MyMA_last && priorClose>MyMA_prev) exit=CLOSE_SELL; if(order==SIGNAL_BUY && buy_ticket==0) { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Buy",MagicNumber,0,DodgerBlue); if(ticket==-1) Print("Error opening Buy order. Error code ",(string)GetLastError()); } else if(order==SIGNAL_SELL && sell_ticket==0) { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,"Short",MagicNumber,0,DodgerBlue); if(ticket==-1) Print("Error opening Sell order. Error code ",(string)GetLastError()); } if(exit==CLOSE_BUY && buy_ticket!=0) { if(OrderSelect(buy_ticket,SELECT_BY_TICKET)) { if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,clrDodgerBlue)) Print("Error closing Buy #",(string)OrderTicket()," Error code ",(string)GetLastError()); } } else if(exit==CLOSE_SELL && sell_ticket!=0) { if(OrderSelect(sell_ticket,SELECT_BY_TICKET)) { if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,clrDodgerBlue)) Print("Error closing Sell #",(string)OrderTicket()," Error code ",(string)GetLastError()); } } } //--- } //+------------------------------------------------------------------+

- 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 I am brand-spanking new new to MQl4, having come over from Tradestation, and I have run into a problem, namely how to identify if I have an open Long or Short position. In Tradestation we could use "If marketposition = 1 then....." for longs, and -1 for shorts. Obviously this is a bit more challenging in MQl4. I've been unable to find a solution online, so hopefully someone might point me in the right direction? Perhaps I have to loop through my "blotter" to see if anything matches my magic number so as to tell if the current EA has an active position?
I am attaching the code for reference - any help is much appreciated, cheers and thanks.