Author: � skymaster
Orders Execution
Checks for the total of open ordersChecks for the total of closed ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites

Profitability Reports

GBP/USD Oct 2024 - Jan 2025
0.00 %
Total Trades 0
Won Trades 0
Lost trades 0
Win Rate 0.0 %
Expected payoff 0.00
Gross Profit 0.00
Gross Loss 0.00
Total Net Profit 0.00
-100%
-50%
0%
50%
100%
NZD/USD Oct 2024 - Jan 2025
0.00 %
Total Trades 0
Won Trades 0
Lost trades 0
Win Rate 0.0 %
Expected payoff 0.00
Gross Profit 0.00
Gross Loss 0.00
Total Net Profit 0.00
-100%
-50%
0%
50%
100%
BT_v4

#property copyright "© skymaster"

#include <stdlib.mqh>
#include <stderror.mqh>

#define major   4
#define minor   0

extern string _tmp1_ = " --- Trade parameters ---";
extern double AppPrice = 0.0;
extern bool OBuy = true;
extern double Lots = 0.1;
extern int StopLoss = 140;
extern int TakeProfit = 180;
extern int Slippage = 3;
extern int Magic = 20050107;

extern string _tmp2_ = " --- MM section ---";
extern bool EnableMM = false;
extern double LotBalancePcnt = 10;
extern double MinLot = 0.1;
extern double MaxLot = 5;
extern int LotPrec = 1;


extern string _tmp4_ = " --- Orders color ---";
extern color clBuy = DodgerBlue;
extern color clSell = Crimson;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void init () {
  if (!IsTesting()) Comment("");
}

void deinit() {
  if (!IsTesting()) Comment("");
}

void start() {

  //-----
  
  if (false)
    if (OrdersCount0() > 0) return;

  //-----

  int BuyCnt = 0;
  int SellCnt = 0;
  
  int cnt = OrdersTotal();
  for (int i=0; i < cnt; i++) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    if (OrderSymbol() != Symbol()) continue;
    if (OrderMagicNumber() != Magic) continue;
    
    int type = OrderType();
    if (type == OP_BUY) BuyCnt++;
    if (type == OP_SELL) SellCnt++;
  }  

  //-----

    
  double price, sl, tp;

  if (Close[1]>AppPrice && Close[2]<=AppPrice)
  {  
    if (BuyCnt > 0) return;
    if (OBuy == false) return;
    
    if (SellCnt > 0) {
      SellCnt = CloseOrders(OP_SELL);
      if (SellCnt > 0) return;
    }
      
    {
      price = Ask;
  
      sl = 0;
      tp = 0;
      if (StopLoss > 0) sl = price - StopLoss*Point;
      if (TakeProfit > 0) tp = price + TakeProfit*Point;

      Buy(Symbol(), GetLots(), price, sl, tp, Magic);
    }
    
    return;
  }

  if (Close[1]<AppPrice && Close[2]>=AppPrice)
  {
    if (SellCnt > 0) return;
    if (OBuy == true) return;
    
    if (BuyCnt > 0) {
      BuyCnt = CloseOrders(OP_BUY);
      if (BuyCnt > 0) return;
    }

    {      
      price = Bid;
    
      sl = 0;
      tp = 0;
      if (StopLoss > 0) sl = price + StopLoss*Point;
      if (TakeProfit > 0) tp = price - TakeProfit*Point;

      Sell(Symbol(), GetLots(), price, sl, tp, Magic);
    }
    
    return;
  } 
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

double GetLots() 
{
  if (!EnableMM) return (Lots);
  
  double Bal0 = 1000;
  if (LotPrec == 2) Bal0 = 100;
  
  double lots = NormalizeDouble(LotBalancePcnt/100*AccountBalance()/Bal0, LotPrec);
  lots = MathMax(lots, MinLot);
  lots = MathMin(lots, MaxLot);
  
  return (lots);
}

int CloseOrders(int type) {
  
  int cnt = OrdersTotal();
  for (int i=cnt-1; i >= 0; i--) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    if (OrderSymbol() != Symbol()) continue;
    if (OrderMagicNumber() != Magic) continue;
    
    if (OrderType() != type) continue;
    
    if (OrderType() == OP_BUY) {
      CloseOrder(OrderTicket(), OrderLots(), Bid);
    }
    else if (OrderType() == OP_SELL) {
      CloseOrder(OrderTicket(), OrderLots(), Ask);
    }    
  }
  
  int orders = 0;
  cnt = OrdersTotal();
  for (i = 0; i < cnt; i++) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    if (OrderSymbol() != Symbol()) continue;
    if (OrderMagicNumber() != Magic) continue;
    
    if (OrderType() == type) orders++;
  }
  
  return (orders); 
}

int OrdersCount0()
{
  int orders = 0;

  int cnt = OrdersTotal();
  for (int i=0; i<cnt; i++) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
 
    if (OrderSymbol() != Symbol()) continue;
    if (OrderMagicNumber() != Magic) continue;

    if (OrderOpenTime() >= Time[0]) orders++;
  }

  cnt = OrdersHistoryTotal();
  for (i=0; i<cnt; i++) {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
 
    if (OrderSymbol() != Symbol()) continue;
    if (OrderMagicNumber() != Magic) continue;

    if (OrderOpenTime() >= Time[0]) orders++;
  }
 
  return (orders);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

int SleepOk = 2000;
int SleepErr = 6000;

int Buy(string symbol, double lot, double price, double sl, double tp, int magic, string comment="") {
  RefreshRates();  
  int dig = MarketInfo(symbol, MODE_DIGITS);

  price = NormalizeDouble(price, dig);
  sl = NormalizeDouble(sl, dig);
  tp = NormalizeDouble(tp, dig);
    
  string _lot = DoubleToStr(lot, 2);
  string _price = DoubleToStr(price, dig);
  string _sl = DoubleToStr(sl, dig);
  string _tp = DoubleToStr(tp, dig);

  Print("Buy \"", symbol, "\", ", _lot, ", ", _price, ", ", Slippage, ", ", _sl, ", ", _tp, ", ", magic, ", \"", comment, "\"");

  int res = OrderSend(symbol, OP_BUY, lot, price, Slippage, sl, tp, comment, magic, 0, clBuy);
  if (res >= 0) {
    Sleep(SleepOk);
    return (res);
  } 	
   	
  int code = GetLastError();
  Print("Error opening BUY order: ", ErrorDescription(code), " (", code, ")");
  Sleep(SleepErr);
	
  return (-1);
}

int Sell(string symbol, double lot, double price, double sl, double tp, int magic, string comment="") {
  RefreshRates();  
  int dig = MarketInfo(symbol, MODE_DIGITS);

  price = NormalizeDouble(price, dig);
  sl = NormalizeDouble(sl, dig);
  tp = NormalizeDouble(tp, dig);
  
  string _lot = DoubleToStr(lot, 2);
  string _price = DoubleToStr(price, dig);
  string _sl = DoubleToStr(sl, dig);
  string _tp = DoubleToStr(tp, dig);

  Print("Sell \"", symbol, "\", ", _lot, ", ", _price, ", ", Slippage, ", ", _sl, ", ", _tp, ", ", magic, ", \"", comment, "\"");
  
  int res = OrderSend(symbol, OP_SELL, lot, price, Slippage, sl, tp, comment, magic, 0, clSell);
  if (res >= 0) {
    Sleep(SleepOk);
    return (res);
  } 	
   	
  int code = GetLastError();
  Print("Error opening SELL order: ", ErrorDescription(code), " (", code, ")");
  Sleep(SleepErr);
	
  return (-1);
}

bool CloseOrder(int ticket, double lot, double price) {

  RefreshRates();
  if (!OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES)) return(false);
  int dig = MarketInfo(OrderSymbol(), MODE_DIGITS);

  string _lot = DoubleToStr(lot, 2);
  string _price = DoubleToStr(price, dig);

  Print("CloseOrder ", ticket, ", ", _lot, ", ", _price, ", ", Slippage);
  
  bool res = OrderClose(ticket, lot, price, Slippage);
  if (res) {
    Sleep(SleepOk);
    return (res);
  } 	
   	
  int code = GetLastError();
  Print("CloseOrder failed: ", ErrorDescription(code), " (", code, ")");
  Sleep(SleepErr);
	
  return (false);
}


Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---