Skip to content

Instantly share code, notes, and snippets.

@ball6847
Created June 24, 2021 02:22
Show Gist options
  • Save ball6847/fce351a933491941e70179c094f84cd1 to your computer and use it in GitHub Desktop.
Save ball6847/fce351a933491941e70179c094f84cd1 to your computer and use it in GitHub Desktop.
mt4 script to place order for multi tp at once
//+------------------------------------------------------------------+
//| ball6847_SendOrder.mq4 |
//| Copyright 2021, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property script_show_inputs
enum Positions
{
BUY,
SELL
};
//--- input parameters
input Positions TYPE=BUY;
input double SL;
input double TP1;
input double TP2;
input double TP3;
input double RISK=1.00;
input double BALANCE;
input string COMMENT="Signal Provider";
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
double takeProfits[];
int totalOrders;
string confirmMessage;
double accountBalance = BALANCE == 0 ? AccountBalance() : BALANCE;
double slPips = 0;
double contractSize = MarketInfo(_Symbol, MODE_LOTSIZE);
double orderLotSize;
bool insufficientFundsWarn = false;
double currentPrice = TYPE == BUY ? MarketInfo(Symbol(), MODE_ASK) : MarketInfo(Symbol(), MODE_BID);
if(SL != 0)
{
slPips = getPointBetweenPrices(currentPrice, SL)
/10;
}
if(TP1 != 0)
{
ArrayPushDouble(takeProfits, TP1);
}
if(TP2 != 0)
{
ArrayPushDouble(takeProfits, TP2);
}
if(TP3 != 0)
{
ArrayPushDouble(takeProfits, TP3);
}
totalOrders = ArraySize(takeProfits);
double totalLotSize = ((RISK/100)*accountBalance)/(PipsToValue(slPips, Digits)*contractSize);
orderLotSize = NormalizeDouble(totalLotSize/totalOrders, 2);
if(orderLotSize < 0.01)
{
insufficientFundsWarn = true;
orderLotSize = 0.01;
}
// Show information dialog
confirmMessage = StringConcatenate(
StringFormat("Balance: %.2f\n", accountBalance),
StringFormat("Current Price: %f\n", currentPrice),
StringFormat("Total Orders: %d\n", ArraySize(takeProfits))
);
if(SL != 0)
{
confirmMessage = StringConcatenate(confirmMessage, StringFormat("SL: -%.0f pips (%.2f %s)\n", slPips, PipsToPrice(slPips, contractSize, orderLotSize * totalOrders), AccountCurrency()));
}
else
{
confirmMessage = StringConcatenate(confirmMessage, "SL: !! No Stoploss !!\n");
}
int i;
for(i=0; i<totalOrders; i++)
{
double tpPips = getPointBetweenPrices(currentPrice, takeProfits[i]);
confirmMessage = StringConcatenate(confirmMessage, StringFormat("TP%d: %.0f pips (%.2f %s)\n", i+1, tpPips,PipsToPrice(tpPips, contractSize, orderLotSize), AccountCurrency()));
}
if(totalOrders > 0)
{
confirmMessage = StringConcatenate(
confirmMessage,
StringFormat("Total lot size: %.2f\n", orderLotSize * totalOrders),
StringFormat("Order lot size: %.2f\n", orderLotSize)
);
if(insufficientFundsWarn)
{
confirmMessage = StringConcatenate(confirmMessage, "\nWarning: You have insufficient account balance to trade at preferred lot size.");
}
}
int confirmed = MessageBox(confirmMessage, "Your trades", MB_OKCANCEL);
if(confirmed == 1)
{
Print("Confirmed, placing orders");
int operation = TYPE == BUY ? OP_BUY : OP_SELL;
PlaceOrders(operation, orderLotSize, SL, takeProfits, COMMENT);
}
else
{
Print("Cancelled");
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PlaceOrders(int operation, double lotsize, double stoploss, double& takeprofits[], string comment)
{
double price = operation == OP_BUY ? Ask : Bid;
int i;
int length = ArraySize(takeprofits);
for(i=0; i<length; i++)
{
int ticket = OrderSend(Symbol(), operation, lotsize, price, 3, stoploss, takeprofits[i], StringFormat("%s: tp%d", comment, i+1));
if(ticket<0)
{
Print("OrderSend failed with error #",GetLastError());
}
else
{
Print("OrderSend placed successfully");
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void ArrayPushDouble(double& array[], double dataToPush)
{
int count = ArrayResize(array, ArraySize(array) + 1);
array[ArraySize(array) - 1] = dataToPush;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double getPointBetweenPrices(double price1, double price2)
{
return MathAbs(NormalizeDouble(price1 - price2,Digits)/Point);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double PipsToValue(double pips, int digits)
{
switch(digits)
{
case 0:
case 1:
return pips * 1.0;
case 2:
case 3:
return pips * 0.01;
case 4:
case 5:
default:
return pips * 0.0001;
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double PipsToPrice(double pips, double contractsize, double lotsize)
{
double pipsValue = PipsToValue(pips, Digits);
return NormalizeDouble(contractsize*lotsize*pipsValue, 2);
}
//+------------------------------------------------------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment