Inside Bar – Rewrite Compo

At times out of nostalgia I browse among my older routines for a current back test to see if any of them holds up well in the current environment.

Hundreds of ideas came and went, but some were on cue all along. I used to have a chess team with not much in common but the idea to exploit an edge.

Now I am thinking more of in terms of a complete, well rounded system, and I expect just as much from the management part as from the “excuses”, the trade initiator routines.

I am happy when I see something with a back test like this:

This is treasure. Something with a great edge. All you need to do is sell instead of buy and vica versa.

I would like you to follow on my path in redoing the piece of code I have coming up here.

I made a few modifications and this is what I ended up with.



Out of the total, 17 “bad turned good” trades I only lost 5 when I applied one 4-hour filter per direction – to eliminate the losing trades.

Can you do this? Could you even better?

Feel free to show me your insights and your results!

The testing period is from the 1st of October, 2018 to the 25th of November, 2018. Tested on EUR/USD with 2 points average spread.


// Inside Trader

#property copyright “by Macdulio”
#property link “https://forexfore.blog
#property description “Inside Trader”
#property description “V1.0”

#include <stdlib.mqh>
extern int ClosePendingInSeconds = 10800;
extern int magic_number = 999;
extern int magic_number2 = 998;
extern int StopLoss = 25;

double RSI2[],stoch[];
int profits;
double nakedshorts[];
double nakedlongs[];
extern double Leverage = 1.5;
extern double AF=1.3;
extern double LT=333;
double open_price;
double stop_loss_price;
double take_profit_price;

int dhdcount;
double OrderOpenPrice;
double OrderProfit;
string symbol = Symbol();
int init() {
ArrayResize(RSI2, 1100);
ArrayInitialize(RSI2, EMPTY_VALUE);
ArrayResize(stoch, 1100);
ArrayInitialize(stoch, EMPTY_VALUE);

return(0);
}

int deinit() {
return(0);
}

int start() {

int i, counter;
int counted_bars=IndicatorCounted();
int longcount, shortcount;
double nlongs;
double nshorts;

double longaveragebuffer;
double shortaveragebuffer;
int order_type;
double account = AccountEquity();
double longsize, shortsize;
double MaxLots = NormalizeDouble(LT/3000000*account*AF*Leverage/StopLoss*260/4,2);
profits = 0;

counter = 0;

for(i=OrdersTotal()-1; i>=0 ; i–)
{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
{
Print(“Access to orders list failed with error (“,GetLastError(),”)”);
break;
}
if (OrderType() == OP_BUY && OrderSymbol()==Symbol())
{
nlongs = nlongs+OrderLots();
longcount = longcount+1;
longaveragebuffer = longaveragebuffer+(OrderOpenPrice()*OrderLots());

}

if (OrderType() == OP_SELL && OrderSymbol()==Symbol())
{
nshorts = nshorts+OrderLots();
shortcount = shortcount+1;
shortaveragebuffer = shortaveragebuffer+(OrderOpenPrice()*OrderLots());
}
// }
}

for (i=1; i<=3; i++)
{
stoch[i]=iStochastic(NULL,30,10,3,3,MODE_SMA,1 ,MODE_SIGNAL,i);
RSI2[i]=iRSI(NULL,30,2,PRICE_MEDIAN,i);
}

double dmulti=1;
double umulti=1;

if (Close[0]>iMA(Symbol(),60,1656,0,MODE_EMA, PRICE_HIGH,0) && Close[0]>iMA(Symbol(),30,414,0,MODE_EMA, PRICE_HIGH,0) && iMA(Symbol(),30,414,0,MODE_EMA, PRICE_HIGH,0)iMA(Symbol(),30,414,0,MODE_EMA, PRICE_HIGH,0) && iMA(Symbol(),30,414,0,MODE_EMA, PRICE_HIGH,0)iMA(Symbol(),30,414,0,MODE_EMA, PRICE_HIGH,0) ) {umulti=1.48; dmulti=1;}
else if (Close[0]<ima(symbol(),60,1656,0,mode_ema, price_low,0)=”” &&=”” close[0]iMA(Symbol(),30,135,0,MODE_EMA, PRICE_HIGH,0) ) {umulti=1; dmulti=2.1;}
else if (Close[0]iMA(Symbol(),30,135,0,MODE_EMA, PRICE_HIGH,0) ) {umulti=1; dmulti=1.77;}
else if (Close[0]<ima(symbol(),30,414,0,mode_ema, price_low,0))=”” {umulti=”1;” dmulti=”1.48;}” s=”” #1=”” if=”” (=”” (mathabs(nlongs)=”=0″ ||=”” nlongs<nshorts)=”” &&=”” ihigh(symbol,60,1)-ilow(symbol,60,1).0013 && iHigh(symbol,60,1)<=iHigh(symbol,60,2) && iLow(symbol,60,1)>=iLow(symbol,60,2) && iRSI(NULL,60,8,PRICE_MEDIAN,1)>23 && iStochastic(NULL,60,10,3,3,MODE_SMA,1 ,MODE_SIGNAL,1)<istochastic(null,60,10,3,3,mode_sma,1 ,mode_signal,2)=”” &&=”” istochastic(null,60,10,3,3,mode_sma,1=”” ,mode_signal,0)<istochastic(null,60,10,3,3,mode_sma,1=”” ,mode_signal,1)=”” close[0]<ima(symbol(),30,135,0,mode_ema,=”” price_high,0)=”” close[0]= 0; i–)

if (OrderSelect(i, SELECT_BY_POS))
if (OrderMagicNumber() == magic_number) {
order_type = OrderType();
if (order_type == ORDER_TYPE_SELL_STOP) {
if ((NormalizeDouble(OrderOpenPrice(), Digits) != open_price) || (NormalizeDouble(OrderStopLoss(), Digits) != stop_loss_price) || (NormalizeDouble(OrderTakeProfit(), Digits) != take_profit_price)) {
if (!OrderModify(OrderTicket(), open_price, stop_loss_price, take_profit_price, OrderExpiration()))
Print(“Error: “, ErrorDescription(_LastError));

}
break;
}
else if (order_type == ORDER_TYPE_SELL)
break;
}
if (i < 0)
if (OrderSend(symbol, OP_SELLSTOP, longsize, open_price, 3, stop_loss_price, take_profit_price, magic_number+” I.T. SSTP “, magic_number) < 0)

Print(“Error: “, ErrorDescription(_LastError));
}
else
for (i = OrdersTotal() – 1; i >= 0; i–)
if (TimeCurrent()-OrderOpenTime()>=ClosePendingInSeconds)
if (OrderSelect(i, SELECT_BY_POS))
if (OrderMagicNumber() == magic_number)
// if (OrderType() == ORDER_TYPE_BUY_LIMIT)
if (!OrderDelete(OrderTicket()))
Print(“Error: “, ErrorDescription(_LastError));

// L #1
if ((MathAbs(nshorts)==0 || nlongs>nshorts) &&
iHigh(symbol,60,1)-iLow(symbol,60,1)<(iHigh(symbol,60,2)-iLow(symbol,60,2))*.75 && iHigh(symbol,60,2)-iLow(symbol,60,2)>.0013 && iHigh(symbol,60,1)<=iHigh(symbol,60,2) && iLow(symbol,60,1)>=iLow(symbol,60,2) && iRSI(NULL,60,8,PRICE_MEDIAN,1)<82 && iStochastic(NULL,60,10,3,3,MODE_SMA,1 ,MODE_SIGNAL,1)>iStochastic(NULL,60,10,3,3,MODE_SMA,1 ,MODE_SIGNAL,2) && iStochastic(NULL,60,10,3,3,MODE_SMA,1 ,MODE_SIGNAL,0)>iStochastic(NULL,60,10,3,3,MODE_SMA,1 ,MODE_SIGNAL,1)

&& Close[0]>iMA(Symbol(),30,414,0,MODE_EMA, PRICE_LOW,0)
&& Close[0]>iMA(Symbol(),30,135,0,MODE_EMA, PRICE_LOW,0)
) {

if (nshorts==0) shortsize =MathAbs(NormalizeDouble(MaxLots*.7,2));
else shortsize =NormalizeDouble(MaxLots-nshorts,2);

open_price = NormalizeDouble((iHigh(symbol,60,1)+10*Point),5);
stop_loss_price = NormalizeDouble(iHigh(symbol,60,1)-(iHigh(symbol,60,1)-iLow(symbol,60,1))/2-40*Point, 6);
take_profit_price = NormalizeDouble(iHigh(symbol,60,1)+(iHigh(symbol,60,1)-iLow(symbol,60,1))*umulti, 6);

for (i = OrdersTotal() – 1; i >= 0; i–)
if (OrderSelect(i, SELECT_BY_POS))
if (OrderMagicNumber() == magic_number) {
order_type = OrderType();
if (order_type == ORDER_TYPE_BUY_STOP) {
if ((NormalizeDouble(OrderOpenPrice(), Digits) != open_price) || (NormalizeDouble(OrderStopLoss(), Digits) != stop_loss_price) || (NormalizeDouble(OrderTakeProfit(), Digits) != take_profit_price)) {
if (!OrderModify(OrderTicket(), open_price, stop_loss_price, take_profit_price, OrderExpiration()))
Print(“Error: “, ErrorDescription(_LastError));
}
break;
}
else if (order_type == ORDER_TYPE_BUY)
break;
}
if (i < 0)
if (OrderSend(symbol, OP_BUYSTOP, shortsize, open_price, 3, stop_loss_price, take_profit_price, magic_number+” I.T. BSTP “, magic_number) < 0)
Print(“Error: “, ErrorDescription(_LastError));
}
else
for (i = OrdersTotal() – 1; i >= 0; i–)
if (TimeCurrent()-OrderOpenTime()>=ClosePendingInSeconds)
if (OrderSelect(i, SELECT_BY_POS))
if (OrderMagicNumber() == magic_number)
// if (OrderType() == ORDER_TYPE_SELL_LIMIT)
if (!OrderDelete(OrderTicket()))
Print(“Error: “, ErrorDescription(_LastError));

return(0);
}

Just for the heck of it, what if I didn’t limit the number of orders that can be opened simultaneously?

Not that bad return for 2 months…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s