Hi,
Can someone please help me understand why this EA is not working unless the "ask manual confirmation" button is ticked???
A trendline drawn on the chart and the "description" field is populated with the trade parameters in a string such as "Action=Buy Trigger=5 TP=60 SL=30 TS=45 BE=10 Lots=1.5"
When the trend line is breached by the trigger pips, a trade should be entered......
Please help it is driving me crazy!
#define EA_NAME "Trader"
#define MAGIC_NUMBER 456456
#define PARAM_ACTION "Action"
#define PARAM_TRIGGER "Trigger"
#define PARAM_TAKEPROFIT "TP"
#define PARAM_STOPLOSS "SL"
#define PARAM_TRAILSTOP "TS"
#define PARAM_BREAKEVEN "BE"
#define PARAM_LOTS "Lots"
#define GRID_ID 0
#define GRID_TYPE 1
#define GRID_TRIGGER 2
#define GRID_TP 3
#define GRID_SL 4
#define GRID_TS 5
#define GRID_BE 6
#define GRID_LOTS 7
extern int Slippage = 30;
extern bool EmailAlert = false;
extern bool PopupAlert = false;
int start() {
// grid to hold order info
string sGrid[0][8];
ArrayResize(sGrid,0);
// Inspect lines for pending orders
doManageLineActions(sGrid);
// Manage open positions
// This is done after lines are checked so if an order is submitted
// it will show up in the grid as an open position.
doManagePositions(sGrid);
doDrawGrid(sGrid);
return(0);
}
void doManageLineActions(string& sGrid[][]) {
// iterate all objects on chart
for (int i=ObjectsTotal()-1;i>=0;i--){
// get object name and price at current bar
// if no price then simply move on
string sName = ObjectName(i);
double dPrice = getLinePrice(sName);
if (dPrice < 0) continue;
// get the params for the object starting with Action.
// if action isn't defined then simply move on
string sAction = getParam(sName,PARAM_ACTION);
if (sAction != "Buy" && sAction != "Sell") continue;
int iTrigger = StrToInteger(getParam(sName,PARAM_TRIGGER));
int iTakeProfit = StrToInteger(getParam(sName,PARAM_TAKEPROFIT));
int iStopLoss = StrToInteger(getParam(sName,PARAM_STOPLOSS));
int iTrailStop = StrToInteger(getParam(sName,PARAM_TRAILSTOP));
int iBreakEven = StrToInteger(getParam(sName,PARAM_BREAKEVEN));
double dLots = StrToDouble(getParam(sName,PARAM_LOTS));
// determine trigger range
double dTriggerUpper = MathMax(dPrice,dPrice + iTrigger*Point);
double dTriggerLower = MathMin(dPrice,dPrice + iTrigger*Point);
// if within trigger range, submit an order
if (Bid <= dTriggerUpper && Bid >= dTriggerLower) {
int iTicket = 0;
if (sAction == "Buy") {
iTicket = OrderSend(Symbol(),OP_BUY,dLots,Ask,Slippage,Ask-iStopLoss*Point,Ask+iTakeProfit*Point,"Line:"+sName,MAGIC_NUMBER);
}
else if (sAction == "Sell") {
iTicket = OrderSend(Symbol(),OP_SELL,dLots,Bid,Slippage,Bid+iStopLoss*Point,Bid-iTakeProfit*Point,"Line:"+sName,MAGIC_NUMBER);
}
// add trailstop and breakeven to globalvars to allow tracking through EA restart
GlobalVariableSet(getGlobalVarName(iTicket,PARAM_BREAKEVEN),iBreakEven);
GlobalVariableSet(getGlobalVarName(iTicket,PARAM_TRAILSTOP),iTrailStop);
// remove order info from the line
ObjectSetText(sName,"");
}
else {
// add to orders array as a pending action
ArrayResize(sGrid,ArrayRange(sGrid,0)+1);
int index = ArrayRange(sGrid,0)-1;
//Print("Name: ",sName, " Action: ", sAction, " Index: ", index);
sGrid[index][GRID_ID] = sName;
sGrid[index][GRID_TYPE] = getParam(sName,PARAM_ACTION);
sGrid[index][GRID_TRIGGER] = getParam(sName,PARAM_TRIGGER);
sGrid[index][GRID_TP] = getParam(sName,PARAM_TAKEPROFIT);
sGrid[index][GRID_SL] = getParam(sName,PARAM_STOPLOSS);
sGrid[index][GRID_TS] = getParam(sName,PARAM_TRAILSTOP);
sGrid[index][GRID_BE] = getParam(sName,PARAM_BREAKEVEN);
sGrid[index][GRID_LOTS] = getParam(sName,PARAM_LOTS);
}
}
}
// Find open positions owned by this EA and manage them.
void doManagePositions(string& sGrid[][]) {
// iterate all orders
for (int i=OrdersTotal()-1;i>=0;i--) {
// select the order
OrderSelect(i,SELECT_BY_POS);
// if not for this symbol or EA, move on
if (OrderSymbol()!=Symbol()) continue;
if (OrderMagicNumber() != MAGIC_NUMBER) continue;
// see if there's a trailing stop or break even defined
// these would have been previously stored in global vars
int iTicket = OrderTicket();
int iBreakEven = GlobalVariableGet(getGlobalVarName(iTicket,PARAM_BREAKEVEN));
int iTrailStop = GlobalVariableGet(getGlobalVarName(iTicket,PARAM_TRAILSTOP));
Print("Trail:", iTrailStop);
// perform stoploss maintenance
double dNewStop = 0;
if (OrderType() == OP_BUY) {
// check breakeven
if (iBreakEven > 0 && OrderOpenPrice() + iBreakEven * Point <= Bid)
dNewStop = OrderOpenPrice();
// check trailstop
if (iTrailStop > 0)
dNewStop = MathMax(dNewStop,Bid-iTrailStop*Point);
// update stoploss if required
if (dNewStop != 0 && dNewStop > OrderStopLoss())
OrderModify(iTicket,OrderOpenPrice(),dNewStop,OrderTakeProfit(),OrderExpiration());
}
if (OrderType() == OP_SELL) {
// check breakeven
if (iBreakEven > 0 && OrderOpenPrice() - iBreakEven * Point >= Bid)
dNewStop = OrderOpenPrice();
// check trailstop
if (iTrailStop > 0)
if (dNewStop == 0) dNewStop = Bid-iTrailStop*Point;
else dNewStop = MathMax(dNewStop,Bid-iTrailStop*Point);
// update stoploss if required
if (dNewStop != 0 && dNewStop < OrderStopLoss())
OrderModify(iTicket,OrderOpenPrice(),dNewStop,OrderTakeProfit(),OrderExpiration());
}
// add to orders array as an open position
ArrayResize(sGrid,ArrayRange(sGrid,0)+1);
int index = ArrayRange(sGrid,0)-1;
sGrid[index][GRID_ID] = iTicket;
if (OrderType() == OP_BUY) sGrid[index][GRID_TYPE] = "Buy";
if (OrderType() == OP_SELL) sGrid[index][GRID_TYPE] = "Sell";
sGrid[index][GRID_TRIGGER] = "";
sGrid[index][GRID_TP] = DoubleToStr(OrderTakeProfit(),Digits);
sGrid[index][GRID_SL] = DoubleToStr(OrderStopLoss(),Digits);
sGrid[index][GRID_TS] = iTrailStop;
sGrid[index][GRID_BE] = iBreakEven;
sGrid[index][GRID_LOTS] = OrderLots();
}
}
void doDrawGrid(string& sGrid[][]) {
int col1 = 5;
int col2 = col1 + 200;
int col3 = col2 + 80;
int col4 = col3 + 80;
int col5 = col4 + 80;
int col6 = col5 + 80;
int col7 = col6 + 80;
int col8 = col7 + 80;
string sName;
for (int i=ObjectsTotal()-1;i>=0;i--) {
sName = ObjectName(i);
if (StringFind(sName,"Grid") ==0) ObjectDelete(sName);
}
// create column labels
sName = "Grid0_ID";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col1);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"ID");
}
sName = "Grid0_Type";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col2);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"Type");
}
sName = "Grid0_Trigger";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col3);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"Trigger");
}
sName = "Grid0_TakeProfit";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col4);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"TakeProfit");
}
sName = "Grid0_StopLoss";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col5);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"StopLoss");
}
sName = "Grid0_TrailStop";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col6);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"TrailStop");
}
sName = "Grid0_BreakEven";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col7);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"BreakEven");
}
sName = "Grid0_Lots";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col8);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"Lots");
}
// iterate objects in grid array and draw them
for (i=0;i<ArrayRange(sGrid,0);i++) {
int yOffset = 15+(i+1)*15;
sName = "Grid"+(i+1)+"_ID";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col1);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_ID]);
sName = "Grid"+(i+1)+"_Type";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col2);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_TYPE]);
sName = "Grid"+(i+1)+"_Trigger";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col3);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_TRIGGER]);
sName = "Grid"+(i+1)+"_TakeProfit";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col4);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_TP]);
sName = "Grid"+(i+1)+"_StopLoss";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col5);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_SL]);
sName = "Grid"+(i+1)+"_TrailStop";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col6);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_TS]);
sName = "Grid"+(i+1)+"_BreakEven";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col7);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_BE]);
sName = "Grid"+(i+1)+"_Lots";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col8);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_LOTS]);
}
}
string getGlobalVarName(int iTicket, string sParam) {
return(EA_NAME + "_" + Symbol() + "_" + iTicket + "_" + sParam);
}
string getParam(string sLine, string sParam) {
// get description and drop an extra space at front and back
string desc = " " + ObjectDescription(sLine) + " ";
// mod the param to match how it would be represented in the desc
sParam = " " + sParam + "=";
// find the index of the param
int index = StringFind(desc,sParam);
// strip the description out from in front of the value
string value = StringSubstr(desc,index + StringLen(sParam));
// strip the description out from behind the value
index = StringFind(value," ");
if (index != -1) value = StringSubstr(value,0,index);
return(value);
}
double getLinePrice(string sObjName) {
int iObjType = ObjectType(sObjName);
// if it's a horizontal line...
if (iObjType == OBJ_HLINE)
return(ObjectGet(sObjName,OBJPROP_PRICE1));
// if it's a trend line...
if (iObjType == OBJ_TREND)
return(ObjectGetValueByShift(sObjName,0));
// otherwise...
return(-1);
}
/*
// Iterate over objects on this chart
for (int i=ObjectsTotal()-1;i>=0;i--) {
string sObjName = ObjectName(i);
string sAction;
string sTrigger;
string sTakeProfit;
string sStopLoss;
string sTrailStop;
string sBreakEven;
string sLots;
// if it's not a horizontal line or trend line, continue
if (ObjectType(sObjName) != OBJ_HLINE && ObjectType(sObjName) != OBJ_TREND)
continue;
// Is no action on this object, continue
sAction = getParam(sObjName,"Action");
if (sAction != "Buy" && sAction != "Sell")
continue;
// Is the line within alert proximity? If not, continue iteration
iProximity = MathAbs(Bid - dObjPrice) / Point;
if (iProximity > iObjAlert) continue;
// Fire the alert and clear alert from description
sMessage = "Alert triggered: " + Symbol() + "@" + DoubleToStr(Bid,Digits) +"."
+ " " + iProximity + " pips from " + sObjName + ".";
if (PopupAlert) Alert(sMessage);
if (EmailAlert) SendMail("Alert on " + Symbol(), sMessage);
// Clear the alert from the object description
clearObjectAlert(sObjName);
}
int getObjectAlert(string sObjName) {
// looking for pattern "alert_##" at end of obj description
string sObjectDesc = ObjectDescription(sObjName);
int iPatternOffset = StringFind(sObjectDesc,OBJDESC_PATTERN);
int iPatternLength = StringLen(OBJDESC_PATTERN);
// if pattern not found, no alert for this object
if (iPatternOffset == -1)
return(-1);
// return value after the pattern as number
//Print("Alert: ", StringSubstr(sObjectDesc,iPatternOffset+iPatternLength));
return(StrToInteger(StringSubstr(sObjectDesc,iPatternOffset+iPatternLength)));
}
void clearObjectAlert(string sObjName) {
// strip "alert_*" from end of obj description
string sObjDesc = ObjectDescription(sObjName);
int iSubStrOffset = StringFind(sObjDesc,"Alert_");
// if found at start of description, clear description
if (iSubStrOffset == 0) {
ObjectSetText(sObjName,"");
return;
}
// if found further on in desc, strip it from description
if (iSubStrOffset > 0) {
ObjectSetText(sObjName,StringSubstr(sObjDesc,0,iSubStrOffset));
return;
}
}
void createAlertLabel(string sObjName) {
// Set the text
string sObjText = "Alerter:";
if (EmailAlert) sObjText = sObjText + " Emails Alerts ON.";
else sObjText = sObjText + " Emails Alerts OFF.";
if (PopupAlert) sObjText = sObjText + " Popup Alerts ON.";
else sObjText = sObjText + " Popup Alerts OFF.";
// Create and position the label
ObjectCreate(sObjName,OBJ_LABEL,0,0,0);
ObjectSetText(sObjName,sObjText,8,"Arial",Yellow);
ObjectSet(sObjName,OBJPROP_XDISTANCE,5);
ObjectSet(sObjName,OBJPROP_YDISTANCE,5);
ObjectSet(sObjName,OBJPROP_CORNER,1);
}
*/
Can someone please help me understand why this EA is not working unless the "ask manual confirmation" button is ticked???
A trendline drawn on the chart and the "description" field is populated with the trade parameters in a string such as "Action=Buy Trigger=5 TP=60 SL=30 TS=45 BE=10 Lots=1.5"
When the trend line is breached by the trigger pips, a trade should be entered......
Please help it is driving me crazy!
#define EA_NAME "Trader"
#define MAGIC_NUMBER 456456
#define PARAM_ACTION "Action"
#define PARAM_TRIGGER "Trigger"
#define PARAM_TAKEPROFIT "TP"
#define PARAM_STOPLOSS "SL"
#define PARAM_TRAILSTOP "TS"
#define PARAM_BREAKEVEN "BE"
#define PARAM_LOTS "Lots"
#define GRID_ID 0
#define GRID_TYPE 1
#define GRID_TRIGGER 2
#define GRID_TP 3
#define GRID_SL 4
#define GRID_TS 5
#define GRID_BE 6
#define GRID_LOTS 7
extern int Slippage = 30;
extern bool EmailAlert = false;
extern bool PopupAlert = false;
int start() {
// grid to hold order info
string sGrid[0][8];
ArrayResize(sGrid,0);
// Inspect lines for pending orders
doManageLineActions(sGrid);
// Manage open positions
// This is done after lines are checked so if an order is submitted
// it will show up in the grid as an open position.
doManagePositions(sGrid);
doDrawGrid(sGrid);
return(0);
}
void doManageLineActions(string& sGrid[][]) {
// iterate all objects on chart
for (int i=ObjectsTotal()-1;i>=0;i--){
// get object name and price at current bar
// if no price then simply move on
string sName = ObjectName(i);
double dPrice = getLinePrice(sName);
if (dPrice < 0) continue;
// get the params for the object starting with Action.
// if action isn't defined then simply move on
string sAction = getParam(sName,PARAM_ACTION);
if (sAction != "Buy" && sAction != "Sell") continue;
int iTrigger = StrToInteger(getParam(sName,PARAM_TRIGGER));
int iTakeProfit = StrToInteger(getParam(sName,PARAM_TAKEPROFIT));
int iStopLoss = StrToInteger(getParam(sName,PARAM_STOPLOSS));
int iTrailStop = StrToInteger(getParam(sName,PARAM_TRAILSTOP));
int iBreakEven = StrToInteger(getParam(sName,PARAM_BREAKEVEN));
double dLots = StrToDouble(getParam(sName,PARAM_LOTS));
// determine trigger range
double dTriggerUpper = MathMax(dPrice,dPrice + iTrigger*Point);
double dTriggerLower = MathMin(dPrice,dPrice + iTrigger*Point);
// if within trigger range, submit an order
if (Bid <= dTriggerUpper && Bid >= dTriggerLower) {
int iTicket = 0;
if (sAction == "Buy") {
iTicket = OrderSend(Symbol(),OP_BUY,dLots,Ask,Slippage,Ask-iStopLoss*Point,Ask+iTakeProfit*Point,"Line:"+sName,MAGIC_NUMBER);
}
else if (sAction == "Sell") {
iTicket = OrderSend(Symbol(),OP_SELL,dLots,Bid,Slippage,Bid+iStopLoss*Point,Bid-iTakeProfit*Point,"Line:"+sName,MAGIC_NUMBER);
}
// add trailstop and breakeven to globalvars to allow tracking through EA restart
GlobalVariableSet(getGlobalVarName(iTicket,PARAM_BREAKEVEN),iBreakEven);
GlobalVariableSet(getGlobalVarName(iTicket,PARAM_TRAILSTOP),iTrailStop);
// remove order info from the line
ObjectSetText(sName,"");
}
else {
// add to orders array as a pending action
ArrayResize(sGrid,ArrayRange(sGrid,0)+1);
int index = ArrayRange(sGrid,0)-1;
//Print("Name: ",sName, " Action: ", sAction, " Index: ", index);
sGrid[index][GRID_ID] = sName;
sGrid[index][GRID_TYPE] = getParam(sName,PARAM_ACTION);
sGrid[index][GRID_TRIGGER] = getParam(sName,PARAM_TRIGGER);
sGrid[index][GRID_TP] = getParam(sName,PARAM_TAKEPROFIT);
sGrid[index][GRID_SL] = getParam(sName,PARAM_STOPLOSS);
sGrid[index][GRID_TS] = getParam(sName,PARAM_TRAILSTOP);
sGrid[index][GRID_BE] = getParam(sName,PARAM_BREAKEVEN);
sGrid[index][GRID_LOTS] = getParam(sName,PARAM_LOTS);
}
}
}
// Find open positions owned by this EA and manage them.
void doManagePositions(string& sGrid[][]) {
// iterate all orders
for (int i=OrdersTotal()-1;i>=0;i--) {
// select the order
OrderSelect(i,SELECT_BY_POS);
// if not for this symbol or EA, move on
if (OrderSymbol()!=Symbol()) continue;
if (OrderMagicNumber() != MAGIC_NUMBER) continue;
// see if there's a trailing stop or break even defined
// these would have been previously stored in global vars
int iTicket = OrderTicket();
int iBreakEven = GlobalVariableGet(getGlobalVarName(iTicket,PARAM_BREAKEVEN));
int iTrailStop = GlobalVariableGet(getGlobalVarName(iTicket,PARAM_TRAILSTOP));
Print("Trail:", iTrailStop);
// perform stoploss maintenance
double dNewStop = 0;
if (OrderType() == OP_BUY) {
// check breakeven
if (iBreakEven > 0 && OrderOpenPrice() + iBreakEven * Point <= Bid)
dNewStop = OrderOpenPrice();
// check trailstop
if (iTrailStop > 0)
dNewStop = MathMax(dNewStop,Bid-iTrailStop*Point);
// update stoploss if required
if (dNewStop != 0 && dNewStop > OrderStopLoss())
OrderModify(iTicket,OrderOpenPrice(),dNewStop,OrderTakeProfit(),OrderExpiration());
}
if (OrderType() == OP_SELL) {
// check breakeven
if (iBreakEven > 0 && OrderOpenPrice() - iBreakEven * Point >= Bid)
dNewStop = OrderOpenPrice();
// check trailstop
if (iTrailStop > 0)
if (dNewStop == 0) dNewStop = Bid-iTrailStop*Point;
else dNewStop = MathMax(dNewStop,Bid-iTrailStop*Point);
// update stoploss if required
if (dNewStop != 0 && dNewStop < OrderStopLoss())
OrderModify(iTicket,OrderOpenPrice(),dNewStop,OrderTakeProfit(),OrderExpiration());
}
// add to orders array as an open position
ArrayResize(sGrid,ArrayRange(sGrid,0)+1);
int index = ArrayRange(sGrid,0)-1;
sGrid[index][GRID_ID] = iTicket;
if (OrderType() == OP_BUY) sGrid[index][GRID_TYPE] = "Buy";
if (OrderType() == OP_SELL) sGrid[index][GRID_TYPE] = "Sell";
sGrid[index][GRID_TRIGGER] = "";
sGrid[index][GRID_TP] = DoubleToStr(OrderTakeProfit(),Digits);
sGrid[index][GRID_SL] = DoubleToStr(OrderStopLoss(),Digits);
sGrid[index][GRID_TS] = iTrailStop;
sGrid[index][GRID_BE] = iBreakEven;
sGrid[index][GRID_LOTS] = OrderLots();
}
}
void doDrawGrid(string& sGrid[][]) {
int col1 = 5;
int col2 = col1 + 200;
int col3 = col2 + 80;
int col4 = col3 + 80;
int col5 = col4 + 80;
int col6 = col5 + 80;
int col7 = col6 + 80;
int col8 = col7 + 80;
string sName;
for (int i=ObjectsTotal()-1;i>=0;i--) {
sName = ObjectName(i);
if (StringFind(sName,"Grid") ==0) ObjectDelete(sName);
}
// create column labels
sName = "Grid0_ID";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col1);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"ID");
}
sName = "Grid0_Type";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col2);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"Type");
}
sName = "Grid0_Trigger";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col3);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"Trigger");
}
sName = "Grid0_TakeProfit";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col4);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"TakeProfit");
}
sName = "Grid0_StopLoss";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col5);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"StopLoss");
}
sName = "Grid0_TrailStop";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col6);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"TrailStop");
}
sName = "Grid0_BreakEven";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col7);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"BreakEven");
}
sName = "Grid0_Lots";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col8);
ObjectSet(sName,OBJPROP_YDISTANCE,15);
ObjectSetText(sName,"Lots");
}
// iterate objects in grid array and draw them
for (i=0;i<ArrayRange(sGrid,0);i++) {
int yOffset = 15+(i+1)*15;
sName = "Grid"+(i+1)+"_ID";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col1);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_ID]);
sName = "Grid"+(i+1)+"_Type";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col2);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_TYPE]);
sName = "Grid"+(i+1)+"_Trigger";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col3);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_TRIGGER]);
sName = "Grid"+(i+1)+"_TakeProfit";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col4);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_TP]);
sName = "Grid"+(i+1)+"_StopLoss";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col5);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_SL]);
sName = "Grid"+(i+1)+"_TrailStop";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col6);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_TS]);
sName = "Grid"+(i+1)+"_BreakEven";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col7);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_BE]);
sName = "Grid"+(i+1)+"_Lots";
if (ObjectFind(sName) == -1) {
ObjectCreate(sName,OBJ_LABEL,0,0,0);
ObjectSet(sName,OBJPROP_XDISTANCE,col8);
ObjectSet(sName,OBJPROP_YDISTANCE,yOffset);
}
ObjectSetText(sName,sGrid[i][GRID_LOTS]);
}
}
string getGlobalVarName(int iTicket, string sParam) {
return(EA_NAME + "_" + Symbol() + "_" + iTicket + "_" + sParam);
}
string getParam(string sLine, string sParam) {
// get description and drop an extra space at front and back
string desc = " " + ObjectDescription(sLine) + " ";
// mod the param to match how it would be represented in the desc
sParam = " " + sParam + "=";
// find the index of the param
int index = StringFind(desc,sParam);
// strip the description out from in front of the value
string value = StringSubstr(desc,index + StringLen(sParam));
// strip the description out from behind the value
index = StringFind(value," ");
if (index != -1) value = StringSubstr(value,0,index);
return(value);
}
double getLinePrice(string sObjName) {
int iObjType = ObjectType(sObjName);
// if it's a horizontal line...
if (iObjType == OBJ_HLINE)
return(ObjectGet(sObjName,OBJPROP_PRICE1));
// if it's a trend line...
if (iObjType == OBJ_TREND)
return(ObjectGetValueByShift(sObjName,0));
// otherwise...
return(-1);
}
/*
// Iterate over objects on this chart
for (int i=ObjectsTotal()-1;i>=0;i--) {
string sObjName = ObjectName(i);
string sAction;
string sTrigger;
string sTakeProfit;
string sStopLoss;
string sTrailStop;
string sBreakEven;
string sLots;
// if it's not a horizontal line or trend line, continue
if (ObjectType(sObjName) != OBJ_HLINE && ObjectType(sObjName) != OBJ_TREND)
continue;
// Is no action on this object, continue
sAction = getParam(sObjName,"Action");
if (sAction != "Buy" && sAction != "Sell")
continue;
// Is the line within alert proximity? If not, continue iteration
iProximity = MathAbs(Bid - dObjPrice) / Point;
if (iProximity > iObjAlert) continue;
// Fire the alert and clear alert from description
sMessage = "Alert triggered: " + Symbol() + "@" + DoubleToStr(Bid,Digits) +"."
+ " " + iProximity + " pips from " + sObjName + ".";
if (PopupAlert) Alert(sMessage);
if (EmailAlert) SendMail("Alert on " + Symbol(), sMessage);
// Clear the alert from the object description
clearObjectAlert(sObjName);
}
int getObjectAlert(string sObjName) {
// looking for pattern "alert_##" at end of obj description
string sObjectDesc = ObjectDescription(sObjName);
int iPatternOffset = StringFind(sObjectDesc,OBJDESC_PATTERN);
int iPatternLength = StringLen(OBJDESC_PATTERN);
// if pattern not found, no alert for this object
if (iPatternOffset == -1)
return(-1);
// return value after the pattern as number
//Print("Alert: ", StringSubstr(sObjectDesc,iPatternOffset+iPatternLength));
return(StrToInteger(StringSubstr(sObjectDesc,iPatternOffset+iPatternLength)));
}
void clearObjectAlert(string sObjName) {
// strip "alert_*" from end of obj description
string sObjDesc = ObjectDescription(sObjName);
int iSubStrOffset = StringFind(sObjDesc,"Alert_");
// if found at start of description, clear description
if (iSubStrOffset == 0) {
ObjectSetText(sObjName,"");
return;
}
// if found further on in desc, strip it from description
if (iSubStrOffset > 0) {
ObjectSetText(sObjName,StringSubstr(sObjDesc,0,iSubStrOffset));
return;
}
}
void createAlertLabel(string sObjName) {
// Set the text
string sObjText = "Alerter:";
if (EmailAlert) sObjText = sObjText + " Emails Alerts ON.";
else sObjText = sObjText + " Emails Alerts OFF.";
if (PopupAlert) sObjText = sObjText + " Popup Alerts ON.";
else sObjText = sObjText + " Popup Alerts OFF.";
// Create and position the label
ObjectCreate(sObjName,OBJ_LABEL,0,0,0);
ObjectSetText(sObjName,sObjText,8,"Arial",Yellow);
ObjectSet(sObjName,OBJPROP_XDISTANCE,5);
ObjectSet(sObjName,OBJPROP_YDISTANCE,5);
ObjectSet(sObjName,OBJPROP_CORNER,1);
}
*/