本帖最后由 hwq597 于 2017-7-3 07:51 编辑
如何把固定的下单量改成下面用公式表示的下单量(只用到小数点后2位,附合下单要求)
下单量=(资金*10/100)/(市场价格/500)
extern double 下单量=0.1;
extern int 止损点数=0;
extern int 止盈点数=0;
extern int 移动止损点数=500;
extern int magic=52142;
datetime buytime=0;
datetime selltime=0;
int OnInit()
{
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
}
void OnTick()
{
double k0=iCustom(Symbol(),0,"KD",0,0);
double d0=iCustom(Symbol(),0,"KD",1,0);
double k1=iCustom(Symbol(),0,"KD",0,1);
double d1=iCustom(Symbol(),0,"KD",1,1);
if(k0>d0 && k1<d1)//buy
{
closesell(Symbol()+"sell",magic);
if(buytime!=Time[0])
{
if(buy(下单量,止损点数,止盈点数,Symbol()+"buy",magic)>0)
{
buytime=Time[0];
}
}
}
if(k0<d0 && k1>d1)//sell
{
closebuy(Symbol()+"buy",magic);
if(selltime!=Time[0])
{
if(sell(下单量,止损点数,止盈点数,Symbol()+"sell",magic)>0)
{
selltime=Time[0];
}
}
}
yidong();
}
void yidong()
{
for(int i=0;i<OrdersTotal();i++)//移动止损通用代码,次代码会自动检测buy和sell单并对其移动止损
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderType()==0 && OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
{
if((Bid-OrderOpenPrice())>=Point*移动止损点数)
{
if(OrderStopLoss()<(Bid-Point*移动止损点数) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*移动止损点数,OrderTakeProfit(),0,Green);
}
}
}
if(OrderType()==1 && OrderSymbol()==Symbol() && OrderMagicNumber()==magic)
{
if((OrderOpenPrice()-Ask)>=(Point*移动止损点数))
{
if((OrderStopLoss()>(Ask+Point*移动止损点数)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*移动止损点数,OrderTakeProfit(),0,Green);
}
}
}
}
}
}
void closebuy(string com,int magic)
{
int t=OrdersTotal();
for(int i=t-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderComment()==com && OrderMagicNumber()==magic)
{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),300,Green);
}
}
}
}
void closesell(string com,int magic)
{
int t=OrdersTotal();
for(int i=t-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderComment()==com && OrderMagicNumber()==magic)
{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),300,Green);
}
}
}
}
int buy(double lots,double sl,double tp,string com,int buymagic)
{
int a=0;
bool zhaodan=false;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
string zhushi=OrderComment();
int ma=OrderMagicNumber();
if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && zhushi==com && ma==buymagic)
{
zhaodan=true;
break;
}
}
}
if(zhaodan==false)
{
if(sl!=0 && tp==0)
{
a=OrderSend(Symbol(),OP_BUY,lots,Ask,50,Ask-sl*Point,0,com,buymagic,0,White);
}
if(sl==0 && tp!=0)
{
a=OrderSend(Symbol(),OP_BUY,lots,Ask,50,0,Ask+tp*Point,com,buymagic,0,White);
}
if(sl==0 && tp==0)
{
a=OrderSend(Symbol(),OP_BUY,lots,Ask,50,0,0,com,buymagic,0,White);
}
if(sl!=0 && tp!=0)
{
a=OrderSend(Symbol(),OP_BUY,lots,Ask,50,Ask-sl*Point,Ask+tp*Point,com,buymagic,0,White);
}
}
return(a);
}
int sell(double lots,double sl,double tp,string com,int sellmagic)
{
int a=0;
bool zhaodan=false;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
string zhushi=OrderComment();
int ma=OrderMagicNumber();
if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && zhushi==com && ma==sellmagic)
{
zhaodan=true;
break;
}
}
}
if(zhaodan==false)
{
if(sl==0 && tp!=0)
{
a=OrderSend(Symbol(),OP_SELL,lots,Bid,50,0,Bid-tp*Point,com,sellmagic,0,Red);
}
if(sl!=0 && tp==0)
{
a=OrderSend(Symbol(),OP_SELL,lots,Bid,50,Bid+sl*Point,0,com,sellmagic,0,Red);
}
if(sl==0 && tp==0)
{
a=OrderSend(Symbol(),OP_SELL,lots,Bid,50,0,0,com,sellmagic,0,Red);
}
if(sl!=0 && tp!=0)
{
a=OrderSend(Symbol(),OP_SELL,lots,Bid,50,Bid+sl*Point,Bid-tp*Point,com,sellmagic,0,Red);
}
}
return(a);
}
|