Hallo, I’m new in mql4 and I need help in programming (ea) that can open a trade every 20 points which have the same type of the order that make profit , I mean if it goes down 20 points it will open a sell trade and if it goes up 20 points it will open a buy trade and thanks for any body read this post
Not sure if people will code the entire thing for you here. You can give it a go yourself and ask code review maybe
input string GridVariables = "------------Grid Variables in Points";
// 11111111111111111111111111111111111111111111111111111111111111111111
input bool G1BSENABLE = true;
input double G1BSLots = 1.0;
input int G1BSWinUp = 200; // Points
input int G1BSStayback = 100; // Points
input int G1BSSpace = 80;
input int G1BSTP = 110;
input int G1BSSL = 1500; // Set at 1500 as in far away
input int G1BSMagic = 1001;
input int G1BSOffset = Offset;
input int G1BSEngine = ORDER_TYPE_BUY_STOP;
input bool G1SSENABLE = true;
input double G1SSLots = 2.0;
input int G1SSWinDn = 360;
input int G1SSStayback = 300;
input int G1SSSpace = 30;
input int G1SSTP = 0;
input int G1SSSL = 0;
input int G1SSMagic = 1002;
input int G1SSOffset = Offset;
input int G1SSEngine = ORDER_TYPE_SELL_STOP;
input bool G1BLENABLE = true;
input double G1BLLots = 2.0;
input int G1BLWinDn = 70;
input int G1BLStayback = 30;
input int G1BLSpace = 30;
input int G1BLTP = 100;
input int G1BLSL = 0;
input int G1BLMagic = 1003;
input int G1BLOffset = Offset;
input int G1BLEngine = ORDER_TYPE_BUY_LIMIT;
input bool G1SLENABLE = true;
input double G1SLLots = 2.0;
input int G1SLWinUp = 70;
input int G1SLStayback = 30;
input int G1SLSpace = 30;
input int G1SLTP = 100;
input int G1SLSL = 0;
input int G1SLMagic = 1004;
input int G1SLOffset = Offset;
input int G1SLEngine = ORDER_TYPE_SELL_LIMIT;
=============================================================================
void PlaceGridTrade() // I think we only need current level, we get the other data from the input s
{
dBid = SymbolInfoDouble(_Symbol, SYMBOL_BID); // Get BidPrice = CurrentPrice
dAsk = SymbolInfoDouble(_Symbol, SYMBOL_ASK); // Get AskPrice
int iMagic;
double dLots, dTP = 0, dSL = 0;
string sText;
double currentLevel = NormalizeDouble((dBid - (2 * G1BSSpace * Point)), Digits); // back down price to below price to find a valid currentLevel 80 90 100 for example
double upLimit = dBid + (G1BSWinUp * Point);
double loLimit = dBid + (G1BSStayback * Point);
iMagic = G1BSMagic;
dLots = G1BSLots * AccountEquity() / 100000; // adjust to $100k account
//Print("Currentlevel = " , currentLevel);
while(currentLevel < upLimit) // look for trades up to the upLimit...we will
{
//Print("in while ...... Currentlevel = " , currentLevel);
if(fmod((currentLevel * MathPow(10, Digits)), (G1BSSpace * Point * MathPow(10, Digits))) < 0.5)
//if(fmod(((currentLevel) * 100000), (((G1BSSpace)* Point) * 100000)) < 1.0) // Incremnt through the prices to find one that is
{
// "in the gear notch" so that all the grids fall correctly
// We needed the "100000" to get the fmod to work at the point level
//Print("i am in MathMod BUYSTOP- in while ", currentLevel);
break;
}
currentLevel = NormalizeDouble((currentLevel + Point), Digits); // increment currentLevel point by point until we get a MathMod = 0
}
currentLevel += G1BSOffset * Point; // This is to allow for the shift due to the Offset Value
//Print("after 1st while..... Currentlevel = " , currentLevel);
while(currentLevel < upLimit) // place trades up to the upper limit
{
if(G1BSEngine == ORDER_TYPE_BUY_STOP && G1BSENABLE == true
&& !TradeInBook(Symbol(), G1BSEngine, G1BSMagic, currentLevel)
&& !TradeInBook(Symbol(), POSITION_TYPE_BUY, G1BSMagic, currentLevel)
&& currentLevel > dBid + gStopLevel * Point // Broker value for distance from price we can put OrderEntryPrice or OrderTakeProfit / SL
&& currentLevel > dBid + G1BSStayback * Point) // are we above the Stayback price
{
if(G1BSTP > 0)
dTP = currentLevel + G1BSTP * Point;
if(G1BSSL > 0)
dSL = currentLevel - G1BSSL * Point;
sText = StringConcatenate(G1BSMagic, DoubleToStr(currentLevel, Digits), sBlank);
if(SendOrder(G1BSEngine, dLots, currentLevel, dSL, dTP, sText, G1BSMagic))
Print("Order placed with G1 BuyStops with lot = ", dLots);
else
Print("Order not placed with G1 BuyStops with lot = ", dLots);
}
currentLevel = currentLevel + (G1BSSpace * Point);
}
// ============================================== End of G1BS - grid one BuyStops
// ============================================== start of G1SS - grid one SellStops
//Print("INTO SELLSTOP");
dTP = 0;
dSL = 0;
currentLevel = NormalizeDouble((dBid + (2 * G1SSSpace * Point)), Digits); // back down price to below price to find a valid currentLevel 80 90 100 for example
double DnLimit = dAsk - (G1SSWinDn * Point);
loLimit = dBid - (G1SSStayback * Point) ;
iMagic = G1SSMagic;
dLots = G1SSLots * AccountEquity() / 100000; // adjust to $100k account
while(currentLevel > DnLimit) // look for trades Dn to the DnLimit...we will
{
// currentLevel = 90, 100, 120 or some valid currentLevel
if(fmod(currentLevel * MathPow(10, Digits), G1SSSpace * Point * MathPow(10, Digits)) < 0.5)
// if(fmod((currentLevel * 100000), ((G1SSSpace * Point) * 100000)) < 1.0)
{
//Print("i am in MathMod SELLSTOP- in while ", currentLevel);
break;
}
currentLevel = NormalizeDouble((currentLevel - Point), Digits); // increment currentLevel point by point until we get a MathMod = 0
}
currentLevel -= G1SSOffset * Point; // This is to allow for the shift due to the Offset Value
while(currentLevel > DnLimit) // place trades Dn to the Dnper limit
{
if(G1SSEngine == ORDER_TYPE_SELL_STOP && G1SSENABLE == true
&& !TradeInBook(Symbol(), G1SSEngine, G1SSMagic, currentLevel)
&& !TradeInBook(Symbol(), POSITION_TYPE_SELL, G1SSMagic, currentLevel)
&& currentLevel < dBid - gStopLevel * Point // The distance from price we can put OrderEntryPrice or OrderTakeProfit / SL
&& currentLevel < dBid - G1SSStayback * Point) // are we above the Stayback price
{
if(G1SSTP > 0)
dTP = currentLevel - G1SSTP * Point;
if(G1SSSL > 0)
dSL = currentLevel + G1SSSL * Point;
sText = StringConcatenate(G1SSMagic, DoubleToStr(currentLevel, Digits), sBlank);
if(SendOrder(G1SSEngine, dLots, currentLevel, dSL, dTP, sText, iMagic))
Print("Order placed with G1 SELLSTOP with lot = ", dLots);
else
Print("Order not placed with G1 SELLSTOP with lot = ", dLots);
}
currentLevel = currentLevel - (G1SSSpace * Point);
}
// ============================================== End of G1SS - grid one SellStops
// ============================================== start of G1BL - grid one BuyLimits
//Print("INTO BUYLIMIT");
dTP = 0;
dSL = 0;
currentLevel = NormalizeDouble((dBid + (2 * G1BLSpace * Point)), Digits); // back down price to below price to find a valid currentLevel 80 90 100 for example
DnLimit = dBid - (G1BLWinDn * Point);
loLimit = dBid - (G1BLStayback * Point);
iMagic = G1BLMagic;
dLots = G1BLLots * AccountEquity() / 100000; // adjust to $100k account
while(currentLevel > DnLimit) // look for trades Dn to the DnLimit...we will
{
// currentLevel = 90, 100, 120 or some valid currentLevel
// if(fmod((currentLevel * 100000), ((G1BLSpace * Point) * 100000)) < 1.0)
if(fmod((currentLevel * MathPow(10, Digits)), (G1BLSpace * Point * MathPow(10, Digits))) < 0.5)
{
//Print("i am in MathMod BUYLIMIT- in while ", currentLevel);
break;
}
currentLevel = NormalizeDouble((currentLevel - Point), Digits); // increment currentLevel point by point until we get a MathMod = 0
}
currentLevel -= G1BLOffset * Point; // This is to allow for the shift due to the Offset Value
while(currentLevel > DnLimit) // place trades Dn to the Dnper limit
{
if(G1BLEngine == ORDER_TYPE_BUY_LIMIT && G1BLENABLE == true
&& !TradeInBook(Symbol(), G1BLEngine, G1BLMagic, currentLevel)
&& !TradeInBook(Symbol(), POSITION_TYPE_BUY, G1BLMagic, currentLevel)
&& currentLevel < dBid + gStopLevel * Point // The distance from price we can put OrderEntryPrice or OrderTakeProfit / SL
&& currentLevel < dBid + G1BLStayback * Point) // are we above the Stayback price
{
if(G1BLTP > 0)
dTP = currentLevel + G1BLTP * Point;
if(G1BLSL > 0)
dSL = currentLevel - G1BLSL * Point;
sText = StringConcatenate(G1BLMagic, DoubleToStr(currentLevel, Digits), sBlank);
SendOrder(G1BLEngine, dLots, currentLevel, dSL, dTP, sText, iMagic);
}
currentLevel = currentLevel - (G1BLSpace * Point);
}
// ============================================== End of G1BL - grid one BuyLimits
// ============================================== start of G1SL - grid one SellLimits
//Print("INTO SELLLIMIT");
//UpdateBidAsk(); // 15Jul20 - Derk: Removed until proven needed
dTP = 0;
dSL = 0;
currentLevel = NormalizeDouble((dBid - (2 * G1SLSpace * Point)), Digits); // back down price to below price to find a valid currentLevel 80 90 100 for example
upLimit = dBid + (G1SLWinUp * Point);
loLimit = dBid + (G1SLStayback * Point);
iMagic = G1SLMagic;
dLots = G1SLLots * AccountEquity() / 100000; // adjust to $100k account
while(currentLevel < upLimit) // look for trades up to the upLimit...we will
{
// currentLevel = 90, 100, 120 or some valid currentLevel
//if(fmod((currentLevel * 100000), ((G1SLSpace * Point) * 100000)) < 1.0)
if(fmod((currentLevel * MathPow(10, Digits)), (G1SLSpace * Point * MathPow(10, Digits))) < 0.5)
{
//Print("i am in MathMod SELLLIMIT- in while ", currentLevel);
break;
}
currentLevel = NormalizeDouble((currentLevel + Point), Digits); // increment currentLevel point by point until we get a MathMod = 0
}
currentLevel += G1SLOffset * Point; // This is to allow for the shift due to the Offset Value
while(currentLevel < upLimit) // place trades up to the upper limit
{
if(G1SLEngine == ORDER_TYPE_SELL_LIMIT && G1SLENABLE == true
&& !TradeInBook(Symbol(), G1SLEngine, G1SLMagic, currentLevel)
&& !TradeInBook(Symbol(), POSITION_TYPE_SELL, G1SLMagic, currentLevel)
&& currentLevel > dBid + gStopLevel * Point // The distance from price we can put OrderEntryPrice or OrderTakeProfit / SL
&& currentLevel > dBid + G1SLStayback * Point) // are we above the Stayback price
{
if(G1SLTP > 0)
dTP = currentLevel - G1SLTP * Point;
if(G1SLSL > 0)
dSL = currentLevel + G1SLSL * Point;
sText = StringConcatenate(G1SLMagic, DoubleToStr(currentLevel, Digits), sBlank);
if(SendOrder(G1SLEngine, dLots, currentLevel, dSL, dTP, sText, iMagic))
Print("Order placed with G1 SELLLIMIT with lot = ", dLots);
else
Print("Order not placed with G1 SELLLIMIT with lot = ", dLots);
}
currentLevel = currentLevel + (G1SLSpace * Point);
}
// ============================================== End of G1SL - grid one SellLimits
There will be a few errors to clear up but the most of the code should work. It was taken from MT4 to be used in MT5 which is partially in process.
Best wishes.