this it what i'm using, do your own changes
double Lots = 0.0; double Profit = 0.0; double OCP = 0.0; double average= 0.0; int OT = 0; for (int i = OrdersTotal() - 1; i >= 0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); //----- if (OrderSymbol() == Symbol() && OrderType() == OP_BUY || OrderType() == OP_SELL) { Lots = Lots + OrderLots(); Profit = Profit + OrderProfit() + OrderCommission() + OrderSwap(); OCP = OrderClosePrice(); OT = OrderType(); } } double pips = Profit/Lots/MarketInfo(Symbol(),MODE_TICKVALUE); if (OT == OP_BUY) { average = OCP - (pips*Point); } if (OT == OP_SELL) { average = OCP + (pips*Point); } Alert ("Lots = ", Lots); Alert ("Profit = ", Profit); Alert ("pips = ", pips); Alert (DoubleToStr(OCP,5)); Alert ("average is: ", DoubleToStr(average,5));
I have code a EA with a AveragePrice_OP_BUY_Orders function ()
This EA buys a lot each hour, and compute the average open price.
//+------------------------------------------------------------------+ //| BacktestHourly.mq4 | //| Copyright © 2012, Pierre8r | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "Copyright © 2012, Pierre8r" #property link "http://www.metaquotes.net" #define MAGICMA 20050610 int HourOfTheLastOrder = 0; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- if(HourOfTheLastOrder != Hour()) { /* int num = 1 + 10*MathRand()/32768; // 1-10 double lots = num*0.1; Print("random value : ", num, " Lots : ", lots); */ OrderSend(Symbol(),OP_BUY,1,Bid,3,0,0,"",MAGICMA,0,Red); Print("average : ", AveragePrice_OP_BUY_Orders()); HourOfTheLastOrder = Hour(); } //---- return(0); } //---------------------------------------------------------------------------------- // Average Price Buy Orders //----------------------------------------------------------------------------- 1 -- double AveragePrice_OP_BUY_Orders() { //---- double Lots = 0.0; double OrderOPrice = 0.0; double LotsPrice = 0.0; double average = 0.0; for (int i = OrdersTotal() - 1; i >= 0; i--) { OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol() && OrderType() == OP_BUY) { Lots = Lots + OrderLots(); LotsPrice = LotsPrice + OrderLots()*OrderOpenPrice(); Print("Lots : ", OrderLots(), " OrderOpenPrice : ", OrderOpenPrice()); } } average = LotsPrice/Lots; //---- return(average); } //----- //+------------------------------------------------------------------+
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello,
Has anyone already coded a function that computes the average price for open long positions and the average price of its open short positions, and would be willing to share this code?
Also with the calculation of the gain / loss potential if closing all positions.
Thanks,
Pierre8r