How to fix Error 130 on MT4 Expert Advisor Short trades? - mt4

I am trying to get this code to trade short. It trades long but give MT4 Error 130 invalid stop loss and is not going short. What do I change so this code goes short?
Code:
string symbol = "EURUSD"; // symbol
double volume = .01; // volume
int slippage = 0; // slippage
double pricelong = Ask; // price
string cmdlong = "OP_BUYLIMIT"; // operation
double stoplosslong = Ask - .0005; // stop loss
double takeprofitlong = Bid + .0005; // take profit
string commentlong="Long"; // comment
color arrow_color_long=Green; // color
double priceshort = Bid; // price
string cmdshort = "OP_SELLLIMIT"; // operation
double stoplossshort = 25; // stop loss
double takeprofitshort = 50; // take profit
string commentshort="Short"; // comment
color arrow_color_short=Red; // color
int magic=0; // magic number
datetime expiration=0; // pending order expiration
orderresults = OrderSend(symbol, cmdshort , volume, priceshort, slippage, stoplossshort, takeprofitshort, commentshort, magic, expiration, arrow_color_short);

Related

How can I find the last moving average crossover price x candle before in mql4?

I want to code a function which will return the price of the last moving average crossover. I will be really happy if anyone here helps me to achive this in mql4 code..
int manyCandles=1000, crossOverCandle;
double theOpenPrice;
for(int lastCandle=1; lastCandle<manyCandles; lastCandle++){
crossOverCandle=0;
double firstMA = iMA(_Symbol,_Period,maPeriod,0,MODE_SMA,PRICE_CLOSE,lastCandle);
double secondMA = iMA(_Symbol,_Period,maPeriod,0,MODE_SMA,PRICE_CLOSE,lastCandle);
double firstMAlast = iMA(_Symbol,_Period,maPeriod,0,MODE_SMA,PRICE_CLOSE,lastCandle+1);
double secondMAlast = iMA(_Symbol,_Period,maPeriod,0,MODE_SMA,PRICE_CLOSE,lastCandle+1);
if(secondMAlast>firstMAlast && secondMA<firstMA){
theOpenPrice = Open[lastCandle];
crossOverCandle = lastCandle;
Break;
}
}
Comment("The last cross over happened on ",lastCandle," candle, the price was ",theOpenPrice);
You can try the above code.

MQL4 question How open multiple orders in EA

I still don't know how code to open multiple orders in EA.
I saw some EA open orders multiple times example buy order open first time and next buy order will open after direction is correct.
How can code it.
Thanks you.
Q : How open multiple orders in EA?
for example this way:
#define NotSetHERE 0.
int retCode = OrderSend( _Symbol, // string symbol, // symbol
OP_BUY, // int cmd, // operation
1.0, // double volume, // volume
Ask, // double price, // price
10, // int slippage, // slippage
NotSetHERE, // double stoploss, // stop loss
NotSetHERE, // double takeprofit, // take profit
"DEMO1", // string comment = NULL, // comment
-1, // int magic = 0, // magic number
0, // datetime expiration = 0, // P/O expiration
clrRed // color arrow_color = clrNONE // color
);
...
...
int retCod2 = OrderSend( "XAGUSD", // string symbol, // symbol
OP_BUY, // int cmd, // operation
2.0, // double volume, // volume
Ask, // double price, // price
10, // int slippage, // slippage
NotSetHERE, // double stoploss, // stop loss
NotSetHERE, // double takeprofit, // take profit
"DEMO2", // string comment = NULL, // comment
-2, // int magic = 0, // magic number
0, // datetime expiration = 0, // P/O expiration
clrWhite // color arrow_color = clrNONE // color
);
...
...
int retCod3 = OrderSend( "EURCHF", // string symbol, // symbol
OP_BUY, // int cmd, // operation
3.0, // double volume, // volume
Ask, // double price, // price
10, // int slippage, // slippage
NotSetHERE, // double stoploss, // stop loss
NotSetHERE, // double takeprofit, // take profit
"DEMO3", // string comment = NULL, // comment
-3, // int magic = 0, // magic number
0, // datetime expiration = 0, // P/O expiration
clrGreen // color arrow_color = clrNONE // color
);

How to convert send order from mql4 to mql5

Below is the method I'm using to place an order after three minutes if it doesn't go through. I've converted the larger part of it from mql4 to mql5. It's just the commented part that I'm not sure how I'll change to mql5 since in mql5 send orders return bool's and not int's. I would be glad if I could get help with fixing this remaining part.
void MakeOrders()
{
static datetime lastTime = 0;
datetime currTime = iTime(Symbol(),PERIOD_M3,0);
if (currTime>lastTime)
{
for (int i=ObjectsTotal(0, 0, -1)-1; i>=0; i--)
{
string name = ObjectName(0, i, 0, -1);
if (ObjectGetString(0, name, OBJPROP_NAME, 0)==OBJ_RECTANGLE && ObjectGetString(0,name,OBJPROP_TEXT)=="")
{
double entryPrice=ObjectGetDouble(0,name,OBJPROP_PRICE,1)-3*_Point;
double stopLoss=ObjectGetDouble(0,name,OBJPROP_PRICE,2);
double slDist=fabs(entryPrice-stopLoss);
double dTakeProfit=entryPrice-2*slDist;
MqlTradeRequest request={0};
MqlTradeResult result={0};
//--- parameters of request
request.action =TRADE_ACTION_DEAL; // type of trade operation
request.symbol =Symbol(); // symbol
request.volume =lotSize; // volume of 0.1 lot
request.type =ORDER_TYPE_BUY_LIMIT; // order type
request.price = entryPrice; // price for opening
//request.deviation=5; // allowed deviation from the price
request.magic =magicnumber; // MagicNumber of the order
request.tp = dTakeProfit;
request.sl = stopLoss;
//--- send the request
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError());
/*
int ticketSell = OrderSend(Symbol(),OP_SELLLIMIT,lotSize, entryPrice,0,stopLoss,dTakeProfit,"SellOrder",magicnumber,0,Red);
if (ticketSell>0)
{
ObjectSetText(name,string(ticketSell));
i = ObjectsTotal()-1; // rather than continuing the 'for' loop, we must restart because arrows (ie new objects) were created.
}
*/
}
}
lastTime = currTime;
}
}
if(result.retcode==10009 || result.order>0)
ObjectSetText(name,string(result.order));

Receive parameters from an URL

I'm getting URL input from the server as follows:
http://10.0.0.70/distance=150/angle=60
Now how can I get the distance and angle parameters from this URL and store it into a another variable?
I have to use this in the Arduino programming language.
This should work
char str[] = "http://10.0.0.70/distance=150/angle=60";
char * pch;
char *ptr;
long ret;
pch = strchr(str,'='); // find '='
ret = strtoul(pch, &ptr, 10); // take the number after it (here ret = 150)
pch = strchr(pch+1,'='); // find the next '='
ret = strtoul(pch, &ptr, 10); // take the number after it (here ret = 60)

Accelerate framework "sign" function

I'm trying to find a super fast way of getting the sign of each value in a vector. I was hoping to find a function in the accelerate framework to do this, but couldn't find one. Here's what it would do:
float *inputVector = .... // some audio vector
int length = ...// length of input vector.
float *outputVector = ....// result
for( int i = 0; i<length; i++ )
{
if( inputVector[i] >= 0 ) outputVector[i] = 1;
else outputVector[i] = -1;
}
Ok, I think I've found a way...
vvcopysignf() "Copies an array, setting the sign of each value based on a second array."
So, one method would be to make an array of 1s, then use this function to change the sign of the 1s based on an input array.
float *ones = ... // a vector filled with 1's
float *input = .... // an input vector
float *output = ... // an output vector
int bufferSize = ... // size of the vectors;
vvcopysignf(output, ones, input, &bufferSize);
//output now is an array of -1s and 1s based the sign of the input.

Resources