128 lines
5.7 KiB
MQL5
128 lines
5.7 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| MaxAllowedVolumeToBalance.mq5 |
|
|
//| Copyright 2018,Neuralrobotica. |
|
|
//| https://www.neuralrobotica.com |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2018,Neuralrobotica."
|
|
#property link "https://www.neuralrobotica.com"
|
|
#property version "1.00"
|
|
//+------------------------------------------------------------------+
|
|
//| Script program start function |
|
|
//+------------------------------------------------------------------+
|
|
|
|
/*
|
|
this script calculates the maximum available volume for a symbol. this scirpt looks at the current balance
|
|
and divides the value of the symbol on the current chart with the current balance, this gives a return value of max volume.
|
|
it is highly recommended that you do not use the full available balance for volume trading.
|
|
also for risk management the maximum allowed open symbol are 4 with a percentage of 20 related to the current balance.
|
|
*/
|
|
|
|
// TODO* add a function that open a new position automatically and also with the correct calculated volume or lot size
|
|
// check if there are no more than 4 symbol postions open, if so then no more symbol positions may be opened.
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void OnStart()
|
|
{
|
|
//---
|
|
// every variable begins with 'var_'
|
|
|
|
// var_AccountBalance, Get the current account balance
|
|
|
|
double var_AccountBalance=AccountInfoDouble(ACCOUNT_BALANCE);
|
|
|
|
// var_SymbolBuy, Get the current ask/long price from the chart symbol
|
|
|
|
double var_SymbolBuy=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
|
|
|
|
// var_SymbolSell, Get the current bid/short price from the chart symbol
|
|
|
|
double var_SymbolSell=SymbolInfoDouble(_Symbol,SYMBOL_BID);
|
|
|
|
// create a formula variable using called var_Formula
|
|
// define balance divided by current symbol = max volume
|
|
//
|
|
|
|
// var_Formula_Buy Divides the current balance with the ask/long price of the chart symbol
|
|
|
|
double var_Formula_Buy=var_AccountBalance/var_SymbolBuy;
|
|
|
|
// var_Formula_Sell Divides the current balance with the bid/short price of the chart symbol
|
|
|
|
double var_Formula_Sell=var_AccountBalance/var_SymbolSell;
|
|
|
|
// Get contract information of the current symbol, some might have 1.0 or 10 or perhaps 100
|
|
|
|
double var_Contract_Size=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_CONTRACT_SIZE);
|
|
|
|
// Get contract name information
|
|
|
|
string var_Contract_Name=SymbolInfoString(_Symbol,SYMBOL_DESCRIPTION);
|
|
|
|
// Convert Volume to 20 percent and lots to 20 percent
|
|
double var_Formula_Buy_Percentage_20=(var_Formula_Buy/100)*20;
|
|
double var_Formula_Buy_Percentage_20_1=var_Formula_Buy_Percentage_20/var_Contract_Size;
|
|
|
|
// Convert Volume to 20 percent and lots to 20 percent
|
|
double var_Formula_Sell_Percentage_20=(var_Formula_Sell/100)*20;
|
|
double var_Formula_Sell_Percentage_20_1=var_Formula_Sell_Percentage_20/var_Contract_Size;
|
|
|
|
// Convert volume to lots
|
|
|
|
double var_Lot_Buy=var_Formula_Buy/var_Contract_Size;
|
|
double var_Lot_Sell=var_Formula_Sell/var_Contract_Size;
|
|
|
|
// set percentage to 20
|
|
|
|
double var_Balance_Percentage=20;
|
|
|
|
// calculate 20 percent of the current balance
|
|
|
|
double var_Calculated_Percentage_Balance=(var_AccountBalance/100)*var_Balance_Percentage;
|
|
|
|
/*
|
|
// Calculate 20 percent of maximum allowed long volume
|
|
|
|
double var_Calculated_Percentage_Volume_Long=NULL;
|
|
|
|
// Calculate 20 percent of maximum allowed short volume
|
|
|
|
double var_Calculated_Percentage_Volume_Short=NULL;
|
|
|
|
// Calculate 20 percent of maximum allowed long lots
|
|
*/
|
|
|
|
double var_Calculated_Percentage_Long_Lots=var_Lot_Buy/var_Balance_Percentage;
|
|
|
|
// Calculate 20 percent of maximum allowed short lots
|
|
|
|
double var_Calculated_Percentage_Short_Lots=var_Lot_Sell/var_Balance_Percentage;
|
|
|
|
// Create the chart information as a comment
|
|
MessageBox("Position info "+var_Contract_Name+" (Max allowed open Positions Symbols is 4, Max usage 80 Percent of the current Balance, Change the parameters at own risk)\n"+
|
|
"\nContract size: "+DoubleToString(var_Contract_Size,2)+" unit(s)"+
|
|
"\nBuy Price of "+var_Contract_Name+" is: "+DoubleToString(var_SymbolBuy,2)+
|
|
"\nRecommend not to open a position when the number of 'Symbol per total units' is below 1.0"+
|
|
"\nMax (100% of the balance) long allowed volume (in symbol per total units): "+DoubleToString(var_Formula_Buy,2)+
|
|
"\nMax (100% of the balance) long volume to lots: "+DoubleToString(var_Lot_Buy,2)+
|
|
"\n20% Balance of long lots: "+DoubleToString(var_Formula_Buy_Percentage_20_1,2)+
|
|
"\nSell Price of "+var_Contract_Name+" is: "+DoubleToString(var_SymbolSell,2)+
|
|
"\nRecommend not to open a position when the number of 'Symbol per total units' is below 1.0"+
|
|
"\nMax (100% of the balance) short allowed volume (in symbol total units): "+DoubleToString(var_Formula_Sell,2)+
|
|
"\nMax (100% of the balance) short volume to lots: "+DoubleToString(var_Lot_Sell,2)+
|
|
"\n20% Balance of short lots: "+DoubleToString(var_Formula_Sell_Percentage_20_1,2),
|
|
"Position and lots information"
|
|
);
|
|
|
|
// Wait 1 second before going to the next line 'Comment("");'
|
|
|
|
// Sleep(1000);
|
|
|
|
// Clear the current chart of any previous comments
|
|
|
|
// Comment("");
|
|
|
|
// End of program
|
|
}
|
|
//+------------------------------------------------------------------+
|