海龟交易系统 MT4 源码
//±-----------------------------------------------------------------+
//| GoHan Turtle Trader.mq4 |
//| Copyright ?2008, gohanforex.com |
//| http://www.gohanforex.com |
//±-----------------------------------------------------------------+
#property copyright “Copyright ?2008, gohanforex.com”
#property link “http://www.zhongjie110.com” extern color LabelColor=White;
extern int LabelFontSize=8;
extern color S1LongEntryLineColor=Blue;
extern color S2LongEntryLineColor=LightBlue;
extern color S1andS2LongEntryLineColor=Lime;
extern color S1ExitLinesColor=Yellow;
extern color S1ShortEntryLineColor=Red;
extern color S2ShortEntryLineColor=Pink;
extern color S1andS2ShortEntryLineColor=Orange; int MaxCommentsToShow=25;
string allcomments[];
datetime lastcheck;
bool periodOK=false;
int MagicNumber=12345; int S1Periods=20;
int S2Periods=55;
double S1LongEntry=0;
double S1ShortEntry=0;
double S2LongEntry=0;
double S2ShortEntry=0; double S1LongExit=0;
double S1ShortExit=0;
double S2LongExit=0;
double S2ShortExit=0; int S1LongEntryBar=0;
int S1ShortEntryBar=0;
int S2LongEntryBar=0;
int S2ShortEntryBar=0; int S1LongExitBar=0;
int S1ShortExitBar=0; double HighPriceArray[];
double LowPriceArray[]; double CurrentATR;
double UnitSizing;
double TradeRisk=0.01;
int ATRTakeProfitMultiplier=2;
int ATRStopLossMultiplier=2; //±-----------------------------------------------------------------+
//| expert initialization function |
//±-----------------------------------------------------------------+
int init()
{
manageupperstatus(“GoHan Turtles Trader - gohanforex.com, CR 2009”);
if(Period()==1440)
{
periodOK=true;
managelowerstatus(“http://www.gohanforex.com”);
managecomments(“Turtle Trading Started”);
CalcTurtleValues();
drawentries(S1LongEntry, S2LongEntry, S1ShortEntry, S2ShortEntry, S1LongExit, S1ShortExit);
}
else
{
periodOK=false;
managelowerstatus(“ERROR: This scanner must be used on an D1 chart”);
managecomments(“MUST USE D1 TIMEFRAME - WILL NOT SCAN”);
deinit();
} return(0);
}
//±-----------------------------------------------------------------+
//| expert deinitialization function |
//±-----------------------------------------------------------------+
int deinit()
{
return(0);
}
//±-----------------------------------------------------------------+
//| expert start function |
//±-----------------------------------------------------------------+
int start()
{
if(periodOK==false) return(0);
//Check for a new bar
if(lastcheck<Time[0])
{
//A new bar has formed and new values need to be computed
string checktime=TimeToStr(TimeLocal(),TIME_DATE);
lastcheck=Time[0];
CalcTurtleValues();
drawentries(S1LongEntry, S2LongEntry, S1ShortEntry, S2ShortEntry, S1LongExit, S1ShortExit);
managecomments(checktime +": New values calculated");
} //managecomments(MarketInfo(Symbol(),MODE_TICKVALUE)); //Always do the trade management functions
//Check for orders to close
//Close positions based on the 10-day number
if(Bid<S1LongExit)
{
//Close long positions
CloseS1LongPositions();
ClosePendingLongPositions();
} if(Ask>S1ShortExit)
{
//Close short positions
CloseS1ShortPositions();
ClosePendingShortPositions();
} //Check for S2 long entry
if((S2LongEntry<Ask)&&(CountLongPositions()==0))
{
//Need new long positions
CurrentATR=iATR(Symbol(),PERIOD_D1,S1Periods,1);
UnitSizing=(AccountBalance()*TradeRisk)/(CurrentATR/Point)/MarketInfo(Symbol(),MODE_TICKVALUE);
UnitSizing=NormalizeDouble(UnitSizing,2);
//Close the short positions
CloseS1ShortPositions();
CloseS2ShortPositions();
ClosePendingShortPositions();
ClosePendingLongPositions();
//Need a new long position
OpenS2LongPositions();
} //Check for S1 long entry
if((S1LongEntry<Ask)&&(CountLongPositions()==0))
{
//Need new long positions
CurrentATR=iATR(Symbol(),PERIOD_D1,S1Periods,1);
UnitSizing=(AccountBalance()*TradeRisk)/(CurrentATR/Point)/MarketInfo(Symbol(),MODE_TICKVALUE);
UnitSizing=NormalizeDouble(UnitSizing,2);
//Close the short positions
CloseS1ShortPositions();
CloseS2ShortPositions();
ClosePendingShortPositions();
ClosePendingLongPositions();
//Need a new long position
OpenS1LongPositions();
} //Check for S2 short entry
if((S2ShortEntry>Bid)&&(CountShortPositions()==0))
{
//Need new short positions
CurrentATR=iATR(Symbol(),PERIOD_D1,S1Periods,1);
UnitSizing=(AccountBalance()*TradeRisk)/(CurrentATR/Point)/MarketInfo(Symbol(),MODE_TICKVALUE);
UnitSizing=NormalizeDouble(UnitSizing,2);
//Close the long positions
CloseS1LongPositions();
CloseS2LongPositions();
ClosePendingLongPositions();
ClosePendingShortPositions();
//Need a new short position
OpenS2ShortPositions();
} //Check for S1 short entry
if((S1ShortEntry>Bid)&&(CountShortPositions()==0))
{
//Need new short positions
CurrentATR=iATR(Symbol(),PERIOD_D1,S1Periods,1);
UnitSizing=(AccountBalance()*TradeRisk)/(CurrentATR/Point)/MarketInfo(Symbol(),MODE_TICKVALUE);
UnitSizing=NormalizeDouble(UnitSizing,2);
//Close the long positions
CloseS1LongPositions();
CloseS2LongPositions();
ClosePendingLongPositions();
ClosePendingShortPositions();
//Need a new short position
OpenS1ShortPositions();
} if(OrdersTotal()>0)
{
//Standardize the stops
StandardStopsForShorts();
StandardStopsForLongs();
}
return(0);
}
//±-----------------------------------------------------------------+ void CalcTurtleValues()
{
//Prepare and fill the two arrays
int pricecounter=0;
ArrayResize(HighPriceArray,S2Periods);
ArrayResize(LowPriceArray,S2Periods);
for(pricecounter=0;pricecounter<S2Periods;pricecounter++)
{
HighPriceArray[pricecounter]=High[pricecounter+1];
LowPriceArray[pricecounter]=Low[pricecounter+1];
} S1LongEntryBar=ArrayMaximum(HighPriceArray,S1Periods,0);
S1ShortEntryBar=ArrayMinimum(LowPriceArray,S1Periods,0);
S2LongEntryBar=ArrayMaximum(HighPriceArray,WHOLE_ARRAY,0);
S2ShortEntryBar=ArrayMinimum(LowPriceArray,WHOLE_ARRAY,0); S1LongExitBar=ArrayMinimum(LowPriceArray,10,0);
S1ShortExitBar=ArrayMaximum(HighPriceArray,10,0); S1LongEntry=HighPriceArray[S1LongEntryBar];
S1ShortEntry=LowPriceArray[S1ShortEntryBar];
S2LongEntry=HighPriceArray[S2LongEntryBar];
S2ShortEntry=LowPriceArray[S2ShortEntryBar]; S1LongExit=LowPriceArray[S1LongExitBar];
S1ShortExit=HighPriceArray[S1ShortExitBar];
S2LongExit=S1ShortEntry;
S2ShortExit=S1LongEntry;
} //±-----------------------------------------------------------------+
//| Manage comments |
//±-----------------------------------------------------------------+
void managecomments(string addcomment)
{
string basemessage = “Current Turtle Order Entry Informationn”;
string tempcomments[];
int commentscroll;
string output = basemessage;
int CommentCount = ArrayRange(allcomments, 0);
if(CommentCount<MaxCommentsToShow)
{
ArrayResize(tempcomments,CommentCount+1);
ArrayCopy(tempcomments,allcomments,1,0,WHOLE_ARRAY);
}
else
{
ArrayResize(tempcomments,MaxCommentsToShow);
ArrayCopy(tempcomments,allcomments,1,0,MaxCommentsToShow-1);
}
tempcomments[0]=addcomment;
CommentCount = ArrayRange(tempcomments, 0);
ArrayResize(allcomments,CommentCount);
ArrayCopy(allcomments,tempcomments,0,0,CommentCount); for(commentscroll=0;commentscroll<CommentCount;commentscroll++)
{
output = output + allcomments[commentscroll] +“n”;
}
Comment(output);
} void drawentries(double S1Long, double S2Long, double S1Short, double S2Short, double S1LongExit, double S1ShortExit)
{
ObjectDelete(“S1Long_Entry”);
ObjectDelete(“S1Long_EntrySign”);
ObjectDelete(“S2Long_Entry”);
ObjectDelete(“S2Long_EntrySign”); ObjectDelete(“S2Short_EntrySign”);
ObjectDelete(“S2Short_Entry”);
ObjectDelete(“S1Short_EntrySign”);
ObjectDelete(“S1Short_Entry”); ObjectDelete(“S1Long_ExitSign”);
ObjectDelete(“S1Long_Exit”); ObjectDelete(“S1Short_ExitSign”);
ObjectDelete(“S1Short_Exit”); if(S1LongExit!=S1Short)
{
ObjectCreate(“S1Long_Exit”, OBJ_HLINE, 0, 0, S1LongExit);// Creating obj.
ObjectSet(“S1Long_Exit”,OBJPROP_COLOR,S1ExitLinesColor);
ObjectCreate(“S1Long_ExitSign”, OBJ_TEXT, 0, Time[0], S1LongExit);// Creating obj.
ObjectSetText(“S1Long_ExitSign”, "S1 Long Exit: "+DoubleToStr(S1LongExit,Digits), LabelFontSize, “Verdana”, LabelColor);
} if(S1ShortExit!=S1Long)
{
ObjectCreate(“S1Short_Exit”, OBJ_HLINE, 0, 0, S1ShortExit);// Creating obj.
ObjectSet(“S1Short_Exit”,OBJPROP_COLOR,S1ExitLinesColor);
ObjectCreate(“S1Short_ExitSign”, OBJ_TEXT, 0, Time[0], S1ShortExit);// Creating obj.
ObjectSetText(“S1Short_ExitSign”, "S1 Short Exit: "+DoubleToStr(S1ShortExit,Digits), LabelFontSize, “Verdana”, LabelColor);
} if(S1Long==S2Long)
{
ObjectCreate(“S1Long_Entry”, OBJ_HLINE, 0, 0, S1Long);// Creating obj.
ObjectSet(“S1Long_Entry”,OBJPROP_COLOR,S1andS2LongEntryLineColor);
ObjectCreate(“S1Long_EntrySign”, OBJ_TEXT, 0, Time[0], S1Long);// Creating obj.
ObjectSetText(“S1Long_EntrySign”, "S1 & S2: "+DoubleToStr(S1Long,Digits), LabelFontSize, “Verdana”, LabelColor);
}
else
{
ObjectCreate(“S1Long_Entry”, OBJ_HLINE, 0, 0, S1Long);// Creating obj.
ObjectSet(“S1Long_Entry”,OBJPROP_COLOR,S1LongEntryLineColor); ObjectCreate("S1Long_EntrySign", OBJ_TEXT, 0, Time[0], S1Long);// Creating obj. ObjectSetText("S1Long_EntrySign", "S1: "+DoubleToStr(S1Long,Digits), LabelFontSize, "Verdana", LabelColor); ObjectCreate("S2Long_Entry", OBJ_HLINE, 0, 0, S2Long);// Creating obj. ObjectSet("S2Long_Entry",OBJPROP_COLOR,S2LongEntryLineColor); ObjectCreate("S2Long_EntrySign", OBJ_TEXT, 0, Time[0], S2Long);// Creating obj. ObjectSetText("S2Long_EntrySign", "S2: "+DoubleToStr(S2Long,Digits), LabelFontSize, "Verdana", LabelColor); }//====== if(S1Short==S2Short)
{
ObjectCreate(“S1Short_Entry”, OBJ_HLINE, 0, 0, S1Short);// Creating obj.
ObjectSet(“S1Short_Entry”,OBJPROP_COLOR,S1andS2ShortEntryLineColor); ObjectCreate("S1Short_EntrySign", OBJ_TEXT, 0, Time[0], S1Short);// Creating obj. ObjectSetText("S1Short_EntrySign", "S1 & S2: "+DoubleToStr(S1Short,Digits), LabelFontSize, "Verdana", LabelColor); }else
{
ObjectCreate(“S1Short_Entry”, OBJ_HLINE, 0, 0, S1Short);// Creating obj.
ObjectSet(“S1Short_Entry”,OBJPROP_COLOR,S1ShortEntryLineColor); ObjectCreate("S1Short_EntrySign", OBJ_TEXT, 0, Time[0], S1Short);// Creating obj. ObjectSetText("S1Short_EntrySign", "S1: "+DoubleToStr(S1Short,Digits), LabelFontSize, "Verdana", LabelColor); ObjectCreate("S2Short_Entry", OBJ_HLINE, 0, 0, S2Short);// Creating obj. ObjectSet("S2Short_Entry",OBJPROP_COLOR,S2ShortEntryLineColor); ObjectCreate("S2Short_EntrySign", OBJ_TEXT, 0, Time[0], S2Short);// Creating obj. ObjectSetText("S2Short_EntrySign", "S2: "+DoubleToStr(S2Short,Digits), LabelFontSize, "Verdana", LabelColor); }} //±-----------------------------------------------------------------+
//| Manage upper status |
//±-----------------------------------------------------------------+
void manageupperstatus(string addstatus)
{
ObjectDelete(“Upper_Status”);
if(addstatus!="")
{
ObjectCreate(“Upper_Status”, OBJ_LABEL, 0, 0, 0);// Creating obj.
ObjectSet(“Upper_Status”, OBJPROP_CORNER,1); // Reference corner
ObjectSet(“Upper_Status”, OBJPROP_XDISTANCE, 2);// X coordinate
ObjectSet(“Upper_Status”, OBJPROP_YDISTANCE, 13);// Y coordinate
ObjectSetText(“Upper_Status”, addstatus, 9, “Verdana”, White);
}
} //±-----------------------------------------------------------------+
//| Manage lower status |
//±-----------------------------------------------------------------+
void managelowerstatus(string addstatus)
{
ObjectDelete(“Lower_Status”);
if(addstatus!="")
{
ObjectCreate(“Lower_Status”, OBJ_LABEL, 0, 0, 0);// Creating obj.
ObjectSet(“Lower_Status”, OBJPROP_CORNER,3); // Reference corner
ObjectSet(“Lower_Status”, OBJPROP_XDISTANCE, 2);// X coordinate
ObjectSet(“Lower_Status”, OBJPROP_YDISTANCE, 1);// Y coordinate
ObjectSetText(“Lower_Status”, addstatus, 9, “Verdana”, Yellow);
}
} void OpenS1LongPositions()
{
double order1price;
double order2price;
double order3price; OrderSend(Symbol(),OP_BUY,UnitSizing,Ask,0,LongStopLoss(Ask,CurrentATR),LongTakeProfit(Ask,CurrentATR),“S1”,MagicNumber,0,Blue);
order1price=Ask+(CurrentATR/2);
order2price=Ask+CurrentATR;
order3price=order2price+(CurrentATR/2); order1price=NormalizeDouble(order1price,Digits);
order2price=NormalizeDouble(order2price,Digits);
order3price=NormalizeDouble(order3price,Digits); OrderSend(Symbol(),OP_BUYSTOP,UnitSizing,order1price,0,LongStopLoss(order1price,CurrentATR),LongTakeProfit(order1price,CurrentATR),“S1”,MagicNumber,0,Blue);
OrderSend(Symbol(),OP_BUYSTOP,UnitSizing,order2price,0,LongStopLoss(order2price,CurrentATR),LongTakeProfit(order2price,CurrentATR),“S1”,MagicNumber,0,Blue);
OrderSend(Symbol(),OP_BUYSTOP,UnitSizing,order3price,0,LongStopLoss(order3price,CurrentATR),LongTakeProfit(order3price,CurrentATR),“S1”,MagicNumber,0,Blue);
} void OpenS2LongPositions()
{
double order1price;
double order2price;
double order3price; OrderSend(Symbol(),OP_BUY,UnitSizing,Ask,0,LongStopLoss(Ask,CurrentATR),LongTakeProfit(Ask,CurrentATR),“S2”,MagicNumber,0,Blue);
order1price=Ask+(CurrentATR/2);
order2price=Ask+CurrentATR;
order3price=order2price+(CurrentATR/2); order1price=NormalizeDouble(order1price,Digits);
order2price=NormalizeDouble(order2price,Digits);
order3price=NormalizeDouble(order3price,Digits); OrderSend(Symbol(),OP_BUYSTOP,UnitSizing,order1price,0,LongStopLoss(order1price,CurrentATR),LongTakeProfit(order1price,CurrentATR),“S2”,MagicNumber,0,Blue);
OrderSend(Symbol(),OP_BUYSTOP,UnitSizing,order2price,0,LongStopLoss(order2price,CurrentATR),LongTakeProfit(order2price,CurrentATR),“S2”,MagicNumber,0,Blue);
OrderSend(Symbol(),OP_BUYSTOP,UnitSizing,order3price,0,LongStopLoss(order3price,CurrentATR),LongTakeProfit(order3price,CurrentATR),“S2”,MagicNumber,0,Blue);
} void OpenS1ShortPositions()
{
double order1price;
double order2price;
double order3price; OrderSend(Symbol(),OP_SELL,UnitSizing,Bid,0,ShortStopLoss(Bid,CurrentATR),ShortTakeProfit(Bid,CurrentATR),“S1”,MagicNumber,0,Red);
order1price=Bid-(CurrentATR/2);
order2price=Bid-CurrentATR;
order3price=order2price-(CurrentATR/2); order1price=NormalizeDouble(order1price,Digits);
order2price=NormalizeDouble(order2price,Digits);
order3price=NormalizeDouble(order3price,Digits); OrderSend(Symbol(),OP_SELLSTOP,UnitSizing,order1price,0,ShortStopLoss(order1price,CurrentATR),ShortTakeProfit(order1price,CurrentATR),“S1”,MagicNumber,0,Red);
OrderSend(Symbol(),OP_SELLSTOP,UnitSizing,order2price,0,ShortStopLoss(order2price,CurrentATR),ShortTakeProfit(order2price,CurrentATR),“S1”,MagicNumber,0,Red);
OrderSend(Symbol(),OP_SELLSTOP,UnitSizing,order3price,0,ShortStopLoss(order3price,CurrentATR),ShortTakeProfit(order3price,CurrentATR),“S1”,MagicNumber,0,Red);
} void OpenS2ShortPositions()
{
double order1price;
double order2price;
double order3price; OrderSend(Symbol(),OP_SELL,UnitSizing,Bid,0,ShortStopLoss(Bid,CurrentATR),ShortTakeProfit(Bid,CurrentATR),“S2”,MagicNumber,0,Red);
order1price=Bid-(CurrentATR/2);
order2price=Bid-CurrentATR;
order3price=order2price-(CurrentATR/2); order1price=NormalizeDouble(order1price,Digits);
order2price=NormalizeDouble(order2price,Digits);
order3price=NormalizeDouble(order3price,Digits); OrderSend(Symbol(),OP_SELLSTOP,UnitSizing,order1price,0,ShortStopLoss(order1price,CurrentATR),ShortTakeProfit(order1price,CurrentATR),“S2”,MagicNumber,0,Red);
OrderSend(Symbol(),OP_SELLSTOP,UnitSizing,order2price,0,ShortStopLoss(order2price,CurrentATR),ShortTakeProfit(order2price,CurrentATR),“S2”,MagicNumber,0,Red);
OrderSend(Symbol(),OP_SELLSTOP,UnitSizing,order3price,0,ShortStopLoss(order3price,CurrentATR),ShortTakeProfit(order3price,CurrentATR),“S2”,MagicNumber,0,Red);
} double LongStopLoss(double StartPrice, double CurrentATR)
{
double result;
result=StartPrice-(CurrentATR*ATRStopLossMultiplier);
result=NormalizeDouble(result,Digits);
return(result);
} double LongTakeProfit(double StartPrice, double CurrentATR)
{
double result;
result=StartPrice+(CurrentATR*ATRTakeProfitMultiplier);
result=NormalizeDouble(result,Digits);
return(result);
} double ShortStopLoss(double StartPrice, double CurrentATR)
{
double result;
result=StartPrice+(CurrentATR*ATRStopLossMultiplier);
result=NormalizeDouble(result,Digits);
return(result);
} double ShortTakeProfit(double StartPrice, double CurrentATR)
{
double result;
result=StartPrice-(CurrentATR*ATRTakeProfitMultiplier);
result=NormalizeDouble(result,Digits);
return(result);
} void CloseS1LongPositions()
{
int totalorders=OrdersTotal();
int orderscroll;
for(orderscroll=0;orderscroll<totalorders;orderscroll++)
{
OrderSelect(orderscroll,SELECT_BY_POS,MODE_TRADES);
if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==MagicNumber)&&((OrderType()OP_BUY))&&((OrderComment()“S1”)))
{
OrderClose(OrderTicket(),OrderLots(),Bid,0,Blue);
}
}
} void CloseS2LongPositions()
{
int totalorders=OrdersTotal();
int orderscroll;
for(orderscroll=0;orderscroll<totalorders;orderscroll++)
{
OrderSelect(orderscroll,SELECT_BY_POS,MODE_TRADES);
if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==MagicNumber)&&((OrderType()OP_BUY))&&((OrderComment()“S2”)))
{
OrderClose(OrderTicket(),OrderLots(),Bid,0,Blue);
}
}
} void ClosePendingLongPositions()
{
int totalorders=OrdersTotal();
int orderscroll;
for(orderscroll=0;orderscroll<totalorders;orderscroll++)
{
OrderSelect(orderscroll,SELECT_BY_POS,MODE_TRADES);
if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==MagicNumber)&&((OrderType()==OP_BUYSTOP)))
{
OrderDelete(OrderTicket());
}
}
} int CountLongPositions()
{
int totalorders=OrdersTotal();
int orderscroll;
int result=0;
for(orderscroll=0;orderscroll<totalorders;orderscroll++)
{
OrderSelect(orderscroll,SELECT_BY_POS,MODE_TRADES);
if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==MagicNumber)&&((OrderType()==OP_BUY)))
{
result++;
}
}
return(result);
} void CloseS1ShortPositions()
{
int totalorders=OrdersTotal();
int orderscroll;
for(orderscroll=0;orderscroll<totalorders;orderscroll++)
{
OrderSelect(orderscroll,SELECT_BY_POS,MODE_TRADES);
if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==MagicNumber)&&((OrderType()OP_SELL))&&((OrderComment()“S1”)))
{
OrderClose(OrderTicket(),OrderLots(),Ask,0,Blue);
}
}
} void CloseS2ShortPositions()
{
int totalorders=OrdersTotal();
int orderscroll;
for(orderscroll=0;orderscroll<totalorders;orderscroll++)
{
OrderSelect(orderscroll,SELECT_BY_POS,MODE_TRADES);
if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==MagicNumber)&&((OrderType()OP_SELL))&&((OrderComment()“S2”)))
{
OrderClose(OrderTicket(),OrderLots(),Ask,0,Blue);
}
}
} void ClosePendingShortPositions()
{
int totalorders=OrdersTotal();
int orderscroll;
for(orderscroll=0;orderscroll<totalorders;orderscroll++)
{
OrderSelect(orderscroll,SELECT_BY_POS,MODE_TRADES);
if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==MagicNumber)&&((OrderType()==OP_SELLSTOP)))
{
OrderDelete(OrderTicket());
}
}
} int CountShortPositions()
{
int totalorders=OrdersTotal();
int orderscroll;
int result=0;
for(orderscroll=0;orderscroll<totalorders;orderscroll++)
{
OrderSelect(orderscroll,SELECT_BY_POS,MODE_TRADES);
if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==MagicNumber)&&((OrderType()==OP_SELL)))
{
result++;
}
}
return(result);
} void StandardStopsForShorts()
{
double stoplevel=10000;
int orderscroll;
int totalshorts=CountShortPositions();
int totalorders=OrdersTotal();
if(totalshorts>1)
{
//scroll to find the lowest stop
for(orderscroll=0;orderscroll<totalorders;orderscroll++)
{
OrderSelect(orderscroll,SELECT_BY_POS,MODE_TRADES);
if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==MagicNumber)&&((OrderType()==OP_SELL)))
{
if(OrderStopLoss()<stoplevel)
{
stoplevel=OrderStopLoss();
}
}
} //scroll to set the stops for(orderscroll=0;orderscroll<totalorders;orderscroll++) { OrderSelect(orderscroll,SELECT_BY_POS,MODE_TRADES); if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==MagicNumber)&&((OrderType()==OP_SELL))) { if(stoplevel!=OrderStopLoss()) { OrderModify(OrderTicket(),OrderOpenPrice(),stoplevel,OrderTakeProfit(),0,CLR_NONE); } } } }} void StandardStopsForLongs()
{
double stoplevel=0;
int orderscroll;
int totalshorts=CountLongPositions();
int totalorders=OrdersTotal();
if(totalshorts>1)
{
//scroll to find the lowest stop
for(orderscroll=0;orderscroll<totalorders;orderscroll++)
{
OrderSelect(orderscroll,SELECT_BY_POS,MODE_TRADES);
if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==MagicNumber)&&((OrderType()==OP_BUY)))
{
if(OrderStopLoss()>stoplevel)
{
stoplevel=OrderStopLoss();
}
}
} //scroll to set the stops for(orderscroll=0;orderscroll<totalorders;orderscroll++) { OrderSelect(orderscroll,SELECT_BY_POS,MODE_TRADES); if((OrderSymbol()==Symbol())&&(OrderMagicNumber()==MagicNumber)&&((OrderType()==OP_BUY))) { if(stoplevel!=OrderStopLoss()) { OrderModify(OrderTicket(),OrderOpenPrice(),stoplevel,OrderTakeProfit(),0,CLR_NONE); } } } } |