i have been trying to store the sum of order buy lots and the order sell lots in a buffer can anyone help pliz, thanks in advance
//+------------------------------------------------------------------+
//| chang.mq4 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LawnGreen
#property indicator_color2 DeepPink
#property indicator_width1 1
#property indicator_width2 1
double totalBuyLots[];
double totalSellLots[];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
void PrintText(string object, string text, int x, int y)
{
ObjectCreate(object, OBJ_LABEL, 0, 0, 0);
ObjectSetText(object, text, 9, "Verdana", Yellow);
ObjectSet(object, OBJPROP_CORNER, 0);
ObjectSet(object, OBJPROP_XDISTANCE, x);
ObjectSet(object, OBJPROP_YDISTANCE, y);
}
void CalculateLots()
{
double totalBuyLots = 0;
double totalSellLots = 0;
for(int i=0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
{
//if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUY)
totalBuyLots += OrderLots();
if(OrderType() == OP_SELL)
totalSellLots += OrderLots();
}
}
}
string sBuyText = "Total Buy = ";
sBuyText += DoubleToString(totalBuyLots, 2);
string sSellText = "Total Sell = ";
sSellText += DoubleToString(totalSellLots, 2);
PrintText("BuyObject", sBuyText, 20, 20);
PrintText("SellObject", sSellText, 20, 40);
}
int OnInit()
{
//--- indicator buffers mapping
//UP Arrow Buffer
SetIndexEmptyValue(0,0.0);
SetIndexStyle(0,DRAW_ARROW,0,EMPTY);
SetIndexArrow(0,233);
SetIndexBuffer(0,totalBuyLots);
//DOWN Arrow Buffer
SetIndexEmptyValue(1,0.0);
SetIndexStyle(1,DRAW_ARROW,0,EMPTY);
SetIndexArrow(1,234);
SetIndexBuffer(1,totalSellLots);
//---
CalculateLots();
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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[])
{
//---
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
CalculateLots();
}
//+------------------------------------------------------------------+
void CalculateLots() { double totalBuyLots = 0; double totalSellLots = 0; double BuyLots[]; double SellLots[]; for(int i=0; i < OrdersTotal(); i++) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true) { //if(OrderSymbol() == Symbol()) { if(OrderType() == OP_BUY) totalBuyLots += OrderLots(); ArrayResize(BuyLots,ArraySize(BuyLots)+1); BuyLots[ArraySize(BuyLots)-1] = OrdersLots(); if(OrderType() == OP_SELL) totalSellLots += OrderLots(); .... same } } }
i have been trying to store the sum of order buy lots and the order sell lots in a buffer can anyone help pliz, thanks in advance
...
Forum on trading, automated trading systems and testing trading strategies
When you post code please use the CODE button (Alt-S)!
Thank you.
chinje: have been trying to store the sum of order buy lots and the order sell lots in a buffer can anyone help pliz,
-
When you post code please use the CODE button (Alt-S)!
(For large amounts of code, attach it.) Please edit your post.
General rules and best pratices of the Forum. - General - MQL5 programming forum
Messages Editor -
Why did you post your MT4 question in the
Root /
MT5 General section
instead of the
MQL4 section, (bottom of the Root page?)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move this thread there soon. int OnInit(){ CalculateLots();
Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:- Terminal starts.
- Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
- OnInit is called.
- For indicators OnCalculate is called with any existing history.
- Human may have to enter password, connection to server begins.
- New history is received, OnCalculate called again.
- New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent and prices are valid.
for(int i=0; i < OrdersTotal(); i++){ if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true) { //if(OrderSymbol() == Symbol()) { // What is the purpose of the extra braces? if(OrderType() == OP_BUY) totalBuyLots += OrderLots(); if(OrderType() == OP_SELL) totalSellLots += OrderLots(); } // What is the purpose of the extra braces? } // if } // for
What is the purpose of the extra braces?-
Using OrdersTotal directly and/or no Magic number filtering on your
OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual
trading.)
Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum - You should be able to read your code out loud and have it make sense. You would
never write if( (2+2 == 4) == true) would you?
if(2+2 == 4) is sufficient. So don't write
if(bool == true), just use if(bool) or
if(!bool). Code becomes self documenting when you use meaningful
variable names, like bool isLongEnabled where as Long_Entry sounds
like a trigger price or a ticket number and "if long entry" is an incomplete
sentence.
- You have buffers, where do you store into them? Help you with what?
You haven't stated a problem, you stated a want.
Show us your attempt
(using CODE button) and state
the nature of your problem.
No free help
urgent help. - Icham Aidibe:
He already has buffers, no need for internal arrays.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
i have been trying to store the sum of order buy lots and the order sell lots in a buffer can anyone help pliz, thanks in advance
//+------------------------------------------------------------------+
//| chang.mq4 |
//| Copyright 2017, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LawnGreen
#property indicator_color2 DeepPink
#property indicator_width1 1
#property indicator_width2 1
double totalBuyLots[];
double totalSellLots[];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
void PrintText(string object, string text, int x, int y)
{
ObjectCreate(object, OBJ_LABEL, 0, 0, 0);
ObjectSetText(object, text, 9, "Verdana", Yellow);
ObjectSet(object, OBJPROP_CORNER, 0);
ObjectSet(object, OBJPROP_XDISTANCE, x);
ObjectSet(object, OBJPROP_YDISTANCE, y);
}
void CalculateLots()
{
double totalBuyLots = 0;
double totalSellLots = 0;
for(int i=0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
{
//if(OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUY)
totalBuyLots += OrderLots();
if(OrderType() == OP_SELL)
totalSellLots += OrderLots();
}
}
}
string sBuyText = "Total Buy = ";
sBuyText += DoubleToString(totalBuyLots, 2);
string sSellText = "Total Sell = ";
sSellText += DoubleToString(totalSellLots, 2);
PrintText("BuyObject", sBuyText, 20, 20);
PrintText("SellObject", sSellText, 20, 40);
}
int OnInit()
{
//--- indicator buffers mapping
//UP Arrow Buffer
SetIndexEmptyValue(0,0.0);
SetIndexStyle(0,DRAW_ARROW,0,EMPTY);
SetIndexArrow(0,233);
SetIndexBuffer(0,totalBuyLots);
//DOWN Arrow Buffer
SetIndexEmptyValue(1,0.0);
SetIndexStyle(1,DRAW_ARROW,0,EMPTY);
SetIndexArrow(1,234);
SetIndexBuffer(1,totalSellLots);
//---
CalculateLots();
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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[])
{
//---
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
CalculateLots();
}
//+------------------------------------------------------------------+