I'm trying to program an expert advisor with some sample EA's I found here: https://www.mql5.com/en/articles/1404. The expert advisor should work similar like the free EA called "Digger". However, I want a different entry and I want to use it on different symbols. Therefore I'm trying to program it and also to improve my programming skills. My issue is now, that I work with up to 5 opened positions for basically one strategy. Everytime I open a new position, I calculate a new Take Profit for all opened positions. In my case I don't know how to modify all positions at the same time. There's probably a better way (like to work with functions) but I'm not so far with my programming skills, yet. If you could give me hint, where my errors in reasoning are, I would really aprecciate it. Since I'm at a point where I don't see the wood for trees if you know what I mean ;) . Thanks in advance.
//+------------------------------------------------------------------+//| 01_STRND_15.mq4 |//| Copyright 2016, MetaQuotes Software Corp. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"andre33"#property link"https://www.mql5.com"#property version"5.00"#property strict#property description"Last change 24.04.2016"//--------------------------------------------------------------- 1 --// Numeric valuesexterndouble F_TP = 10; // Take Profit Pipsexternint Delta = 100; // Distance in Pips from Open Price before open new positionexterndouble Lots = 0.1; // Starting Lots externint F_Dec = 10; // Faktor um Lots auf eine einstellige Zahl zu multiplizierenint TimeFrame = 0;
int
b_cnt = 0, // Counting opened BUY Orders
s_cnt = 0, // Counting opened SELL Orders
b_MagicNumber = 500, // Magic Number for BUY Orders
s_MagicNumber = 600; // Magic Number for SELL Ordersdouble
b_TP = 0, // BUY Take Profit
s_TP = 0, // SELL Take Profit
doOpen0 = 0, // Open Price 1st Position
doOpen1 = 0, // Open Price 2nd Position
doOpen2 = 0, // Open Price 3rd Position
doOpen3 = 0, // Open Price 4th Position
doOpen4 = 0, // Open Price 5th Position
doLots0 = 0, // Lots 1st Position
doLots1 = 0, // Lots 2nd Position
doLots2 = 0, // Lots 3rd Position
doLots3 = 0, // Lots 4th Position
doLots4 = 0; // Lots 5th Position bool
open1 = true,
modify = false;
////--------------------------------------------------------------- //| +------------------------------------------------------------------+ |//+------------------------------------------------------------------+voidOnTick(void)
{
int
cnt = 0,
ticket = 0,
_GetLastError = 0,
total = OrdersTotal(),
totalHist = OrdersHistoryTotal();
double
b_Lots = Lots,
doTrendUp0 = iCustom(NULL,TimeFrame,"super_trend",10,0,1),
doTrendDown0 = iCustom(NULL,TimeFrame,"super_trend",10,1,1),
doTrendUp1 = iCustom(NULL,TimeFrame,"super_trend",10,0,2),
doTrendDown1 = iCustom(NULL,TimeFrame,"super_trend",10,1,2);
// search in all open positions:for(int z=total-1;z>=0;z--)
{
if(!OrderSelect(z,SELECT_BY_POS,MODE_TRADES))
continue;
if(OrderType()==OP_BUY &&
OrderSymbol()==Symbol()&&
OrderMagicNumber() == b_MagicNumber)
// if the Price is Delta Pips away from the Open Price, // open another order if(Ask < (OrderOpenPrice() - (Delta*(b_cnt+1))) && b_cnt <= 4)
{
b_cnt = b_cnt + 1;
ticket=OrderSend(NULL,OP_BUY,b_Lots,Ask,5,0,0,"",b_MagicNumber,0,Green);
if(OrderSelect(ticket, SELECT_BY_TICKET)==true)
{
Print("Open Buy Order #",b_cnt+1,", ",OrderOpenPrice(),", Magic : ",OrderMagicNumber(),", Ticket : ",OrderTicket(),", TP : ",OrderTakeProfit());
// Store the new data in the global variables:if (b_cnt == 1)
{
doOpen1 = OrderOpenPrice();
doLots1 = OrderLots();
}
if (b_cnt == 2)
{
doOpen2 = OrderOpenPrice();
doLots2 = OrderLots();
}
if (b_cnt == 3)
{
doOpen3 = OrderOpenPrice();
doLots3 = OrderLots();
}
if (b_cnt == 4)
{
doOpen4 = OrderOpenPrice();
doLots4 = OrderLots();
}
}
// Calculating the new Take Profit for all open Positions
b_TP = F_TP + (((doOpen0 * doLots0 * F_Dec)+(doOpen1 * doLots1 * F_Dec)+(doOpen2 * doLots2 * F_Dec)+(doOpen3 * doLots3 * F_Dec)+(doOpen4 * doLots4 * F_Dec)) / ((doLots0+doLots1+doLots2+doLots3+doLots4)*F_Dec));
}
// Modify all open positions //with the new Take Profitif(OrderTakeProfit() != b_TP)
{
if(!OrderModify(OrderTicket(),OrderOpenPrice(),0,b_TP,0,Orange))
{
Alert("OrderModify Error #",GetLastError());
return;
}
}
return;
}
//+------------------------------------------------------------------+//| If execution has reached this point, it means there are no |//| open positions |//+------------------------------------------------------------------+//---- Check if BUY Order was opened at the current candle: for(cnt=0;cnt<totalHist;cnt++)
{
if(!OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY))
continue;
if(OrderType()==OP_BUY &&
OrderSymbol()==Symbol()&&
OrderMagicNumber() == b_MagicNumber &&
iBarShift(NULL,TimeFrame,OrderOpenTime()) == 0)
open1 = false;
}
// Check for condition to open a BUY Orderif (doTrendUp1 == doTrendDown1 && doTrendUp0 < doTrendDown0 && open1 == true)
{
ticket = OrderSend(NULL,OP_BUY,b_Lots,Ask,5,0,Ask + F_TP,"",b_MagicNumber,0,Green);
if(OrderSelect(ticket, SELECT_BY_TICKET)==true)
{
Print("Open Buy Order #1 ",OrderOpenPrice(),", Magic : ",OrderMagicNumber(),", Ticket : ",OrderTicket(),", TP : ",OrderTakeProfit());
doOpen0 = OrderOpenPrice();
doLots0 = OrderLots();
b_TP = OrderTakeProfit();
}
}
//---
}
//+------------------------------------------------------------------+
Hello all,
I'm trying to program an expert advisor with some sample EA's I found here: https://www.mql5.com/en/articles/1404. The expert advisor should work similar like the free EA called "Digger". However, I want a different entry and I want to use it on different symbols. Therefore I'm trying to program it and also to improve my programming skills. My issue is now, that I work with up to 5 opened positions for basically one strategy. Everytime I open a new position, I calculate a new Take Profit for all opened positions. In my case I don't know how to modify all positions at the same time. There's probably a better way (like to work with functions) but I'm not so far with my programming skills, yet. If you could give me hint, where my errors in reasoning are, I would really aprecciate it. Since I'm at a point where I don't see the wood for trees if you know what I mean ;) . Thanks in advance.