// Ratio Hedger by Macdulio for Automated Trading
#include <stdlib.mqh>
extern int magic_number = 51;
extern int magic_number2 = 52;
extern double Ratio = .8;
extern double MarginCallPercentage = 100;
#property copyright “by Macdulio in 2018”
#property link “https://forexfore.blog”
#property description “Ratio Hedger”
#property description “Clicks on half a hedge upon the Equity/Balance ”
#property description “Ratio dropping below the set value (at market)”
#property description “and another half 10% lower(at market)”
#property description “1/2 hedge also triggered by 150% of the set”
#property description “margin call level (by your broker)”
int profits;
double nakedshorts[];
double nakedlongs[];
double open_price;
double stop_loss_price;
double take_profit_price;
double OrderOpenPrice;
double OrderProfit;
string symbol = Symbol();
int init() {
return(0);
}
int deinit() {
return(0);
}
int start() {
int i, counter;
int counted_bars=IndicatorCounted();
int longcount, shortcount;
double nlongs;
double nshorts;
double longaveragebuffer;
double shortaveragebuffer;
int order_type;
profits = 0;
int hstTotal=OrdersHistoryTotal();
counter = 0;
for(i=OrdersTotal()-1; i>=0 ; i–)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
{
Print(“Access to orders list failed with error (“,GetLastError(),”)”);
break;
}
// if ( Symbol()==”EURUSD” && OrderStopLoss()==0 ) {
if (OrderType() == OP_BUY)
{
nlongs = nlongs+OrderLots();
longcount = longcount+1;
longaveragebuffer = longaveragebuffer+(OrderOpenPrice()*OrderLots());
}
if (OrderType() == OP_SELL )
{
nshorts = nshorts+OrderLots();
shortcount = shortcount+1;
shortaveragebuffer = shortaveragebuffer+(OrderOpenPrice()*OrderLots());
}
// }
}
double comparison = AccountEquity()/AccountBalance();
// Half Hedge for shorts
if (nlongs<nshorts && (comparison<Ratio || AccountMargin()<MarginCallPercentage*1.5) ) {
open_price = open_price = NormalizeDouble(Ask, Digits);
stop_loss_price = NormalizeDouble(0,Digits);
take_profit_price = NormalizeDouble(0,Digits);
for (i = OrdersTotal() – 1; i >= 0; i–)
if (OrderSelect(i, SELECT_BY_POS))
if (OrderMagicNumber() == magic_number) {
order_type = OrderType();
if (order_type == ORDER_TYPE_BUY) {
if ((NormalizeDouble(OrderOpenPrice(), Digits) != open_price) || (NormalizeDouble(OrderStopLoss(), Digits) != stop_loss_price) || (NormalizeDouble(OrderTakeProfit(), Digits) != take_profit_price)) {
if (!OrderModify(OrderTicket(), open_price, stop_loss_price, take_profit_price, OrderExpiration()))
Print(“Error: “, ErrorDescription(_LastError));
}
break;
}
else if (order_type == ORDER_TYPE_BUY)
break;
}
if (i < 0)
if (OrderSend(symbol, OP_BUY, NormalizeDouble((nshorts-nlongs)/2,2), open_price, 3, stop_loss_price, take_profit_price, “RATIO HEDGER HALF BUY 0/0”, magic_number) < 0)
Print(“Error: “, ErrorDescription(_LastError));
}
else
for (i = OrdersTotal() – 1; i >= 0; i–)
if (OrderSelect(i, SELECT_BY_POS))
if (OrderMagicNumber() == magic_number )
if (OrderType() == ORDER_TYPE_BUY_STOP)
if (!OrderDelete(OrderTicket()))
Print(“Error: “, ErrorDescription(_LastError));
// Full Hedge for shorts
if (nlongs<nshorts && comparison<Ratio-.1 ) {
open_price = open_price = NormalizeDouble(Ask, Digits);
stop_loss_price = NormalizeDouble(0,Digits);
take_profit_price = NormalizeDouble(0,Digits);
for (i = OrdersTotal() – 1; i >= 0; i–)
if (OrderSelect(i, SELECT_BY_POS))
if (OrderMagicNumber() == magic_number2) {
order_type = OrderType();
if (order_type == ORDER_TYPE_BUY) {
if ((NormalizeDouble(OrderOpenPrice(), Digits) != open_price) || (NormalizeDouble(OrderStopLoss(), Digits) != stop_loss_price) || (NormalizeDouble(OrderTakeProfit(), Digits) != take_profit_price)) {
if (!OrderModify(OrderTicket(), open_price, stop_loss_price, take_profit_price, OrderExpiration()))
Print(“Error: “, ErrorDescription(_LastError));
}
break;
}
else if (order_type == ORDER_TYPE_BUY)
break;
}
if (i < 0)
if (OrderSend(symbol, OP_BUY, NormalizeDouble(nshorts-nlongs,2), open_price, 3, stop_loss_price, take_profit_price, “RATIO HEDGER BUY 0/0”, magic_number2) < 0)
Print(“Error: “, ErrorDescription(_LastError));
}
else
for (i = OrdersTotal() – 1; i >= 0; i–)
if (OrderSelect(i, SELECT_BY_POS))
if (OrderMagicNumber() == magic_number2 )
if (OrderType() == ORDER_TYPE_BUY_STOP)
if (!OrderDelete(OrderTicket()))
Print(“Error: “, ErrorDescription(_LastError));
// Half Hedge For Longs
if (nshorts<nlongs && (comparison<Ratio || AccountMargin()<MarginCallPercentage*1.5)) {
open_price = NormalizeDouble(Bid, Digits);
stop_loss_price = NormalizeDouble(0,Digits);
take_profit_price = NormalizeDouble(0,Digits);
for (i = OrdersTotal() – 1; i >= 0; i–)
if (OrderSelect(i, SELECT_BY_POS))
if (OrderMagicNumber() == magic_number) {
order_type = OrderType();
if (order_type == ORDER_TYPE_SELL) {
if ((NormalizeDouble(OrderOpenPrice(), Digits) != open_price) || (NormalizeDouble(OrderStopLoss(), Digits) != stop_loss_price) || (NormalizeDouble(OrderTakeProfit(), Digits) != take_profit_price)) {
if (!OrderModify(OrderTicket(), open_price, stop_loss_price, take_profit_price, OrderExpiration()))
Print(“Error: “, ErrorDescription(_LastError));
}
break;
}
else if (order_type == ORDER_TYPE_SELL)
break;
}
if (i < 0)
if (OrderSend(symbol, OP_SELL, NormalizeDouble((nlongs-nshorts)/2,2), open_price, 3, stop_loss_price, take_profit_price, “RATIO HEDGER HALF SELL 0/0”, magic_number) < 0)
Print(“Error: “, ErrorDescription(_LastError));
}
else
for (i = OrdersTotal() – 1; i >= 0; i–)
if (OrderSelect(i, SELECT_BY_POS))
if (OrderMagicNumber() == magic_number)
if (OrderType() == ORDER_TYPE_SELL_STOP)
if (!OrderDelete(OrderTicket()))
Print(“Error: “, ErrorDescription(_LastError));
// Full Hedge For Longs
if (nshorts<nlongs && comparison<Ratio-.1) {
open_price = NormalizeDouble(Bid, Digits);
stop_loss_price = NormalizeDouble(0,Digits);
take_profit_price = NormalizeDouble(0,Digits);
for (i = OrdersTotal() – 1; i >= 0; i–)
if (OrderSelect(i, SELECT_BY_POS))
if (OrderMagicNumber() == magic_number2) {
order_type = OrderType();
if (order_type == ORDER_TYPE_SELL) {
if ((NormalizeDouble(OrderOpenPrice(), Digits) != open_price) || (NormalizeDouble(OrderStopLoss(), Digits) != stop_loss_price) || (NormalizeDouble(OrderTakeProfit(), Digits) != take_profit_price)) {
if (!OrderModify(OrderTicket(), open_price, stop_loss_price, take_profit_price, OrderExpiration()))
Print(“Error: “, ErrorDescription(_LastError));
}
break;
}
else if (order_type == ORDER_TYPE_SELL)
break;
}
if (i < 0)
if (OrderSend(symbol, OP_SELL, NormalizeDouble(nlongs-nshorts,2), open_price, 3, stop_loss_price, take_profit_price, “RATIO HEDGER SELL 0/0”, magic_number2) < 0)
Print(“Error: “, ErrorDescription(_LastError));
}
else
for (i = OrdersTotal() – 1; i >= 0; i–)
if (OrderSelect(i, SELECT_BY_POS))
if (OrderMagicNumber() == magic_number2)
if (OrderType() == ORDER_TYPE_SELL_STOP)
if (!OrderDelete(OrderTicket()))
Print(“Error: “, ErrorDescription(_LastError));
return(0);
}