I'm trying to finish up an EA and I just can't for the life of me figure out why it wont place trades.
Below is my function for placing orders. I am aware it's probably a little lackluster. This is my first EA and I'm learning.
Any assistance would be great!
Thanks!
Any errors?
Because this doesn't look good:
OP_SELL,Lots,Bid,1000,NormalizeDouble(Bid-StopLoss, Digits)
I looked it up and it said something like "object already exists" or something like that. I think it might have been an error in my print function showing me an error for a different function.
I changed the Stoploss, however, it's still saying invalid stoploss.
void PlaceOrder(){ if(CurBias == 0 && OrdersTotal() == 0 && GetSpread()<MaxSpread){//Note to self: Replace conditions with a "IsTradeAllowed" function int openbuy=OrderSend(_Symbol,OP_BUY,Lots,Ask,1000,NormalizeDouble(Ask-StopLoss-GetSpread(), Digits),NormalizeDouble(Ask+GetATR(1)+GetSpread(),Digits),"Phoenix Trade",MagicNumber,0,Blue); Alert("Order Fired!"); }else{ int opensell=OrderSend(_Symbol,OP_SELL,Lots,Bid,1000,NormalizeDouble(Bid+StopLoss+GetSpread(), Digits),NormalizeDouble(Bid-GetATR(1)-GetSpread(),Digits),"Phoenix Trade",MagicNumber,0,Blue); } }
I looked it up and it said something like "object already exists" or something like that. I think it might have been an error in my print function showing me an error for a different function.
I changed the Stoploss, however, it's still saying invalid stoploss.
Is this a price?
NormalizeDouble(Bid+StopLoss+GetSpread()
- There is no such error code 2400 in MQL4. There is however code 4200 "Object already exists", but it has nothing to do with the code you have shown.
- For your code to work correctly, the variable "StopLoss" and the functions "GetSpread()" or "GetATR()" should be in price changes and not in "points". If it is in "points" then you should scale it by multiplying it with "_Point" for example.
- All prices should not be normalised by the number of "digits". It should be rounded according to the tick size (see below).
- You are not checking if your stops are acceptable according to the symbols contract specifications. You must check for the Stops Level condition for example. Read the Requirements and Limitations in Making Trades.
- As an extra note, you should also be checking the volume (lots) properly in accordance with the contract specifications (min, max and step values).
Forum on trading, automated trading systems and testing trading strategies
Tick size vs Point(), can be a little tricky in Multicurrency EA
Fernando Carreiro, 2022.03.09 12:11
Tick Size and Point Size can be very different especially on stocks and other symbols besides forex.
Always use Tick Size to adjust and align your prices, not the point size. In essence, make sure that your price quotes, are properly aligned to the Tick size (see following examples).
... double tickSize = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE ); ... double normalised_price = round( price / tick_size ) * tick_size; ... // Or use a function double Round2Ticksize( double price ) { double tick_size = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE ); return( round( price / tick_size ) * tick_size ); };
There are several problems with your code, but here are some to get you started with correcting it. There are more.
- There is no such error code 2400 in MQL4. There is however code 4200 "Object already exists", but it has nothing to do with the code you have shown.
- For your code to work correctly, the variable "StopLoss" and the functions "GetSpread()" or "GetATR()" should be in price changes and not in "points". If it is in "points" then you should scale it by multiplying it with "_Point" for example.
- All prices should not be normalised by the number of "digits". It should be rounded according to the tick size (see below).
- You are not checking if your stops are acceptable according to the symbols contract specifications. You must check for the Stops Level condition for example. Read the Requirements and Limitations in Making Trades.
- As an extra note, you should also be checking the volume (lots) properly in accordance with the contract specifications (min, max and step values).
OK, first I would like to thank you guys for your time. :)
Second, I have attempted to put your fixes into practice. However, being very new to this, I'm sure I have probably butchered it. I'm struggling to understand some things and am doing my best to try and bumble my way through it. Please don't crucify me lol. I'm currently watching my EA to see what happens when it hits a buy trigger.
void PlaceOrder(){ int SLpips = MarketInfo(Symbol(), MODE_STOPLEVEL); int TPpips = MarketInfo(Symbol(), MODE_STOPLEVEL); //Buy Order Variables if(SLpips>0) double BuyStopLoss = NormalizeDouble((Ask-StopLoss-GetSpread())*_Point, Digits); BuyStopLoss = round(BuyStopLoss / TickSize) * TickSize; if(TPpips>0)double BuyTakeProfit = NormalizeDouble(Ask+(GetATR(1)+GetSpread())*_Point, Digits); BuyTakeProfit = round(BuyTakeProfit / TickSize) * TickSize; //Sell Order Variables if(SLpips>0)double SellStopLoss = NormalizeDouble(Bid+(StopLoss+GetSpread())*_Point, Digits); SellStopLoss = round(SellStopLoss / TickSize) * TickSize; if(TPpips>0)double SellTakeProfit = NormalizeDouble(Bid-(GetATR(1)-GetSpread())*_Point,Digits); SellTakeProfit = round(SellTakeProfit / TickSize) * TickSize; if(CurBias == 0 && OrdersTotal() == 0 && GetSpread()<MaxSpread){//Note to self: Replace conditions with a "IsTradeAllowed" function int openbuy=OrderSend(_Symbol,OP_BUY,Lots,Ask,1000,BuyStopLoss,BuyTakeProfit,"Phoenix Trade",MagicNumber,0,Blue); Alert("Order Fired!"); }else{ int opensell=OrderSend(_Symbol,OP_SELL,Lots,Bid,1000,SellStopLoss,SellTakeProfit,"Phoenix Trade",MagicNumber,0,Blue); } }Edit: As far as the stop loss. I'm eventually trying to use the ZigZag idicator to set the stoploss at a particular low/high. Haven't figured that part out yet.
That is why you have this invalid stop... OrderSend() function is expecting a price...StopLoss , takeProfit should be normalized prices.
Using Fernando's functions:
int StopLoss =300 ; // Point //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double Round2Ticksize(double price) { double tick_size = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE); return(round(price / tick_size) * tick_size); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double tickstoprice(int ticks) { double tickstoPrice= ticks * SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE); return(tickstoPrice); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ //StopLoss Price for OP_BUY double stoploss = Round2Ticksize(Ask - tickstoprice(StopLoss));
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I'm trying to finish up an EA and I just can't for the life of me figure out why it wont place trades.
Below is my function for placing orders. I am aware it's probably a little lackluster. This is my first EA and I'm learning.
Any assistance would be great!
Thanks!
EDIT: Played with things a bit, and now it says "invalid stoploss."