继续优化下单组件,并封装buy和sell方向订单
在函数内部的变量或者函数名称,如果需要定义私有,那么前面定义_
, 表示私有变量和私有函数. (快捷键: Shift
+ -
)
// buy方向下单函数组件
bool Buy( const string symbol=NULL, // 交易品种 1
const double volume=0.01, // 定单手数 3
const int sl=0, // 止损价 4
const int tp=0, // 止盈价 5
const int deviation=5, // 滑点 6
const string comment="", // 定单注释 7
const ulong magic=8199231 // EA识别码 8
)
{
return PositionSend(symbol, 0,volume,sl,tp,deviation,comment,magic);
}
// sell方向下单函数组件
bool Sell( const string symbol=NULL, // 交易品种 1
const double volume=0.01, // 定单手数 3
const int sl=0, // 止损价 4
const int tp=0, // 止盈价 5
const int deviation=5, // 滑点 6
const string comment="", // 定单注释 7
const ulong magic=8199231 // EA识别码 8
)
{
return PositionSend(symbol, 1,volume,sl,tp,deviation,comment,magic);
}
double _valStopLossPrice(const string symbol, const int type, const int sl)
{
if(sl <= 0.0) return 0.0;
double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
double bid = SymbolInfoDouble(symbol,SYMBOL_BID);
double m_sl = 0.0;
if(type == 0) m_sl = valStopLossPrice(symbol,type,ask,sl);
else if(type == 1)m_sl = valStopLossPrice(symbol,type,bid,sl);
return m_sl;
}
double _valTakeProfitPrice(const string symbol, const int type, const int tp)
{
if(tp <= 0.0) return 0.0;
double ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
double bid = SymbolInfoDouble(symbol,SYMBOL_BID);
double m_tp = 0.0;
if(type == 0) m_tp = valTakeProfitPrice(symbol,type,ask,tp);
else if(type == 1)m_tp = valTakeProfitPrice(symbol,type,bid,tp);
return m_tp;
}
调用层
#property copyright "Copyright 2022, Author:阿龙."
#property link "https://www.guapit.com"
#property description "MT5智能交易编程课程"
#property description "QQ: 8199231"
#property version "1.00"
// #include <Guapit/Symbols.mqh>
#include <Guapit/Trade.mqh>
void OnStart()
{
// PositionSend(NULL, 0, 0.18, 100, 200, 5, "buy + 1", 819921);
//PositionSend(_Symbol, 1, 0.1111, 100, 180, 0, "sell + 1", 819921);
// Buy(NULL,0.18,100);
Sell(NULL,0.2,120, 200,5,"sell + 1");
}