#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
class jiaoyi
{
public:
int buy(string symbol, double lots, int slPoint, int tpPoint, string com, int magic)
{
int a;
MqlTradeRequest request = {0};
MqlTradeResult result = {0};
request.action = TRADE_ACTION_DEAL;
request.symbol = symbol;
request.type = ORDER_TYPE_BUY;
request.volume = lots;
request.deviation = 10;
request.price = SymbolInfoDouble(symbol,SYMBOL_ASK);
request.sl = SymbolInfoDouble(symbol,SYMBOL_ASK)- slPoint*Point();
request.tp = SymbolInfoDouble(symbol,SYMBOL_ASK)+tpPoint*Point();
request.comment = com;
request.magic = magic;
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError());
PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
return result.order;
}
int sell(string symbol, double lots, int slPoint, int tpPoint, string com, int magic)
{
int a;
MqlTradeRequest request = {0};
MqlTradeResult result = {0};
request.action = TRADE_ACTION_DEAL;
request.symbol = symbol;
request.type = ORDER_TYPE_SELL;
request.volume = lots;
request.deviation = 10;
request.price = SymbolInfoDouble(symbol,SYMBOL_ASK);
request.sl = SymbolInfoDouble(symbol,SYMBOL_ASK)+ slPoint*Point();
request.tp = SymbolInfoDouble(symbol,SYMBOL_ASK)-tpPoint*Point();
request.comment = com;
request.magic = magic;
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError());
PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
return result.order;
}
void closeAllBuy(string symbol)
{
if(symbol == NULL)
{
symbol =Symbol();
}
int t = PositionsTotal();
for(int i = t-1; i>=0; i--)
{
if(PositionGetTicket(i)>0)
{
if(PositionGetSymbol(i) == symbol && PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_BUY)
{
MqlTradeRequest request = {0};
MqlTradeResult result = {0};
request.action = TRADE_ACTION_DEAL;
request.position = PositionGetTicket(i);
request.type =ORDER_TYPE_SELL;
request.volume = PositionGetDouble(POSITION_VOLUME);
request.symbol = symbol;
request.deviation = 10;
request.price = SymbolInfoDouble(symbol, SYMBOL_BID);
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError());
PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
}
}
}
}
void closeAllSell(string symbol)
{
if(symbol == NULL)
{
symbol =Symbol();
}
int t = PositionsTotal();
for(int i = t-1; i>=0; i--)
{
if(PositionGetTicket(i)>0)
{
if(PositionGetSymbol(i) == symbol && PositionGetInteger(POSITION_TYPE)== POSITION_TYPE_SELL)
{
MqlTradeRequest request = {0};
MqlTradeResult result = {0};
request.action = TRADE_ACTION_DEAL;
request.position = PositionGetTicket(i);
request.type =ORDER_TYPE_BUY;
request.volume = PositionGetDouble(POSITION_VOLUME);
request.symbol = symbol;
request.deviation = 10;
request.price = SymbolInfoDouble(symbol, SYMBOL_ASK);
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError());
PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
}
}
}
}
void closeAll(string symbol)
{
this.closeAllBuy(symbol);
this.closeAllSell(symbol);
}
void mofiysltp(string symbol,ENUM_POSITION_TYPE type, double sl, double tp)
{
int t = PositionsTotal();
for(int i = t-1; i>=0; i--)
{
if(PositionGetTicket(i)>0)
{
if(type == POSITION_TYPE_BUY)
{
MqlTradeRequest request = {0};
MqlTradeResult result = {0};
request.action = TRADE_ACTION_SLTP;
request.position = PositionGetTicket(i);
request.type =ORDER_TYPE_BUY;
request.volume = PositionGetDouble(POSITION_VOLUME);
request.symbol = symbol;
if(sl != NULL)
{
request.sl =NormalizeDouble(sl, Digits());
}
if(tp != NULL)
{
request.tp = NormalizeDouble(tp, Digits());
}
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError());
PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
}
}
}
}
void deleteGuadan(string symbol)
{
int t = OrdersTotal();
for(int i = t-1; i>=0; i--)
{
if(OrderGetTicket(i)>0)
{
if(OrderGetString(ORDER_SYMBOL) == symbol)
{
MqlTradeRequest request = {0};
MqlTradeResult result = {0};
request.action = TRADE_ACTION_REMOVE;
request.position = OrderGetTicket(i);
request.symbol = symbol;
if(!OrderSend(request,result))
PrintFormat("OrderSend error %d",GetLastError());
PrintFormat("retcode=%u deal=%I64u order=%I64u",result.retcode,result.deal,result.order);
}
}
}
}
};