MT人工智能之五(Omni)
/*[[
Name := Omni
Author := Copyright ?2004, Allan G. Black, [email protected]
Link := http://www.metaquotes.net/
Lots := 1.00
Stop Loss := 0
Take Profit := 0
Trailing Stop := 0
]]*/
var: i(0),interval(200);
var: buyref(0),sellref(0);
var: buysetup(false),sellsetup(false);
var: upper(0),uppertmp(0),toporder(0),lower(0),lowertmp(0),loworder(0);
var: direction("none");
// Take profit parameters
var: timing("waitforprofit"),grabprofit(1000),closeall(False);
// Timing
if timing=="waitforprofit" and totalprofit>=grabprofit then {
closeall=true;
}
if closeall then {
if TotalTrades==0 then {
buysetup=false;
sellsetup=false;
closeall=false;exit;
}
if ord(1,VAL_TYPE)==0 then {
CloseOrder(ord(1,VAL_TICKET),lots,Ask,0,Green);exit;
}
if ord(1,VAL_TYPE)==1 then {
CloseOrder(ord(1,VAL_TICKET),lots,Bid,0,Green);exit;
}
}
// Initialize
if TotalTrades==0 and !buysetup then {
buyref=ask;
SetOrder(OP_BUY,1,Ask,0,0,0,Green);
buysetup=true;
exit;
}
if TotalTrades==1 and !sellsetup then {
sellref=bid;
SetOrder(OP_SELL,1,Bid,0,0,0,Green);
sellsetup=true;
exit;
}
//Find extreme lots
upper=0.0;
uppertmp=0.0;
lower=500.0;
lowertmp=500.0;
for i=1 to TotalTrades {
//PrintTrade(i);
if ord(i,VAL_TYPE)==0 then {
upper=max(upper,ord(i,VAL_OPENPRICE));
if upper!=uppertmp then {toporder=i;}
uppertmp=upper;
}
if ord(i,VAL_TYPE)==1 then {
lower=min(lower,ord(i,VAL_OPENPRICE));
if lower!=lowertmp then {loworder=i;}
lowertmp=lower;
}
}
// Determine direction
if ask>buyref then {direction="up";}
if bid<sellref then {direction="down";}
if ask<buyref and bid>sellref then {direction="none";exit;}
// Adjust stop and add
if TotalTrades>=2 then {
if direction="up" then {
if Ord(toporder,VAL_OPENPRICE)+interval*Point<=ask then {
if ord(toporder,VAL_STOPLOSS)<ord(toporder,VAL_OPENPRICE) then {
// Push up Stops to openprice
// for i=1 to TotalTrades{PrintTrade(i);}
ModifyOrder(ord(toporder,VAL_TICKET),
Ord(toporder,VAL_OPENPRICE),
Ord(toporder,VAL_OPENPRICE),
Ord(toporder,VAL_TAKEPROFIT),Green);
}
// BUY
if FreeMargin>2000 then {
SetOrder(OP_BUY,1,Ask,0,0,0,Green);
// toporder=TotalTrades;
exit;
}
}
}
if direction="down" then {
if Ord(loworder,VAL_OPENPRICE)-interval*Point>=bid then {
if ord(loworder,VAL_STOPLOSS)>ord(loworder,VAL_OPENPRICE) then {
// Push down Stops to openprice
ModifyOrder(ord(loworder,VAL_TICKET),
Ord(loworder,VAL_OPENPRICE),
Ord(loworder,VAL_OPENPRICE),
Ord(loworder,VAL_TAKEPROFIT),Red);
}
// SELL
if FreeMargin>2000 then {
SetOrder(OP_SELL,1,Bid,0,0,0,Red);
//loworder=TotalTrades;
exit;
}
}
}
}