Author: Copyright � 2007, fxid10t@yahoo.com

This script is an automated trading tool designed to manage and execute trades based on a combination of momentum indicators, multi-timeframe analysis, and specific exit rules. It aims to identify "overbought" or "oversold" market conditions to determine optimal entry and exit points.

Core Monitoring Logic

The system continuously tracks several "Stochastic Oscillators" across different timeframes (ranging from 15 seconds up to 1440 minutes/24 hours). By looking at multiple timeframes simultaneously, the script attempts to understand both the immediate price action and the broader market trend.

Trade Entry Rules

The script looks for specific mathematical "extremes" to trigger new trades:

  • Overbought Conditions (Selling): The tool identifies when a market is potentially "too high" by checking if indicators on various timeframes are above certain thresholds (e.g., 80% or 90%). If these conditions align across multiple periods, it triggers a Sell order.
  • Oversold Conditions (Buying): Conversely, it identifies when a market is "too low" by checking if indicators fall below specific bottom thresholds (e.g., 10%, 20%, or 25%). When these levels are hit across multiple periods, it triggers a Buy order.
  • Trend Filtering: The logic includes checks to ensure that trades aren't just taken on raw numbers but also consider the relationship between short-term and long-term price movements (e.g., comparing 15-minute momentum against 1440-minute trends).

Trade Management and Exits

Once a trade is open, the script actively manages it based on the specific "Comment" attached to the trade at the time of opening:

  • Targeted Exits: Depending on whether the trade was entered as a "Daily," "4hr," or "Uptrend/Downtrend" play, the script checks different timeframes to see if the price has reached a point where it should be closed for profit.
  • Minimum Profit Protection: The tool ensures that trades are generally only closed when they have reached a specific minimum profit threshold.
  • Stop-Loss Logic: To protect the account from significant losses, the script includes a safety mechanism that will automatically close a trade if it hits a predefined negative loss limit.
  • Manual Override/Cleanup: It also monitors for instances where a trade might need to be closed simply because a different timeframe indicator has shifted significantly, regardless of the initial entry reason.

Visual and Informational Features

Beyond executing trades, the script provides several visual aids for the user:

  • Pivot Point Plotting: It automatically draws "Pivot Points" (support and resistance levels) on the chart. These include standard Pivot Points, Support/Resistance levels (S1, R1, S2, R2), and High/Low points from previous sessions.
  • Live Dashboard: It displays a real-time text readout on the screen showing critical account information, including current profit, spread, active indicator values, account balance, and equity.
  • Swap Information: It provides a notice regarding "Swap" costs (interest paid or earned for holding positions overnight) to help the user understand the cost of holding trades.
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each barSeries array that contains open time of each barSeries array that contains close prices for each bar
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
Indicators Used
Stochastic oscillator
9 Views
0 Downloads
0 Favorites
wip
//+------------------------------------------------------------------+
//|                                                          wip.mq4 |
//|                              Copyright © 2007, fxid10t@yahoo.com |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, fxid10t@yahoo.com"
#property link      "http://www.metaquotes.net"

//internal variables
double sto15s,sto60s,sto240s,sto1440s,sto10080s,sto43200s;
double sto15m,sto60m,sto240m,sto1440m,sto10080m,sto43200m;

double rng,sum.rng,avg.rng;
string DR;
int ticket,cnt,total;
double profit;
int MagicNumber=9876543210;
extern double lots=0.01;
extern double min.profit=0.05;
datetime TimePrev;
string Ordercomment="";
color Color;
//--**--
int init()  {return(0);}
int deinit()   {return(0);}
int start() {  Pivot.Plot();
//stoch signal
sto15s = iStochastic(Symbol(),15,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
sto60s = iStochastic(Symbol(),60,5,3,3,MODE_SMA,0,MODE_SIGNAL,1);
sto240s = iStochastic(Symbol(),240,5,3,3,MODE_SMA,0,MODE_SIGNAL,1);
sto1440s = iStochastic(Symbol(),1440,5,3,3,MODE_SMA,0,MODE_SIGNAL,1);
/*
sto10080s = iStochastic(Symbol(),10080,5,3,3,MODE_SMA,0,MODE_SIGNAL,1);
sto43200s = iStochastic(Symbol(),43200,5,3,3,MODE_SMA,0,MODE_SIGNAL,1);    */
//stoch main
sto15m = iStochastic(Symbol(),15,5,3,3,MODE_SMA,0,MODE_MAIN,0);
sto60m = iStochastic(Symbol(),60,5,3,3,MODE_SMA,0,MODE_MAIN,1);
sto240m = iStochastic(Symbol(),240,5,3,3,MODE_SMA,0,MODE_MAIN,1);
sto1440m = iStochastic(Symbol(),1440,5,3,3,MODE_SMA,0,MODE_MAIN,1);
/*
sto10080m = iStochastic(Symbol(),10080,5,3,3,MODE_SMA,0,MODE_MAIN,1);
sto43200m = iStochastic(Symbol(),43200,5,3,3,MODE_SMA,0,MODE_MAIN,1);      */
//
total=OrdersTotal();
if(total>0)  {
   for(cnt=0; cnt<OrdersTotal(); cnt++) {
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) {
         profit=0;

         if(OrderType()==OP_BUY) {  profit=OrderProfit();
            if(OrderComment()=="Oversold Daily" && sto240s>=80.0000 && profit > min.profit &&
               sto15m > 80.0000)   {
               OrderClose(OrderTicket(),OrderLots(),Bid,MarketInfo(Symbol(),MODE_SPREAD),Olive);
               return;   }
            if(OrderComment()=="Oversold 4hr" && sto60m>=80.0000 && profit > min.profit)    {
               OrderClose(OrderTicket(),OrderLots(),Bid,MarketInfo(Symbol(),MODE_SPREAD),Olive);
               return;   }
            if(OrderComment()=="Oversold uptrend" && sto15s > 80.0000 && profit > min.profit)  { 
               OrderClose(OrderTicket(),OrderLots(),Bid,MarketInfo(Symbol(),MODE_SPREAD),Olive);
               return;   }
            if(profit <= (-3.00/0.01)*lots) {
               OrderClose(OrderTicket(),OrderLots(),Bid,MarketInfo(Symbol(),MODE_SPREAD),Olive);
               return;   }
            if(sto15m >= 75.000 && profit > min.profit && OrderComment()=="")  {
               OrderClose(OrderTicket(),OrderLots(),Bid,MarketInfo(Symbol(),MODE_SPREAD),MintCream);
               return;   }
              }  }

         if(OrderType()==OP_SELL)   {  profit=OrderProfit();
            if(OrderComment()=="Overbought Daily" && sto240s<=20.0000 && profit > min.profit &&
               sto15m < 20.0000)   {
               OrderClose(OrderTicket(),OrderLots(),Ask,MarketInfo(Symbol(),MODE_SPREAD),Violet);
               return;  }
            if(OrderComment()=="Overbought 4hr" && sto60m<=20.0000 && profit > min.profit)     {
               OrderClose(OrderTicket(),OrderLots(),Ask,MarketInfo(Symbol(),MODE_SPREAD),Violet);
               return;  }
            if(OrderComment()=="Overbought downtrend" && sto15s < 20.0000 && profit > min.profit) {
               OrderClose(OrderTicket(),OrderLots(),Ask,MarketInfo(Symbol(),MODE_SPREAD),Violet);
               return;  }
            if(profit <= (-3.00/0.01)*lots) {
               OrderClose(OrderTicket(),OrderLots(),Ask,MarketInfo(Symbol(),MODE_SPREAD),Violet);
               return;  }             
            if(sto15m <= 25.000 && profit > min.profit && OrderComment()=="") {
               OrderClose(OrderTicket(),OrderLots(),Ask,MarketInfo(Symbol(),MODE_SPREAD),MintCream);
               return;  }

              }  }  }
comments();
Ordercomment="";
Daily.Range();
if( Time[0] == TimePrev ) { return(0); }
//---- Yes we have a new bar ... set TimePrev & continue
TimePrev = Time[0] ;
if(sto15m==0   || sto15s==0   ||
   sto60m==0   || sto60s==0   ||
   sto240m==0  || sto240s==0  ||
   sto1440m==0 || sto1440s==0)   {return(0);}


if(sto1440s > 90.0000 && sto15m >80.0000)   {
   Ordercomment="Overbought Daily"; Color=Red; Sell();   }
if(sto1440s < 10.0000 && sto15m < 20.0000)   {
   Ordercomment="Oversold Daily"; Color=Blue; Buy();  }        

/*
if(sto240s > 90.0000 && sto60m > 80 && sto15m > 80.0000)   {
   Ordercomment="Overbought 4hr"; Color=Crimson; Sell(); }     sells to early
if(sto240s < 10.0000 && sto60m < 20 && sto15m < 20.0000)   {
   Ordercomment="Oversold 4hr"; Color=MediumBlue; Buy(); }     buys to early*/

if( sto1440m > 20.0000 &&/* sto240s  > 20.0000 &&
    (sto1440m < sto1440s) &&*/ (sto240m  < 75.0000)  && 
    sto60s > 80.0000 && sto15s > 80.0000)  {
   Ordercomment="Overbought downtrend"; Color=OrangeRed; Sell();  }
if( sto1440m < 80.0000 &&/* sto240s  < 80.0000 &&
    (sto1440m > sto1440s) &&*/ (sto240m  > 25.0000)  && 
    sto60s < 20.0000 && sto15s < 20.0000)  {
   Ordercomment="Oversold uptrend"; Color=RoyalBlue; Buy();   }
/*
if( sto15m > 80.0000 && sto60m > 80.0000)   {
   Ordercomment=""; Color=DeepPink; Sell();  }
if( sto15m < 20.0000 && sto60m < 20.0000)   {
   Ordercomment=""; Color=Lime; Buy(); }           */

// origional scripting
if( ((sto1440s < 80 && (sto1440m > sto1440s)) || sto1440m < 10) &&
  ( ((sto240s  < 80 && (sto240m  > sto240s))  || sto240m  < 10)  &&
     sto60s<20 && sto15s<20) ) {
//   buy...
   Ordercomment=""; Color=Lime; Buy(); }
   
if( ((sto1440s > 20 && (sto1440m < sto1440s)) || sto1440m > 90) &&
  ( ((sto240s  > 20 && (sto240m  < sto240s))  || sto240m  > 90)  &&
     sto60s>80 && sto15s>80)  ) {
//   sell...
   Ordercomment=""; Color=DeepPink; Sell();  }

return(0);}
//+------------------------------------------------------------------+
void Buy()  {
   ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,//Ask
                    MarketInfo(Symbol(),MODE_SPREAD),//slippage
                    0,//no stoploss defined
                    0,//no tp defined
                    Ordercomment,//comment
                    MagicNumber,
                    0,//time expire,
                    Color);
                    if(ticket>0)   {
                        if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))  {Print(ticket);}
                        else Print("Error Opening BuyStop Order: ",GetLastError());
                    return(0);   }  }
void Sell() {
   ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,//Bid
                    MarketInfo(Symbol(),MODE_SPREAD),//slippage
                    0,//no stoploss set when stop order is executed
                    0,//no tp defined
                    Ordercomment,
                    MagicNumber,
                    0,//time expire,
                          Color);
                          if(ticket>0)   {
                              if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))   {Print(ticket);}
                              else Print("Error Opening SellStop Order: ",GetLastError());
                          return(0);   }  }

void comments()   {
if(MarketInfo(Symbol(),MODE_SWAPLONG)>0) string swap="longs,"; else swap="shorts.";
if(MarketInfo(Symbol(),MODE_SWAPLONG)<0 && MarketInfo(Symbol(),MODE_SWAPSHORT)<0) swap="your broker, :(";
   Comment("Last Tick: ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS),"\n",
           "Swap favors ",swap,
           " Swap Long ",MarketInfo(Symbol(),MODE_SWAPLONG),
           ", Swap Short ",MarketInfo(Symbol(),MODE_SWAPSHORT),"\n",
           /*"Average Daily Range: ",NormalizeDouble(avg.rng,Digits),"\n",*/
           "Current Spread: ",Ask-Bid,", Profit:   ",profit,"\n",
           "sto15s:   ",sto15s,"   sto15m:   ",sto15m,"\n",
           "sto60s:   ",sto60s,"   sto60m:   ",sto60m,"\n",
           "sto240s:  ",sto240s,"  sto240m:  ",sto240m,"\n",
           "sto1440s: ",sto1440s," sto1440m: ",sto1440m,"\n",
           "AccountProfit() ",AccountProfit(),"\n",
           "AccountBalance() ",AccountBalance(),"\n",
           "AccountEquity() ",AccountEquity()); }

double Daily.Range() {
   if(DR==TimeToStr(TimeCurrent(),TIME_DATE)) 
{return(NormalizeDouble(avg.rng,Digits));}
   //Print(DR,"  ",NormalizeDouble(avg.rng,Digits));
   rng=0;sum.rng=0;avg.rng=0;
   for(int i=0;i<iBars(Symbol(),1440);i++) {
      rng=(iHigh(Symbol(),PERIOD_D1,i)-iLow(Symbol(),PERIOD_D1,i));
      sum.rng+=rng;}

   double db=iBars(Symbol(),1440);   
   avg.rng=sum.rng/db;
   DR=TimeToStr(TimeCurrent(),TIME_DATE);
return(NormalizeDouble(avg.rng,Digits));}

void Pivot.Plot()  {
   string t1,t2,PPn,s1n,s2n,r1n,r2n,LOPS1n,LOPS2n,HOPS1n,HOPS2n;
   double PP,s1,s2,r1,r2,LOPS1,LOPS2,HOPS1,HOPS2,H,L,C;
   
   PPn="PP "+TimeToStr(CurTime(),TIME_DATE);
   s1n="S1 "+TimeToStr(CurTime(),TIME_DATE);
   s2n="S2 "+TimeToStr(CurTime(),TIME_DATE);
   r1n="R1 "+TimeToStr(CurTime(),TIME_DATE);
   r2n="R2 "+TimeToStr(CurTime(),TIME_DATE);
   LOPS1n="LOPS1 "+TimeToStr(CurTime(),TIME_DATE);
   LOPS2n="LOPS2 "+TimeToStr(CurTime(),TIME_DATE);
   HOPS1n="HOPS1 "+TimeToStr(CurTime(),TIME_DATE);
   HOPS2n="HOPS2 "+TimeToStr(CurTime(),TIME_DATE);
   
   H=iHigh(Symbol(),PERIOD_D1,1);
   L=iLow(Symbol(),PERIOD_D1,1);
   C=iClose(Symbol(),PERIOD_D1,1);
   LOPS1=iLow(Symbol(),PERIOD_D1,1);
   LOPS2=iLow(Symbol(),PERIOD_D1,2);
   HOPS1=iHigh(Symbol(),PERIOD_D1,1);
   HOPS2=iHigh(Symbol(),PERIOD_D1,2);
   
   t1=TimeToStr(CurTime(),TIME_DATE)+" 00:00:00";
   t2=TimeToStr(CurTime(),TIME_DATE)+" 23:59:59";
   
   PP=NormalizeDouble((H+L+C)/3,Digits);
   s1=NormalizeDouble((PP*2)-H,Digits);
   r1=NormalizeDouble((PP*2)-L,Digits);
   s2=NormalizeDouble(PP-(r1-s1),Digits);
   r2=NormalizeDouble(PP+(r1-s1),Digits);
   ObjectCreate(PPn,OBJ_TREND,0,StrToTime(t1),PP,StrToTime(t2),PP);
   ObjectSet(PPn,10,false);
   ObjectSet(PPn,6,Gray);
   ObjectSet(PPn,7,STYLE_DOT);
   ObjectSet(PPn,9,true);
   ObjectCreate(s1n,OBJ_TREND,0,StrToTime(t1),s1,StrToTime(t2),s1);
   ObjectSet(s1n,10,false);
   ObjectSet(s1n,6,LightCoral);
   ObjectSet(s1n,7,STYLE_DOT);
   ObjectSet(s1n,9,true);
   ObjectCreate(s2n,OBJ_TREND,0,StrToTime(t1),s2,StrToTime(t2),s2);
   ObjectSet(s2n,10,false);
   ObjectSet(s2n,6,Tomato);
   ObjectSet(s2n,7,STYLE_DOT);
   ObjectSet(s2n,9,true);
   ObjectCreate(r1n,OBJ_TREND,0,StrToTime(t1),r1,StrToTime(t2),r1);
   ObjectSet(r1n,10,false);
   ObjectSet(r1n,6,LightGreen);
   ObjectSet(r1n,7,STYLE_DOT);
   ObjectSet(r1n,9,true);
   ObjectCreate(r2n,OBJ_TREND,0,StrToTime(t1),r2,StrToTime(t2),r2);
   ObjectSet(r2n,10,false);
   ObjectSet(r2n,6,SpringGreen);
   ObjectSet(r2n,7,STYLE_DOT);
   ObjectSet(r2n,9,true);
   ObjectCreate(LOPS1n,OBJ_TREND,0,StrToTime(t1),LOPS1,StrToTime(t2),LOPS1);
   ObjectSet(LOPS1n,10,false);
   ObjectSet(LOPS1n,6,LightGreen);
   ObjectSet(LOPS1n,7,STYLE_DASH);
   ObjectSet(LOPS1n,9,true);
   ObjectCreate(LOPS2n,OBJ_TREND,0,StrToTime(t1),LOPS2,StrToTime(t2),LOPS2);
   ObjectSet(LOPS2n,10,false);
   ObjectSet(LOPS2n,6,SpringGreen);
   ObjectSet(LOPS2n,7,STYLE_DASH);
   ObjectSet(LOPS2n,9,true);
   ObjectCreate(HOPS1n,OBJ_TREND,0,StrToTime(t1),HOPS1,StrToTime(t2),HOPS1);
   ObjectSet(HOPS1n,10,false);
   ObjectSet(HOPS1n,6,LightCoral);
   ObjectSet(HOPS1n,7,STYLE_DASH);
   ObjectSet(HOPS1n,9,true);
   ObjectCreate(HOPS2n,OBJ_TREND,0,StrToTime(t1),HOPS2,StrToTime(t2),HOPS2);
   ObjectSet(HOPS2n,10,false);
   ObjectSet(HOPS2n,6,Tomato);
   ObjectSet(HOPS2n,7,STYLE_DASH);
   ObjectSet(HOPS2n,9,true);

   if(Period()>=1440)   {ObjectDelete(PPn);   ObjectDelete(s1n);   ObjectDelete(s2n);   ObjectDelete(r1n);
                         ObjectDelete(r2n);   ObjectDelete(LOPS1n);ObjectDelete(LOPS2n);ObjectDelete(HOPS1n);
                         ObjectDelete(HOPS2n); }}

Comments