//+------------------------------------------------------------------+
//| PositionCalculator.mq4 |
//| Copyright 2015, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_separate_window//副窗口显示
#property indicator_buffers 2;
#property indicator_color1 SeaGreen //文字颜色
#property indicator_color2 Red //警告文字颜色
input int ATRPeriod=14;//ATR活动周期
input int MarginRatio=10;//保证金比例 Min: 1(1%) Max 100(100%)
int windowsHandle=0;//WindowFind(WindowExpertName());//获取当前指标所在窗口序号 0 主窗口
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
IndicatorShortName("PositionCalculator("+IntegerToString(ATRPeriod)+","+IntegerToString(MarginRatio)+")");//指标标题
windowsHandle=WindowFind("PositionCalculator("+IntegerToString(ATRPeriod)+","+IntegerToString(MarginRatio)+")");//获取当前指标所在窗口序号 0 主窗口
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
ObjectsDeleteAll(windowsHandle,OBJ_LABEL);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---
//ObjectsDeleteAll(myWindowsHandle,OBJ_LABEL);
PositionCalculator();
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
}
//+------------------------------------------------------------------+
//| PositionCalculator Function |
//+------------------------------------------------------------------+
/*
函数:仓位计算器
*/
void PositionCalculator()
{
//账户信息
if(IsConnected())
{
string symbol=Symbol();
double marginValue=MarketInfo(symbol,MODE_MARGINREQUIRED);
double BuyPrice=MarketInfo(symbol,MODE_ASK);
double SellPrice=MarketInfo(symbol,MODE_BID);
double SpreadValue=MarketInfo(symbol,MODE_SPREAD);
double SpreadPrice=MathAbs(BuyPrice-SellPrice);
double ATRIndexValue=iATR(symbol,0,ATRPeriod,0);//ATR 指标读数
double Account_Equity=AccountEquity()*MarginRatio/100;//账户资金
double Account_FreeMargin=AccountFreeMargin()*MarginRatio/100;流动资金
double TickValue=MarketInfo(symbol,MODE_TICKVALUE);//单点价值
int Account_Leverage=AccountLeverage();//杠杆
if(Account_Leverage == 0) return;
double myDvol=ATRIndexValue*TickValue;//绝对波幅
double MinLot=MarketInfo(symbol,MODE_MINLOT);//最小交易量
double UnitLots=iFundsToHands(symbol,(0.01*Account_Equity*(1/Account_Leverage))/myDvol);//单位头寸
if(UnitLots<MinLot) UnitLots=MinLot;//避免计算错误
double MaxLots=iFundsToHands(Symbol(),Account_Equity);
//if(MaxLots<MinLot) MaxLots=MinLot;
double MaxNum=MaxLots/UnitLots;//最大头寸数量
double MaxFreeLots=iFundsToHands(Symbol(),Account_FreeMargin);
double MaxFreeNum=MaxFreeLots/UnitLots;//最大头寸数量
iDisplayInfo("symbol","当前货币:"+symbol,0,15,20,8,"",indicator_color1);
iDisplayInfo("marginValue","标准保证金:"+DoubleToStr(marginValue,2),0,15,35,8,"",indicator_color1);
iDisplayInfo("SpreadValue","浮动点差:"+DoubleToStr(SpreadValue,0),0,15,50,8,"",indicator_color1);
iDisplayInfo("SpreadPrice","买卖点差:"+DoubleToStr(SpreadPrice,Digits),0,15,65,8,"",indicator_color1);
iDisplayInfo("SpreadPrice","买卖点差:"+DoubleToStr(SpreadPrice,Digits),0,15,65,8,"",indicator_color1);
iDisplayInfo("MinLot","最小下单量:"+DoubleToStr(MinLot,2),0,15,80,8,"",indicator_color1);
iDisplayInfo("Leverage","杠杆比例:1:"+IntegerToString(Account_Leverage),0,150,20,8,"",indicator_color1);
iDisplayInfo("TickValue","单点价值:"+DoubleToStr(TickValue,Digits),0,150,35,8,"",indicator_color1);
iDisplayInfo("ATRIndexReadings","ATR读数:"+DoubleToStr(ATRIndexValue,Digits),0,150,50,8,"",indicator_color1);
iDisplayInfo("Dvol","绝对波幅:"+DoubleToStr(myDvol,Digits),0,150,65,8,"",indicator_color1);
iDisplayInfo("Unit","单位头寸:"+DoubleToStr(UnitLots,2),0,150,80,8,"",indicator_color1);
iDisplayInfo("AccountEquity","账户资金:"+DoubleToStr(Account_Equity,2),0,285,20,8,"",indicator_color1);
iDisplayInfo("AccountFreeMargin","自由资金:"+DoubleToStr(Account_FreeMargin,2),0,285,35,8,"",indicator_color2);
iDisplayInfo("MaxLots","最大头寸:"+DoubleToStr(MaxLots,2)+"/"+DoubleToStr(MaxNum,0),0,285,50,8,"",indicator_color1);
iDisplayInfo("newLots","自由头寸:"+DoubleToStr(MaxFreeLots,2)+"/"+DoubleToStr(MaxFreeNum,0),0,285,65,8,"",indicator_color2);
int TradeCount=OrdersTotal();//订单数
long PositionSizeCount=0;//订单量
double PositionSizeTotal=0.0;//订单实量
long symbolTradeTotal=0; //货币单数
double symbolPositionSize=0.00;//货币单量
if(TradeCount>0)
{
for(int OrderIndex=0;OrderIndex<=TradeCount-1;OrderIndex++)
{
if(OrderSelect(OrderIndex,SELECT_BY_POS,MODE_TRADES))
{
string Order_Symbol=OrderSymbol();
double Order_Lots=OrderLots();
double OrderSymbolMinLot=MarketInfo(Order_Symbol,MODE_MINLOT);//最小交易量
PositionSizeCount+=StringToInteger(DoubleToStr(Order_Lots/OrderSymbolMinLot,0));
PositionSizeTotal+=(Order_Lots/OrderSymbolMinLot)*MinLot;
if((Order_Symbol==symbol))
{
symbolTradeTotal++;
symbolPositionSize+=Order_Lots;
}
}
}
}
iDisplayInfo("TradeCount","订单总数:"+IntegerToString(TradeCount),0,420,20,8,"",indicator_color2);
iDisplayInfo("PositionSizeTotal","订单总量:"+DoubleToStr(PositionSizeTotal,2)+"("+DoubleToStr(PositionSizeTotal*100.00/MaxLots,2)+"%)",0,420,35,8,"",indicator_color2);
if(TradeCount>0)
{
iDisplayInfo("symbolTradeTotal","货币单数:"+IntegerToString(symbolTradeTotal)+"("+DoubleToStr(symbolTradeTotal*100.00/TradeCount,2)+"%)",0,420,50,8,"",indicator_color2);
}
else
{
iDisplayInfo("symbolTradeTotal","货币单数:"+IntegerToString(symbolTradeTotal)+"("+DoubleToStr(symbolTradeTotal*100.00/100,2)+"%)",0,420,50,8,"",indicator_color2);
}
iDisplayInfo("symbolPositionSize","货币单量:"+DoubleToStr(symbolPositionSize,2)+"("+DoubleToStr(symbolPositionSize*100.00/MaxLots,2)+"%)",0,420,65,8,"",indicator_color2);
}
}
//+------------------------------------------------------------------+
//| iDisplayInfo Function |
//+------------------------------------------------------------------+
void iDisplayInfo(string LableName,string LableDoc,int LableCorner,
int LableX,int LableY,int DocSize,string DocStyle,color DocColor)
{
if(LableCorner == -1 ) return;
LableName=LableName+DoubleToStr(windowsHandle,0);
ObjectCreate(LableName,OBJ_LABEL,windowsHandle,0,0);//建立卷标对象
ObjectSetText(LableName,LableDoc,DocSize,DocStyle,DocColor);//定义对象属性
ObjectSet(LableName,OBJPROP_CORNER,LableCorner);//确定坐标原点。0 左上角 1 右上角 2 左下角 3 右下角 -1 不显示
ObjectSet(LableName,OBJPROP_XDISTANCE,LableX);//定义横坐标,单位:像素
ObjectSet(LableName,OBJPROP_YDISTANCE,LableY);//定义纵坐标,单位:像素
}
//+------------------------------------------------------------------+
//| iFundsToHands Function |
//+------------------------------------------------------------------+
/*
函数:金额转换手数
参数:symbol 货币对
priceFunds 资金基数
*/
double iFundsToHands(string symbol,double priceFunds)
{
//计算可开仓手数 1标准手开仓自由保证金
double openLots=priceFunds/MarketInfo(symbol,MODE_MARGINREQUIRED);
//手数转整形 最小允许标准手数
openLots=MathFloor(openLots/MarketInfo(symbol,MODE_MINLOT))*MarketInfo(symbol,MODE_MINLOT);
return openLots;
}
//+------------------------------------------------------------------+
//| PositionCalculator.mq4 |
//| Copyright 2015, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_separate_window//副窗口显示
#property indicator_buffers 2;
#property indicator_color1 SeaGreen //文字颜色
#property indicator_color2 Red //警告文字颜色
input int ATRPeriod=14;//ATR活动周期
input int MarginRatio=10;//保证金比例 Min: 1(1%) Max 100(100%)
int windowsHandle=0;//WindowFind(WindowExpertName());//获取当前指标所在窗口序号 0 主窗口
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
IndicatorShortName("PositionCalculator("+IntegerToString(ATRPeriod)+","+IntegerToString(MarginRatio)+")");//指标标题
windowsHandle=WindowFind("PositionCalculator("+IntegerToString(ATRPeriod)+","+IntegerToString(MarginRatio)+")");//获取当前指标所在窗口序号 0 主窗口
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
ObjectsDeleteAll(windowsHandle,OBJ_LABEL);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---
//ObjectsDeleteAll(myWindowsHandle,OBJ_LABEL);
PositionCalculator();
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam)
{
//---
}
//+------------------------------------------------------------------+
//| PositionCalculator Function |
//+------------------------------------------------------------------+
/*
函数:仓位计算器
*/
void PositionCalculator()
{
//账户信息
if(IsConnected())
{
string symbol=Symbol();
double marginValue=MarketInfo(symbol,MODE_MARGINREQUIRED);
double BuyPrice=MarketInfo(symbol,MODE_ASK);
double SellPrice=MarketInfo(symbol,MODE_BID);
double SpreadValue=MarketInfo(symbol,MODE_SPREAD);
double SpreadPrice=MathAbs(BuyPrice-SellPrice);
double ATRIndexValue=iATR(symbol,0,ATRPeriod,0);//ATR 指标读数
double Account_Equity=AccountEquity()*MarginRatio/100;//账户资金
double Account_FreeMargin=AccountFreeMargin()*MarginRatio/100;流动资金
double TickValue=MarketInfo(symbol,MODE_TICKVALUE);//单点价值
int Account_Leverage=AccountLeverage();//杠杆
if(Account_Leverage == 0) return;
double myDvol=ATRIndexValue*TickValue;//绝对波幅
double MinLot=MarketInfo(symbol,MODE_MINLOT);//最小交易量
double UnitLots=iFundsToHands(symbol,(0.01*Account_Equity*(1/Account_Leverage))/myDvol);//单位头寸
if(UnitLots<MinLot) UnitLots=MinLot;//避免计算错误
double MaxLots=iFundsToHands(Symbol(),Account_Equity);
//if(MaxLots<MinLot) MaxLots=MinLot;
double MaxNum=MaxLots/UnitLots;//最大头寸数量
double MaxFreeLots=iFundsToHands(Symbol(),Account_FreeMargin);
double MaxFreeNum=MaxFreeLots/UnitLots;//最大头寸数量
iDisplayInfo("symbol","当前货币:"+symbol,0,15,20,8,"",indicator_color1);
iDisplayInfo("marginValue","标准保证金:"+DoubleToStr(marginValue,2),0,15,35,8,"",indicator_color1);
iDisplayInfo("SpreadValue","浮动点差:"+DoubleToStr(SpreadValue,0),0,15,50,8,"",indicator_color1);
iDisplayInfo("SpreadPrice","买卖点差:"+DoubleToStr(SpreadPrice,Digits),0,15,65,8,"",indicator_color1);
iDisplayInfo("SpreadPrice","买卖点差:"+DoubleToStr(SpreadPrice,Digits),0,15,65,8,"",indicator_color1);
iDisplayInfo("MinLot","最小下单量:"+DoubleToStr(MinLot,2),0,15,80,8,"",indicator_color1);
iDisplayInfo("Leverage","杠杆比例:1:"+IntegerToString(Account_Leverage),0,150,20,8,"",indicator_color1);
iDisplayInfo("TickValue","单点价值:"+DoubleToStr(TickValue,Digits),0,150,35,8,"",indicator_color1);
iDisplayInfo("ATRIndexReadings","ATR读数:"+DoubleToStr(ATRIndexValue,Digits),0,150,50,8,"",indicator_color1);
iDisplayInfo("Dvol","绝对波幅:"+DoubleToStr(myDvol,Digits),0,150,65,8,"",indicator_color1);
iDisplayInfo("Unit","单位头寸:"+DoubleToStr(UnitLots,2),0,150,80,8,"",indicator_color1);
iDisplayInfo("AccountEquity","账户资金:"+DoubleToStr(Account_Equity,2),0,285,20,8,"",indicator_color1);
iDisplayInfo("AccountFreeMargin","自由资金:"+DoubleToStr(Account_FreeMargin,2),0,285,35,8,"",indicator_color2);
iDisplayInfo("MaxLots","最大头寸:"+DoubleToStr(MaxLots,2)+"/"+DoubleToStr(MaxNum,0),0,285,50,8,"",indicator_color1);
iDisplayInfo("newLots","自由头寸:"+DoubleToStr(MaxFreeLots,2)+"/"+DoubleToStr(MaxFreeNum,0),0,285,65,8,"",indicator_color2);
int TradeCount=OrdersTotal();//订单数
long PositionSizeCount=0;//订单量
double PositionSizeTotal=0.0;//订单实量
long symbolTradeTotal=0; //货币单数
double symbolPositionSize=0.00;//货币单量
if(TradeCount>0)
{
for(int OrderIndex=0;OrderIndex<=TradeCount-1;OrderIndex++)
{
if(OrderSelect(OrderIndex,SELECT_BY_POS,MODE_TRADES))
{
string Order_Symbol=OrderSymbol();
double Order_Lots=OrderLots();
double OrderSymbolMinLot=MarketInfo(Order_Symbol,MODE_MINLOT);//最小交易量
PositionSizeCount+=StringToInteger(DoubleToStr(Order_Lots/OrderSymbolMinLot,0));
PositionSizeTotal+=(Order_Lots/OrderSymbolMinLot)*MinLot;
if((Order_Symbol==symbol))
{
symbolTradeTotal++;
symbolPositionSize+=Order_Lots;
}
}
}
}
iDisplayInfo("TradeCount","订单总数:"+IntegerToString(TradeCount),0,420,20,8,"",indicator_color2);
iDisplayInfo("PositionSizeTotal","订单总量:"+DoubleToStr(PositionSizeTotal,2)+"("+DoubleToStr(PositionSizeTotal*100.00/MaxLots,2)+"%)",0,420,35,8,"",indicator_color2);
if(TradeCount>0)
{
iDisplayInfo("symbolTradeTotal","货币单数:"+IntegerToString(symbolTradeTotal)+"("+DoubleToStr(symbolTradeTotal*100.00/TradeCount,2)+"%)",0,420,50,8,"",indicator_color2);
}
else
{
iDisplayInfo("symbolTradeTotal","货币单数:"+IntegerToString(symbolTradeTotal)+"("+DoubleToStr(symbolTradeTotal*100.00/100,2)+"%)",0,420,50,8,"",indicator_color2);
}
iDisplayInfo("symbolPositionSize","货币单量:"+DoubleToStr(symbolPositionSize,2)+"("+DoubleToStr(symbolPositionSize*100.00/MaxLots,2)+"%)",0,420,65,8,"",indicator_color2);
}
}
//+------------------------------------------------------------------+
//| iDisplayInfo Function |
//+------------------------------------------------------------------+
void iDisplayInfo(string LableName,string LableDoc,int LableCorner,
int LableX,int LableY,int DocSize,string DocStyle,color DocColor)
{
if(LableCorner == -1 ) return;
LableName=LableName+DoubleToStr(windowsHandle,0);
ObjectCreate(LableName,OBJ_LABEL,windowsHandle,0,0);//建立卷标对象
ObjectSetText(LableName,LableDoc,DocSize,DocStyle,DocColor);//定义对象属性
ObjectSet(LableName,OBJPROP_CORNER,LableCorner);//确定坐标原点。0 左上角 1 右上角 2 左下角 3 右下角 -1 不显示
ObjectSet(LableName,OBJPROP_XDISTANCE,LableX);//定义横坐标,单位:像素
ObjectSet(LableName,OBJPROP_YDISTANCE,LableY);//定义纵坐标,单位:像素
}
//+------------------------------------------------------------------+
//| iFundsToHands Function |
//+------------------------------------------------------------------+
/*
函数:金额转换手数
参数:symbol 货币对
priceFunds 资金基数
*/
double iFundsToHands(string symbol,double priceFunds)
{
//计算可开仓手数 1标准手开仓自由保证金
double openLots=priceFunds/MarketInfo(symbol,MODE_MARGINREQUIRED);
//手数转整形 最小允许标准手数
openLots=MathFloor(openLots/MarketInfo(symbol,MODE_MINLOT))*MarketInfo(symbol,MODE_MINLOT);
return openLots;
}
//+------------------------------------------------------------------+