海龟交易代码java_[转载]海龟交易系统 MT4 源码

本文介绍了一段使用Java编写的海龟交易系统MT4源码,适用于D1时间框架。代码中定义了各种颜色、周期参数,并实现了交易入口和出口的计算、订单管理以及交易风险管理。关键功能包括:检查新的bar、计算海龟交易值、绘制交易入口退出线、管理订单等。

//+------------------------------------------------------------------+

//| GoHan Turtle Trader.mq4 |

//| Copyright ?2008, gohanforex.com |

//| http://www.gohanforex.com |

//+------------------------------------------------------------------+

#property copyright "Copyright ?2008, gohanforex.com"

#property

link "http://www.gohanforex.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

{

//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

{

//Close long positions

CloseS1LongPositions();

ClosePendingLongPositions();

}

if(Ask>S1ShortExit)

{

//Close short positions

CloseS1ShortPositions();

ClosePendingShortPositions();

}

//Check for S2 long

entry

if((S2LongEntry

{

//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

{

//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

{

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

{

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

{

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值