#property copyright "Copyright 2014, 浪人EA工作室"
#property link "www.langrenea.com"
#property version "1.00"
#property strict
#property description "[简 介] “双向马丁,适合震荡行情"
#property description "[所 有 权] “本EA由浪人EA工作室开发"
#property description "[合作模式] “资管、EA代挂,策略编写 联系方式 QQ:353666939 WX:N18616865140"
#property description "[操作建议] “适合震荡行情,更多详情请联系浪人EA工作室"
input double starLotsSize = 0.01; //第一单大小
input int getProfitPoint = 100; //需要获利多少个点
input int startGrowthDis = 100; //默认加仓点数
input double overweight = 1.5; //翻倍系数
input int closeCurrentOrderNo = 100 ; //大于多少单止损
input bool notOpenBuy = false; // 不开buy 单
input bool notOpenSell = false; // 不开Sell 单
input bool OpenIncreaseOpint = false; // 是否增涨点数
input int increasePoint = 0; //增涨点数
input bool SecondOpen = false; //改变间距
input bool isfixedDis = true; //是否为固定间距
input int fixedDis = 1000; //固定间距
input double SecondOpenPrice = 0.00000;// 填写价格
input int MagicNumber = 16888888;
extern bool InformationOnChart = TRUE; // 是否显示信息
double lastPoint =0.0;
bool isWorked = true; //是否工作
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------+
// 我写的funcation 买卖
//+-------------------------------------------------------------
void checkFristOrder(){
if(OrdersTotal() != 0){
if(!checkHadOrderByType(OP_BUY) && notOpenBuy == false){
iOpenOrder(OP_BUY,starLotsSize,0,getProfitPoint);
}
if(!checkHadOrderByType(OP_SELL) && notOpenSell == false){
iOpenOrder(OP_SELL,starLotsSize,0,getProfitPoint);
}
}else{
if(notOpenBuy== false){
iOpenOrder(OP_BUY,starLotsSize,0,getProfitPoint);
}
if(notOpenSell == false){
iOpenOrder(OP_SELL,starLotsSize,0,getProfitPoint);
}
}
}
bool checkHadOrderByType(int opt_Type){
bool returnValue = false;
for (int index = OrdersTotal() - 1; index >= 0; index--){
if(OrderSelect(index,SELECT_BY_POS,MODE_TRADES)==true){
if (OrderMagicNumber() != MagicNumber || OrderSymbol() != Symbol()) continue;
if(OrderType() == opt_Type ){
returnValue = true;
}
}
}
return returnValue;
}
/**获取下一次开仓点位*/
int getNextOrderDis(int orderCount){
if(orderCount < 0){
return -1;
}
int temp_dis = startGrowthDis;
if(OpenIncreaseOpint == true && orderCount >=1){//从第二单开始增加
temp_dis = startGrowthDis + ( orderCount * increasePoint);
}
return temp_dis;
}
void openNextOrder(){
if(OrdersTotal() < 1 ) return;
if(notOpenBuy== false){
openBuyNextOrder();
}
if(notOpenSell == false){
openSellNextOrder();
}
}
/**
开buy单的下一单
**/
void openBuyNextOrder(){
//获取当前第几单
int buy_total = getEaSymbolTotal(OP_BUY)-1;
//获取最大的
int lastBuyTicket = getLastOrderByType(OP_BUY);
if(OrderSelect(lastBuyTicket,SELECT_BY_TICKET,MODE_TRADES)==true){
double tempBuyPrice = OrderOpenPrice();
int tempBuyDis = (Bid - tempBuyPrice)/Point;
int temp_dis = startGrowthDis;
if(buy_total > 0){
temp_dis = getNextOrderDis(buy_total);
}
if(tempBuyDis < 0 && MathAbs(tempBuyDis)>= temp_dis){
if(buy_total >= closeCurrentOrderNo){
if(SecondOpen ==true && isfixedDis == true){//
if(MathAbs(tempBuyDis) >= fixedDis){
iOpenOrder(OP_BUY,NormalizeDouble(OrderLots() * overweight,2),0,0);
alterGetProfitByBuy();
}
}else if(SecondOpen ==true && isfixedDis == false){
if(Ask <= SecondOpenPrice){
iOpenOrder(OP_BUY,NormalizeDouble(OrderLots() * overweight,2),0,0);
alterGetProfitByBuy();
}
}
}else{
iOpenOrder(OP_BUY,NormalizeDouble(OrderLots() * overweight,2),0,0);
alterGetProfitByBuy();
}
}
}
}
void openSellNextOrder(){
//获取当前第几单
int sell_total = getEaSymbolTotal(OP_SELL)-1;
//获取最大的
int lastSellTicket = getLastOrderByType(OP_SELL);
if(OrderSelect(lastSellTicket,SELECT_BY_TICKET,MODE_TRADES)==true){//SEll单
double tempSellPrice = OrderOpenPrice();
int tempSellDis = (tempSellPrice - Ask)/Point;
int temp_dis = startGrowthDis;
if(sell_total > 0){
temp_dis = getNextOrderDis(sell_total);
}
if(tempSellDis < 0 && MathAbs(tempSellDis) >= temp_dis){
if(sell_total >= closeCurrentOrderNo){
if(SecondOpen ==true && isfixedDis == true){
if(MathAbs(tempSellDis)>= fixedDis){
iOpenOrder(OP_SELL,NormalizeDouble(OrderLots() * overweight,2),0,0);
alterGetProfitBySell();
}
}else if(SecondOpen ==true && isfixedDis == false){
if(Bid >= SecondOpenPrice){
iOpenOrder(OP_SELL,NormalizeDouble(OrderLots() * overweight,2),0,0);
alterGetProfitBySell();
}
}
}else{
iOpenOrder(OP_SELL,NormalizeDouble(OrderLots() * overweight,2),0,0);
alterGetProfitBySell();
}
}
}
}
/**
修改BUY止盈
*/
void alterGetProfitByBuy(){
//获取所有的手数
double temp_Lots = getRealLotByType(OP_BUY);
if(temp_Lots == 0){
return ;
}
//获取所有的亏损记录
double temp_prifit = getProfitBuyType(OP_BUY);
//获取货币对的点值
double ticket_Value = MarketInfo(Symbol(),MODE_TICKVALUE);
if(temp_prifit < 0){
temp_prifit = MathAbs(temp_prifit);
//浮亏点数
double lostPoint = (temp_prifit/ticket_Value)/temp_Lots;
double temp_price = Ask + NormalizeDouble((lostPoint + getProfitPoint) * Point,5);
for (int index = OrdersTotal() - 1; index >= 0; index--){
OrderSelect(index, SELECT_BY_POS, MODE_TRADES);
if (OrderMagicNumber() != MagicNumber || OrderSymbol() != Symbol() || OrderType() != OP_BUY) continue;
if(Ask >= temp_price){
OrderModify(OrderTicket(), OrderOpenPrice(), temp_price ,0,0,White);
}else{
OrderModify(OrderTicket(), OrderOpenPrice(), 0 ,temp_price,0,White);
}
}
}
}
/**
修改SELL单止盈
*/
void alterGetProfitBySell(){
//获取所有的手数
double temp_Lots = getRealLotByType(OP_SELL);
if(temp_Lots == 0){
return ;
}
//获取所有的亏损记录
double temp_prifit = getProfitBuyType(OP_SELL);
//获取货币对的点值
double ticket_Value = MarketInfo(Symbol(),MODE_TICKVALUE);
if(temp_prifit < 0){
temp_prifit = MathAbs(temp_prifit);
//浮亏点数
double lostPoint = (temp_prifit/ticket_Value)/temp_Lots;
double temp_price = Bid - NormalizeDouble((lostPoint + getProfitPoint) * Point,5);
for (int index = OrdersTotal() - 1; index >= 0; index--){
OrderSelect(index, SELECT_BY_POS, MODE_TRADES);
if (OrderMagicNumber() != MagicNumber || OrderSymbol() != Symbol() || OrderType() != OP_SELL ) continue;
if(Bid <= temp_price){
OrderModify(OrderTicket(), OrderOpenPrice(), temp_price ,0,0,White);
}else{
OrderModify(OrderTicket(), OrderOpenPrice(), 0 ,temp_price,0,White);
}
}
}
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Print("hello word OnDeinit ");
}
int getFristOrder(int order_type){
int ticketNo = -1;
for(int i = OrdersTotal() - 1; i >= 0; i --){
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
//第一单大小等于开始单大小
if(OrderLots() == starLotsSize && Symbol() == OrderSymbol()&& OrderType() == order_type && OrderMagicNumber() == MagicNumber){
ticketNo = OrderTicket();
}
}
return ticketNo;
}
//获取最后一单的ticket
int getLastOrderByType(int opt_type)
{
double tempLot = 0;
int retunTicket = -1;
for(int i = OrdersTotal() - 1; i >= 0; i --)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == TRUE)
{
if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol() && OrderType() == opt_type)
{
if(OrderLots() > tempLot)
{
tempLot = OrderLots();
retunTicket = OrderTicket();
}
}
}else {
return -1;
}
}
return retunTicket;
}
//获取当前品种单数
int getEaSymbolTotal(int opt_type){
double returnValue = 0;
for(int i = OrdersTotal() - 1; i >= 0; i --) {
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == TRUE)
{
if(OrderMagicNumber() == MagicNumber && OrderSymbol() == Symbol() && OrderType()== opt_type){
returnValue ++;
}
}else {
return -1;
}
}
return returnValue;
}
int start()
{
if(isWorked == false)return 0;
checkFristOrder();
openNextOrder();
return 0;
}
//+------------------------------------------------------------------+
/**获取买单或者卖单手数*/
double getRealLotByType(int opt_type){
double returnValue = 0;
for(int index = OrdersTotal() - 1; index >= 0; index --)
{
OrderSelect(index, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber ) continue;
if(OrderType() == opt_type){
returnValue += OrderLots();
}
}
return (returnValue);
}
/* 获取当前的账户盈亏金额*/
double getProfitBuyType(int opt_type) {
double returnValue = 0;
for(int i =0; i< OrdersTotal(); i++){
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!= Symbol() || OrderMagicNumber() != MagicNumber || OrderType() != opt_type)continue;
else{
returnValue = returnValue + OrderProfit() + OrderSwap()+ OrderCommission();
}
}
return returnValue;
}
/*
mType:BUY, SELL
myLots: order size,example:0.01
myLossStop: loss point
myTakePoint: profit point
return: int the order ticket.
*/
int iOpenOrder(int myType, double myLots, int myLossStop, int myTakePoint)
{
ResetLastError();
int returnValue = 0;
int mySpread = MarketInfo(Symbol(), MODE_SPREAD);
double BuyLossStop = Ask - myLossStop * Point;
double BuyTakeProfit = Ask + myTakePoint * Point;
double SellLossStop = Bid + myLossStop * Point;
double SellTakeProfit = Bid - myTakePoint * Point;
if(myLossStop <= 0)
{
BuyLossStop=0;
SellLossStop = 0;
}
if(myTakePoint <= 0)
{
BuyTakeProfit= 0;
SellTakeProfit = 0;
}
if(myType == OP_BUY)
{
returnValue = OrderSend(Symbol(), OP_BUY, myLots, Ask, mySpread, BuyLossStop, BuyTakeProfit, "联系方式 QQ:353666939", MagicNumber,0, Blue);
}
if(myType == OP_SELL)
{
returnValue = OrderSend(Symbol(), OP_SELL, myLots, Bid, mySpread, SellLossStop, SellTakeProfit, "联系方式 WX:N18616865140", MagicNumber,0, Red);
}
if(returnValue > -1)
{
isWorked = true;
}
Print(GetLastError());
return returnValue;
}
/**
关闭订单操作
*/
bool iCloseOrder(int orderTicket, double orderLots,double price, int slippage, color clrOrder)
{
int i = 0;
bool isClosed = false;
while(i < 3)
{
ResetLastError();
RefreshRates();
isClosed = OrderClose(orderTicket, orderLots, price, slippage, clrOrder);
if(isClosed == false)
{
PrintFormat("关闭时被服务器拒绝,订单号:%d, 原因:%d", orderTicket, GetLastError());
i ++;
Sleep(800);
}else{
break;
}
}
return isClosed;
}
void closeAllOrderByType(int opt_type){
for(int i = OrdersTotal()-1; i >=0; i --)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
{
continue;
}
if(OrderSymbol() ==Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == opt_type){
color orderColor = clrBlue;
if(OrderType() == OP_SELL) {
orderColor = clrRed;
}
RefreshRates();
double closePrice = 0;
int slippage = 3;
if(OrderType() == OP_BUY)
closePrice = MarketInfo(OrderSymbol(), MODE_BID);
else
closePrice = MarketInfo(OrderSymbol(), MODE_ASK);
iCloseOrder(OrderTicket(), OrderLots(),closePrice, slippage, orderColor);
}
}
}
double getProfitOrLot(string mType) {
double returnValue = 0;
for(int i =0; i< OrdersTotal(); i++){
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
if(OrderSymbol()!= Symbol() || OrderMagicNumber() != MagicNumber)continue;
else{
if(mType == "Balance") returnValue = returnValue + OrderProfit() + OrderSwap()+ OrderCommission();
if(mType == "Lot") returnValue = returnValue + OrderLots();
}
}
}
return (returnValue);
}
double getAllLots(){
double returnValue = 0;
for(int index = OrdersTotal() - 1; index >= 0; index --)
{
if(OrderSelect(index, SELECT_BY_POS, MODE_TRADES)){
if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber ) continue;
if(OrderType() ==OP_BUY){
returnValue += OrderLots();
}else if(OrderType() == OP_SELL){
returnValue -= OrderLots();
}
}
}
return (returnValue);
}
void closeAllOrders(){
for(int i = OrdersTotal()-1; i >=0; i --)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false)
{
continue;
}
if(OrderSymbol() ==Symbol() && OrderMagicNumber() == MagicNumber){
color orderColor = clrBlue;
if(OrderType() == OP_SELL) {
orderColor = clrRed;
}
RefreshRates();
double closePrice = 0;
int slippage = 3;
if(OrderType() == OP_BUY)
closePrice = MarketInfo(OrderSymbol(), MODE_BID);
else
closePrice = MarketInfo(OrderSymbol(), MODE_ASK);
iCloseOrder(OrderTicket(), OrderLots(),closePrice, slippage, orderColor);
}
}
}
|