Sie sind auf Seite 1von 31

double FindLastSellPrice()

{
double oldorderopenprice=0, orderprice;
int cnt, oldticketnumber=0, ticketnumber;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
continue;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber &&
OrderType()==OP_SELL)
{
ticketnumber=OrderTicket();
if(ticketnumber>oldticketnumber)
{
orderprice=OrderOpenPrice();
oldorderopenprice=orderprice;
oldticketnumber=ticketnumber;
}
}
}
return(orderprice);
}

double LastBuyLots;
double LastSellLots;
for(int cnt=OrdersTotal()-1;cnt>=0;cnt--){
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)continue;
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
if(OrderType()==OP_BUY)
{
LastBuyLots=OrderLots();
break;
}
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)

PDF "pdfFactory Pro" www.fineprint.cn

if(OrderType()==OP_SELL)
{
LastSellLots=OrderLots();
break;
}
}

,,

for(int i=1; i<=OrdersTotal(); i++)


{
if (OrderSelect(i-1,SELECT_BY_POS)==true)
{
Ticket=OrderTicket();
Tip =OrderType();
Price =OrderOpenPrice();
SL
=OrderStopLoss();
TP
=OrderTakeProfit();
Lot =OrderLots();
}
}

extern double Lots = 0.1;


extern int ChasStart = 10;
extern int ChasStop = 22;
extern int Step
= 15;
extern int TP
= 200;
if (TimeHour(TimeCurrent())==ChasStart && TimeMinute(TimeCurrent())==0)
{
OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+Step*Point,3,0,Ask+(Step+TP)*Point,"",12321,0,
Green);
OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-Step*Point,3,0,Bid-(Step+TP)*Point,"",12321,0,R
ed);
}

PDF "pdfFactory Pro" www.fineprint.cn

void closeshrts()
{
int trade;
int trades=OrdersTotal();
for(trade=0;trade<trades;trade++)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol())
continue;
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,0,Red);
}//for
}

bool Fun_New_Bar()
{
static datetime New_Time = 0;
bool New_Bar = false;
if (New_Time!= Time[0])
{
New_Time = Time[0];
New_Bar = true;
}
return(New_Bar);
}

bool isNewBar()
{ static datetime TimeBar=0;
bool flag=false;
if(TimeBar!=Time[0])
{
TimeBar=Time[0];
flag=true;
}
return (flag);
}

PDF "pdfFactory Pro" www.fineprint.cn

extern double Lots = 0.1;


extern int ChasStart = 10;
extern int ChasStop = 22;
extern int Step
= 15;
extern int TP
= 200;
if (TimeHour(TimeCurrent())==ChasStop && TimeMinute(TimeCurrent())==0)
{
i=0;
total = OrdersTotal();
for(i = 0; i <= total; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderMagicNumber() == 12321)
{
if (OrderType()==OP_BUY)OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
if (OrderType()==OP_SELL)OrderClose(OrderTicket(),OrderLots(),Ask,3,Green);
if (OrderType()>1)OrderDelete(OrderTicket());
}
}
}

int CloseEverything()
{
double myAsk;
double myBid;
int
myTkt;
double myLot;
int
myTyp;
int i;
bool result = false;
for(i=OrdersTotal();i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
myAsk=MarketInfo(OrderSymbol(),MODE_ASK);
myBid=MarketInfo(OrderSymbol(),MODE_BID);

PDF "pdfFactory Pro" www.fineprint.cn

myTkt=OrderTicket();
myLot=OrderLots();
myTyp=OrderType();
switch( myTyp )
{
//Close opened long positions
case OP_BUY
:result = OrderClose(myTkt, myLot, myBid, Slippage, Red);
break;
//Close opened short positions
case OP_SELL
:result = OrderClose(myTkt, myLot, myAsk, Slippage, Red);
break;
//Close pending orders
case OP_BUYLIMIT :
case OP_BUYSTOP :
case OP_SELLLIMIT:
case OP_SELLSTOP :result = OrderDelete( OrderTicket() );
}
if(result == false)
{
Alert("Order " , myTkt , " failed to close. Error:" , GetLastError() );
Print("Order " , myTkt , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
Sleep(1000);
} //for
} // closeeverything

void closelongs()
{
int trade;
int trades=OrdersTotal();
for(trade=0;trade<trades;trade++)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol())
continue;

PDF "pdfFactory Pro" www.fineprint.cn

if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,0,Blue);
}//for
}

extern int Magic = 20080829;


extern color clModify = Silver;
extern int TrailingStart = 30;
extern int TrailingSize = 30;
void TrailPositions()
{
int cnt = OrdersTotal();
for (int i=0; i<cnt; i++)
{
if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
if (OrderSymbol() != Symbol()) continue;
if (OrderMagicNumber() != Magic) continue;
int type = OrderType();
if (type == OP_BUY)
{
if (Bid-OrderOpenPrice() > TrailingStart*Point)
{
if (OrderStopLoss() < Bid - (TrailingSize+1)*Point)
{
OrderModify(OrderTicket(),
OrderOpenPrice(),
Bid-TrailingSize*Point,
OrderTakeProfit(), 0, clModify);
}
}
}
if (type == OP_SELL)
{
if (OrderOpenPrice()-Ask > TrailingStart*Point)
{
if (OrderStopLoss() > Ask + (TrailingSize+1)*Point || OrderStopLoss() == 0)
{
OrderModify(OrderTicket(),
OrderOpenPrice(),
Ask+TrailingSize*Point,
OrderTakeProfit(), 0, clModify);
}
}
}

PDF "pdfFactory Pro" www.fineprint.cn

}
}

int Save_Profit(){
if (AccountProfit()>= Profit_to_Close)
{
for(int i=OrdersTotal()-1;i>=0;i--)
{
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;
switch(type)
{
//Close opened long positions
case
OP_BUY
:
result
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Pink);
break;
//Close opened short positions
case
OP_SELL
:
result
OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,Pink);
}
if(result == false)
{
Sleep(3000);
}
else if (Cancel_Trading_On_Profit) cantrade=false;
}
Print ("Account Profit Reached. All Open Trades Have Been Closed");
return(0);
}

double FindLastBuyPrice() {

PDF "pdfFactory Pro" www.fineprint.cn

double l_ord_open_price_0;
int l_ticket_8;
double ld_unused_12 = 0;
int l_ticket_20 = 0;
for (int l_pos_24 = OrdersTotal() - 1; l_pos_24 >= 0; l_pos_24--) {
OrderSelect(l_pos_24, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber &&
OrderType() == OP_BUY) {
l_ticket_8 = OrderTicket();
if (l_ticket_8 > l_ticket_20) {
l_ord_open_price_0 = OrderOpenPrice();
ld_unused_12 = l_ord_open_price_0;
l_ticket_20 = l_ticket_8;
}
}
}
return (l_ord_open_price_0);
}

int DeleteSellPendingOrders(int Magic)


{
int total = OrdersTotal();
for (int cnt = total - 1; cnt >= 0; cnt--)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if
(OrderMagicNumber()
==
Magic
&&
OrderSymbol()==Symbol()
(OrderType()==OP_SELLSTOP || OrderType()==OP_SELLLIMIT))
{
OrderDelete(OrderTicket());
}
}
return(0);
}

OrderSend
if(ticket>0)

PDF "pdfFactory Pro" www.fineprint.cn

&&

{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);

Print("BUY

void CloseAllOrders(string a_symbol_0, int a_magic_8, int a_magic_12, int a_magic_16, bool
ai_20, int ai_24) {
int l_cmd_28 = 0;
int li_unused_32 = 0;
while (CountAll(a_symbol_0, a_magic_8, a_magic_12, a_magic_16, ai_20, ai_24) > 0) {
for (int l_pos_36 = OrdersTotal() - 1; l_pos_36 >= 0; l_pos_36--) {
OrderSelect(l_pos_36, SELECT_BY_POS, MODE_TRADES);
if (!ai_24 || OrderSymbol() == a_symbol_0) {
if (!ai_20) {
l_cmd_28 = OrderType();
li_unused_32 = 0;
switch (l_cmd_28) {
case OP_BUY:
Wait();
li_unused_32
=
OrderClose(OrderTicket(),
OrderLots(),
MarketInfo(OrderSymbol(), MODE_BID), 10, CLR_NONE);
break;
case OP_SELL:
Wait();
li_unused_32
=
OrderClose(OrderTicket(),
OrderLots(),
MarketInfo(OrderSymbol(), MODE_ASK), 10, CLR_NONE);
break;
case OP_BUYLIMIT:
case OP_BUYSTOP:
case OP_SELLLIMIT:
case OP_SELLSTOP:
Wait();
li_unused_32 = OrderDelete(OrderTicket());
}
} else {
if (a_magic_16 != 0) {
if (a_magic_16 == OrderMagicNumber()) {
l_cmd_28 = OrderType();
li_unused_32 = 0;

PDF "pdfFactory Pro" www.fineprint.cn

switch (l_cmd_28) {
case OP_BUY:
Wait();
li_unused_32
=
OrderClose(OrderTicket(),
OrderLots(),
MarketInfo(OrderSymbol(), MODE_BID), 10, CLR_NONE);
break;
case OP_SELL:
Wait();
li_unused_32
=
OrderClose(OrderTicket(),
OrderLots(),
MarketInfo(OrderSymbol(), MODE_ASK), 10, CLR_NONE);
break;
case OP_BUYLIMIT:
case OP_BUYSTOP:
case OP_SELLLIMIT:
case OP_SELLSTOP:
Wait();
li_unused_32 = OrderDelete(OrderTicket());
}
}
} else {
if (OrderMagicNumber() == a_magic_8 || OrderMagicNumber() ==
a_magic_12) {
l_cmd_28 = OrderType();
li_unused_32 = 0;
switch (l_cmd_28) {
case OP_BUY:
Wait();
li_unused_32
=
OrderClose(OrderTicket(),
OrderLots(),
MarketInfo(OrderSymbol(), MODE_BID), 10, CLR_NONE);
break;
case OP_SELL:
Wait();
li_unused_32
=
OrderClose(OrderTicket(),
OrderLots(),
MarketInfo(OrderSymbol(), MODE_ASK), 10, CLR_NONE);
break;
case OP_BUYLIMIT:
case OP_BUYSTOP:
case OP_SELLLIMIT:
case OP_SELLSTOP:
Wait();
li_unused_32 = OrderDelete(OrderTicket());
}
}
}

PDF "pdfFactory Pro" www.fineprint.cn

}
}
}
}
GlobalVariableDel(a_magic_8 + "CHAMELEON-START-TRAIL");
}
int CountAll(string a_symbol_0, int a_magic_8, int a_magic_12, int a_magic_16, bool ai_20, int
ai_24) {
int l_count_28 = 0;
for (int l_pos_32 = OrdersTotal() - 1; l_pos_32 >= 0; l_pos_32--) {
OrderSelect(l_pos_32, SELECT_BY_POS, MODE_TRADES);
if (!ai_24 || OrderSymbol() == a_symbol_0) {
if (!ai_20) l_count_28++;
else {
if (a_magic_16 != 0) {
if (a_magic_16 == OrderMagicNumber()) l_count_28++;
else
if (OrderMagicNumber() == a_magic_8 || OrderMagicNumber() ==
a_magic_12) l_count_28++;
}
}
}
}
return (l_count_28);
}

void closeshrts()
{
int trade;
int trades=OrdersTotal();
for(trade=0;trade<trades;trade++)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol())
continue;
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,0,Red);
}//for
}

PDF "pdfFactory Pro" www.fineprint.cn


for(i = 0; i < OrdersTotal(); i++)
{ // 1
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
// Trail Long / Buy
if ( (OrderType() == OP_BUY) && (OrderMagicNumber() == magic) )
{ // 2
if ( (OrderClosePrice() - OrderOpenPrice()) > (TrailingStop*Point) )
{ // 3
if ( OrderStopLoss() < (OrderClosePrice() - TrailingStop*Point) )
{ // 4
OrderModify(OrderTicket(),
OrderOpenPrice(),
OrderClosePrice() - TrailingStop*Point,
OrderTakeProfit(),
Orchid);
} // 4
} // 3
} // 2
// Trail Short / Sell
if ( (OrderType() == OP_SELL) && (OrderMagicNumber() == magic) )
{ // 2
if ( (OrderOpenPrice() - OrderClosePrice()) > (TrailingStop*Point) )
{ // 3
if ( (OrderStopLoss() > (OrderClosePrice() + TrailingStop*Point)) ||
(OrderStopLoss() == 0)
{ // 4
OrderModify(OrderTicket(),
OrderOpenPrice(),
OrderClosePrice() + TrailingStop*Point,
OrderTakeProfit(),
Orchid);
} // 4
} // 3
} // 2
} // 1
//+- End of trading control -----------------------------------------------------------------+
} // B if (UserAcceptsAllLiability == true)
return(0);
} // A init start()

PDF "pdfFactory Pro" www.fineprint.cn

3D7#pid8722

void CloseBuys(int MagicNumber, int Slippage)


{
for(int i = 0; i < OrdersTotal(); i++)
{
// already closed
if(OrderSelect(i, SELECT_BY_POS) == false) continue;
// not current symbol
if(OrderSymbol() != Symbol()) continue;
// order was opened in another way
if(OrderMagicNumber() != MagicNumber) continue;
if(OrderType() == OP_BUY)
{
if(OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Blue))
{
i--;
}
RefreshRates();
}
}
}

int LastBuyStop() {
for (int l_pos_0 = OrdersHistoryTotal() - 1; l_pos_0 >= 0; l_pos_0--) {
if
(OrderSelect(l_pos_0,
SELECT_BY_POS,
MODE_HISTORY)
OrderMagicNumber() == -2101841039 && OrderType() == OP_BUY) {
if (OrderTicket() == g_ticket_172) return (-1);
if (OrderTicket() > g_ticket_172) {
g_ticket_172 = OrderTicket();
return (g_ticket_172);
}
}
}
return (-1);
}

PDF "pdfFactory Pro" www.fineprint.cn

&&

int CheckForOpen()
{int Limit=30;
double L = Low[iLowest(NULL,0,MODE_LOW,PeriodX,0)];
double H = High[iHighest(NULL,0,MODE_HIGH,PeriodX,0)];
double Lots = MathRound(AccountBalance()/100)/1000;
if((H-Bid>Limit*Point))
{OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,0,"",MAGICMA,0,CLR_NONE);
for(int
i=1;
i<5;
i++){OrderSend(Symbol(),OP_BUYLIMIT,MathPow(2,i)*Lots,Ask-i*Grid*Point,1,0,0,"",MAGI
CMA,0,CLR_NONE);}
}
if((Bid-L>Limit*Point))
{OrderSend(Symbol(),OP_SELL,Lots,Bid,1,0,0,"",MAGICMA,0,CLR_NONE);
for(int
j=1;
j<5;
j++){OrderSend(Symbol(),OP_SELLLIMIT,MathPow(2,j)*Lots,Bid+j*Grid*Point,1,0,0,"",MAG
ICMA,0,CLR_NONE);}
}
}

int LastLongBuy() {
for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {
if (OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES) && OrderTicket() >
g_ticket_164) {
g_ticket_164 = OrderTicket();
return (g_ticket_164);
}
}
return (-1);

if (Hour() >= Order_Hour)


{
for(cnt=0;cnt<OrdersTotal();cnt++)
{
// check selection result because order may be closed or deleted at this time!
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol())//check for opened position and symbol
{

PDF "pdfFactory Pro" www.fineprint.cn

if (OrderType()==OP_SELLSTOP && OpenBuys>0) OrderDelete( OrderTicket());


if (OrderType()==OP_BUYSTOP && OpenSells>0) OrderDelete( OrderTicket());
if (Hour() >= CloseOrdersHour && Minute() >= 45 && OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
if (Hour() >= CloseOrdersHour && Minute() >= 45 && OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
}
}
}

int CalculateBuyOrders()//
{
int buys=0,sells=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderType()==OP_BUY
&&
OrderSymbol()==symbol
OrderMagicNumber()==iMagic) buys++;
}
return(buys);
}

&&

int CalculateSellOrders()//
{
int buys=0,sells=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderType()==OP_SELL
OrderMagicNumber()==iMagic) sells++;
}
return(sells);
}

&&

OrderSymbol()==symbol

total=OrdersTotal();
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position

PDF "pdfFactory Pro" www.fineprint.cn

&&

OrderSymbol()==Symbol()) // check for symbol


{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(total>0 && Hour()==22 && Minute()>45)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}
}
else // go to short position
{
// should it be closed?
if(total>0 && Hour()==22 && Minute()>45)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit
}
}
}
}

int LotSize()
// The function opens a short position with lot size=volume
{
if (AccountBalance()>=50) lot=0.02;
if (AccountBalance()>=100) lot=0.04;
if (AccountBalance()>=200) lot=0.08;
if (AccountBalance()>=300) lot=0.12;
if (AccountBalance()>=400) lot=0.16;
if (AccountBalance()>=500) lot=0.2;
if (AccountBalance()>=600) lot=0.24;
if (AccountBalance()>=700) lot=0.28;
if (AccountBalance()>=800) lot=0.32;
if (AccountBalance()>=900) lot=0.36;
if (AccountBalance()>=1000) lot=0.4;
if (AccountBalance()>=1500) lot=0.6;
if (AccountBalance()>=2000) lot=0.8;
if (AccountBalance()>=2500) lot=1.0;
if (AccountBalance()>=3000) lot=1.2;
if (AccountBalance()>=3500) lot=1.4;

PDF "pdfFactory Pro" www.fineprint.cn

if (AccountBalance()>=4000) lot=1.6;
if (AccountBalance()>=4500) lot=1.8;
if (AccountBalance()>=5000) lot=2.0;
if (AccountBalance()>=5500) lot=2.2;
if (AccountBalance()>=6000) lot=2.4;
if (AccountBalance()>=7000) lot=2.8;
if (AccountBalance()>=8000) lot=3.2;
if (AccountBalance()>=9000) lot=3.6;
if (AccountBalance()>=10000) lot=4.0;
if (AccountBalance()>=15000) lot=6.0;
if (AccountBalance()>=20000) lot=8.0;
if (AccountBalance()>=30000) lot=12;
if (AccountBalance()>=40000) lot=16;
if (AccountBalance()>=50000) lot=20;
if (AccountBalance()>=60000) lot=24;
if (AccountBalance()>=70000) lot=28;
if (AccountBalance()>=80000) lot=32;
if (AccountBalance()>=90000) lot=36;
if (AccountBalance()>=100000) lot=40;
if (AccountBalance()>=200000) lot=80;
}

int

totalorders=HistoryTotal();
for(int j=0; j<totalorders;j++)
{
if(OrderSelect(j,
SELECT_BY_POS,
MODE_HISTORY)
(OrderMagicNumber()==Magic))
{
if((OrderType()==OP_BUY) || (OrderType()==OP_SELL))
{
StrategyProfit+=OrderProfit();
}
}
}

int CheckOpenPositions()
{
int cnt, total, NumPositions;
int NumBuyTrades, NumSellTrades; // Number of buy and sell trades in this symbol
NumBuyTrades = 0;
NumSellTrades = 0;

PDF "pdfFactory Pro" www.fineprint.cn

&&

total=OrdersTotal();
for(cnt=0;cnt<TOTAL;CNT++)
{
OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol() != Symbol()) continue;
if ( OrderMagicNumber() != MagicNumber) continue;
if(OrderType() == OP_BUY ) NumBuyTrades++;
if(OrderType() == OP_SELL ) NumSellTrades++;
}
NumPositions = NumBuyTrades + NumSellTrades;
return (NumPositions);
}

int subTotalTrade()
{
int
cnt,
total=0;
//
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol() &&
OrderMagicNumber()==MagicNumber) total++;
}
return(total);
}

double subTotalProfit()
{
int
cnt,
total = 0;
double Profit = 0;
for(cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);

PDF "pdfFactory Pro" www.fineprint.cn

if(OrderType()<=OP_SELL&&OrderSymbol()==Symbol()&&
(OrderMagicNumber()==MagicNumber))Profit = Profit +OrderProfit();
}
return(Profit);
}

bool CheckAddPos () {
//return true if we can enter new position, or adding another one
int _num=0;
int _opentime=0;
bool _return=false;
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i,SELECT_BY_POS)==true && OrderSymbol()==Symbol() && OrderType()<3
&& ((OrderMagicNumber()==MagicNumber) || MagicNumber==0)) {
_num++;
if (OrderOpenTime()>_opentime) _opentime=OrderOpenTime();
}
}
if (_num==0) return(true);
if (_num>0 && ((Time[0]-_opentime))>0) return(true);
else return(false);
}

int T=0;
int B=0;
for(int i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderComment()==Name_Expert)
{
int type=OrderType();
switch(type)
{
case OP_BUY:
sB=1;
T++;
LastB=OrderOpenPrice();
break;

PDF "pdfFactory Pro" www.fineprint.cn

case OP_SELL:
sS=2;
B++;
LastS=OrderOpenPrice();
break;
}
}
}

for(int j=0; j<totalorders;j++)


{
if(OrderSelect(j,
SELECT_BY_POS,
MODE_HISTORY)
(OrderMagicNumber()==Magic))
{
if((OrderType()==OP_BUY) || (OrderType()==OP_SELL))
{
StrategyOrders++;
StrategyProfit+=OrderProfit();
}
}
}

int cnt1=0;
int TradesOnSymbol=0;
for(cnt1=0;cnt1<OrdersTotal();cnt1++)
{
OrderSelect(cnt1, SELECT_BY_POS, MODE_TRADES);
if ((OrderType()==OP_SELLSTOP || OrderType()==OP_BUYSTOP ||
OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYLIMIT ||
OrderType()==OP_BUY || OrderType()==OP_SELL) &&
OrderSymbol()==Symbol()) { TradesOnSymbol=TradesOnSymbol+1;}
}

int CalculateCurrentOrders(string mySymbol, int MagicNumber)


{
int buys = 0, sells = 0, num = 0;
for(int i=0;i<OrdersTotal();i++)
{

PDF "pdfFactory Pro" www.fineprint.cn

&&

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!= mySymbol) continue;
if (OrderMagicNumber()!= MagicNumber) continue;
if(OrderType() == OP_BUY) buys++;
if(OrderType() == OP_SELL) sells++;
if(OrderType() == OP_BUYSTOP) buys++;
if(OrderType() == OP_SELLSTOP) sells++;
}
num = buys + sells;
return(num);
}

void CloseSells(int MagicNumber, int Slippage)


{
for(int i = 0; i < OrdersTotal(); i++)
{
// already closed
if(OrderSelect(i, SELECT_BY_POS) == false) continue;
// not current symbol
if(OrderSymbol() != Symbol()) continue;
// order was opened in another way
if(OrderMagicNumber() != MagicNumber) continue;
if(OrderType() == OP_SELL)
{
if (OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red))
{
i--;
}
RefreshRates();
}

int GetOrdersCount(int MagicNumber, int Type)


{
int count = 0;
for(int i = 0; i < OrdersTotal(); i++)
{
// already closed

PDF "pdfFactory Pro" www.fineprint.cn

if(OrderSelect(i, SELECT_BY_POS) == false) continue;


// not current symbol
if(OrderSymbol() != Symbol()) continue;
// order was opened in another way
if(OrderMagicNumber() != MagicNumber) continue;
if(OrderType() == Type)
{
count++;
}
}
return (count);
}

int CountLongs(string a_symbol_0, int a_magic_8) {


int l_count_12 = 0;
for (int l_pos_16 = OrdersTotal() - 1; l_pos_16 >= 0; l_pos_16--) {
OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == a_symbol_0) {
if (OrderMagicNumber() == a_magic_8)
if (OrderType() == OP_BUY) l_count_12++;
}
}
return (l_count_12);
}

int CountShorts(string a_symbol_0, int a_magic_8) {


int l_count_12 = 0;
for (int l_pos_16 = OrdersTotal() - 1; l_pos_16 >= 0; l_pos_16--) {
OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == a_symbol_0) {
if (OrderMagicNumber() == a_magic_8)
if (OrderType() == OP_SELL) l_count_12++;
}
}
return (l_count_12);
}

PDF "pdfFactory Pro" www.fineprint.cn

int CountAll(string a_symbol_0, int a_magic_8, int a_magic_12, int a_magic_16, bool ai_20, int
ai_24) {
int l_count_28 = 0;
for (int l_pos_32 = OrdersTotal() - 1; l_pos_32 >= 0; l_pos_32--) {
OrderSelect(l_pos_32, SELECT_BY_POS, MODE_TRADES);
if (!ai_24 || OrderSymbol() == a_symbol_0) {
if (!ai_20) l_count_28++;
else {
if (a_magic_16 != 0) {
if (a_magic_16 == OrderMagicNumber()) l_count_28++;
else
if (OrderMagicNumber() == a_magic_8 || OrderMagicNumber() ==
a_magic_12) l_count_28++;
}
}
}
}
return (l_count_28);
}

int IsTradeTime() {
int li_8;
if (DayOfWeek() < StartingTradeDay || DayOfWeek() > EndingTradeDay) return (0);
int li_0 = 60 * TimeHour(TimeCurrent()) + TimeMinute(TimeCurrent());
int li_4 = 60 * StartHour + StartMinute;
li_8 = 60 * StopHour + li_8;
if (li_4 == li_8) return (1);
if (li_4 < li_8) {
if (!(li_0 >= li_4 && li_0 < li_8)) return (0);
return (1);
}
if (li_4 > li_8) {
if (!(li_0 >= li_4 || li_0 < li_8)) return (0);
return (1);
}
return (0);
}
,,
OrdersPerSymbol = 0;

PDF "pdfFactory Pro" www.fineprint.cn

for(cnt = OrdersTotal(); cnt >= 0; cnt--)


{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
OrdersPerSymbol++;
if(OrderType() == OP_BUY)
{
OrdersBUY++;
}
if(OrderType() == OP_SELL)
{
OrdersSELL++;
}
}
}

int CalculateBuyOrders(string myType)//


{
int cnt = 0;
int total = OrdersTotal();//
for(int i = 0;i<total;i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) ==false) continue;//
if
(OrderType()==OP_BUY)//
)
cnt++;
}
return(cnt);
}

int CalculateSellOrders(string myType)


{
int cnt = 0;
int total = OrdersTotal();
for(int i = 0;i<total;i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) ==false) continue;
if
(OrderType()==OP_SELL)
)

PDF "pdfFactory Pro" www.fineprint.cn

cnt++;
}
return(cnt);
}

bool CloseOrder(int Ticket, double Lots, int myOrderType,string myOrderSymbol)


{
bool res;
int ErrorCode;
double Price;
bool Status = false;
if(myOrderType == OP_BUYLIMIT || myOrderType == OP_BUYSTOP ||
myOrderType == OP_SELLLIMIT || myOrderType == OP_SELLSTOP)
{
OrderDelete(OrderTicket());
return(true);
}
if (myOrderType == OP_BUY) Price = MarketInfo(myOrderSymbol,MODE_BID);
if (myOrderType == OP_SELL) Price = MarketInfo(myOrderSymbol,MODE_ASK);
if (MarketInfo(myOrderSymbol,MODE_DIGITS) > 0)
Price = NormalizeDouble(Price, MarketInfo(myOrderSymbol,MODE_DIGITS));
int cnt = 0;
while (!Status) {
if (IsTradeAllowed() == true) {
res = OrderClose(Ticket, Lots, Price, MarketInfo(myOrderSymbol,MODE_SPREAD),
Violet);
ErrorCode = GetLastError();
} else cnt++;
if (res == true) Status = true;
ErrorCode = GetLastError();
switch (ErrorCode) {
case 0/* NO_ERROR */:
Status = true;
break;
case 4/* SERVER_BUSY */:
case 6/* NO_CONNECTION */:
case 129/* INVALID_PRICE */:
case 136/* OFF_QUOTES */:
case 137/* BROKER_BUSY */:

PDF "pdfFactory Pro" www.fineprint.cn

case 146/* TRADE_CONTEXT_BUSY */:


case 128/* TRADE_TIMEOUT */:
RefreshRates();
cnt++;
break;
case 135/* PRICE_CHANGED */:
case 138/* REQUOTE */:
RefreshRates();
continue;
default:
Status = TRUE;
}
if (cnt > 20) Status = TRUE; //10 time try
if (!Status)
{
Sleep(500);
RefreshRates();
}
}
if (res == true || ErrorCode == 0/* NO_ERROR */) return (true);
Print(" Error closing order : (", ErrorCode, ") " + ErrorDescription(ErrorCode));
return (false);
}

void TrailingStairs(int ticket,int trldistance)


{
int Spred=Ask - Bid;
if (OrderType()==OP_BUY)
{
if((Bid-OrderOpenPrice())>(Point*trldistance))
{
if(OrderStopLoss()<Bid-Point*trldistance || (OrderStopLoss()==0))
{
OrderModify(ticket,OrderOpenPrice(),Bid-Point*trldistance,OrderTakeProfit(),0,Green);
if (PolLots)
if
(NormalizeDouble(OrderLots()/2,2)>MarketInfo(Symbol(),
MODE_MINLOT))
{
OrderClose(ticket,NormalizeDouble(OrderLots()/2,2),Ask,3,Green);
}

PDF "pdfFactory Pro" www.fineprint.cn

else
{
OrderClose(ticket,OrderLots(),Ask,3,Green);
}
}
}
}
else
{
if((OrderOpenPrice()-Ask)>(Point*trldistance))
{
if((OrderStopLoss()>(Ask+Point*trldistance)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*trldistance,OrderTakeProfit(),0,Red);
if (PolLots)
if
(NormalizeDouble(OrderLots()/2,2)>MarketInfo(Symbol(),
MODE_MINLOT))
{
OrderClose(ticket,NormalizeDouble(OrderLots()/2,2),Bid,3,Green);
}
else
{
OrderClose(ticket,OrderLots(),Bid,3,Green);
}
}
}
}
}

for(int l_pos_76 = OrdersTotal() - 1; l_pos_76 >= 0; l_pos_76--)


{
OrderSelect(l_pos_76, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
// check for trailing stop
{

PDF "pdfFactory Pro" www.fineprint.cn

OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-200*Point,0,0,Green);
return(0);}

}
}
}

double GetOrderLot(){
for (int i=0; i<OrdersTotal(); i++){
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){double lot = OrderLots();}}
if(lot==0){lot=Lots;}else{lot=lot+Lots;}
return(lot);}

int CheckOpenBUYPositions()
{
int cnt, NumPositions;
int NumBuyTrades, NumSellTrades; // Number of buy and sell trades in this symbol
NumBuyTrades = 0;
NumSellTrades = 0;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol() != Symbol()) continue;
if ( OrderMagicNumber() != QQ) continue;
if(OrderType() == OP_BUY &&OrderProfit() < 0)
{
NumBuyTrades++;
}
return (NumBuyTrades);
}
}

PDF "pdfFactory Pro" www.fineprint.cn


extern double =0.1;
extern double =30;
extern double =90;
extern double =3;
extern double TrailingStop=20; // 20
int start()
{ int i;int ticket1;
//+---------- --------------------------------------------------------+
if(
OrdersTotal()==0
&& Close[1]-Open[1]>1*Point
//+------------------------------------------------------------------+
)
{ticket1=OrderSend(Symbol(),OP_BUY, ,Ask, ,Ask-
*Point,Ask+*Point,"",20101018,0,0);
}
if(
OrdersTotal()==0
&& Open[1]-Close[1]>1*Point
//+------------------------------------------------------------------+
){ticket1=OrderSend(Symbol(),OP_SELL, ,Bid, ,Bid+
*Point,Bid-*Point,"",20101018,0,0);
}
//+--------- 20 -----------------+

int total=OrdersTotal();
for(int cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Green);

PDF "pdfFactory Pro" www.fineprint.cn

return(0);
}
}
}
}
else // go to short position
{
// should it be closed?
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
{
OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}

//+------------------------------------------------------------------+
return(0);}

if((TimeHour(CurTime()) < StartHour) || (TimeHour(CurTime()) >= EndHour) ||


(DayOfWeek() == KillDay && TimeHour(CurTime()) == KillHour))
{
/*Comment("Outside Trading Hours, Exiting...",
"\n", "Version 0.0.4",
"\n", Symbol(),
"\n", "sig:", MACD_Signal, " main:", MACD_Main);*/
total = OrdersTotal();
//---for(cnt = 0; cnt < total; cnt++)

PDF "pdfFactory Pro" www.fineprint.cn

{// 3
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
//---if(OrderSymbol() == Symbol())
{ //4
if(OrderType() == OP_BUY)
{ //5
OrderClose(OrderTicket(),Lots,Bid,Slippage,Green);
return(0);
} //5
//---if(OrderType() == OP_SELL)
{ //5
OrderClose(OrderTicket(), Lots, Ask, Slippage, Green);
return(0);
} //5
} //4
} //3
return(0);
}

PDF "pdfFactory Pro" www.fineprint.cn

Das könnte Ihnen auch gefallen