以下是一些简单的源码,供参考学习。
//+------------------------------------------------------------------+ //| designed by okwh, china | //| copyright 2007, okwh dxdcn | //| | //+------------------------------------------------------------------+
#property copyright "copyright 2007 , dxd, china." #property link "" #define magicma 200610011231 //+------------------------------------------------------------------+
//| 注意没有指标文件那些property | //+------------------------------------------------------------------+ extern int whichmethod = 1; //1~4 种下单方式 1 仅开仓, 2 有止损无止赢, 3 有止赢无止损, 4 有止赢也有止损 extern double takeprofit = 100; //止赢点数 extern double stoploss = 20; //止损点数 extern double maximumrisk = 0.3; //资金控制,控制下单量 extern double trailingstop =25;
//跟踪止赢点数设置 extern int maxopen = 3; //最多开仓次数限制 extern int maxlots = 5; //最多单仓持仓量限制 extern int bb = 0; //非零就允许跟踪止赢 extern double matrendperiod=26;
//使用26均线 开仓条件参数 本例子 int i, p2, xxx,p1, res; double lots; datetime lasttime; //时间控制, 仅当一个时间周期完成才检查条件 int init() //初始化 { lots = 1; lasttime = null; return(0); } int deinit() { return(0); }
//反初始化 //主程序 int start() { checkforopen(); //开仓 平仓 条件检查 和操作 if (bb>0) ctp(); //跟踪止赢 return(0); }
//+------下面是各子程序--------------------------------------------+ double lotsoptimized() //确定下单量,开仓调用 资金控制 { double lot=lots; int orders=historytotal();
// history orders total int losses=0; // number of losses orders without a break //marketinfo(symbol(),mode_minlot); 相关信息 //marketinfo(symbol(),mode_maxlot);
//marketinfo(symbol(),mode_lotstep); lot=normalizedouble(maximumrisk * accountbalance()/accountleverage(),1); //开仓量计算 if(lot<0.1) lot=0.1; if(lot>maxlots) lot=maxlots; return(lot); }
//平仓持有的买单 void closebuy() { if (orderstotal( ) > 0 ) { for(i=orderstotal()-1;i>=0;i--) { if(orderselect(i,select_by_pos,mode_trades)==false) break; if(ordertype()==op_buy)
{ orderclose(orderticket(),orderlots(),bid,3,white); sleep(5000); } } } } //平仓持有的卖单 void closesell() { if (orderstotal( ) > 0 ) { for(i=orderstotal()-1;i>=0;i--)
{ if(orderselect(i,select_by_pos,mode_trades)==false) break; if(ordertype()==op_sell) { orderclose(orderticket(),orderlots(),ask,3,white); sleep(5000); } } } }
//判断是否买或卖或平仓 int buyorsell() //在这个函数计算设置你的交易信号 这里使用macd 和ma 做例子
{ double macdcurrent, macdprevious, signalcurrent; double signalprevious, macurrent, maprevious; macdcurrent=imacd(null,0,12,26,9,price_close,mode_main,0);
macdprevious=imacd(null,0,12,26,9,price_close,mode_main,1); signalcurrent=imacd(null,0,12,26,9,price_close,mode_signal,0);
signalprevious=imacd(null,0,12,26,9,price_close,mode_signal,1); macurrent=ima(null,0,matrendperiod,0,mode_ema,price_close,0);
maprevious=ima(null,0,matrendperiod,0,mode_ema,price_close,1); if(macdcurrent<0 && macdcurrent>signalcurrent && macdpreviousmaprevious) return (1);
// 买 ma在上升,macd在0线上,并且两线上交叉 if(macdcurrent>0 && macdcurrentsignalprevious && macurrent