# How To Write an Auto-Trading Routine?

First you need to notice a feature that seems to repeat itself. The feature I found today was a sell signal.

Once you had 3 closes below a LEMA – and price was residing on the other side earlier -, you would start looking at a short sell starting 4 bars out. The short entry level is given: the low of the LEMA, and your stop can be put at the top of the LEMA.

So let’s make a screen plot for the sell condition – and include it in the God Awesome indicator – of course! (Oranged out for the sell level.)

//expiration 16 hours out

int ClosePendingInSeconds = 57600;

// You’ll need considerate sizing – meaning based on current equity size

extern double AF = 0.35;

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

{
break;
}
{
nlongs = nlongs+OrderLots();
longcount = longcount+1;
longaveragebuffer = longaveragebuffer+(OrderOpenPrice()*OrderLots());

}

if (OrderType() == OP_SELL )
{
nshorts = nshorts+OrderLots();
shortcount = shortcount+1;
shortaveragebuffer = shortaveragebuffer+(OrderOpenPrice()*OrderLots());

}
}

double equity=NormalizeDouble(AccountEquity()/1000*.9*AF,2);
double currentsize;

if (nlongs>nshorts) currentsize=nlongs;
else currentsize=nshorts;

double difference = equity-currentsize;

//Target distance calculation based on 3-period daily average true range (plus 30%)

//I used simple math to save on run time by avoiding a cycle

double ATRAVG=((iATR(NULL,1440,14,1)+iATR(NULL,1440,14,2)+iATR(NULL,1440,14,3))/3*1.3);

string symbol = Symbol();

int LEMA=414;
// Sell

if ( iClose(symbol,30,1)<iMA(symbol,30,LEMA,0,MODE_EMA, PRICE_LOW,1) &&
iClose(symbol,30,5)<iMA(symbol,30,LEMA,0,MODE_EMA, PRICE_CLOSE,5) &&
iClose(symbol,30,6)<iMA(symbol,30,LEMA,0,MODE_EMA, PRICE_CLOSE,6) &&
iClose(symbol,30,7)<iMA(symbol,30,LEMA,0,MODE_EMA, PRICE_CLOSE,7) &&
iClose(symbol,30,20)>iMA(symbol,30,LEMA,0,MODE_EMA, PRICE_HIGH,20) &&
iClose(symbol,30,21)>iMA(symbol,30,LEMA,0,MODE_EMA, PRICE_HIGH,21) &&
iClose(symbol,30,22)>iMA(symbol,30,LEMA,0,MODE_EMA, PRICE_HIGH,22)
)
{

open_price = NormalizeDouble(iMA(symbol,30,LEMA,0,MODE_EMA, PRICE_LOW,1), Digits);
stop_loss_price = NormalizeDouble(iMA(symbol,30,LEMA,0,MODE_EMA, PRICE_HIGH,1)+(iMA(symbol,30,LEMA,0,MODE_EMA, PRICE_HIGH,1)-iMA(symbol,30,LEMA,0,MODE_EMA, PRICE_HIGH,1)), Digits);
take_profit_price = NormalizeDouble(open_price – ATRAVG , Digits);

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_SELL_LIMIT) {
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(NULL, OP_SELLLIMIT, lots, open_price, 3, stop_loss_price, take_profit_price, magic_number+” LEMA SELL @ RSI “+DoubleToString(iRSI(NULL,0,2,PRICE_MEDIAN,0),2), magic_number,1) < 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));

That’s about 80% of what you will need.

The next step is to download and plug in the historical data you would need for the back testing. Additional filters may be needed of course. That is what back testing is for.

Cheerio, Fulliautomatix 