Hi all,
Thank you in advance for even reading this.
I have been using the Nanningbob EA system for 3 months successfully with about a 15% return per month.
I have since added a few features to the existing code of one of the old EA's, and kept modifying it over and over until it looked like a big plate of spaghetti.
I ported it over to this one attached, but can't get it to work properly:
1. Variable overwriting
2. Old Tick Data errors in demo servers
3. Sometimes it doesn't draw the indicator on the screen? (Per post below, I discovered why..duh..)
4. Only fires orders occasionally, sometimes in the wrong direction
The strange thing, this thing works-(ed) when I was backtesting over the weekend.
I realize my code is nothing fantastic, but if you could do a quick run-through and guide me on the big issues you see. Thanks in advance!
Aloha!
---Please ignore the comments about TP not working.
Thank you in advance for even reading this.
I have been using the Nanningbob EA system for 3 months successfully with about a 15% return per month.
I have since added a few features to the existing code of one of the old EA's, and kept modifying it over and over until it looked like a big plate of spaghetti.
I ported it over to this one attached, but can't get it to work properly:
1. Variable overwriting
2. Old Tick Data errors in demo servers
3. Sometimes it doesn't draw the indicator on the screen? (Per post below, I discovered why..duh..)
4. Only fires orders occasionally, sometimes in the wrong direction
The strange thing, this thing works-(ed) when I was backtesting over the weekend.
I realize my code is nothing fantastic, but if you could do a quick run-through and guide me on the big issues you see. Thanks in advance!
Aloha!
Inserted Code
//Order Settings
extern double LotSize = 0.01;
extern double StopGapRatio = 0.015;
extern double MinimumReEntry = 200;
extern double Multiplier = 1.5;
extern int MaxTrades = 6;
extern double MinimumProfitOfBasket = 1.0; //Minimum Profit in Dollars for all trades
//Sixth's indicator settings
extern string SixthsInfo = "Sixths settings for calculation";
extern int BarTF = 240;
extern int BarCount = 260; //Number of Bars to calculate on BarTF
extern double SixthsLevelEntry = 2.0; //1.0 = gold lines, 2.0 = blue lines, GBPJPY = 1.0, GBPNZD = 0.5, EURGBP = 2.5, etc..
//Settings for Trailing Stop
extern string TsInfo = "Trailing Stop Settings";
extern int PercentTrailingFromMax = 20;
extern double Trail_From = 12.0;
extern double Trail_Max = 50.0;
extern double Trail_Percent = 50;
//extern int StopLoss = 3600; //Not used, manual close, use Hedging EA
//Basic settings
extern int MagicNumber = 0; //If 0, will dynamically create a random Magic Number
extern string Comments = "Double Dragon Master";
//Variable Declarations
int OrderDirection,
Slippage = 99;
double Pip,
LotDigits,
StopGap,
TakeProfit,
BasketTakeProfit,
TotalBasketProfit,
LastOrderOpenPrice,
LastOrderLotSize,
SellLevel,
BuyLevel,
ChartHigh,
ChartLow,
ChartDistance,
OneSixthOfChart;
string Sym[];
double Equity[];
double Lots[];
color ArrowsColor = White,
A_High = Gold,
B_High = Blue,
Center = Red,
B_Low = Blue,
A_Low = Gold,
PendingArrowsColor = Red; // color for the orders arrows
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
Pip = Point;
if (Digits == 3 || Digits == 5){Pip *= 10;}
if (MarketInfo(Symbol(),MODE_LOTSTEP) == 0.01){LotDigits = 2;}
MinimumProfitOfBasket *= Pip;
StopGap *= Pip;
TakeProfit *= Pip;
MinimumReEntry *= Pip;
//---- Sixth Indicator Lines
ObjectCreate("A Low",1,0,TimeCurrent(),Low[BarCount]);
ObjectSet("A Low",OBJPROP_COLOR,A_Low);
ObjectSet("A Low",OBJPROP_STYLE,STYLE_SOLID);
ObjectSet("A Low",OBJPROP_WIDTH,2);
ObjectSet("A Low",OBJPROP_FONTSIZE,20);
ObjectCreate("B Low",1,0,TimeCurrent(),Low[BarCount]);
ObjectSet("B Low",OBJPROP_COLOR,B_Low);
ObjectSet("B Low",OBJPROP_STYLE,STYLE_SOLID);
ObjectSet("B Low",OBJPROP_WIDTH,2);
ObjectCreate("Center",1,0,TimeCurrent(),Low[BarCount]);
ObjectSet("Center",OBJPROP_COLOR,Center);
ObjectSet("Center",OBJPROP_STYLE,STYLE_SOLID);
ObjectSet("Center",OBJPROP_WIDTH,2);
ObjectCreate("B High",1,0,TimeCurrent(),Low[BarCount]);
ObjectSet("B High",OBJPROP_COLOR,B_High);
ObjectSet("B High",OBJPROP_STYLE,STYLE_SOLID);
ObjectSet("B High",OBJPROP_WIDTH,2);
ObjectCreate("A High",1,0,TimeCurrent(),Low[BarCount]);
ObjectSet("A High",OBJPROP_COLOR,A_High);
ObjectSet("A High",OBJPROP_STYLE,STYLE_SOLID);
ObjectSet("A High",OBJPROP_WIDTH,2);
//----
//Create a MagicNumber if it is 0
if (MagicNumber == 0) {
MagicNumber = create_MagicNumber("");
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
//Setting arrays to 0
/*ArrayResize(Sym, 0);
ArrayResize(Equity, 0);
ArrayResize(Lots, 0);*/
RefreshRates();
SixthsIndicator();
SetOrderType();
PendingOrderTrail(); //------****Take Profit for entries is not working? Should I just put them into the ProfitProtection() function?
OpenOrders(); //------****Take Profit for entries is not working? Should I just put them into the ProfitProtection() function?
TrailStop();
ProfitProtection();
//Check for minimum profit to set TP
//Close all orders if TP is hit... :)
//----
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//Sets LONG or SHORT direction
//Identifies last open order Price
void SetOrderType()
//+------------------------------------------------------------------+
{
if(OrdersTotal() > 0)
{
for(int ot = 0; ot < OrdersTotal(); ot++)
{
if (OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
{
if (OrderSelect(ot, SELECT_BY_POS, MODE_TRADES))
{
OrderDirection = OrderType();
if(OrderDirection == 4) OrderDirection = 0;
if(OrderDirection == 5) OrderDirection = 1;
LastOrderOpenPrice = OrderOpenPrice();
LastOrderLotSize = OrderLots();
}
}
}
}
}
//+------------------------------------------------------------------+
//Opens first long/short if under proper 6th
//Opens higher level trades if greater than minimum distance
void OpenOrders()
//+------------------------------------------------------------------+
{
double EntryPrice;
double OrderSize;
//If less than 1 order, then open initial orders
if(OrdersTotal() < 1)
{
RefreshRates();
if((Bid + StopGap) <= BuyLevel)
{
EntryPrice = Bid+StopGap;
//This first OrderSend is for working on getting TP working
//OrderSend(Symbol(),4,LotSize,(EntryPrice),Slippage,0,NormalizeDouble(EntryPrice+TakeProfit,Digits),DoubleToStr(LotSize,2),MagicNumber,0,PendingArrowsColor);
OrderSend(Symbol(),4,LotSize,(EntryPrice),Slippage,0,0,DoubleToStr(LotSize,2),MagicNumber,0,PendingArrowsColor);
}
else if((Ask - StopGap) >= SellLevel)
{
EntryPrice = Ask-StopGap;
//OrderSend(Symbol(),5,LotSize,(EntryPrice),Slippage,0,NormalizeDouble(EntryPrice-TakeProfit,Digits),DoubleToStr(LotSize,2),MagicNumber,0,PendingArrowsColor);
OrderSend(Symbol(),5,LotSize,(EntryPrice),Slippage,0,0,DoubleToStr(LotSize,2),MagicNumber,0,PendingArrowsColor);
}
}
//If more than 1 order, then open up higher level trades
else if(OrdersTotal() >= 1)
{
RefreshRates();
if(OrderDirection == 0)
{
///*************++++++++++++++This needs SuperTrend confirmation possibly in future
if((Ask + MinimumReEntry + StopGap) <= LastOrderOpenPrice)
{
EntryPrice = Ask+StopGap;
OrderSize = NormalizeDouble(LastOrderLotSize * Multiplier,2);
//OrderSend(Symbol(),4,OrderSize,(EntryPrice),Slippage,0,NormalizeDouble(EntryPrice+TakeProfit,Digits),DoubleToStr(OrderSize,Point),MagicNumber,0,PendingArrowsColor);
OrderSend(Symbol(),4,OrderSize,(EntryPrice),Slippage,0,0,DoubleToStr(OrderSize,Point),MagicNumber,0,PendingArrowsColor);
LastOrderOpenPrice = OrderOpenPrice();
}
}
if(OrderDirection == 1)
{
///*************++++++++++++++This needs SuperTrend confirmation possibly in future
if((Bid - MinimumReEntry - StopGap) >= LastOrderOpenPrice)
{
EntryPrice = Bid-StopGap;
OrderSize = NormalizeDouble(LastOrderLotSize * Multiplier,2);
//OrderSend(Symbol(),5,OrderSize,(EntryPrice),Slippage,0,NormalizeDouble(EntryPrice-TakeProfit,Digits),DoubleToStr(OrderSize,Point),MagicNumber,0,PendingArrowsColor);
OrderSend(Symbol(),5,OrderSize,(EntryPrice),Slippage,0,0,DoubleToStr(OrderSize,Point),MagicNumber,0,PendingArrowsColor);
LastOrderOpenPrice = OrderOpenPrice();
}
}
}
}
//+------------------------------------------------------------------+
double ProfitProtection()
//+------------------------------------------------------------------+
{
bool ClosedInProfit = 0;
string OrderSymbolClosedInProfit;
double TP;
double ProfitThisOrder;
TakeProfit = OneSixthOfChart;
TotalBasketProfit = 0;
RefreshRates();
int OT = OrdersTotal();
for(int bp=0; bp < OT; bp++) //cycle throug the open market orders
{
if (OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
{
if (OrderSelect(bp, SELECT_BY_POS, MODE_TRADES))
{
TotalBasketProfit += OrderProfit() + OrderCommission() + OrderSwap();
if(OrderLots() != LotSize && TotalBasketProfit >= MinimumProfitOfBasket)
{
ClosedInProfit = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Center);
OrderSymbolClosedInProfit = Symbol();
}
}
}
if(ClosedInProfit)
{
for(int co = 0; co < OrdersTotal(); co++)
{
if (OrderSelect(co, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == OrderSymbolClosedInProfit)
{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Center);
}
}
}
}
}
}
//+------------------------------------------------------------------+
void PendingOrderTrail()
//+------------------------------------------------------------------+
//when a pending order is open, this function lets it trail at "StopGap"
{
int OT = OrdersTotal();
for(int in=0; in < OT; in++) //cycle throug the open market orders
{
RefreshRates();
if (OrderSelect(in, SELECT_BY_POS, MODE_TRADES))
{
if (OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
{
if (OrderType() == 4)
{
if ((OrderOpenPrice() - (StopGap)) > Ask)
{
OrderModify(OrderTicket(),(Ask + StopGap), 0, 0, 0, PendingArrowsColor);
}
}
if (OrderType() == 5)
{
if ((OrderOpenPrice() + (StopGap)) < Bid) //if bid price moved further away from pending order open price
{
OrderModify(OrderTicket(), (Bid - StopGap), 0, 0, 0, PendingArrowsColor);
}
}
}
}
}
}
//+------------------------------------------------------------------+
bool TrailStop()
//+------------------------------------------------------------------+
{
bool
mod,
lresult = false;
double
My_Profit,
My_Trail,
My_SL,
lTrail_Max,
lTrail_From,
Stop_Level;
//----
for (int i = 0; i < OrdersTotal(); i++){
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber)
{
RefreshRates();
//If Trailing Stop would eliminate the possibility of new entry, then do not do trail any further
//If orderlots() greater than Lotsize break;)
if(OrderDirection == 0)
{
if((OrderOpenPrice() + StopGap) > BuyLevel)break;
}
else if(OrderDirection == 1)
{
if((OrderOpenPrice() - StopGap) < SellLevel)break;
}
//Must be level "1" type trade
if(NormalizeDouble(StrToDouble(OrderComment()),2) == LotSize && LastOrderLotSize <= LotSize)
{
lTrail_Max = Trail_Max * Pip;
lTrail_From = Trail_From * Pip;
Stop_Level = MarketInfo(OrderSymbol(),MODE_STOPLEVEL)*Pip;
switch(OrderType()){
case OP_BUY : My_Profit = Bid - OrderOpenPrice();
My_Trail = MathMin(My_Profit * Trail_Percent/100,lTrail_Max);
My_SL = NormalizeDouble(OrderClosePrice()-My_Trail,Digits);
lresult = true;
if(My_Profit > lTrail_From){
if(OrderClosePrice() - My_SL > Stop_Level){
if(OrderStopLoss() < My_SL||OrderStopLoss() == 0)mod = OrderModify(OrderTicket(),OrderOpenPrice(),My_SL,OrderTakeProfit(),0, CLR_NONE);
}
}
break;
case OP_SELL : My_Profit = OrderOpenPrice() - Ask;
My_Trail = MathMin(My_Profit * Trail_Percent/100,lTrail_Max);
My_SL = NormalizeDouble(Ask+My_Trail,Digits);
lresult = true;
if(My_Profit > lTrail_From){
if(My_SL - OrderClosePrice() > Stop_Level){
if(My_SL < OrderStopLoss()||OrderStopLoss() == 0)mod = OrderModify(OrderTicket(),OrderOpenPrice(),My_SL,OrderTakeProfit(),0,CLR_NONE);
}
}
break;
}
if(!mod && GetLastError() > 1)Print("Error entering Trailing Stop - Error " + GetLastError());
}
}
}
else Print("Error selecting order");
}
//----
return(lresult);
}
//+------------------------------------------------------------------+
//Returns buy or sell level
double SixthsIndicator()
//+------------------------------------------------------------------+
{
ChartHigh = High[iHighest(NULL,BarTF,MODE_HIGH,BarCount,1)];
ChartLow = Low[iLowest(NULL,BarTF,MODE_LOW,BarCount,1)];
ChartDistance = ChartHigh - ChartLow; //value top of the chart - value bottom
OneSixthOfChart = ChartDistance / 6;
SellLevel = NormalizeDouble(High[iHighest(NULL,BarTF,MODE_HIGH,BarCount,1)] - (OneSixthOfChart * SixthsLevelEntry),Digits);
BuyLevel = NormalizeDouble(Low[iLowest(NULL,BarTF,MODE_LOW,BarCount,1)] + (OneSixthOfChart * SixthsLevelEntry),Digits);
StopGap = NormalizeDouble((ChartDistance*StopGapRatio),Digits);
if(ObjectFind("A High")==-1)init();
ObjectMove("A Low",0,TimeCurrent(),Low[iLowest(NULL,0,MODE_LOW,BarCount,1)]+OneSixthOfChart);
ObjectMove("B Low",0,TimeCurrent(),Low[iLowest(NULL,0,MODE_LOW,BarCount,1)]+OneSixthOfChart+OneSixthOfChart);
ObjectMove("Center",0,TimeCurrent(),Low[iLowest(NULL,0,MODE_LOW,BarCount,1)]+OneSixthOfChart+OneSixthOfChart+OneSixthOfChart);
ObjectMove("B High",0,TimeCurrent(),Low[iLowest(NULL,0,MODE_LOW,BarCount,1)]+OneSixthOfChart+OneSixthOfChart+OneSixthOfChart+OneSixthOfChart);
ObjectMove("A High",0,TimeCurrent(),Low[iLowest(NULL,0,MODE_LOW,BarCount,1)]+OneSixthOfChart+OneSixthOfChart+OneSixthOfChart+OneSixthOfChart+OneSixthOfChart);
RefreshRates();
//Comments section
Comment(
"Magic Number=",MagicNumber,"n",
"TF SELECTED=",BarTF,"; BAR COUNT=",BarCount,"n",
"High = ", ChartHigh, " Low = ", ChartLow, "n",
"High to Low = ", (ChartDistance/Point), " pips", "n" ,
"1/6th of Chart = ", NormalizeDouble((OneSixthOfChart/Point),0), " pips", "n" ,
"Total Basket Profit = ", "$", TotalBasketProfit, " Dollars", "n",
"Highest Level Order Profit = ", "$", (OrderProfit()+OrderCommission()+OrderSwap()), " Dollars", "n",
"Sell Level = ", SellLevel, "n" ,
"Buy Level = ", BuyLevel, "n" ,
"Stop Gap = ", (StopGap/Point),"n",
"Last Opened Order Price = ",LastOrderOpenPrice,"n",
"Spread: ",(Ask-Bid)/Point, " Pips"
);
return(OneSixthOfChart);
}
//+------------------------------------------------------------------+
int create_MagicNumber(string s)
//+------------------------------------------------------------------+
{
// create a magic number that is "unique" for a given {EA_name,Symbol,Period} combo
int magic=0;
s = s+WindowExpertName()+Symbol()+Period();
magic = hash_string(s);
while (magic < 10000) {// magic number is not long enough, make another one
s = s+magic;
magic = hash_string(s);
}
return (magic);
}
//+------------------------------------------------------------------+
int hash_string(string s)
//+------------------------------------------------------------------+
{
// this is the djb2 string hash algo
int h = 5381, l = StringLen(s), i;
for (i=0; i<l; i++) h = h * 33 + StringGetChar(s,i);
return (h);
} /*hash_string()*/ ---Please ignore the comments about TP not working.
Attached File(s)
We are our own best indicator.