Create a gist now

Instantly share code, notes, and snippets.

@felixdv /ForexWall-E.mq4 Secret
Last active May 10, 2017

What would you like to do?
//+------------------------------------------------------------------+
//| ForexWall-E.mq4 |
//| Smart Forex Learning |
//| https://smartforexlearning.com |
//+------------------------------------------------------------------+
#property copyright "Smart Forex Learning"
#property link "https://smartforexlearning.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if (OrdersTotal() > 0) {
TrailStops();
return;
}
double lots = 0.01;
int stopLoss = 500;
int takeProfit = 500;
double ema = iMA(NULL, 0, 300, 0, MODE_EMA, PRICE_CLOSE, 0);
if (Ask + 500 * Point < ema) {
if (OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Ask - stopLoss * Point, Ask + takeProfit * Point, "my forex wall-e order", 12345, 0, Green)) {
Print("Buy order succeeded!");
}
}
if (Bid - 500 * Point > ema) {
if (OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Bid + stopLoss * Point, Bid - takeProfit * Point, "my forex wall-e order", 12345, 0, Red)) {
Print("Sell order succeeded!");
}
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Trailing stop function |
//+------------------------------------------------------------------+
void TrailStops()
{
int trailingStop = 300;
for (int i = 0; i < OrdersTotal(); i++) {
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
continue;
}
if (OrderSymbol() != Symbol()) {
continue;
}
if (OrderType() == OP_BUY) {
if (Bid - OrderOpenPrice() > trailingStop * Point && OrderStopLoss() < Bid - trailingStop * Point) {
if (!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - trailingStop * Point, OrderTakeProfit(), 0, Green)) {
Print("OrderModify error ",GetLastError());
}
return;
}
} else if (OrderType() == OP_SELL) {
if (OrderOpenPrice() - Ask > trailingStop * Point && OrderStopLoss() > Ask + trailingStop * Point) {
if (!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + trailingStop * Point, OrderTakeProfit(), 0, Green)) {
Print("OrderModify error ",GetLastError());
}
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment