/*============================================================= Info: Order Library Name: OrderLib.mq4 Author: Eric Pribitzer Version: 0.94 Update: 2011-11-03 Notes: MAX_ORDERS issue. OL_ClearBuffer()removed (Thx Valentin!) Version: 0.93 Update: 2011-05-24 Notes: StopLoss calculation for LOT and RISK, OL_ postfix added Version: 0.92 Update: 2011-05-05 Notes: add OP_ALL at OL_enumOrderList(...), OL_PROFITPIPS (calc TP+SL) Version: 0.9 Update: 2011-03-30 Notes: LotSize Calculation Update, CUSTOM_TICKVALUE Version: 0.8 Update: 2011-03-28 Notes: Bug WriteBuffer removed in TestingMode OL_RISK_PIPS and OL_PROFIT_PIPS added and tested OL_MAX_ORDERS added Version: 0.7 Update: 2010-02-11 Notes: --- Copyright (C) 2010 Erich Pribitzer =============================================================*/ #property copyright "Copyright © 2011, Erich Pribitzer" #property link "http://www.wartris.com" #property library #include #include bool OL_ALLOW_ORDER = true; double OL_RISK_PERC = 0; // in % int OL_RISK_PIPS = 0; // 0=auto (calc price-stoploss) int OL_PROFIT_PIPS = 0; int OL_TRAILING_STOP = 0; double OL_LOT_SIZE = 1; // 0=auto lot depends on risk pips and risk percent per trade double OL_INITIAL_LOT = 0; double OL_CUSTOM_TICKVALUE = 0; // 0=no custom tickvalue int OL_SLIP_PAGE = 0; bool OL_STOPSBYMODIFY = false; // true = sl,tp will set by modify order double OL_MAX_LOT = 2; int OL_MAGIC = 0xA1200; // 0xA12xx Note: the first two digits must be zero int OL_MAX_ORDERS = 3; bool OL_ORDER_DUPCHECK = true; bool OL_OPPOSITE_CLOSE = false; // oder will only closed when the new TP lies between SL and Price of an active order bool OL_ORDER_COLOR = true; bool OL_MYSQL_LOG = true; double OL_orderInfo[OL_SIZE][OL_ORDER_BUFFER_SIZE]; string OL_orderInfoDesc[OL_ORDER_BUFFER_SIZE]; double OL_orderProp[OL_SIZE]; string OL_orderDesc; #define OL_ORDER_OPEN_CLR Red #define OL_ORDER_CLOSE_CLR Green #define OL_ORDER_MODIFY_CLR Violet #define OL_CL_OPPOSITCLOSE 1 #define OL_CL_TIMEEXPIRE 2 #define OL_CL_STOPLOSS 3 #define OL_CL_TAKEPROFIT 4 #define OL_CL_UNKNOWN 5 #define OL_CL_BYEA 6 #define OL_CL_DUPCHECK 7 #define OL_CL_SIZE 8 string OL_CL_STRING[OL_CL_SIZE]={"","Opposit","Expire","StopLoss","TakePorfit","Unknown","ByEA","Dupcheck"}; double OL_BUFFER_COUNT = 0; bool OrderLib_Initialized; int OL_Init(bool ALLOW_ORDER,double RISK_PERC,int RISK_PIPS,int PROFIT_PIPS,int TRAILING_STOP, double LOT_SIZE,double INITIAL_LOT,double CUSTOM_TICKVALUE, int SLIPPAGE, bool STOPSBYMODIFY, double MAX_LOT, int MAX_ORDERS, int MAGIC, bool ORDER_DUPCHECK, bool OPPOSITE_CLOSE, bool ORDER_COLOR, bool MYSQL_LOG) { OL_ALLOW_ORDER = ALLOW_ORDER; OL_RISK_PERC = RISK_PERC; OL_RISK_PIPS = RISK_PIPS; OL_PROFIT_PIPS = PROFIT_PIPS; OL_TRAILING_STOP = TRAILING_STOP; OL_LOT_SIZE = LOT_SIZE; OL_INITIAL_LOT = INITIAL_LOT; OL_CUSTOM_TICKVALUE = CUSTOM_TICKVALUE; OL_SLIP_PAGE = SLIPPAGE; OL_STOPSBYMODIFY = STOPSBYMODIFY; OL_MAX_LOT = MAX_LOT; OL_MAX_ORDERS = MAX_ORDERS; OL_MAGIC = MAGIC; OL_ORDER_DUPCHECK = ORDER_DUPCHECK; OL_OPPOSITE_CLOSE = OPPOSITE_CLOSE; OL_ORDER_COLOR = ORDER_COLOR; OL_MYSQL_LOG = MYSQL_LOG; BA_Init(); if(IsTesting()==true) { //OL_ClearBuffer(); //Print("Testing Mode: Clear Order Buffer"); } if(OrderLib_Initialized==false) { Print("Init OrderLib V0.94"); OL_ReadBuffer(); OL_SyncBuffer(); OrderLib_Initialized=true; } } int OL_Deinit() { OL_WriteBuffer(); OrderLib_Initialized=false; Print("Deinit OrderLib"); } int OL_SyncBuffer() { if(IsTesting()==true) return; int total=OrdersTotal(); int magic=0; string symbol=""; double tp=0; double sl=0; double lot=0; int ma=0; int inx=0; double buff[OL_ORDER_BUFFER_SIZE]; ArrayInitialize(buff,0); for(int pos=0;pos0) { tp=NormalizeDouble(OrderTakeProfit(),DIGITS); sl=NormalizeDouble(OrderStopLoss(),DIGITS); if(tp>0) OL_orderInfo[OL_TAKEPROFIT][magic&0xFF]=tp; if(sl>0) OL_orderInfo[OL_STOPLOSS][magic&0xFF]=sl; OL_orderInfo[OL_LOTSIZE][magic&0xFF]=NormalizeDouble(OrderLots(),LOTDIGITS); } // } } } for(int binx=0;binx0) // only running orders { OL_orderInfo[OL_ID][binx]=0; OL_orderInfo[OL_CLOSEREASON][binx]=OL_CL_UNKNOWN; Print("Clear Index: ",binx); } } } int OL_ReadBuffer() { string key=""; int count=0; ArrayInitialize(OL_orderProp,0); for(int binx=0;binx> if(OL_orderInfo[OL_CLOSEREASON][binx]>0) { int clinx=OL_orderInfo[OL_CLOSEREASON][binx]; OL_orderInfo[OL_CTIME][binx]=TimeCurrent(); OL_orderInfo[OL_CBAR][binx]=Bars; OL_orderInfo[OL_CSPREAD][binx]=NormalizeDouble(Ask-Bid,DIGITS)/POINT; OL_orderInfo[OL_CTICKCOUNT][binx]= GetTickCount() & 0x7FFFFFFF; // was active ? if(OL_orderInfo[OL_OID][binx]>0) // yes >> { // print stats Print("Close reason: ",OL_CL_STRING[clinx], " / Order ID: ",OL_orderInfo[OL_OID][binx], " / Minutes: ",NormalizeDouble((OL_orderInfo[OL_CTIME][binx]-OL_orderInfo[OL_OTIME][binx])/60,1), " / HiProfit: ",OL_orderInfo[OL_HIPROFIT][binx], " / LoProfit: ",OL_orderInfo[OL_LOPROFIT][binx], " / Profit: ",OL_orderInfo[OL_PROFIT][binx], " / Desc: ",OL_orderInfoDesc[binx]); } int xx=OL_orderInfo[OL_CLOSEREASON][binx]; OL_orderInfo[OL_CLOSEREASON][binx]=0; OL_orderInfo[OL_ERRORNO][binx]=0; Print("Clear ",binx,",",OL_CL_STRING[xx]); } if(IsTesting()==false ) { for(int inx=0;inx= OL_MAX_ORDERS) { Print("Info: max order limit reached"); return (0); } int inx; int binx=OL_GetFreeIndex(); if(binx==-1) { ArrayInitialize(OL_orderProp,0); return (-1); } if(OL_OPPOSITE_CLOSE==true) OL_processOppositClose(OL_orderProp[OL_TYPE],OL_orderProp[OL_TAKEPROFIT]); OL_orderProp[OL_OPRICE]=NormalizeDouble(OL_orderProp[OL_OPRICE],DIGITS); OL_orderProp[OL_TAKEPROFIT]=NormalizeDouble(OL_orderProp[OL_TAKEPROFIT],DIGITS); OL_orderProp[OL_STOPLOSS]=NormalizeDouble(OL_orderProp[OL_STOPLOSS],DIGITS); if(OL_orderProp[OL_OPRICE]==0) { if(OL_orderProp[OL_TYPE]==OP_BUY) OL_orderProp[OL_OPRICE]=NormalizeDouble(Ask,DIGITS); else OL_orderProp[OL_OPRICE]=NormalizeDouble(Bid,DIGITS); } if(OL_ORDER_DUPCHECK==true) { for(inx=0;inx0) { ArrayInitialize(OL_orderProp,0); return (-1); // same order is running!! cancel register order } } } } OL_orderInfo[OL_ID][binx]=OL_SCHEDULED; // oder SCHEDULED!! for(inx=1;inx0 && OL_orderInfo[OL_CLOSEREASON][binx]==0) // change only active and none closed orders { OL_orderInfo[property][binx]=value; } } // Modify or open new order void OL_processOrder() { OL_processClose(); double price=0; for(int binx=0;binx