BE
Trendline Trading
20 Jun 2016, 12:27
Hi There,
I would love the ability to have cTrader execute an order when price either touches or closes over a trend line...
I have a MT4 EA that I am currently using for this exact function and was wondering if this is something that you would possibly build into the software...
Code for EA below
//+------------------------------------------------------------------+
//| TrendLine_EA.mq4 |
//| oromek |
//| oromeks@gmail.com |
//+------------------------------------------------------------------+
#property copyright "oromek"
#property link "oromeks@gmail.com"
#include <stderror.mqh>
#include <stdlib.mqh>
extern string BuyTouchDescription="tbuy-";
extern string SellTouchDescription="tsell-";
extern string BuyCrossDescription="cbuy-";
extern string SellCrossDescription="csell-";
extern string CloseTouchDescription="tclose-";
extern string CloseCrossDescription="cclose-";
extern string PermanentCloseTouchDescription="ptclose-";
extern string PermanentCloseCrossDescription="pcclose-";
extern string TicketCloseTouchDescription="tic-tclose-";
extern string TicketCloseCrossDescription="tic-cclose-";
extern string AddTouchBuyDescription="atbuy-";
extern string AddCrossBuyDescription="acbuy-";
extern string AddTouchSellDescription="atsell-";
extern string AddCrossSellDescription="acsell-";
extern string RO="----Rectangle Options----";
extern double RectangleTradeSize=1;
extern bool RectangleHedgeOn=true;
extern string TradeMode_Help="0-time delay,1-candle close,2-immediate entry";
extern int TradeMode=0;
//extern bool TradeAfterCandleClose=false;
//extern bool ImmediateEntryOn=false;
extern int ImmediateEntryBuffer=10;
extern int TimeWaitSeconds=60;
extern int NumberOfHedgeTrades=2;
extern string PermanentBuyDescription="pbuy-";
extern string PermanentSellDescription="psell-";
extern string PermanentBuySellDescription="pbs-";
extern bool UsePipsProfit=false;
extern int PipsProfit=0;
extern bool ProfitOnFirstTrade=false;
extern int TakeProfit=0;
extern int StopLoss=0;
extern string BO="----BreakEven Options----";
extern bool BreakEvenOn=false;
extern bool BEAfterFirstCandleOn=false;
extern int MoveSLToBEAfter=10;
extern double MaxSlippage=3;
extern string TS="----Text Settings----";
extern bool DisplayStatistics=true;
extern int X = 63;
extern int Y = 40;
extern int Corner = 1;
extern color TextColor=White;
int currBars, currBars1, currBars2, currBars3, currBars4, mp, ticket, err;
double price_0, selectedLot;
double lValue_1, lValue_2, closeLot;
string name;
int sellNameLength, buyNameLength, closeNameLength, permanentSellNameLength, permanentBuyNameLength, permanentBuySellNameLength;
int pcloseNameLength, addTouchBuyNameLength, addCrossBuyNameLength, addTouchSellNameLength, addCrossSellNameLength;
int ticCloseNameLength, ticTouchNameLength;
int trades, bstrades, candleCount, btrades, strades;
bool bTopLong, bTopShort, bBottomShort, bBottomLong;
bool buyTopTradeOpen, sellTopTradeOpen, sellBottomTradeOpen, buyBottomTradeOpen;
bool buyTopOpenAllow, sellTopOpenAllow, sellBottomOpenAllow, buyBottomOpenAllow;
datetime timeTopLong, timeTopShort, timeBottomShort, timeBottomLong;
bool pbuyBuySellLineAllow, psellBuySellLineAllow;//, pSellLineAllow;
bool buyBuySellLineTradeOpen, sellBuySellLineTradeOpen;
bool bBuySellLineLong, bBuySellLineShort;
bool pbuyBuySellMode, psellBuySellMode;
datetime timeBuySellLineLong, timeBuySellLineShort;
bool Buy_Condition_2, Sell_Condition_2, BESet;
double pBuySellLevel, pBuySellLevel_1, pBuySellLevel_2, ptradeSize;
string pbsLineName, pbsLineDesc;
double pbuyLineLevel, pbuyLineLevel_1, pbuyLineLevel_2, pbuyTradeLineSize;
string pbLineName, pbLineDesc;
bool buyBuyLineTradeOpen, bBuyLineLong, bBuyLineShort;
double pBuyLevel, pBuyLevel_1, pBuyLevel_2;
datetime timeBuyLineLong, timeBuyLineShort;
double psellLineLevel, psellLineLevel_1, psellLineLevel_2, psellTradeLineSize;
string psLineName, psLineDesc;
double pSellLevel, pSellLevel_1, pSellLevel_2;
bool sellSellLineTradeOpen, bSellLineLong, bSellLineShort;
datetime timeSellLineShort, timeSellLineLong;
bool buysellLineAllow, buyLineAllow, sellLineAllow;
datetime EAStartTime;
int OrdersHistoryTotal_0;
double totalProfit, totalLoss;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
if (Digits==5 || Digits==3) mp=10;
else mp=1;
pbuyBuySellLineAllow=true;
psellBuySellLineAllow=true;
if (UsePipsProfit) EAStartTime=TimeCurrent();
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
ObjectDelete("pSL_txt");
ObjectDelete("nSL_txt");
ObjectDelete("totp_lab");
ObjectDelete("totl_lab");
ObjectDelete("BEd_lab");
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
bool closeAll=false;
//open and close orders after cross
if (currBars!=Bars)
{
double BuyCrossPrice, SellCrossPrice;
for(int i=0;i<ObjectsTotal();i++)
{
name=ObjectName(i);
sellNameLength=StringLen(SellCrossDescription);
buyNameLength=StringLen(BuyCrossDescription);
closeNameLength=StringLen(CloseCrossDescription);
pcloseNameLength=StringLen(PermanentCloseCrossDescription);
ticCloseNameLength = StringLen(TicketCloseCrossDescription);
addCrossBuyNameLength=StringLen(AddCrossBuyDescription);
addCrossSellNameLength=StringLen(AddCrossSellDescription);
bool ticket_closing = false;
int tic;
//closing trades
if ((ObjectType(name)==OBJ_HLINE || ObjectType(name)==OBJ_TREND) && tradeCount()>0 &&
(StringSubstr(ObjectDescription(name),0,closeNameLength)==CloseCrossDescription || StringSubstr(ObjectDescription(name),0,pcloseNameLength)==PermanentCloseCrossDescription ||
StringSubstr(ObjectDescription(name),0,ticCloseNameLength)==TicketCloseCrossDescription))
{
if (StringSubstr(ObjectDescription(name),0,ticCloseNameLength)==TicketCloseCrossDescription){
ticket_closing = true;
tic = StrToInteger(StringSubstr(ObjectDescription(name),ticCloseNameLength,0));
}
double closeCrossPrice;
if (ObjectType(name)==OBJ_HLINE) closeCrossPrice=ObjectGet(name,OBJPROP_PRICE1);
if (ObjectType(name)==OBJ_TREND) closeCrossPrice=ObjectGetValueByShift(name,1);
double closeSize=StrToDouble(StringSubstr(ObjectDescription(name),closeNameLength,0));
if (closeSize==0) closeAll=true;
else closeAll=false;
if ((Close[2]<closeCrossPrice && Close[1]>=closeCrossPrice) || (Close[2]>closeCrossPrice && Close[1]<=closeCrossPrice))
{
if (!ticket_closing)
{
if (!closeAll)
{
while(closeSize>0)
{
for(int cnt=0;cnt<=OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
ticket=OrderTicket();
selectedLot=OrderLots();
if (OrderSymbol()==Symbol())
{
if (closeSize<=selectedLot) {closeLot=closeSize; closeSize=0;}
if (closeSize>selectedLot && tradeCount()==1) {closeLot=selectedLot; closeSize=0;}
if (closeSize>selectedLot && tradeCount()>1) {closeLot=selectedLot; closeSize=closeSize-selectedLot;}
if (OrderType()==OP_BUY) closePartTrade(OP_BUY,closeLot); //OrderClose(ticket,closeLot,Bid,MaxSlippage*Point*mp,Red);
if (OrderType()==OP_SELL) closePartTrade(OP_SELL,closeLot); //OrderClose(ticket,closeLot,Ask,MaxSlippage*Point*mp,Green);
//Print("Closed ",closeLot," lot after crossing line, err:",GetLastError());
break;
}
}
}
}
else
{
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol() && OrderType()==OP_BUY)
{
//RefreshRates();
closeTrade(OP_BUY); //OrderClose(OrderTicket(),OrderLots(),Bid,MaxSlippage*Point*mp,Red);
//Print(cnt,":BUY order closed, err:",GetLastError());
}
if (OrderSymbol()==Symbol() && OrderType()==OP_SELL)
{
closeTrade(OP_SELL);
/*RefreshRates();
OrderClose(OrderTicket(),OrderLots(),Ask,MaxSlippage*Point*mp,Green);
Print(cnt,":SELL order closed, err:",GetLastError());*/
}
}
}
}
else
{
for(cnt=0;cnt<=OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
ticket=OrderTicket();
if (OrderSymbol() == Symbol() && ticket_closing && OrderTicket() == tic && OrderType() == OP_BUY) closeTradebyTicket(OP_BUY, tic);
if (OrderSymbol() == Symbol() && ticket_closing && OrderTicket() == tic && OrderType() == OP_SELL) closeTradebyTicket(OP_SELL, tic);
}
}
if (StringSubstr(ObjectDescription(name),0,closeNameLength)==CloseCrossDescription) ObjectSetText(name,"executed-"+ObjectDescription(name));
}
buysellLineAllow=false;
buyLineAllow=false;
sellLineAllow=false;
}
//opening trades
if (ObjectType(name)==OBJ_HLINE && StringSubstr(ObjectDescription(name),0,sellNameLength)==SellCrossDescription)
{
SellCrossPrice=ObjectGet(name,OBJPROP_PRICE1);
double sellSize=StrToDouble(StringSubstr(ObjectDescription(name),sellNameLength,0));
if ((Close[2]<SellCrossPrice && Close[1]>=SellCrossPrice) || (Close[2]>SellCrossPrice && Close[1]<=SellCrossPrice))
{
//OrderSend(Symbol(), OP_SELL, sellSize, Bid, MaxSlippage*Point*mp, 0, 0, "", 10001, 0, Red);
OpenTrade(Symbol(),OP_SELL,sellSize);
Print("Sell order open after horizontal line cross");
ObjectSetText(name,"executed-"+ObjectDescription(name));
}
}
if (ObjectType(name)==OBJ_TREND && StringSubstr(ObjectDescription(name),0,sellNameLength)==SellCrossDescription)
{
sellSize=StrToDouble(StringSubstr(ObjectDescription(name),sellNameLength,0));
lValue_1=ObjectGetValueByShift(name,1);
lValue_2=ObjectGetValueByShift(name,2);
if ((Close[2]>lValue_2 && Close[1]<=lValue_1) || (Close[2]<lValue_2 && Close[1]>=lValue_1))
{
//OrderSend(Symbol(), OP_SELL, sellSize, Bid, MaxSlippage*Point*mp, 0, 0, "", 10001, 0, Red);
OpenTrade(Symbol(),OP_SELL,sellSize);
Print("Sell order open after trendline line cross");
ObjectSetText(name,"executed-"+ObjectDescription(name));
}
}
if (ObjectType(name)==OBJ_HLINE && StringSubstr(ObjectDescription(name),0,buyNameLength)==BuyCrossDescription)
{
BuyCrossPrice=ObjectGet(name,OBJPROP_PRICE1);
double buySize=StrToDouble(StringSubstr(ObjectDescription(name),buyNameLength,0));
if ((Close[2]>BuyCrossPrice && Close[1]<=BuyCrossPrice) || (Close[2]<BuyCrossPrice && Close[1]>=BuyCrossPrice))
{
//OrderSend(Symbol(), OP_BUY, buySize, Ask, MaxSlippage*Point*mp, 0, 0, "", 10001, 0, Green);
OpenTrade(Symbol(),OP_BUY,buySize);
Print("Buy order open after horizontal line cross");
ObjectSetText(name,"executed-"+ObjectDescription(name));
}
}
if (ObjectType(name)==OBJ_TREND && StringSubstr(ObjectDescription(name),0,buyNameLength)==BuyCrossDescription)
{
buySize=StrToDouble(StringSubstr(ObjectDescription(name),buyNameLength,0));
lValue_1=ObjectGetValueByShift(name,1);
lValue_2=ObjectGetValueByShift(name,2);
if ((Close[2]>lValue_2 && Close[1]<=lValue_1) || (Close[2]<lValue_2 && Close[1]>=lValue_1))
{
//OrderSend(Symbol(), OP_BUY, buySize, Ask, MaxSlippage*Point*mp, 0, 0, "", 10001, 0, Green);
OpenTrade(Symbol(),OP_BUY,buySize);
Print("Buy order open after trendline line cross");
ObjectSetText(name,"executed-"+ObjectDescription(name));
}
}
//add buy trades
if ((ObjectType(name)==OBJ_HLINE || ObjectType(name)==OBJ_TREND) && StringSubstr(ObjectDescription(name),0,addCrossBuyNameLength)==AddCrossBuyDescription && tradeExist(OP_BUY))
{
buySize=StrToDouble(StringSubstr(ObjectDescription(name),addCrossBuyNameLength,0));
if (ObjectType(name)==OBJ_HLINE) {lValue_1=ObjectGet(name,OBJPROP_PRICE1); lValue_2=lValue_1;}
if (ObjectType(name)==OBJ_TREND) {lValue_1=ObjectGetValueByShift(name,1); lValue_2=ObjectGetValueByShift(name,2);}
if ((Close[2]>lValue_2 && Close[1]<=lValue_1) || (Close[2]<lValue_2 && Close[1]>=lValue_1))
{
//OrderSend(Symbol(), OP_BUY, buySize, Ask, MaxSlippage*Point*mp, 0, 0, "", 10001, 0, Green);
OpenTrade(Symbol(),OP_BUY,buySize);
Print("Buy order added after trendline line cross");
ObjectSetText(name,"executed-"+ObjectDescription(name));
}
}
//add sell trades
if ((ObjectType(name)==OBJ_HLINE || ObjectType(name)==OBJ_TREND) && StringSubstr(ObjectDescription(name),0,addCrossSellNameLength)==AddCrossSellDescription && tradeExist(OP_SELL))
{
sellSize=StrToDouble(StringSubstr(ObjectDescription(name),addCrossSellNameLength,0));
if (ObjectType(name)==OBJ_HLINE) {lValue_1=ObjectGet(name,OBJPROP_PRICE1); lValue_2=lValue_1;}
if (ObjectType(name)==OBJ_TREND) {lValue_1=ObjectGetValueByShift(name,1); lValue_2=ObjectGetValueByShift(name,2);}
if ((Close[2]>lValue_2 && Close[1]<=lValue_1) || (Close[2]<lValue_2 && Close[1]>=lValue_1))
{
//OrderSend(Symbol(), OP_SELL, sellSize, Bid, MaxSlippage*Point*mp, 0, 0, "", 10001, 0, Red);
OpenTrade(Symbol(),OP_SELL,sellSize);
Print("Sell order added after trendline line cross");
ObjectSetText(name,"executed-"+ObjectDescription(name));
}
}
}
currBars=Bars;
}
bool pbuysellLinesExist, pbuyLinesExist, psellLinesExist;
pBuySellLevel=0;
pBuySellLevel_1=0;
pBuySellLevel_2=0;
pbuyLineLevel=0;
pbuyLineLevel_1=0;
pbuyLineLevel_2=0;
psellLineLevel=0;
psellLineLevel_1=0;
psellLineLevel_2=0;
pBuySellLevel=0;
//open and close touch orders
double BuyTouchPrice, SellTouchPrice;
bool recFound;
for(i=0;i<ObjectsTotal();i++)
{
name=ObjectName(i);
sellNameLength=StringLen(SellTouchDescription);
buyNameLength=StringLen(BuyTouchDescription);
closeNameLength=StringLen(CloseTouchDescription);
pcloseNameLength=StringLen(PermanentCloseTouchDescription);
ticTouchNameLength=StringLen(TicketCloseTouchDescription);
permanentBuyNameLength=StringLen(PermanentBuyDescription);
permanentSellNameLength=StringLen(PermanentSellDescription);
permanentBuySellNameLength=StringLen(PermanentBuySellDescription);
addTouchBuyNameLength=StringLen(AddTouchBuyDescription);
addTouchSellNameLength=StringLen(AddTouchSellDescription);
//permanent buy-sell line
if ((ObjectType(name)==OBJ_HLINE || ObjectType(name)==OBJ_TREND) && StringSubstr(ObjectDescription(name),0,permanentBuySellNameLength)==PermanentBuySellDescription)
{
double lineValue, lineValue_1, lineValue_2, priceDistance;
if (ObjectType(name)==OBJ_HLINE) {lineValue=ObjectGet(name,OBJPROP_PRICE1); lineValue_1=lineValue; lineValue_2=lineValue;}
if (ObjectType(name)==OBJ_TREND) {lineValue=ObjectGetValueByShift(name,0); lineValue_1=ObjectGetValueByShift(name,1); lineValue_2=ObjectGetValueByShift(name,2);}
priceDistance=MathAbs(Close[0]-lineValue);
if (pBuySellLevel==0 || MathAbs(Close[0]-pBuySellLevel)>priceDistance)
{
pBuySellLevel=lineValue;
pBuySellLevel_1=lineValue_1;
pBuySellLevel_2=lineValue_2;
pbsLineName=name;
pbsLineDesc=ObjectDescription(name);
ptradeSize=StrToDouble(StringSubstr(ObjectDescription(name),permanentBuySellNameLength,0));
}
pbuysellLinesExist=true;
}
//permanent buy line
if ((ObjectType(name)==OBJ_HLINE || ObjectType(name)==OBJ_TREND) && StringSubstr(ObjectDescription(name),0,permanentBuyNameLength)==PermanentBuyDescription)
{
double buyLineValue, buyLineValue_1, buyLineValue_2, buyPriceDistance;
if (ObjectType(name)==OBJ_HLINE) {buyLineValue=ObjectGet(name,OBJPROP_PRICE1); buyLineValue_1=buyLineValue; buyLineValue_2=buyLineValue;}
if (ObjectType(name)==OBJ_TREND) {buyLineValue=ObjectGetValueByShift(name,0); buyLineValue_1=ObjectGetValueByShift(name,1); buyLineValue_2=ObjectGetValueByShift(name,2);}
buyPriceDistance=MathAbs(Close[0]-buyLineValue);
if (pbuyLineLevel==0 || MathAbs(Close[0]-pBuyLevel)>buyPriceDistance)
{
pbuyLineLevel=buyLineValue;
pbuyLineLevel_1=buyLineValue_1;
pbuyLineLevel_2=buyLineValue_2;
pbLineName=name;
pbLineDesc=ObjectDescription(name);
pbuyTradeLineSize=StrToDouble(StringSubstr(ObjectDescription(name),permanentBuyNameLength,0));
}
pbuyLinesExist=true;
}
//permanent sell line
if ((ObjectType(name)==OBJ_HLINE || ObjectType(name)==OBJ_TREND) && StringSubstr(ObjectDescription(name),0,permanentSellNameLength)==PermanentSellDescription)
{
double sellLineValue, sellLineValue_1, sellLineValue_2, sellPriceDistance;
if (ObjectType(name)==OBJ_HLINE) {sellLineValue=ObjectGet(name,OBJPROP_PRICE1); sellLineValue_1=sellLineValue; sellLineValue_2=sellLineValue;}
if (ObjectType(name)==OBJ_TREND) {sellLineValue=ObjectGetValueByShift(name,0); sellLineValue_1=ObjectGetValueByShift(name,1); sellLineValue_2=ObjectGetValueByShift(name,2);}
sellPriceDistance=MathAbs(Close[0]-sellLineValue);
if (psellLineLevel==0 || MathAbs(Close[0]-pSellLevel)>sellPriceDistance)
{
psellLineLevel=sellLineValue;
psellLineLevel_1=sellLineValue_1;
psellLineLevel_2=sellLineValue_2;
psLineName=name;
psLineDesc=ObjectDescription(name);
psellTradeLineSize=StrToDouble(StringSubstr(ObjectDescription(name),permanentSellNameLength,0));
}
psellLinesExist=true;
}
//closing trades
if ((ObjectType(name)==OBJ_HLINE || ObjectType(name)==OBJ_TREND) && tradeCount()>0 &&
(StringSubstr(ObjectDescription(name),0,closeNameLength)==CloseTouchDescription || StringSubstr(ObjectDescription(name),0,pcloseNameLength)==PermanentCloseTouchDescription ||
StringSubstr(ObjectDescription(name),0,ticTouchNameLength)==TicketCloseTouchDescription))
{
if (StringSubstr(ObjectDescription(name),0,ticTouchNameLength)==TicketCloseTouchDescription){
ticket_closing = true;
tic = StrToInteger(StringSubstr(ObjectDescription(name),ticTouchNameLength,0));
}
double closeTouchPrice;
if (ObjectType(name)==OBJ_HLINE) closeTouchPrice=ObjectGet(name,OBJPROP_PRICE1);
if (ObjectType(name)==OBJ_TREND) closeTouchPrice=ObjectGetValueByShift(name,0);
closeSize=StrToDouble(StringSubstr(ObjectDescription(name),closeNameLength,0));
if (closeSize==0) closeAll=true;
else closeAll=false;
if ((price_0<closeTouchPrice && Close[0]>=closeTouchPrice) || (price_0>closeTouchPrice && Close[0]<=closeTouchPrice) && price_0!=0)
{
if (!ticket_closing)
{
if (!closeAll)
{
while(closeSize>0)
{
for(cnt=0;cnt<=OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
ticket=OrderTicket();
selectedLot=OrderLots();
if (OrderSymbol()==Symbol())
{
if (closeSize<=selectedLot) {closeLot=closeSize; closeSize=0;}
if (closeSize>selectedLot && tradeCount()==1) {closeLot=selectedLot; closeSize=0;}
if (closeSize>selectedLot && tradeCount()>1) {closeLot=selectedLot; closeSize=closeSize-selectedLot;}
if (OrderType()==OP_BUY) closePartTrade(OP_BUY,closeLot); //OrderClose(ticket,closeLot,Bid,MaxSlippage*Point*mp,Red);
if (OrderType()==OP_SELL) closePartTrade(OP_SELL,closeLot); //OrderClose(ticket,closeLot,Ask,MaxSlippage*Point*mp,Green);
break;
}
}
}
}
else
{
for(cnt=OrdersTotal();cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol() && OrderType()==OP_BUY)
{
closeTrade(OP_BUY);
/*RefreshRates();
OrderClose(OrderTicket(),OrderLots(),Bid,MaxSlippage*Point*mp,Red);
Print(cnt,":BUY order closed, err:",GetLastError());*/
}
if (OrderSymbol()==Symbol() && OrderType()==OP_SELL)
{
closeTrade(OP_SELL);
/*RefreshRates();
OrderClose(OrderTicket(),OrderLots(),Ask,MaxSlippage*Point*mp,Green);
Print(cnt,":SELL order closed, err:",GetLastError());*/
}
}
}
}
else
{
for(cnt=0;cnt<=OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
ticket=OrderTicket();
if (OrderSymbol() == Symbol() && ticket_closing && OrderTicket() == tic && OrderType() == OP_BUY) closeTradebyTicket(OP_BUY, tic);
if (OrderSymbol() == Symbol() && ticket_closing && OrderTicket() == tic && OrderType() == OP_SELL) closeTradebyTicket(OP_SELL, tic);
}
}
if (StringSubstr(ObjectDescription(name),0,closeNameLength)==CloseTouchDescription) ObjectSetText(name,"executed-"+ObjectDescription(name));
buysellLineAllow=false;
buyLineAllow=false;
sellLineAllow=false;
}
}
if (ObjectType(name)==OBJ_HLINE && StringSubstr(ObjectDescription(name),0,sellNameLength)==SellTouchDescription)
{
SellTouchPrice=ObjectGet(name,OBJPROP_PRICE1);
sellSize=StrToDouble(StringSubstr(ObjectDescription(name),sellNameLength,0));
if ((price_0<SellTouchPrice && Close[0]>=SellTouchPrice) || (price_0>SellTouchPrice && Close[0]<=SellTouchPrice) && price_0!=0)
{
//OrderSend(Symbol(), OP_SELL, sellSize, Bid, MaxSlippage*Point*mp, 0, 0, "", 10001, 0, Red);
OpenTrade(Symbol(),OP_SELL,sellSize);
Print("Sell order open after horizontal line touch");
ObjectSetText(name,"executed-"+ObjectDescription(name));
}
}
if (ObjectType(name)==OBJ_TREND && StringSubstr(ObjectDescription(name),0,sellNameLength)==SellTouchDescription)
{
sellSize=StrToDouble(StringSubstr(ObjectDescription(name),sellNameLength,0));
lValue_1=ObjectGetValueByShift(name,1);
if ((price_0<lValue_1 && Close[0]>=lValue_1) || (price_0>lValue_1 && Close[0]<=lValue_1) && price_0!=0)
{
//OrderSend(Symbol(), OP_SELL, sellSize, Bid, MaxSlippage*Point*mp, 0, 0, "", 10001, 0, Red);
OpenTrade(Symbol(),OP_SELL,sellSize);
Print("Sell order open after trendline line touch");
ObjectSetText(name,"executed-"+ObjectDescription(name));
}
}
if (ObjectType(name)==OBJ_HLINE && StringSubstr(ObjectDescription(name),0,buyNameLength)==BuyTouchDescription)
{
BuyTouchPrice=ObjectGet(name,OBJPROP_PRICE1);
buySize=StrToDouble(StringSubstr(ObjectDescription(name),buyNameLength,0));
if ((price_0>BuyTouchPrice && Close[0]<=BuyTouchPrice) || (price_0<BuyTouchPrice && Close[0]>=BuyTouchPrice) && price_0!=0)
{
//OrderSend(Symbol(), OP_BUY, buySize, Ask, MaxSlippage*Point*mp, 0, 0, "", 10001, 0, Green);
OpenTrade(Symbol(),OP_BUY,buySize);
Print("Buy order open after horizontal line touch");
ObjectSetText(name,"executed-"+ObjectDescription(name));
}
}
if (ObjectType(name)==OBJ_TREND && StringSubstr(ObjectDescription(name),0,buyNameLength)==BuyTouchDescription)
{
buySize=StrToDouble(StringSubstr(ObjectDescription(name),buyNameLength,0));
lValue_1=ObjectGetValueByShift(name,1);
if ((price_0>lValue_1 && Close[0]<=lValue_1) || (price_0<lValue_1 && Close[0]>=lValue_1) && price_0!=0)
{
//OrderSend(Symbol(), OP_BUY, buySize, Ask, MaxSlippage*Point*mp, 0, 0, "", 10001, 0, Green);
OpenTrade(Symbol(),OP_BUY,buySize);
Print("Buy order open after trendline line touch");
ObjectSetText(name,"executed-"+ObjectDescription(name));
}
}
//add buy trades
if ((ObjectType(name)==OBJ_HLINE || ObjectType(name)==OBJ_TREND) && StringSubstr(ObjectDescription(name),0,addTouchBuyNameLength)==AddTouchBuyDescription && tradeExist(OP_BUY))
{
buySize=StrToDouble(StringSubstr(ObjectDescription(name),addTouchBuyNameLength,0));
if (ObjectType(name)==OBJ_HLINE) {lValue_1=ObjectGet(name,OBJPROP_PRICE1);}
if (ObjectType(name)==OBJ_TREND) {lValue_1=ObjectGetValueByShift(name,0);}
if ((price_0>lValue_1 && Close[0]<=lValue_1) || (price_0<lValue_1 && Close[0]>=lValue_1) && price_0!=0)
{
//OrderSend(Symbol(), OP_BUY, buySize, Ask, MaxSlippage*Point*mp, 0, 0, "", 10001, 0, Green);
OpenTrade(Symbol(),OP_BUY,buySize);
Print("Buy order added after trendline line touch");
ObjectSetText(name,"executed-"+ObjectDescription(name));
}
}
//add sell trades
if ((ObjectType(name)==OBJ_HLINE || ObjectType(name)==OBJ_TREND) && StringSubstr(ObjectDescription(name),0,addTouchSellNameLength)==AddTouchSellDescription && tradeExist(OP_SELL))
{
sellSize=StrToDouble(StringSubstr(ObjectDescription(name),addTouchSellNameLength,0));
if (ObjectType(name)==OBJ_HLINE) {lValue_1=ObjectGet(name,OBJPROP_PRICE1);}
if (ObjectType(name)==OBJ_TREND) {lValue_1=ObjectGetValueByShift(name,0);}
if ((price_0>lValue_1 && Close[0]<=lValue_1) || (price_0<lValue_1 && Close[0]>=lValue_1) && price_0!=0)
{
//OrderSend(Symbol(), OP_SELL, sellSize, Bid, MaxSlippage*Point*mp, 0, 0, "", 10001, 0, Red);
OpenTrade(Symbol(),OP_SELL,sellSize);
Print("Sell order added after trendline line touch");
ObjectSetText(name,"executed-"+ObjectDescription(name));
}
}
//-----------------------------------------------------------------------------------------------------------------------------
if (ObjectType(name)==OBJ_RECTANGLE && ObjectDescription(name)!="executed")
{
double p1=ObjectGet(name,OBJPROP_PRICE1);
double p2=ObjectGet(name,OBJPROP_PRICE2);
datetime d2=ObjectGet(name,OBJPROP_TIME2);
color recColor=ObjectGet(name,OBJPROP_COLOR);
double recHigh, recLow;
if (p1>p2) {recHigh=p1; recLow=p2;}
else {recHigh=p2; recLow=p1;}
if (ObjectDescription(name)=="1" || ObjectDescription(name)=="3") recHigh=0;
if (ObjectDescription(name)=="2" || ObjectDescription(name)=="3") recLow=0;
//Comment("recHigh:",recHigh,",recLow:",recLow,",buyTopOpenAllow:",buyTopOpenAllow,",sellTopOpenAllow:",sellTopOpenAllow,",sellBottomOpenAllow:",sellBottomOpenAllow,
// ",buyBottomOpenAllow:",buyBottomOpenAllow);
Comment("");
bool Buy_Condition_1, Sell_Condition_1;
if (TradeMode==2 && currBars1!=Bars)
{
//open buy trade on the top of the rectangle
if (buyTopOpenAllow && Close[2]<=recHigh && Close[1]>recHigh && trades<NumberOfHedgeTrades && recHigh>0)
{
Buy_Condition_1=true;
buyTopOpenAllow=false;
buyTopTradeOpen=true;
trades++;
if (sellTopTradeOpen)
{
closeTrade(OP_SELL);
sellTopOpenAllow=true;
sellTopTradeOpen=false;
}
}
//open sell trade on the top of the rectangle
if (sellTopOpenAllow && Close[2]>=recHigh && Close[1]<recHigh && recHigh>0)
{
if (trades<NumberOfHedgeTrades && RectangleHedgeOn) Sell_Condition_1=true;
sellTopOpenAllow=false;
sellTopTradeOpen=true;
if (buyTopTradeOpen)
{
closeTrade(OP_BUY);
buyTopOpenAllow=true;
buyTopTradeOpen=false;
}
if (trades==NumberOfHedgeTrades)
{
if (ObjectDescription(name)=="") ObjectSetText(name,"1");
if (ObjectDescription(name)=="2") ObjectSetText(name,"3");
sellTopTradeOpen=false;
trades=0;
}
}
//switch to bottom of the rectangle
if (sellTopTradeOpen && Close[2]>=recLow && Close[1]<=recLow && RectangleHedgeOn)
{
sellTopTradeOpen=false;
ObjectSetText(name,"");
sellBottomOpenAllow=false;
buyBottomOpenAllow=true;
sellBottomTradeOpen=true;
buyTopOpenAllow=false;
sellTopOpenAllow=false;
trades=0;
}
//rectangle bottom
//open sell trade on bottom of the rectangle and close buy trade if it is open
if (sellBottomOpenAllow && Close[2]>=recLow && Close[1]<recLow && recLow>=0)
{
Sell_Condition_1=true;
sellBottomOpenAllow=false;
sellBottomTradeOpen=true;
trades++;
if (buyBottomTradeOpen)
{
closeTrade(OP_BUY);
buyBottomOpenAllow=true;
buyBottomTradeOpen=false;
}
}
//open buy trade on bottom of the rectangle and close sell if it is open
if (buyBottomOpenAllow && Close[2]<=recLow && Close[1]>recLow && recLow>=0)
{
if (trades<NumberOfHedgeTrades && RectangleHedgeOn) Buy_Condition_1=true;
buyBottomOpenAllow=false;
buyBottomTradeOpen=true;
if (sellBottomTradeOpen)
{
closeTrade(OP_SELL);
sellBottomOpenAllow=true;
sellBottomTradeOpen=false;
}
if (trades==NumberOfHedgeTrades)
{
if (ObjectDescription(name)=="") ObjectSetText(name,"2");
if (ObjectDescription(name)=="1") ObjectSetText(name,"3");
buyBottomTradeOpen=false;
trades=0;
}
}
//switch to the top of the rectangle
if (buyBottomTradeOpen && Close[2]<=recHigh && Close[1]>recHigh && RectangleHedgeOn)
{
buyBottomTradeOpen=false;
ObjectSetText(name,"");
buyTopOpenAllow=false;
sellTopOpenAllow=true;
buyTopTradeOpen=true;
trades=0;
sellBottomOpenAllow=false;
buyBottomOpenAllow=false;
}
currBars1=Bars;
}
if (TradeMode==0)
{
//rectangle top
//activate time counting before open buy trade on top of the rectangle
if (buyTopOpenAllow && !bTopLong && price_0<=recHigh && Close[0]>=recHigh && trades<NumberOfHedgeTrades && recHigh>0)
{
timeTopLong=TimeCurrent();
bTopLong=true;
}
//deactivate time counting before open long trade on top of the rectangle
if (bTopLong && TimeCurrent()<=timeTopLong+TimeWaitSeconds && Close[0]<recHigh)
{
bTopLong=false;
}
//comment
if (bTopLong && buyTopOpenAllow && recHigh>0) Comment("Seconds to open buy trade:",(timeTopLong+TimeWaitSeconds)-TimeCurrent());
//open buy trade on top of the rectangle and close sell trade if it is open
if (buyTopOpenAllow && bTopLong && TimeCurrent()>timeTopLong+TimeWaitSeconds && Close[0]>recHigh && recHigh>=0)
{
Buy_Condition_1=true;
buyTopOpenAllow=false;
bTopLong=false;
buyTopTradeOpen=true;
trades++;
if (sellTopTradeOpen)
{
closeTrade(OP_SELL);
sellTopOpenAllow=true;
sellTopTradeOpen=false;
}
}
//activate time counting before open sell trade on top of the rectangle
if (sellTopOpenAllow && !bTopShort && price_0>=recHigh && Close[0]<=recHigh && trades<=NumberOfHedgeTrades && recHigh>0)
{
timeTopShort=TimeCurrent();
bTopShort=true;
}
//deactivate time counting before open sell trade on top of the rectangle
if (bTopShort && TimeCurrent()<=timeTopShort+TimeWaitSeconds && Close[0]>recHigh)
{
bTopShort=false;
}
//comment
if (bTopShort && sellTopOpenAllow && recHigh>0 && RectangleHedgeOn) Comment("Seconds to open sell trade:",(timeTopShort+TimeWaitSeconds)-TimeCurrent());
//open sell trade on top of the rectangle and close buy if it is open
if (sellTopOpenAllow && bTopShort && TimeCurrent()>timeTopShort+TimeWaitSeconds && Close[0]<recHigh && recHigh>=0)
{
if (trades<NumberOfHedgeTrades && RectangleHedgeOn) Sell_Condition_1=true;
sellTopOpenAllow=false;
bTopShort=false;
sellTopTradeOpen=true;
if (buyTopTradeOpen)
{
closeTrade(OP_BUY);
buyTopOpenAllow=true;
buyTopTradeOpen=false;
}
if (trades==NumberOfHedgeTrades)
{
if (ObjectDescription(name)=="") ObjectSetText(name,"1");
if (ObjectDescription(name)=="2") ObjectSetText(name,"3");
sellTopTradeOpen=false;
trades=0;
}
}
//switch to bottom of the rectangle
if (sellTopTradeOpen && price_0>=recLow && Close[0]<=recLow && RectangleHedgeOn)
{
sellTopTradeOpen=false;
ObjectSetText(name,"");
sellBottomOpenAllow=false;
buyBottomOpenAllow=true;
bBottomShort=false;
sellBottomTradeOpen=true;
trades=0;
}
//rectangle bottom
//activate time counting before open sell trade on bottom of the rectangle
if (sellBottomOpenAllow && !bBottomShort && price_0>=recLow && Close[0]<=recLow && trades<NumberOfHedgeTrades && recLow>0)
{
timeBottomShort=TimeCurrent();
bBottomShort=true;
}
//deactivate time counting before open sell trade on bottom of the rectangle
if (bBottomShort && TimeCurrent()<=timeBottomShort+TimeWaitSeconds && Close[0]>recLow)
{
bBottomShort=false;
}
//comment
if (bBottomShort && sellBottomOpenAllow && recLow>0) Comment("Seconds to open sell trade:",(timeBottomShort+TimeWaitSeconds)-TimeCurrent());
//open sell trade on bottom of the rectangle and close buy trade if it is open
if (sellBottomOpenAllow && bBottomShort && TimeCurrent()>timeBottomShort+TimeWaitSeconds && Close[0]<recLow && recLow>=0)
{
Sell_Condition_1=true;
sellBottomOpenAllow=false;
bBottomShort=false;
sellBottomTradeOpen=true;
trades++;
if (buyBottomTradeOpen)
{
closeTrade(OP_BUY);
buyBottomOpenAllow=true;
buyBottomTradeOpen=false;
}
}
//activate time counting before open buy trade on bottom of the rectangle
if (buyBottomOpenAllow && !bBottomLong && price_0<=recLow && Close[0]>=recLow && trades<=NumberOfHedgeTrades && recLow>0)
{
timeBottomLong=TimeCurrent();
bBottomLong=true;
}
//deactivate time counting before open buy trade on bottom of the rectangle
if (bBottomLong && TimeCurrent()<=timeBottomLong+TimeWaitSeconds && Close[0]<recLow)
{
bBottomLong=false;
}
//comment
if (bBottomLong && buyBottomOpenAllow && recLow>0 && RectangleHedgeOn) Comment("Seconds to open buy trade:",(timeBottomLong+TimeWaitSeconds)-TimeCurrent());
//open buy trade on bottom of the rectangle and close sell if it is open
if (buyBottomOpenAllow && bBottomLong && TimeCurrent()>timeBottomLong+TimeWaitSeconds && Close[0]>recLow && recLow>=0)
{
if (trades<NumberOfHedgeTrades && RectangleHedgeOn) Buy_Condition_1=true;
buyBottomOpenAllow=false;
bBottomLong=false;
buyBottomTradeOpen=true;
if (sellBottomTradeOpen)
{
closeTrade(OP_SELL);
sellBottomOpenAllow=true;
sellBottomTradeOpen=false;
}
if (trades==NumberOfHedgeTrades)
{
if (ObjectDescription(name)=="") ObjectSetText(name,"2");
if (ObjectDescription(name)=="1") ObjectSetText(name,"3");
buyBottomTradeOpen=false;
trades=0;
}
}
//switch to the top of the rectangle
if (buyBottomTradeOpen && price_0<=recHigh && Close[0]>=recHigh && RectangleHedgeOn)
{
buyBottomTradeOpen=false;
ObjectSetText(name,"");
buyTopOpenAllow=false;
sellTopOpenAllow=true;
bTopLong=false;
buyTopTradeOpen=true;
trades=0;
}
}
}
}
//permanent trades section
if (pbuysellLinesExist)
{
if (TradeMode==0)
{
Comment("");
if (pbuyBuySellLineAllow && price_0<=pBuySellLevel && Close[0]>=pBuySellLevel && bstrades<=NumberOfHedgeTrades && !bBuySellLineLong && price_0!=0 && pBuySellLevel!=0)
{
if (!sellBuySellLineTradeOpen && !psellBuySellMode) {pbuyBuySellMode=true; psellBuySellMode=false;}
if (pbuyBuySellMode || (psellBuySellMode && sellBuySellLineTradeOpen))
{
bBuySellLineLong=true;
timeBuySellLineLong=TimeCurrent();
}
}
if (bBuySellLineLong && TimeCurrent()<=timeBuySellLineLong+TimeWaitSeconds && Close[0]<pBuySellLevel)
{
bBuySellLineLong=false;
}
//comment
if (bBuySellLineLong && pbuyBuySellLineAllow) Comment("Seconds to open buy trade:",(timeBuySellLineLong+TimeWaitSeconds)-TimeCurrent());
if (pbuyBuySellLineAllow && bBuySellLineLong && TimeCurrent()>timeBuySellLineLong+TimeWaitSeconds && Close[0]>pBuySellLevel)
{
if ((bstrades<NumberOfHedgeTrades && psellBuySellMode) || pbuyBuySellMode)
{
Buy_Condition_2=true;
BESet=false;
}
pbuyBuySellLineAllow=false;
bBuySellLineLong=false;
buyBuySellLineTradeOpen=true;
if (pbuyBuySellMode) bstrades++;
//if (sellBuySellLineTradeOpen)
//{
closeTrade(OP_SELL);
psellBuySellLineAllow=true;
sellBuySellLineTradeOpen=false;
//}
if (bstrades==NumberOfHedgeTrades && psellBuySellMode)
{
ObjectSetText(pbsLineName,"executed-"+pbsLineDesc);
buyBuySellLineTradeOpen=false;
pbuyBuySellMode=false;
psellBuySellMode=false;
pbuyBuySellLineAllow=true;
psellBuySellLineAllow=true;
bstrades=0;
pBuySellLevel=0;
}
}
if (psellBuySellLineAllow && !bBuySellLineShort && price_0>=pBuySellLevel && Close[0]<=pBuySellLevel && bstrades<=NumberOfHedgeTrades && price_0!=0 && pBuySellLevel!=0)
{
if (!buyBuySellLineTradeOpen && !pbuyBuySellMode) {pbuyBuySellMode=false; psellBuySellMode=true;}
if (psellBuySellMode || (pbuyBuySellMode && buyBuySellLineTradeOpen))
{
timeBuySellLineShort=TimeCurrent();
bBuySellLineShort=true;
}
}
//deactivate time counting before open sell trade on permanent buysell line
if (bBuySellLineShort && TimeCurrent()<=timeBuySellLineShort+TimeWaitSeconds && Close[0]>pBuySellLevel)
{
bBuySellLineShort=false;
}
//comment
if (bBuySellLineShort && psellBuySellLineAllow) Comment("Seconds to open sell trade:",(timeBuySellLineShort+TimeWaitSeconds)-TimeCurrent());
//open sell trade on buysell line and close buy if it is open
if (psellBuySellLineAllow && bBuySellLineShort && TimeCurrent()>timeBuySellLineShort+TimeWaitSeconds && Close[0]<pBuySellLevel)
{
if ((bstrades<NumberOfHedgeTrades && pbuyBuySellMode) || (psellBuySellMode))
{
Sell_Condition_2=true;
BESet=false;
}
psellBuySellLineAllow=false;
bBuySellLineShort=false;
sellBuySellLineTradeOpen=true;
if (psellBuySellMode) bstrades++;
//if (buyBuySellLineTradeOpen)
//{
closeTrade(OP_BUY);
pbuyBuySellLineAllow=true;
buyBuySellLineTradeOpen=false;
//}
if (bstrades==NumberOfHedgeTrades && pbuyBuySellMode)
{
ObjectSetText(pbsLineName,"executed-"+pbsLineDesc);
sellBuySellLineTradeOpen=false;
pbuyBuySellMode=false;
psellBuySellMode=false;
pbuyBuySellLineAllow=true;
psellBuySellLineAllow=true;
bstrades=0;
pBuySellLevel=0;
}
}
}
if (TradeMode==1 && currBars2!=Bars)
{
if (!buyBuySellLineTradeOpen && Close[2]<=pBuySellLevel_2 && Close[1]>=pBuySellLevel_1 && bstrades<=NumberOfHedgeTrades && pBuySellLevel_1>0)
{
if (!sellBuySellLineTradeOpen) {pbuyBuySellMode=true; psellBuySellMode=false;}
if ((bstrades<NumberOfHedgeTrades && psellBuySellMode) || pbuyBuySellMode)
{
Buy_Condition_2=true;
BESet=false;
}
pbuyBuySellLineAllow=false;
buyBuySellLineTradeOpen=true;
if (pbuyBuySellMode) bstrades++;
if (sellBuySellLineTradeOpen)
{
closeTrade(OP_SELL);
psellBuySellLineAllow=true;
sellBuySellLineTradeOpen=false;
}
if (bstrades==NumberOfHedgeTrades && psellBuySellMode)
{
ObjectSetText(pbsLineName,"executed-"+pbsLineDesc);
buyBuySellLineTradeOpen=false;
pbuyBuySellMode=false;
psellBuySellMode=false;
pbuyBuySellLineAllow=true;
psellBuySellLineAllow=true;
bstrades=0;
pBuySellLevel=0;
}
}
if (!sellBuySellLineTradeOpen && Close[2]>=pBuySellLevel_2 && Close[1]<=pBuySellLevel_1 && bstrades<=NumberOfHedgeTrades && pBuySellLevel_1>0)
{
if (!buyBuySellLineTradeOpen) {pbuyBuySellMode=false; psellBuySellMode=true;}
if ((bstrades<NumberOfHedgeTrades && pbuyBuySellMode) || (psellBuySellMode))
{
Sell_Condition_2=true;
BESet=false;
}
psellBuySellLineAllow=false;
sellBuySellLineTradeOpen=true;
if (psellBuySellMode) bstrades++;
if (buyBuySellLineTradeOpen)
{
closeTrade(OP_BUY);
pbuyBuySellLineAllow=true;
buyBuySellLineTradeOpen=false;
}
if (bstrades==NumberOfHedgeTrades && pbuyBuySellMode)
{
ObjectSetText(pbsLineName,"executed-"+pbsLineDesc);
sellBuySellLineTradeOpen=false;
pbuyBuySellMode=false;
psellBuySellMode=false;
pbuyBuySellLineAllow=true;
psellBuySellLineAllow=true;
bstrades=0;
pBuySellLevel=0;
pBuySellLevel_1=0;
pBuySellLevel_2=0;
}
}
currBars2=Bars;
}
if (TradeMode==2)
{
double immbuyBuySellLevel=pBuySellLevel+ImmediateEntryBuffer*Point*mp;
double immsellBuySellLevel=pBuySellLevel-ImmediateEntryBuffer*Point*mp;
//Comment("immBuyLevel:",immBuyLevel,",immSellLevel:",immSellLevel);
if (!buysellLineAllow && price_0!=0 && ((price_0<=pBuySellLevel && Close[0]>=pBuySellLevel) || (price_0>=pBuySellLevel && Close[0]<=pBuySellLevel)) && pBuySellLevel!=0) buysellLineAllow=true;
if (!buyBuySellLineTradeOpen && price_0<immbuyBuySellLevel && Close[0]>=immbuyBuySellLevel && bstrades<=NumberOfHedgeTrades && pBuySellLevel>0 && buysellLineAllow)
{
if (!sellBuySellLineTradeOpen && !psellBuySellMode) {pbuyBuySellMode=true; psellBuySellMode=false;}
if ((bstrades<NumberOfHedgeTrades && psellBuySellMode) || pbuyBuySellMode)
{
Buy_Condition_2=true;
BESet=false;
}
pbuyBuySellLineAllow=false;
buyBuySellLineTradeOpen=true;
if (pbuyBuySellMode) bstrades++;
if (sellBuySellLineTradeOpen)
{
closeTrade(OP_SELL);
psellBuySellLineAllow=true;
sellBuySellLineTradeOpen=false;
}
if (bstrades==NumberOfHedgeTrades && psellBuySellMode)
{
ObjectSetText(pbsLineName,"executed-"+pbsLineDesc);
buyBuySellLineTradeOpen=false;
pbuyBuySellMode=false;
psellBuySellMode=false;
pbuyBuySellLineAllow=true;
psellBuySellLineAllow=true;
bstrades=0;
pBuySellLevel=0;
}
}
if (!sellBuySellLineTradeOpen && price_0>immsellBuySellLevel && Close[0]<=immsellBuySellLevel && bstrades<=NumberOfHedgeTrades && pBuySellLevel_1>0 && buysellLineAllow && price_0!=0)
{
if (!buyBuySellLineTradeOpen && !pbuyBuySellMode) {pbuyBuySellMode=false; psellBuySellMode=true;}
if ((bstrades<NumberOfHedgeTrades && pbuyBuySellMode) || (psellBuySellMode))
{
Sell_Condition_2=true;
BESet=false;
}
psellBuySellLineAllow=false;
sellBuySellLineTradeOpen=true;
if (psellBuySellMode) bstrades++;
if (buyBuySellLineTradeOpen)
{
closeTrade(OP_BUY);
pbuyBuySellLineAllow=true;
buyBuySellLineTradeOpen=false;
}
if (bstrades==NumberOfHedgeTrades && pbuyBuySellMode)
{
ObjectSetText(pbsLineName,"executed-"+pbsLineDesc);
sellBuySellLineTradeOpen=false;
pbuyBuySellMode=false;
psellBuySellMode=false;
pbuyBuySellLineAllow=true;
psellBuySellLineAllow=true;
bstrades=0;
pBuySellLevel=0;
}
}
}
}
else buysellLineAllow=false;
//permanent buy line
if (pbuyLinesExist)
{
bool Buy_Condition_3;
if (TradeMode==0)
{
Comment("");
//activate time counting before open buy trade
if (!buyBuyLineTradeOpen && price_0<=pbuyLineLevel && Close[0]>=pbuyLineLevel && btrades<=NumberOfHedgeTrades && !bBuyLineLong && price_0!=0 && pbuyLineLevel!=0)
{
bBuyLineLong=true;
timeBuyLineLong=TimeCurrent();
}
//deactivate time counting before open buy trade
if (bBuyLineLong && TimeCurrent()<=timeBuyLineLong+TimeWaitSeconds && Close[0]<pbuyLineLevel)
{
bBuyLineLong=false;
}
//comment
if (bBuyLineLong && !buyBuyLineTradeOpen) Comment("Seconds to open buy trade:",(timeBuyLineLong+TimeWaitSeconds)-TimeCurrent());
//open buy trade
if (!buyBuyLineTradeOpen && bBuyLineLong && TimeCurrent()>timeBuyLineLong+TimeWaitSeconds && Close[0]>pbuyLineLevel)
{
closeTrade(OP_SELL);
if (btrades<NumberOfHedgeTrades)
{
Buy_Condition_3=true;
BESet=false;
}
buyBuyLineTradeOpen=true;
bBuyLineLong=false;
btrades++;
}
//activate time counting before close buy trade
if (buyBuyLineTradeOpen && !bBuyLineShort && price_0>=pbuyLineLevel && Close[0]<=pbuyLineLevel && btrades<=NumberOfHedgeTrades)
{
timeBuyLineShort=TimeCurrent();
bBuyLineShort=true;
}
//deactivate time counting before open sell trade on permanent buysell line
if (bBuyLineShort && TimeCurrent()<=timeBuyLineShort+TimeWaitSeconds && Close[0]>pbuyLineLevel)
{
bBuyLineShort=false;
}
//comment
if (bBuyLineShort && buyBuyLineTradeOpen) Comment("Seconds to close buy trade:",(timeBuyLineShort+TimeWaitSeconds)-TimeCurrent());
//close buy trade
if (buyBuyLineTradeOpen && bBuyLineShort && TimeCurrent()>timeBuyLineShort+TimeWaitSeconds && Close[0]<pbuyLineLevel)
{
closeTrade(OP_BUY);
buyBuyLineTradeOpen=false;
bBuyLineShort=false;
if (btrades==NumberOfHedgeTrades)
{
ObjectSetText(pbLineName,"executed-"+pbLineDesc);
btrades=0;
pbuyLineLevel=0;
}
}
}
if (TradeMode==1 && currBars3!=Bars)
{
Comment("");
//open buy trade after candle cross permanent buy line up
if (!buyBuyLineTradeOpen && Close[2]<=pbuyLineLevel_2 && Close[1]>=pbuyLineLevel_1 && btrades<=NumberOfHedgeTrades && pbuyLineLevel_1!=0)
{
closeTrade(OP_SELL);
if (btrades<NumberOfHedgeTrades)
{
Buy_Condition_3=true;
BESet=false;
}
buyBuyLineTradeOpen=true;
btrades++;
}
//close buy trade after candle cross permanent buy line down
if (buyBuyLineTradeOpen && Close[2]>=pbuyLineLevel_2 && Close[1]<pbuyLineLevel_1 && btrades<=NumberOfHedgeTrades)
{
closeTrade(OP_BUY);
buyBuyLineTradeOpen=false;
if (btrades==NumberOfHedgeTrades)
{
ObjectSetText(pbLineName,"executed-"+pbLineDesc);
btrades=0;
pbuyLineLevel=0;
pbuyLineLevel_1=0;
pbuyLineLevel_2=0;
}
}
currBars3=Bars;
}
if (TradeMode==2)
{
double immbuyBuyLevel=pbuyLineLevel+ImmediateEntryBuffer*Point*mp;
double immsellBuyLevel=pbuyLineLevel-ImmediateEntryBuffer*Point*mp;
//Comment("pbuyLineLevel:",pbuyLineLevel,",immbuyBuyLevel:",immbuyBuyLevel,",immsellBuyLevel:",immsellBuyLevel,",buyLineAllow:",buyLineAllow);
if (!buyLineAllow && price_0!=0 && ((price_0<=pbuyLineLevel && Close[0]>=pbuyLineLevel) || (price_0>=pbuyLineLevel && Close[0]<=pbuyLineLevel)) && pbuyLineLevel!=0) buyLineAllow=true;
if (!buyBuyLineTradeOpen && Close[0]>=immbuyBuyLevel && btrades<=NumberOfHedgeTrades && pbuyLineLevel>0 && buyLineAllow)
{
closeTrade(OP_SELL);
if (btrades<NumberOfHedgeTrades)
{
Buy_Condition_3=true;
BESet=false;
}
buyBuyLineTradeOpen=true;
btrades++;
}
if (buyBuyLineTradeOpen && Close[0]<=immsellBuyLevel && btrades<=NumberOfHedgeTrades && pbuyLineLevel>0 && buyLineAllow)
{
closeTrade(OP_BUY);
buyBuyLineTradeOpen=false;
if (btrades==NumberOfHedgeTrades)
{
ObjectSetText(pbLineName,"executed-"+pbLineDesc);
btrades=0;
pbuyLineLevel=0;
}
}
}
}
else buyLineAllow=false;
//permanent sell line
if (psellLinesExist)
{
bool Sell_Condition_3;
if (TradeMode==0)
{
Comment("");
//activate time counting before open sell trade
if (!sellSellLineTradeOpen && price_0>=psellLineLevel && Close[0]<=psellLineLevel && strades<=NumberOfHedgeTrades && !bSellLineShort && price_0!=0 && psellLineLevel!=0)
{
bSellLineShort=true;
timeSellLineShort=TimeCurrent();
}
//deactivate time counting before open buy trade
if (bSellLineShort && TimeCurrent()<=timeSellLineShort+TimeWaitSeconds && Close[0]>psellLineLevel)
{
bSellLineShort=false;
}
//comment
if (bSellLineShort && !sellSellLineTradeOpen) Comment("Seconds to open sell trade:",(timeSellLineShort+TimeWaitSeconds)-TimeCurrent());
//open sell trade
if (!sellSellLineTradeOpen && bSellLineShort && TimeCurrent()>timeSellLineShort+TimeWaitSeconds && Close[0]<psellLineLevel)
{
closeTrade(OP_BUY);
if (strades<NumberOfHedgeTrades)
{
Sell_Condition_3=true;
BESet=false;
}
sellSellLineTradeOpen=true;
bSellLineShort=false;
strades++;
}
//activate time counting before close sell trade
if (sellSellLineTradeOpen && !bSellLineLong && price_0<=psellLineLevel && Close[0]>=psellLineLevel && strades<=NumberOfHedgeTrades && price_0!=0 && psellLineLevel!=0)
{
timeSellLineLong=TimeCurrent();
bSellLineLong=true;
}
//deactivate time counting before open sell trade on permanent buysell line
if (bSellLineLong && TimeCurrent()<=timeSellLineLong+TimeWaitSeconds && Close[0]<psellLineLevel)
{
bSellLineLong=false;
}
//comment
if (bSellLineLong && sellSellLineTradeOpen) Comment("Seconds to close sell trade:",(timeSellLineLong+TimeWaitSeconds)-TimeCurrent());
//close sell trade
if (sellSellLineTradeOpen && bSellLineLong && TimeCurrent()>timeSellLineLong+TimeWaitSeconds && Close[0]>psellLineLevel)
{
closeTrade(OP_SELL);
sellSellLineTradeOpen=false;
bSellLineLong=false;
if (strades==NumberOfHedgeTrades)
{
ObjectSetText(psLineName,"executed-"+psLineDesc);
strades=0;
psellLineLevel=0;
}
}
}
if (TradeMode==1 && currBars4!=Bars)
{
Comment("");
Comment("strades:",strades,",psellLineLevel_1:",psellLineLevel_1,",psellLineLevel_2:",psellLineLevel_2);
//open sell trade after permanent sell line cross down
if (!sellSellLineTradeOpen && Close[2]>=psellLineLevel_2 && Close[1]<=psellLineLevel_1 && strades<=NumberOfHedgeTrades && psellLineLevel_1!=0)
{
closeTrade(OP_BUY);
if (strades<NumberOfHedgeTrades)
{
Sell_Condition_3=true;
BESet=false;
}
sellSellLineTradeOpen=true;
strades++;
}
//close sell trade after permanent sell line cross up
if (sellSellLineTradeOpen && Close[2]<=psellLineLevel_2 && Close[1]>=psellLineLevel_1 && strades<=NumberOfHedgeTrades)
{
closeTrade(OP_SELL);
sellSellLineTradeOpen=false;
if (strades==NumberOfHedgeTrades)
{
ObjectSetText(psLineName,"executed-"+psLineDesc);
strades=0;
psellLineLevel=0;
psellLineLevel_1=0;
psellLineLevel_2=0;
}
}
currBars4=Bars;
}
if (TradeMode==2)
{
double immbuySellLevel=psellLineLevel+ImmediateEntryBuffer*Point*mp;
double immsellSellLevel=psellLineLevel-ImmediateEntryBuffer*Point*mp;
if (!sellLineAllow && price_0!=0 && ((price_0<=psellLineLevel && Close[0]>=psellLineLevel) || (price_0>=psellLineLevel && Close[0]<=psellLineLevel)) && psellLineLevel!=0) sellLineAllow=true;
//open sell trade after cross sell line level down
if (!sellSellLineTradeOpen && Close[0]<=immsellSellLevel && strades<=NumberOfHedgeTrades && psellLineLevel>0 && sellLineAllow)
{
closeTrade(OP_BUY);
if (strades<NumberOfHedgeTrades)
{
Sell_Condition_3=true;
BESet=false;
}
sellSellLineTradeOpen=true;
strades++;
}
//close sell trade after cross sell line level up
if (sellSellLineTradeOpen && Close[0]>=immbuySellLevel && strades<=NumberOfHedgeTrades && sellLineAllow)
{
closeTrade(OP_SELL);
sellSellLineTradeOpen=false;
if (strades==NumberOfHedgeTrades)
{
ObjectSetText(psLineName,"executed-"+psLineDesc);
strades=0;
psellLineLevel=0;
psellLineLevel_1=0;
psellLineLevel_2=0;
}
}
}
}
else sellLineAllow=false;
if (UsePipsProfit)
{
double pipsLoss=calculatePipsLoss(EAStartTime);
Comment("Profit of closed trades:",pipsLoss/Point/mp);
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && Bid-OrderOpenPrice()>=-pipsLoss+PipsProfit*Point*mp &&
((pipsLoss<0 && !ProfitOnFirstTrade) || (pipsLoss+(Bid-OrderOpenPrice())>=PipsProfit*Point*mp)))
{
Print("Bid-OrderOpenPrice():",Bid-OrderOpenPrice(),",-pipsLoss+PipsProfit*Point*mp:",-pipsLoss+PipsProfit*Point*mp);
closeTrade(OP_BUY);
EAStartTime=TimeCurrent();
}
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderOpenPrice()-Ask>=-pipsLoss+PipsProfit*Point*mp &&
((pipsLoss<0 && !ProfitOnFirstTrade) || (pipsLoss+(OrderOpenPrice()-Ask)>=PipsProfit*Point*mp)))
{
Print("OrderOpenPrice()-Ask:",OrderOpenPrice()-Ask,",-pipsLoss+PipsProfit*Point*mp:",-pipsLoss+PipsProfit*Point*mp);
closeTrade(OP_SELL);
EAStartTime=TimeCurrent();
}
}
}
//move to BE
if (BreakEvenOn && !BESet)
{
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==10002 && OrderType()==OP_BUY &&
((Close[0]-OrderOpenPrice()>=MoveSLToBEAfter*Point*mp && !BEAfterFirstCandleOn) || (BEAfterFirstCandleOn && candleCount+1==Bars)))
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);
BESet=true;
}
if(OrderSymbol()==Symbol() && OrderMagicNumber()==10002 && OrderType()==OP_SELL &&
((OrderOpenPrice()-Close[0]>=MoveSLToBEAfter*Point*mp && !BEAfterFirstCandleOn) || (BEAfterFirstCandleOn && candleCount+1==Bars)))
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,CLR_NONE);
BESet=true;
}
}
}
double tsize;
double TP, SL;
if (Buy_Condition_1 || Buy_Condition_2 || Buy_Condition_3)
{
if (Buy_Condition_1) tsize=RectangleTradeSize;
if (Buy_Condition_2) tsize=ptradeSize;
if (Buy_Condition_3) tsize=pbuyTradeLineSize;
err=1;
while(err>0)
{
RefreshRates();
if (TakeProfit>0) TP=Ask+TakeProfit*Point*mp;
else TP=0;
if (StopLoss>0) SL=Ask-StopLoss*Point*mp;
else SL=0;
ticket=OrderSend(Symbol(), OP_BUY, tsize, Ask, MaxSlippage*Point*mp, 0, 0, "", 10002, 0, Green);
err=GetLastError();
Print("Buy open, err:",err);
if (err==4 || err==6 || err==8 || err==128 || err==129 || err==135 || err==136 || err==138 || err==139 || err==146) err=1;
else err=0;
if (err!=0) Sleep(1000);
}
if (ticket>0 && (TP>0 || SL>0))
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
OrderModify(ticket, OrderOpenPrice(), SL, TP, 0, CLR_NONE);
}
Buy_Condition_1=false;
Buy_Condition_2=false;
candleCount=Bars;
}
if (Sell_Condition_1 || Sell_Condition_2 || Sell_Condition_3)
{
if (Sell_Condition_1) tsize=RectangleTradeSize;
if (Sell_Condition_2) tsize=ptradeSize;
if (Sell_Condition_3) tsize=psellTradeLineSize;
err=1;
while(err>0)
{
RefreshRates();
if (TakeProfit>0) TP=Bid-TakeProfit*Point*mp;
else TP=0;
if (StopLoss>0) SL=Bid+StopLoss*Point*mp;
else SL=0;
ticket=OrderSend(Symbol(), OP_SELL, tsize, Bid, MaxSlippage*Point*mp, 0, 0, "", 10002, 0, Green);
err=GetLastError();
Print("Sell open, err:",err);
if (err==4 || err==6 || err==8 || err==128 || err==129 || err==135 || err==136 || err==138 || err==139 || err==146) err=1;
else err=0;
if (err!=0) Sleep(1000);
}
if (ticket>0 && (TP>0 || SL>0))
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
OrderModify(ticket, OrderOpenPrice(), SL, TP, 0, CLR_NONE);
}
Sell_Condition_1=false;
Sell_Condition_2=false;
candleCount=Bars;
}
if (recTradeCount()==0)
{
buyTopOpenAllow=true;
sellTopOpenAllow=true;
sellBottomOpenAllow=true;
buyBottomOpenAllow=true;
psellBuySellLineAllow=true;
pbuyBuySellLineAllow=true;
buyBuySellLineTradeOpen=false;
sellBuySellLineTradeOpen=false;
sellBuySellLineTradeOpen=false;
buyBuySellLineTradeOpen=false;
//trades=0;
}
//display winning/losing statistics
if (DisplayStatistics && OrdersHistoryTotal()!=OrdersHistoryTotal_0)
{
int wcount, lcount;
double profitPips, lossPips;
string text;
for(cnt=OrdersHistoryTotal();cnt>=0;cnt--)
{
OrderSelect(cnt,SELECT_BY_POS, MODE_HISTORY);
if (OrderMagicNumber()==10002 && OrderSymbol()==Symbol() && OrderProfit()>=0)
{
wcount++;
if (OrderType()==OP_BUY) profitPips=profitPips+(OrderClosePrice()-OrderOpenPrice());
if (OrderType()==OP_SELL) profitPips=profitPips+(OrderOpenPrice()-OrderClosePrice());
}
if (OrderMagicNumber()==10002 && OrderSymbol()==Symbol() && OrderProfit()<0)
{
lcount++;
if (OrderType()==OP_BUY) lossPips=lossPips+(OrderClosePrice()-OrderOpenPrice());
if (OrderType()==OP_SELL) lossPips=lossPips+(OrderOpenPrice()-OrderClosePrice());
}
}
//text=wcount+" winning trades,"+lcount+" losing trades";
//SetLabel("tot_lab", X, Y+20, text, TextColor, "Calibri",10);
if (wcount>0)
{
double avgprofit=(profitPips/Point/mp)/wcount;
totalProfit=profitPips/Point/mp;
text=wcount+" winning trades, average profit;"+DoubleToStr(avgprofit,0)+",total:"+DoubleToStr(totalProfit,0);
SetLabel("totp_lab", X, Y, text, TextColor, "Calibri",10);
}
if (lcount>0)
{
double avgloss=(lossPips/Point/mp)/lcount;
totalLoss=lossPips/Point/mp;
text=lcount+" losing trades, average loss;"+DoubleToStr(avgloss,0)+",total:"+DoubleToStr(totalLoss,0);
SetLabel("totl_lab", X, Y+10, text, TextColor, "Calibri",10);
}
}
if (DisplayStatistics)
{
double BEd=totalProfit+totalLoss+currentProfit();
text="BE distance:"+DoubleToStr(BEd,0);
//text="totalProfit:"+totalProfit,;
SetLabel("BEd_lab", X, Y+20, text, TextColor, "Calibri",10);
}
price_0=Close[0];
//----
//----
return(0);
}
//+------------------------------------------------------------------+
void OpenTrade(string sym, int tmode, double tsize)
{
int MagicNumber=10001;
int err, ticket;
double TP, SL;
if(!IsTradeAllowed())
{
while(true)
{
if(IsTradeAllowed()) break;
Sleep(100);
}
}
RefreshRates();
if (tmode==OP_BUY)
{
err=1;
while(err>0)
{
RefreshRates();
if (TakeProfit>0) TP=Ask+TakeProfit*Point*mp;
else TP=0;
if (StopLoss>0) SL=Ask-StopLoss*Point*mp;
else SL=0;
ticket=OrderSend(sym, OP_BUY, tsize, Ask, MaxSlippage*Point*mp, 0, 0, "", MagicNumber, 0, Green);
err=GetLastError();
Print(sym+" Buy open, err:",err);
if (err==3 || err==64 || err==65 || err==130 || err==131 || err==133 || err==134 || err==148) err=0;
if (err!=0) Sleep(1000);
}
if (ticket>0 && (TP>0 || SL>0))
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
OrderModify(ticket, OrderOpenPrice(), SL, TP, 0, CLR_NONE);
}
}
if (tmode==OP_SELL)
{
err=1;
while(err>0)
{
RefreshRates();
if (TakeProfit>0) TP=Bid-TakeProfit*Point*mp;
else TP=0;
if (StopLoss>0) SL=Bid+StopLoss*Point*mp;
else SL=0;
ticket=OrderSend(sym, OP_SELL, tsize, Bid, MaxSlippage*Point*mp, SL, TP, "", MagicNumber, 0, Green);
err=GetLastError();
Print(sym+" Sell open, err:",err);
if (err==3 || err==64 || err==65 || err==130 || err==131 || err==133 || err==134 || err==148) err=0;
if (err!=0) Sleep(1000);
}
if (ticket>0 && (TP>0 || SL>0))
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
OrderModify(ticket, OrderOpenPrice(), SL, TP, 0, CLR_NONE);
}
}
}
void closePartTrade(int ttype,double cSize)
{
int err;
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderType()==ttype && ttype==OP_SELL)
{
err=1;
while(err>0)
{
RefreshRates();
OrderClose(OrderTicket(),cSize,Ask,MaxSlippage*Point*mp,Green);
err=GetLastError();
Print("SELL order closed, err:",ErrorDescription(err));
Alert("SELL order closed, err:",ErrorDescription(err));
if (err==4 || err==6 || err==8 || err==128 || err==129 || err==135 || err==136 || err==138 || err==139 || err==146) err=1;
else err=0;
if (err!=0) Sleep(1000);
}
}
if(OrderSymbol()==Symbol() && OrderType()==ttype && ttype==OP_BUY)
{
err=1;
while(err>0)
{
RefreshRates();
OrderClose(OrderTicket(),cSize,Bid,MaxSlippage*Point*mp,Red);
err=GetLastError();
Print("Buy order closed, err:",ErrorDescription(err));
Alert("Buy order closed, err:",ErrorDescription(err));
if (err==4 || err==6 || err==8 || err==128 || err==129 || err==135 || err==136 || err==138 || err==139 || err==146) err=1;
else err=0;
if (err!=0) Sleep(1000);
}
}
}
}
int tradeCount()
{
int total = OrdersTotal();
int tc=0;
for(int cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol())
{
tc++;
}
}
return(tc);
}
int recTradeCount()
{
int total = OrdersTotal();
int tc=0;
for(int cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==10002)
{
tc++;
}
}
return(tc);
}
void closeTrade(int ttype)
{
int err;
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderType()==ttype && ttype==OP_SELL)
{
err=1;
while(err>0)
{
RefreshRates();
OrderClose(OrderTicket(),OrderLots(),Ask,MaxSlippage*Point*mp,Green);
err=GetLastError();
Print("SELL order closed, err:",err);
if (err==4 || err==6 || err==8 || err==128 || err==129 || err==135 || err==136 || err==138 || err==139 || err==146) err=1;
else err=0;
if (err!=0) Sleep(1000);
}
}
if(OrderSymbol()==Symbol() && OrderType()==ttype && ttype==OP_BUY)
{
err=1;
while(err>0)
{
RefreshRates();
OrderClose(OrderTicket(),OrderLots(),Bid,MaxSlippage*Point*mp,Red);
err=GetLastError();
Print("BUY order closed, err:",err);
if (err==4 || err==6 || err==8 || err==128 || err==129 || err==135 || err==136 || err==138 || err==139 || err==146) err=1;
else err=0;
if (err!=0) Sleep(1000);
}
}
}
}
void closeTradebyTicket(int ttype, int tic)
{
int err;
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderType()==ttype && ttype==OP_SELL && OrderTicket() == tic)
{
err=1;
while(err>0)
{
RefreshRates();
OrderClose(OrderTicket(),OrderLots(),Ask,MaxSlippage*Point*mp,Green);
err=GetLastError();
Print("SELL order closed, err:",err);
if (err==4 || err==6 || err==8 || err==128 || err==129 || err==135 || err==136 || err==138 || err==139 || err==146) err=1;
else err=0;
if (err!=0) Sleep(1000);
}
}
if(OrderSymbol()==Symbol() && OrderType()==ttype && ttype==OP_BUY && OrderTicket() == tic)
{
err=1;
while(err>0)
{
RefreshRates();
OrderClose(OrderTicket(),OrderLots(),Bid,MaxSlippage*Point*mp,Red);
err=GetLastError();
Print("BUY order closed, err:",err);
if (err==4 || err==6 || err==8 || err==128 || err==129 || err==135 || err==136 || err==138 || err==139 || err==146) err=1;
else err=0;
if (err!=0) Sleep(1000);
}
}
}
}
bool tradeExist(int ttype)
{
int total = OrdersTotal();
int OrdNumber=-1;
for(int cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderType()==ttype)
{
OrdNumber=cnt;
}
}
if(OrdNumber==-1)
{
return(false);
}
else
{
return(true);
}
}
double calculatePipsLoss(datetime stime)
{
double ploss;
for(int cnt=0;cnt<OrdersHistoryTotal();cnt++)
{
OrderSelect(cnt,SELECT_BY_POS, MODE_HISTORY);
if(OrderSymbol()==Symbol() && OrderOpenTime()>=stime)
{
if (OrderType()==OP_BUY) ploss=ploss+(OrderClosePrice()-OrderOpenPrice());
if (OrderType()==OP_SELL) ploss=ploss+(OrderOpenPrice()-OrderClosePrice());
}
}
return(ploss);
}
double currentProfit()
{
double p;
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt,SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==10002 && OrderType()==OP_BUY)
{
p=Bid-OrderOpenPrice();
}
if(OrderSymbol()==Symbol() && OrderMagicNumber()==10002 && OrderType()==OP_SELL)
{
p=OrderOpenPrice()-Ask;
}
}
return(p/Point/mp);
}
void SetLabel(string name, int x, int y, string text, color col, string font, int fontsize)
{
ObjectDelete(name);
ObjectCreate(name,OBJ_LABEL, 0, 0, 0);
ObjectSet(name,OBJPROP_COLOR,col);
ObjectSet(name, OBJPROP_CORNER,Corner);
ObjectSet(name, OBJPROP_XDISTANCE, x);
ObjectSet(name, OBJPROP_YDISTANCE, y);
ObjectSetText(name,text, fontsize, font, col);
}
