//+------------------------------------------------------------------+ //| SpiNNaker v0.50b.mq4 | //| Copyright © 2009 Tom Balfe | //| http://www.forex-tsd.com/members/nittany1.html | //| redcarsarasota@yahoo.com | //| | //| This is an Neural Network expert advisor that is fed data | //| from the stochastics and ADX indicators. It is a single | //| layer perceptron which needs to be trained manually to adjust | //| the weights. | //| | //| This EA is multi-timeframe in that it uses data from both the | //| 5m and 15m timeframes to feed into the input neurodes. | //| | //| There are six units in the neural network: four input | //| units (stochastics 5m, stochastics 15m, adx 5m, adx 15m) and | //| two output units (buy, sell). There are 8 adjustable weights | //| that need to be adjusted by optimization. | //| | //| If you are going to use this EA on separate currency pairs at | //| the same time please change the MagicNumber to something unique | //| for each currency pair. | //| | //| Also optimize the weights and indicator settings for each | //| currency pair. | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| RESEARCH | //| Here are a list of websites that I used for learning about | //| neural networks. | //| | //| http://www.benbest.com/computer/nn.html | //| http://www.ai-junkie.com/ann/evolved/nnt1.html | //| http://www.ibm.com/developerworks/library/l-neural/ | //| | //| There were others but I lost track of all my URLs. | //+------------------------------------------------------------------+ #property copyright "Copyright © 2009 Tom Balfe" #property link "mailto:redcarsarasota@yahoo.com" #property link "http://www.forex-tsd.com/members/nittany1.html" #property link "http://www.myspace.com/tomsarasota" bool BUY = false; // initialize output units to false bool SELL = false; // we don't want trades by mistake double bTemp=0,sTemp=0; // place to hold buy signal before putting it through // threshold function int s5m_t=0,s15m_t=0,a5m_t=0,a15m_t=0; // place to hold indicator signals for perceptron // calculations //+------------------------------------------------------------------+ //| USER ADJUSTABLE STUFF | //+------------------------------------------------------------------+ extern string TopOfSetting = "=== SpiNNaker v0.50b EA ==="; extern int MagicNumber = 322112; extern string Order_Settings = "=== Order Settings ==="; extern double StaticLots = 0.01; extern int Slippage = 5; extern int TP = 15; extern int SL = 25; //--- only change the values during backtesting/optimization //--- do *not* change the signs (neg/pos) or you'll bust the neural network //--- for example, try backtest/optimize of 0.1 to 0.9 in 0.1 step, or //--- -0.1 to -0.9 in 0.1 step extern string NN_Weights = "=== NN Weights ==="; extern double w1b = 0.5; // 5 min stoch weight for buy extern double w1s = -0.5; // 5 min stoch weight for sell extern double w2b = 0.5; // 15 min stoch weight for buy extern double w2s = -0.5; // 15 min stoch weight for sell extern double w3b = 0.5; // 5 min ADX weight for buy extern double w3s = -0.5; // 5 min ADX weight for sell extern double w4b = 0.5; // 15 min ADX weight for buy extern double w4s = -0.5; // 15 min ADX weight for sell extern string NN_Threshold = "== NN Threshold ==="; extern double ThresholdBUY = 1.50; extern double ThresholdSELL = 1.50; extern string Stoch_Settings = "=== Stochastic Settings ==="; extern int PercentK_m5 = 8; extern int PercentD_m5 = 3; extern int Slowing_m5 = 3; extern int PercentK_m15 = 8; extern int PercentD_m15 = 3; extern int Slowing_m15 = 3; extern string ADX_Settings = "=== ADX Settings ==="; extern int ADXTrend = 20; extern string MM_Settings = "=== Money Management ==="; extern bool UseMM = false; extern int PercentAtRisk = 3; //+------------------------------------------------------------------+ //| DON'T CHANGE BELOW HERE | //+------------------------------------------------------------------+ //| Program initializes and deinitializes here. | //+------------------------------------------------------------------+ string EAComment = "SpiNNaker v0.50b EA"; string EATitle = "SPINNAKER v0.50b EA © 2009 TOM BALFE"; int OpenOrders = 1; datetime PreviousBar; int init() { ObjectCreate("Title",OBJ_LABEL,0,0,0); ObjectSet("Title",OBJPROP_XDISTANCE,325); ObjectSet("Title",OBJPROP_YDISTANCE,2); ObjectSetText("Title",EATitle,9,"Lucida Sans Regular",PaleTurquoise); return(0); } int deinit() { if (TotalOpenOrders() > 0) { CloseAll(); } ObjectsDeleteAll(); ObjectDelete("Title"); return(0); } int start() { //--- INDICATORS //--- Stochastics M5 double sMain_m5 = iStochastic(NULL,5,PercentK_m5,PercentD_m5,Slowing_m5,MODE_SMA,0,MODE_MAIN,0); double sSignal_m5 = iStochastic(NULL,5,PercentK_m5,PercentD_m5,Slowing_m5,MODE_SMA,0,MODE_SIGNAL,0); //--- Stochastics M15 double sMain_m15 = iStochastic(NULL,15,PercentK_m15,PercentD_m15,Slowing_m15,MODE_SMA,0,MODE_MAIN,0); double sSignal_m15 = iStochastic(NULL,15,PercentK_m15,PercentD_m15,Slowing_m15,MODE_SMA,0,MODE_SIGNAL,0); //--- M5 ADX data double adx_m5 = iADX(NULL,5,14,PRICE_CLOSE,0,0); // ADX 5 min double di_p_m5 = iADX(NULL,5,14,PRICE_CLOSE,1,0); // DI+ 5 min double di_m_m5 = iADX(NULL,5,14,PRICE_CLOSE,2,0); // DI- 5 min //--- M15 ADX data double adx_m15 = iADX(NULL,15,14,PRICE_CLOSE,0,0); // ADX 15 min double di_p_m15 = iADX(NULL,15,14,PRICE_CLOSE,1,0); // DI+ 15 min double di_m_m15 = iADX(NULL,15,14,PRICE_CLOSE,2,0); // DI- 15 min //--- INDICATORS TURNED INTO SIGNALS //--- feed indicator data to give signals for each timeframe if (sMain_m5>sSignal_m5) { s5m_t = 1; } // stoch 5m s5m_t else if (sMain_m5<sSignal_m5) { s5m_t = -1; } else { s5m_t = 0; } if (sMain_m15>sSignal_m15) { s15m_t = 1; } // stoch 15m s15m_t else if (sMain_m15<sSignal_m15) { s15m_t = -1; } else { s15m_t = 0; } if ((adx_m5>ADXTrend)&&(di_p_m5>di_m_m5)) { a5m_t = 1; } // adx 5m a5m_t else if ((adx_m5>ADXTrend)&&(di_p_m5<di_m_m5)) { a5m_t = -1; } else { a5m_t = 0; } if ((adx_m15>ADXTrend)&&(di_p_m15>di_m_m15)) { a15m_t = 1; } // adx 15m a15m_t else if ((adx_m15>ADXTrend)&&(di_p_m15<di_m_m15)) { a15m_t = -1; } else { a15m_t = 0; } //--- NEURAL NETWORK STUFF //--- buy perceptron first, order: 5m stoch, 15m stoch, 5m adx, 15m adx bTemp = ((s5m_t*w1b) + (s15m_t*w2b) + (a5m_t*w3b) + (a15m_t*w4b)); //--- sell perceptron next, order: 5m stoch, 15m stoch, 5m adx, 15m adx sTemp = ((s5m_t*w1s) + (s15m_t*w2s) + (a5m_t*w3s) + (a15m_t*w4s)); //--- buy threshold function if (bTemp>ThresholdBUY) { BUY = true; } //--- sell threshold function if (sTemp>ThresholdSELL) { SELL = true; } //--- ORDER MANAGEMENT //--- look for closes first, if none are found, open one if conditions are right if (NewBar() == true) { //---------------- check indicators and look for a CLOSE ---// if (TotalOpenOrders() == OpenOrders) { if (OrderType() == OP_BUY) { //--- look for close of open buy positions if (BUY == false) { CloseAll(); } } if (OrderType() == OP_SELL) { //--- look for close of open sell positions if (SELL == false) { CloseAll(); } } } //---------------- check indicators and look for a ORDER ENTRY ---// if (TotalOpenOrders() < OpenOrders) { //-- BUY if (BUY == true) { OrderSend(Symbol(),OP_BUY,LotSize(),Ask,Slippage,Ask-SL*Point,Ask+TP*Point,EAComment,MagicNumber,0,Lime); } //-- SELL if (SELL == true) { OrderSend(Symbol(),OP_SELL,LotSize(),Bid,Slippage,Bid+SL*Point,Ask-TP*Point,EAComment,MagicNumber,0,Red); } } } ShowInfo(); return(0); } //+------------------------------------------------------------------+ //| COMMON FUNCTIONS | //+------------------------------------------------------------------+ //| The expert will call these from the main loop. | //+------------------------------------------------------------------+ //+-----------------------------------------------------+ //| new bar | //+-----------------------------------------------------+ bool NewBar() { if(PreviousBar<Time[0]) { PreviousBar = Time[0]; return(true); } else { return(false); } return(false); } //+-----------------------------------------------------+ //| calculate lot size | //+-----------------------------------------------------+ double LotSize() { if (UseMM == true) { double Lots = MathCeil(AccountFreeMargin() * PercentAtRisk / 1000) / 100; } else { Lots = StaticLots; } return(Lots); } //+-----------------------------------------------------+ //| close all | //+-----------------------------------------------------+ //| Finds any open orders & closes them | //+-----------------------------------------------------+ void CloseAll() { int total = OrdersTotal(); for(int i=total-1;i>=0;i--) { OrderSelect(i, SELECT_BY_POS); int type = OrderType(); if (OrderSymbol()==Symbol() && (OrderMagicNumber() == MagicNumber)) { //-- Close open BUYs if (type == OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,CornflowerBlue); //-- Close open SELLS if (type == OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,CornflowerBlue); } } return; } //+-----------------------------------------------------+ //| calculate open orders | //+-----------------------------------------------------+ int TotalOpenOrders() { int j = 0; j=OrdersTotal(); int TotalOpenOrders = 0; if(j==0) { return (0); } else { for(;j>=0;j--) { RefreshRates(); OrderSelect(j,SELECT_BY_POS); if(OrderMagicNumber()==MagicNumber) { TotalOpenOrders++; } } } return(TotalOpenOrders); } //+-----------------------------------------------------+ //| show summary panel on chart | //+-----------------------------------------------------+ void ShowInfo() { Comment("...::: SpiNNaker v0.50b :::...\n", "Copyright © 2009 Tom Balfe\n", "Currency Pair: ", Symbol(),"\n", "Price: ",NormalizeDouble(Bid,4),"\n", "Date: ",Month(),"-",Day(),"-",Year()," Server Time: ",Hour(),":",Minute(),":",Seconds(),"\n", "Minimum Lot Size: ",MarketInfo(Symbol(),MODE_MINLOT)); return(0); }
Sample
Analysis
Market Information Used:
Series array that contains open time of each bar
Indicator Curves created:
Indicators Used:
Stochastic oscillator
Movement directional index
Custom Indicators Used:
Order Management characteristics:
It automatically opens orders when conditions are reached
Checks for the total of open orders
It Closes Orders by itself
Other Features: