From 7b6aa140388bcc043df0fbb15ddcea6c5c66e63f Mon Sep 17 00:00:00 2001 From: asenzett Date: Mon, 29 Aug 2011 06:02:39 +0200 Subject: [PATCH] bleh --- SpeedTrainNeuNeu.mq4 | 359 +++++++-------- indicators/jmarket_sentiment.mq4 | 62 ++- indicators/jmarket_sentiment.mq4.BACKUP.4229.mq4 | 554 ----------------------- indicators/jmarket_sentiment.mq4.BASE.4229.mq4 | 533 ---------------------- indicators/jmarket_sentiment.mq4.LOCAL.4229.mq4 | 533 ---------------------- indicators/jmarket_sentiment.mq4.REMOTE.4229.mq4 | 550 ---------------------- 6 files changed, 204 insertions(+), 2387 deletions(-) delete mode 100755 indicators/jmarket_sentiment.mq4.BACKUP.4229.mq4 delete mode 100644 indicators/jmarket_sentiment.mq4.BASE.4229.mq4 delete mode 100644 indicators/jmarket_sentiment.mq4.LOCAL.4229.mq4 delete mode 100644 indicators/jmarket_sentiment.mq4.REMOTE.4229.mq4 diff --git a/SpeedTrainNeuNeu.mq4 b/SpeedTrainNeuNeu.mq4 index 4d62435..5a33ca1 100644 --- a/SpeedTrainNeuNeu.mq4 +++ b/SpeedTrainNeuNeu.mq4 @@ -1,35 +1,39 @@ #property copyright "Zarko Asenov" #property link "jaltoh.6x.to" + +#define RETRIES 5 +#define SLIPPAGE 3 + + /* General functionality settings */ extern string _ = " General Settings "; extern double Lot_Size = 0.12; -extern double Pip_Step = 65.0; // For variable spread brokers keep it well above the average guaranteed spread -extern bool Auto_Pip_Step = true; // Pip step is deduced from average bar height -extern int Magic_Number = 775225426; -extern bool Clear_Stops = true; -extern double Order_Lifetime_Hours = 1; -extern bool Add_Spread_to_PipStep = false; // For fixed spread brokers - be sure about this! -extern bool Add_MinStop_to_PipStep = true; -extern double Max_spread = 22; // dont send orders above this spread -extern double Hours_Bring_Even = 0; // wait for hours before an active order stop loss is pushed up +extern double Pip_Step = 0; // On variable spread brokers keep it over average spread, 0 automatic step +extern bool Add_Spread_to_Pip_Step = false; // For fixed spread brokers - be sure about this! +extern bool Add_Min_Stop_to_Pip_Step = false; +extern double Order_Lifetime_Hours = 1; // Pending order life time +extern double Max_spread = 22; // dont send orders with prices above this spread +extern double Hours_Bring_Even = 0; // wait for N hours before an active order stop loss is pushed up extern bool Debug_Print = true; extern double Panic_Profit = -200.0; extern int Midprice_Time_Frame = PERIOD_H1; extern double Calmdown_Hours = 2; extern double Go_After = 0.4; // periods +extern int Magic_Number = 775225426; /* Grid Bandwidth */ extern string __ = " Market noise bandwidth "; -extern int BW_Back_Period = 32; -extern int BW_Time_Frame = PERIOD_M30; +extern int BW_Back_Period = 39; +extern int BW_Time_Frame = 0; /* Sentiment */ extern string ___ = " Sentiment"; -extern double Sentiment_Threshold = 0.0007; -extern int Sentiment_MA_Bars = 32; -extern double Sentiment_Time_Frame = PERIOD_M15; -extern int Sentiment_Bars_Check = 2; +extern double Sentiment_Threshold = 0.15; +extern int Sentiment_MA_Bars = 12; +extern double Sentiment_Time_Frame = 0; +extern int Sentiment_Bars_Check = 1; +extern double Sentiment_Time_Factor = 0.01; /* Average True Range */ extern string ____ = " ATR"; @@ -47,11 +51,19 @@ double current_atr = 0.0; double start_price = 0.0; bool init_done = false; double spread; + double pipstep_adjusted; +double pipstep_pt; + int order_lifetime; double time_bring_even; + double tick_value; +double tick_value_pt; + double min_stop; +double min_stop_pt; + double total_spread_loss; int start_bin; double inv_sentiment_threshold; @@ -62,16 +74,27 @@ double auto_step; datetime prev_bar_time; -#define RETRIES 5 -#define SLIPPAGE 3 + + int init() { + if (RefreshRates() == false) + Print("Refresh rates failed!"); + + order_lifetime = MathRound(Order_Lifetime_Hours * 3600); time_bring_even = 3600 * Hours_Bring_Even; + min_stop = MarketInfo(Symbol(), MODE_STOPLEVEL); + min_stop_pt = Point * MarketInfo(Symbol(), MODE_STOPLEVEL); + tick_value = MarketInfo(Symbol(), MODE_TICKVALUE); + tick_value_pt = Point * MarketInfo(Symbol(), MODE_TICKVALUE); + + pipstep_pt = Pip_Step * Point; + inv_sentiment_threshold = -1.0 * Sentiment_Threshold; calmdown_period = Calmdown_Hours * 3600; @@ -79,7 +102,11 @@ int init() auto_step = 0; prev_bar_time = 0; - Print("Min stop: " + min_stop + ", Tick Value: " + tick_value); + Print( + "Min stop: " + min_stop + + ", Tick Value: " + tick_value + + ", Number of our orders: " + num_orders + + ", Grid height: " + pipstep_pt); return(0); } @@ -114,7 +141,7 @@ calc_profit() return (presult); } -datetime Last_Go = 0; +datetime last_go_time = 0; void close_all_orders() @@ -201,28 +228,33 @@ int sentiment = NEUTRAL; double get_grid() { - return ( iCustom(Symbol(), BW_Time_Frame, "jmarket_sentiment", Sentiment_MA_Bars, 0, BW_Back_Period, 1, "", "", "", sentiment, "", "", "", 3, 0) ); + return ( + 1.2 * iCustom( + Symbol(), BW_Time_Frame, "Bandwidth", + 0, BW_Back_Period, 1, "", false, PRICE_WEIGHTED, + 2, 0) + ); } double get_sentiment(int index, int bar) { - return ( iCustom(Symbol(), Sentiment_Time_Frame, "jmarket_sentiment", Sentiment_MA_Bars, 0, Sentiment_MA_Bars, 0, "", "", "", NEUTRAL, "", "", "", index, bar) ); + return ( + iCustom( + Symbol(), Sentiment_Time_Frame, "jmarket_sentiment", + Sentiment_MA_Bars, Sentiment_MA_Bars, 0, Sentiment_MA_Bars, Sentiment_Time_Factor, 0.9, 0, "", "", "", NEUTRAL, "", "", "", + index, bar) + ); } bool go_buy() { - if (end_is_neigh(Sentiment_Time_Frame) == false) return (false); - double threshold_adjusted = Sentiment_Threshold; - if ( pipstep_adjusted > threshold_adjusted ) - threshold_adjusted = pipstep_adjusted; for (int ix = 0; ix < Sentiment_Bars_Check; ix++) { - if (get_sentiment(2, ix) < threshold_adjusted) return (false); if (get_sentiment(0, ix) < threshold_adjusted) return (false); - } + } sentiment = BULLISH; return (true); @@ -231,14 +263,9 @@ go_buy() bool go_sell() { - if (end_is_neigh(Sentiment_Time_Frame) == false) return (false); - double inv_threshold_adjusted = inv_sentiment_threshold; - if ( (-1. * pipstep_adjusted) < inv_threshold_adjusted ) - inv_threshold_adjusted = -1. * pipstep_adjusted; for (int ix = 0; ix < Sentiment_Bars_Check; ix++) { - if (get_sentiment(2, ix) > inv_threshold_adjusted) return (false); if (get_sentiment(0, ix) > inv_threshold_adjusted) return (false); } @@ -249,88 +276,106 @@ go_sell() double get_auto_step() { + // init auto step if (auto_step == 0) auto_step = get_grid(); + // return previous unchaged step if already trading + if (num_orders > 0) return (auto_step); + + // same bar same step if (Time[0] == prev_bar_time) return (auto_step); - if (num_orders != 0) return (auto_step); + + // Lets see .. + // get recommended grid height double _step = get_grid(); - if (_step == 0) return (auto_step); - else auto_step = _step; + + // if errored out stick with previous one + if (_step == 0) { + Print("Fatal, got zero auto step. Returning old step " + auto_step); + return (auto_step); + } else { + auto_step = _step; + } if (Debug_Print == true) Print("New step set to: " + auto_step); + prev_bar_time = Time[0]; - init_done = false; // reinit! return (auto_step); } int start() { - if(start_price == 0.0) { - start_price = Ask; - if (Debug_Print) Print("Start price: " + start_price); + num_orders = count_orders(); + if (num_orders < 1) { + init_done = false; // reinit! + last_go_time = 0; } - num_orders = count_orders(); RefreshRates(); spread = Point * MarketInfo(Symbol(), MODE_SPREAD); - double current_profit = 0; - + if ( - (Last_Go != 0) && - ((TimeCurrent() - Last_Go) > calmdown_period) + (last_go_time != 0) && + ((TimeCurrent() - last_go_time) > calmdown_period) ) { close_all_orders(); - Last_Go = 0; + last_go_time = 0; } - pipstep_adjusted = 0; - if (Auto_Pip_Step == true) pipstep_adjusted = get_auto_step(); - else pipstep_adjusted = Pip_Step * Point; + if (pipstep_pt != 0) pipstep_adjusted = pipstep_pt; + else pipstep_adjusted = get_auto_step(); /* nothing to do with step 0 */ - if (pipstep_adjusted == 0.0) return (0); - - if (Add_Spread_to_PipStep == true) pipstep_adjusted += spread; - if (Add_MinStop_to_PipStep == true) pipstep_adjusted += (min_stop * Point); + if (pipstep_adjusted == 0) { + Print("Fatal, pipstep is zero! Skipping tick."); + return (0); + } + + if (Add_Spread_to_Pip_Step) pipstep_adjusted += spread; + if (Add_Min_Stop_to_Pip_Step) pipstep_adjusted += min_stop_pt; if ( init_done == false ) { - /* fire up new grid */ + /* fire up new grid */ if ( go_buy() ) { start_price = Ask; + start_bin = MathFloor(start_price / pipstep_adjusted); + if (Debug_Print) Print("Init go long, start price: " + start_price + " bin: " + start_bin); - place_pending_buy(start_price, 2 * order_lifetime + TimeCurrent() ); + place_pending_buy(start_price, order_lifetime + TimeCurrent() ); //place_pending_sell(start_price, order_lifetime + TimeCurrent() ); place_buy(start_price); - Last_Go = TimeCurrent(); + last_go_time = TimeCurrent(); init_done = true; } else if ( go_sell() ) { start_price = Bid; + start_bin = MathFloor(start_price / pipstep_adjusted); + if (Debug_Print) Print("Init go short, start price: " + start_price + " bin: " + start_bin); - //place_pending_buy(start_price, order_lifetime + TimeCurrent() ); - place_pending_sell(start_price, 2 * order_lifetime + TimeCurrent() ); + //place_pending_buy(start_price, order_lifetime + TimeCurrent() ); place_sell(start_price); + place_pending_sell(start_price, order_lifetime + TimeCurrent() ); - Last_Go = TimeCurrent(); + last_go_time = TimeCurrent(); init_done = true; } } else if (start_price != 0) { - /* in trend */ + /* in trend */ if ( Ask > (start_price + pipstep_adjusted) ) { start_price = Ask; int bin = MathFloor(start_price / pipstep_adjusted); - if (Debug_Print) Print("Go buy new start_price:" + start_price); + if (Debug_Print) Print("Go buy new start_price:" + start_price + " bin: " + bin); /* things to do once per bin */ if (start_bin != bin) { @@ -341,23 +386,21 @@ start() // place buy stops if ( go_buy() ) { close_all_orders_type(OP_SELL); - place_pending_buy(start_price, 2 * order_lifetime + TimeCurrent()); - //place_pending_sell(start_price, order_lifetime + TimeCurrent()); - Last_Go = TimeCurrent(); + place_pending_buy(start_price, order_lifetime + TimeCurrent()); + place_pending_sell(start_price, order_lifetime + TimeCurrent()); + last_go_time = TimeCurrent(); } else if ( go_sell() ) { close_all_orders_type(OP_BUY); - place_pending_sell(start_price, 2 * order_lifetime + TimeCurrent()); - //place_pending_buy(start_price, order_lifetime + TimeCurrent()); - Last_Go = TimeCurrent(); + place_pending_sell(start_price, order_lifetime + TimeCurrent()); + place_pending_buy(start_price, order_lifetime + TimeCurrent()); + last_go_time = TimeCurrent(); } - } else if ( - (Bid < (start_price - pipstep_adjusted)) && - (start_bin != bin) ) { + } else if ( Bid < (start_price - pipstep_adjusted) ) { start_price = Bid; bin = MathFloor(start_price / pipstep_adjusted); - if (Debug_Print) Print("Go sell new start_price: " + start_price); + if (Debug_Print) Print("Go sell new start_price: " + start_price + " bin: " + bin); /* things to do once per bin */ if (start_bin != bin) { @@ -370,12 +413,12 @@ start() close_all_orders_type(OP_BUY); place_pending_sell(start_price, order_lifetime + TimeCurrent()); place_pending_buy(start_price, order_lifetime + TimeCurrent()); - Last_Go = TimeCurrent(); + last_go_time = TimeCurrent(); } else if ( go_buy() ) { close_all_orders_type(OP_SELL); place_pending_buy(start_price, order_lifetime + TimeCurrent()); place_pending_sell(start_price, order_lifetime + TimeCurrent()); - Last_Go = TimeCurrent(); + last_go_time = TimeCurrent(); } } @@ -391,7 +434,6 @@ start() "Pipstep adjusted: " + pipstep_adjusted + "\n" + "Last orders price: " + start_price + "\n" + "Current close M15 price: " + iClose(NULL, PERIOD_M15, 0) + "\n" + - "Profit: " + current_profit + " USD\n" + "Total spread loss: " + total_spread_loss + " USD\n" ); } @@ -475,12 +517,11 @@ place_buy(double base_price) { double stop_loss = base_price - pipstep_adjusted; - if (Debug_Print) Print("Placing buy stop order at " + base_price + " with SL at " + stop_loss); + if (Debug_Print) Print("Placing long order at " + base_price + " with SL at " + stop_loss); int ticket = -1; int retrct = 0; while (ticket < 0 && retrct < RETRIES) { - ticket = OrderSend( Symbol(), OP_BUY, @@ -494,24 +535,22 @@ place_buy(double base_price) 0, Blue); retrct++; - } - - if (ticket < 0) { - if (Debug_Print) Print("Long OrderSend failed with error #", GetLastError()); - return; - } - - if (Debug_Print) Print("Buy stop order placed with ticket " + ticket + " at " + Ask); + + if (Debug_Print == false) return; + + if (ticket < 0) Print("Long OrderSend failed with error #", GetLastError()); + else + Print("Buy stop order placed with ticket " + ticket + " at " + Ask); } void place_sell(double base_price) { - double stop_loss = base_price + pipstep_adjusted * Point; + double stop_loss = base_price + pipstep_adjusted; - if (Debug_Print) Print("Placing sell stop order at " + base_price + " with SL at " + stop_loss); + if (Debug_Print) Print("Placing short order at " + base_price + " with SL at " + stop_loss); int ticket = -1; int retrct = 0; @@ -554,28 +593,35 @@ move_up_buys_sl() (TimeCurrent() - OrderOpenTime()) > time_bring_even) { double stop_loss = OrderStopLoss(); - double new_stop_loss = NormalizeDouble(stop_loss + pipstep_adjusted, Digits); + + for ( + int new_stop_loss_bin = MathRound( (Ask - min_stop_pt - stop_loss) / pipstep_adjusted ); + new_stop_loss_bin > 0; + new_stop_loss_bin--) { - if (Ask < new_stop_loss) continue; + double new_stop_loss = NormalizeDouble(stop_loss + (new_stop_loss * pipstep_adjusted), Digits); - if (Debug_Print) Print( - "Moving long SL @ " + OrderOpenPrice() + " SL " + stop_loss + - " to " + new_stop_loss + " current ASK " + Ask); + if (Debug_Print) Print( + "Moving long ticket " + OrderTicket() + " stop loss " + stop_loss + + " to " + new_stop_loss + ", current ask price " + Ask); - bool res = false; - int retrct = 0; - while (res == false && retrct < RETRIES) { - res = OrderModify( + bool res = false; + int retrct = 0; + while (res == false && retrct < RETRIES) { + res = OrderModify( OrderTicket(), OrderOpenPrice(), new_stop_loss, OrderTakeProfit(), OrderExpiration(), Blue); - retrct++; - } + retrct++; + } - if (res == false) Print("Failed moving order " + OrderTicket() + " stop loss to " + new_stop_loss); + // Order modify failed. + if (res == false) Print("Failed moving order " + OrderTicket() + " stop loss to " + new_stop_loss); + else break; + } if (OrderProfit() < Panic_Profit) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), SLIPPAGE, Blue); } @@ -596,28 +642,36 @@ move_down_sells_sl() ((TimeCurrent() - OrderOpenTime()) > time_bring_even)) { double stop_loss = OrderStopLoss(); - double new_stop_loss = NormalizeDouble(stop_loss - pipstep_adjusted, Digits); - - if (new_stop_loss > Bid) continue; - - if (Debug_Print) Print( + + for ( + int new_stop_loss_bin = MathRound( (stop_loss - min_stop_pt - Bid) / pipstep_adjusted ); + new_stop_loss_bin > 0; + new_stop_loss_bin--) { + + double new_stop_loss = NormalizeDouble(stop_loss - (new_stop_loss_bin * pipstep_adjusted), Digits); + if (Debug_Print) Print( "Moving short SL @ " + OrderOpenPrice() + " SL " + stop_loss + " to " + new_stop_loss + " current BID " + Bid); - bool res = false; - int retrct = 0; - while (res == false && retrct < RETRIES) { + + bool res = false; + int retrct = 0; + while (res == false && retrct < RETRIES) { - res = OrderModify( + res = OrderModify( OrderTicket(), OrderOpenPrice(), new_stop_loss, OrderTakeProfit(), OrderExpiration(), Red); - retrct++; - } + retrct++; + } + + // Order modify failed. + if (res == false) Print("Failed moving order " + OrderTicket() + " stop loss to " + new_stop_loss); + else break; + } - if (res == false) Print("Failed moving order "+OrderTicket()+" stop loss to " + new_stop_loss); if (OrderProfit() < Panic_Profit) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), SLIPPAGE, Red); } @@ -625,90 +679,3 @@ move_down_sells_sl() } } -void -moveLastBuyStopToBE() -{ - int lastTicket = 0; - - for(int j = 0; j < OrdersTotal(); j++) - { - OrderSelect(j, SELECT_BY_POS, MODE_TRADES); - if (OrderMagicNumber() != Magic_Number || OrderType() != OP_BUYSTOP) continue; - - int ticketId = OrderTicket(); - if (ticketId > lastTicket) lastTicket = ticketId; - - } - - if(lastTicket > 0) { - OrderSelect(lastTicket, SELECT_BY_TICKET, MODE_TRADES); - - if (Debug_Print) - Print("Moving long order number " + lastTicket + " to BE at " + OrderOpenPrice() ); - - bool res = OrderModify( - lastTicket, - OrderOpenPrice(), - OrderOpenPrice(), - OrderTakeProfit(), - OrderExpiration(), - Blue); - - - if (!res && Debug_Print) - Print("Long SL2BE order failed with error #", GetLastError()); - - if (false && res == false) - res = OrderModify( - lastTicket, - OrderOpenPrice(), - OrderOpenPrice() - (min_stop * Point), - OrderTakeProfit(), - OrderExpiration(), - Blue); - - } -} - -void moveLastSellStopToBE() -{ - int lastTicket=0; - - for(int j = 0; j < OrdersTotal(); j++) { - OrderSelect(j, SELECT_BY_POS, MODE_TRADES); - if (OrderMagicNumber() != Magic_Number || OrderType() != OP_SELLSTOP) continue; - - int ticketId = OrderTicket(); - if(ticketId > lastTicket) lastTicket = ticketId; - - } - - if(lastTicket > 0) { - OrderSelect(lastTicket, SELECT_BY_TICKET, MODE_TRADES); - - if (Debug_Print) - Print("Moving short order number " + lastTicket+ " to BE at " + OrderOpenPrice()); - - bool res = OrderModify( - lastTicket, - OrderOpenPrice(), - OrderOpenPrice(), - OrderTakeProfit(), - OrderExpiration(), - Red); - - if (false && res == false) { - if (Debug_Print) Print("Short SL2BE order failed with error #",GetLastError()); - res = OrderModify( - lastTicket, - OrderOpenPrice(), - OrderOpenPrice() + Point * min_stop, - OrderTakeProfit(), - OrderExpiration(), - Red); - } - - } - -} - diff --git a/indicators/jmarket_sentiment.mq4 b/indicators/jmarket_sentiment.mq4 index 562ed6d..74f1d0c 100644 --- a/indicators/jmarket_sentiment.mq4 +++ b/indicators/jmarket_sentiment.mq4 @@ -15,7 +15,7 @@ #property indicator_color5 Lime #property indicator_width1 1 #property indicator_width2 1 -#property indicator_width3 2 +#property indicator_width3 1 #property indicator_width4 2 #property indicator_width5 1 #property indicator_level1 1.0 @@ -35,12 +35,13 @@ #define BEARISH 2 -extern int MA_Period = 32; +extern int MA_Period = 16; extern int MA2_Period = 8; extern int Time_Frame_Mins = 0; -extern int Retracement_Look_Back = 32; -extern int Mode_Function = 1; -extern bool Normalize_MA = true; +extern int Retracement_Look_Back = 12; +extern double Time_Factor = 0.2; +extern double Volume_Factor = 0.5; +extern int Mode_Function = 0; extern string _Mode_0 = "Bar to bar weighted hi-lo difference aggregate to volume ratio"; // nakaj stiska extern string _Mode_1 = "Bar to bar any tick max difference"; // grid extern string _Mode_2 = "Bar to bar tick max difference"; // pletkanje @@ -145,9 +146,15 @@ int init() prev_calc_time = 0; prev_best_step = 0; - if (Mode_Function != WEIGHT_MOVE) remove_trendline(); - else create_trendline(); - + if (Mode_Function != WEIGHT_MOVE) { + remove_trendline(); + SetIndexStyle(2, DRAW_LINE, STYLE_SOLID, indicator_width3, indicator_color3); + SetIndexStyle(4, DRAW_LINE, STYLE_SOLID, indicator_width5, indicator_color5); + } else { + create_trendline(); + SetIndexStyle(2, DRAW_LINE, STYLE_DOT, indicator_width3, CLR_NONE); + SetIndexStyle(4, DRAW_LINE, STYLE_DOT, indicator_width5, indicator_color5); + } //---- return(0); } @@ -188,19 +195,21 @@ double get_time_ratio(bool last_bar) { if (DEBUG == 1) Print("Debug get_time_ratio"); + return (1); double time_ratio; if (last_bar == false) { time_ratio = 1; } else { - double frame_delta = TimeCurrent() - iTime(NULL, time_frame, 0); - if (frame_delta > 0) time_ratio = time_frame * 60. / frame_delta; + double frame_delta = TimeCurrent() - (60. * iTime(NULL, time_frame, 0)); + if (frame_delta > 0) time_ratio = (time_frame * 60.) / frame_delta; else time_ratio = 0; } return(time_ratio); } + double get_weighted_move( double &hi, @@ -209,27 +218,27 @@ get_weighted_move( double &prevhi, double &prevlow, double &prevclose, - int ix) + int ix, int prev_ix) { if (DEBUG == 1) Print("Debug get_weighted_move"); if (hi < prevlow || low > prevhi) return (0); // disconnected bars //current weighted high minus previous weighted high - double hidiff = ((2. * hi + close) / 3.) - ((2. * prevhi + prevclose) / 3.); - double lowdiff = ((2. * low + close) / 3.) - ((2. * prevlow + prevclose) / 3.); + double hidiff = ((3. * hi + close) / 4.) - ((3. * prevhi + prevclose) / 4.); + double lowdiff = ((3. * low + close) / 4.) - ((3. * prevlow + prevclose) / 4.); double absdiff = hidiff + lowdiff; double volume_ratio; double vol = iVolume(NULL, time_frame, ix); - double prevol = iVolume(NULL, time_frame, ix+1); + double prevol = iVolume(NULL, time_frame, prev_ix); if (vol < INVALID_VOLUME) { volume_ratio = 0; } else if (prevol < INVALID_VOLUME) { volume_ratio = 1; } else { - volume_ratio = vol / prevol; + volume_ratio = MathPow(vol / prevol, Volume_Factor); } double time_ratio = get_time_ratio( (ix == 0) ); @@ -253,7 +262,7 @@ if (DEBUG == 1) Print("Debug get_absolute_diff"); double lowdiff = MathAbs(low - prevlow); double absdiff = hidiff + lowdiff; - double time_ratio = get_time_ratio(ix); + double time_ratio = get_time_ratio( (ix == 0) ); return (absdiff * time_ratio); } @@ -445,7 +454,18 @@ int start() switch (Mode_Function) { case WEIGHT_MOVE: - ExtMapBuffer1[i] = get_weighted_move(hi, low, close, prevhi, prevlow, prevclose, ix); + ExtMapBuffer1[i] = 0; + for (int j = 1; j < MA_Period; j++) { + int prev_ix = ix+j; + prevhi = iHigh(NULL, time_frame, prev_ix); + prevlow = iLow(NULL, time_frame, prev_ix); + prevclose = iClose(NULL, time_frame, prev_ix); + + double time_coef = (1. * j) / MA_Period; + time_coef = MathPow(1. - time_coef, Time_Factor); + + ExtMapBuffer1[i] += time_coef * get_weighted_move(hi, low, close, prevhi, prevlow, prevclose, ix, prev_ix); + } ExtMapBuffer2[i] = ExtMapBuffer1[i]; break; @@ -455,7 +475,7 @@ int start() ExtMapBuffer6[i] = MathAbs(ExtMapBuffer1[i]); // fixup previous bars - for (int j = i + 1; + for (j = i + 1; ((ExtMapBuffer2[i] > 0) == (ExtMapBuffer2[j] > 0.0)) && (j < Bars); j++) ExtMapBuffer2[j] = ExtMapBuffer2[i]; @@ -470,11 +490,11 @@ int start() } if (Mode_Function == WEIGHT_MOVE) { - if (Normalize_MA) double ma_coef = 10; - else ma_coef = 1; + double ma_coef = 1; for (i = limit; i >= 0; i--) - ExtMapBuffer3[i] = ma_coef * iMAOnArray(ExtMapBuffer1, Bars, MA_Period, 0, MODE_EMA, i); + ExtMapBuffer3[i] = MathAbs(ExtMapBuffer1[i]); + for (i = limit; i >= 0; i--) ExtMapBuffer5[i] = iMAOnArray(ExtMapBuffer3, Bars, MA2_Period, 0, MODE_EMA, i); diff --git a/indicators/jmarket_sentiment.mq4.BACKUP.4229.mq4 b/indicators/jmarket_sentiment.mq4.BACKUP.4229.mq4 deleted file mode 100755 index 93dfc94..0000000 --- a/indicators/jmarket_sentiment.mq4.BACKUP.4229.mq4 +++ /dev/null @@ -1,554 +0,0 @@ -//+------------------------------------------------------------------+ -//| jmarket_sentiment.mq4 | -//| Copyright 2011, Zarko Asenov | -//| http://jaltoh.6x.to | -//+------------------------------------------------------------------+ -#property copyright "Copyright 2011, Zarko Asenov" -#property link "http://jaltoh.6x.to" - -#property indicator_separate_window -#property indicator_buffers 5 -#property indicator_color1 Red -#property indicator_color2 OrangeRed -#property indicator_color3 Gold -#property indicator_color4 SteelBlue -#property indicator_color5 Lime -#property indicator_width1 1 -#property indicator_width2 1 -#property indicator_width3 2 -#property indicator_width4 2 -#property indicator_width5 1 -#property indicator_level1 1.0 -#property indicator_level2 -1.0 - -#define DEBUG 0 - - -#define WEIGHT_MOVE 0 -#define ABS_MOVE 1 -#define ABS_PUSH 2 -#define TRENDLINE_NAME "jnextbar_predict" -#define INVALID_VOLUME 50 - -#define NEUTRAL 0 -#define BULLISH 1 -#define BEARISH 2 - - -extern int MA_Period = 32; -extern int MA2_Period = 64; -extern int Time_Frame_Mins = 0; -extern int Retracement_Look_Back = 32; -extern int Mode_Function = ABS_MOVE; -extern bool Normalize_MA = true; -extern string _Mode_0 = "Bar to bar weighted hi-lo difference aggregate to volume ratio"; // nakaj stiska -extern string _Mode_1 = "Bar to bar any tick max difference"; // grid -extern string _Mode_2 = "Bar to bar tick max difference"; // pletkanje -extern int Sentiment_Expectation = NEUTRAL; -extern string _Expectation_0 = "Neutral sentiment"; -extern string _Expectation_1 = "Bullish sentiment"; -extern string _Expectation_2 = "Bearish sentiment"; - -//--- buffers -double ExtMapBuffer1[]; -double ExtMapBuffer2[]; -double ExtMapBuffer3[]; -double ExtMapBuffer4[]; -double ExtMapBuffer5[]; -double ExtMapBuffer6[]; - - - -int time_frame; -double index_factor = 1.0; -int hTrendLine = -1; -int ind_window_handle; -double prev_best_step; -datetime prev_calc_time; - -void -create_trendline() -{ - ind_window_handle = WindowFind("jmarket_sentiment"); - - if (hTrendLine == -1 && ind_window_handle > -1) { - - hTrendLine = ObjectCreate( - TRENDLINE_NAME, - OBJ_TREND, - ind_window_handle, - iTime(NULL, time_frame, 0), - 0.0, - iTime(NULL, time_frame, 0) + Period() * 60, - 0.0); - - ObjectSet(TRENDLINE_NAME, OBJPROP_COLOR, indicator_color2); - ObjectSet(TRENDLINE_NAME, OBJPROP_STYLE, STYLE_SOLID); - ObjectSet(TRENDLINE_NAME, OBJPROP_WIDTH, indicator_width2); - } -} - -void -remove_trendline() -{ - ObjectDelete(TRENDLINE_NAME); -} - -//+------------------------------------------------------------------+ -//| Custom indicator initialization function | -//+------------------------------------------------------------------+ -int init() - { - IndicatorBuffers(6); - -//---- indicators - SetIndexStyle(0,DRAW_HISTOGRAM); - SetIndexBuffer(0,ExtMapBuffer1); - SetIndexLabel(0,"Movement"); - SetIndexEmptyValue(0, EMPTY_VALUE); - - SetIndexStyle(1,DRAW_LINE); - SetIndexBuffer(1,ExtMapBuffer2); - SetIndexLabel(1,"Movement Sum"); - SetIndexEmptyValue(1, EMPTY_VALUE); - - SetIndexStyle(2,DRAW_LINE); - SetIndexBuffer(2,ExtMapBuffer3); - SetIndexLabel(2,"Smoothed "+MA_Period); - SetIndexEmptyValue(2, EMPTY_VALUE); - - SetIndexStyle(3,DRAW_LINE); - SetIndexBuffer(3,ExtMapBuffer4); - SetIndexLabel(3,"Trend step"); - SetIndexEmptyValue(3, EMPTY_VALUE); - - SetIndexStyle(4,DRAW_LINE); - SetIndexBuffer(4,ExtMapBuffer5); - SetIndexLabel(4,"Smoothed "+MA2_Period); - SetIndexEmptyValue(4, EMPTY_VALUE); - - SetIndexStyle(5,DRAW_LINE); - SetIndexBuffer(5,ExtMapBuffer6); - SetIndexLabel(5,"Absolute movement"); - SetIndexEmptyValue(5, EMPTY_VALUE); - - if (Time_Frame_Mins == 0) - time_frame = Period(); - else - time_frame = Time_Frame_Mins; - - if (time_frame < 1) time_frame = 1; - - index_factor = Period(); - index_factor = index_factor / time_frame; - - prev_calc_time = 0; - prev_best_step = 0; - - if (Mode_Function != WEIGHT_MOVE) remove_trendline(); - else create_trendline(); - -//---- - return(0); - } -//+------------------------------------------------------------------+ -//| Custom indicator deinitialization function | -//+------------------------------------------------------------------+ -int deinit() - { -//---- - remove_trendline(); -//---- - return(0); - } - -double -get_delta_ratio( - double &SignalBuffer[], - int SignalBufferLen) -{ -if (DEBUG == 1) Print("Debug get_delta_ratio"); - - double delta = 0; - bool going_up = false; - - for (int i = 0; i < SignalBufferLen; i++) { - if (SignalBuffer[i] == 0 || SignalBuffer[i+1] == 0) continue; - else if (delta != 0 && going_up != (SignalBuffer[i] > SignalBuffer[i+1]) ) break; - - if (delta == 0) going_up = (SignalBuffer[i] > SignalBuffer[i+1]); - - delta = ( (delta * i) + (SignalBuffer[i] - SignalBuffer[i+1])) / (i + 1.); - } - - return (delta); -} - -double -get_time_ratio(bool last_bar) -{ -if (DEBUG == 1) Print("Debug get_time_ratio"); - - double time_ratio; - if (last_bar == false) { - time_ratio = 1; - } else { - double frame_delta = TimeCurrent() - iTime(NULL, time_frame, 0); - if (frame_delta > 0) time_ratio = time_frame * 60. / frame_delta; - else time_ratio = 0; - } - - return(time_ratio); -} - -double -get_weighted_move( - double &hi, - double &low, - double &close, - double &prevhi, - double &prevlow, - double &prevclose, - int ix) -{ -if (DEBUG == 1) Print("Debug get_weighted_move"); - - if (hi < prevlow || low > prevhi) return (0); // disconnected bars - - //current weighted high minus previous weighted high - double hidiff = ((2. * hi + close) / 3.) - ((2. * prevhi + prevclose) / 3.); - double lowdiff = ((2. * low + close) / 3.) - ((2. * prevlow + prevclose) / 3.); - double absdiff = hidiff + lowdiff; - - double volume_ratio; - double vol = iVolume(NULL, time_frame, ix); - double prevol = iVolume(NULL, time_frame, ix+1); - - if (vol < INVALID_VOLUME) { - volume_ratio = 0; - } else if (prevol < INVALID_VOLUME) { - volume_ratio = 1; - } else { - volume_ratio = vol / prevol; - } - - double time_ratio = get_time_ratio( (ix == 0) ); - - return (absdiff * time_ratio * volume_ratio); -} - - -double -get_absolute_diff( - double &hi, - double &low, - double &prevhi, - double &prevlow, - int ix) -{ -if (DEBUG == 1) Print("Debug get_absolute_diff"); - - //current weighted high minus previous weighted high - double hidiff = MathAbs(hi - prevhi); - double lowdiff = MathAbs(low - prevlow); - double absdiff = hidiff + lowdiff; - - double time_ratio = get_time_ratio(ix); - - return (absdiff * time_ratio); -} - -/* largest price difference between two bars */ -double -get_absolute_move( - double hi, - double low, - double close, - double prevhi, - double prevlow, - double prevclose) -{ -if (DEBUG == 1) Print("Debug get_absolute_move"); - - double abs_move = 0.0; - - double cur_hlc[3]; - cur_hlc[0] = hi; - cur_hlc[1] = low; - cur_hlc[2] = close; - - double prev_hlc[3]; - prev_hlc[0] = prevhi; - prev_hlc[1] = prevlow; - prev_hlc[2] = prevclose; - - for (int i = 0; i < 3; i++) - for (int j = 0; j < 3; j++) - if ( MathAbs(cur_hlc[i] - prev_hlc[j]) > MathAbs(abs_move) ) - abs_move = cur_hlc[i] - prev_hlc[j]; - - if ( (hi - low) > abs_move ) { - if (close > prevclose) abs_move = hi - low; - else abs_move = low - hi; - } - - return (abs_move); -} - -void -get_bar_move( - int now_bar, - int ref_bar, - double &max_move) -{ -if (DEBUG == 1) Print("Debug get_bar_move"); - - for (int ix = now_bar - 1; ix >= ref_bar; ix--) { - double now_move = get_absolute_move( - iHigh(NULL, time_frame, ix), - iLow(NULL, time_frame, ix), - iClose(NULL, time_frame, ix), - iHigh(NULL, time_frame, now_bar), - iLow(NULL, time_frame, now_bar), - iClose(NULL, time_frame, now_bar) ); - - if ( MathAbs(now_move) > MathAbs(max_move) ) max_move = now_move; - } -} - -/* largest price difference since start_bar trend */ -double -get_max_move(int ref_bar) -{ - if (DEBUG == 1) Print("Debug get_max_move"); - - double max_move = 0; - bool ref_bull = (ExtMapBuffer1[ref_bar] > 0); - - for (int now_bar = ref_bar + 1; - (ref_bull == (ExtMapBuffer1[now_bar] > 0)) && (now_bar < Bars); - now_bar++) { - - get_bar_move(now_bar, ref_bar, max_move); - } - - get_bar_move(now_bar, ref_bar, max_move); - - return (max_move); -} - -#define MAXLEN 1024 - -double bears_array[MAXLEN]; -double bulls_array[MAXLEN]; - - -/* sorted arrays expected ! */ -double -calc_pips( - double &trending[], - int trending_len, - double &retracements[], - int retracements_len - ) -{ - if (DEBUG == 1) Print("Debug calc_pips"); - - double best_ratio = 0; - double best_step = 0; - - if ( (TimeCurrent() - prev_calc_time) < (time_frame * Period() * 5) ) return(prev_best_step); - - double step_base = 0; - for (int ix = 1; - (step_base == 0) && (ix < Bars); - ix++) - { - step_base = ExtMapBuffer6[ix]; - } - if (step_base == 0) return (prev_best_step); - - double interpolation_step = step_base * 0.0312; // 1/16 - - - for (int z = 0; z < 64; z++) { - - double cur_step = step_base + (z * interpolation_step); - - double won_pips = 0; - double lost_pips = 0; - - for (int j = 0; j < retracements_len; j++) { - lost_pips += cur_step * MathFloor(retracements[j] / cur_step) / Point; - if (cur_step >= retracements[j]) lost_pips += 1. * MathMod(retracements[j], cur_step) / Point; - } - - for (j = 0; j < trending_len; j++) { - won_pips += cur_step * MathFloor(trending[j] / cur_step) / Point; - if (cur_step >= trending[j]) lost_pips += 1. * MathMod(trending[j], cur_step) / Point; - } - - if (won_pips == 0) double cur_ratio = 0; - else if (lost_pips == 0) cur_ratio = won_pips; - else cur_ratio = won_pips / lost_pips; - - if (cur_ratio > best_ratio) { - best_step = cur_step; - best_ratio = cur_ratio; - } - - } - - if (best_step == 0) return (prev_best_step); - - prev_best_step = best_step; - prev_calc_time = TimeCurrent(); - - if (DEBUG == 1) Print( - "Recomended minimal pip step " + best_step + " past " + - trending_len + " trending blocks, " + retracements_len + " retracements blocks."); - - return (best_step); -} - -int -gcd(int a, int b) -{ - if (b == 0) return (a); - else return ( gcd(b, a % b) ); -} - -//+------------------------------------------------------------------+ -//| Custom indicator iteration function | -//+------------------------------------------------------------------+ -int start() - { - int counted_bars = IndicatorCounted(); -//---- - int limit = Bars - counted_bars - 1; - if (limit < 0) return (0); - - for (int i = limit; i >= 0; i--) { - - int ix = MathFloor(index_factor * i); - - double hi = iHigh(NULL, time_frame, ix); - double low = iLow(NULL, time_frame, ix); - double close = iClose(NULL, time_frame, ix); - - double prevhi = iHigh(NULL, time_frame, ix+1); - double prevlow = iLow(NULL, time_frame, ix+1); - double prevclose = iClose(NULL, time_frame, ix+1); - - if (prevhi == 0 || prevlow == 0 || prevclose == 0) continue; - if (hi == 0 || low == 0 || close == 0) continue; - - switch (Mode_Function) { - case WEIGHT_MOVE: - ExtMapBuffer1[i] = get_weighted_move(hi, low, close, prevhi, prevlow, prevclose, ix); - ExtMapBuffer2[i] = ExtMapBuffer1[i]; - break; - - case ABS_MOVE: - ExtMapBuffer1[i] = get_absolute_move(hi, low, close, prevhi, prevlow, prevclose); - ExtMapBuffer2[i] = get_max_move(i); - ExtMapBuffer6[i] = MathAbs(ExtMapBuffer1[i]); - - // fixup previous bars - for (int j = i + 1; - ((ExtMapBuffer2[i] > 0) == (ExtMapBuffer2[j] > 0.0)) && (j < Bars); - j++) - ExtMapBuffer2[j] = ExtMapBuffer2[i]; - break; - - case ABS_PUSH: - ExtMapBuffer1[i] = get_absolute_diff(hi, low, prevhi, prevlow, i); - ExtMapBuffer2[i] = ExtMapBuffer1[i]; - break; - } - - } - - if (Mode_Function == WEIGHT_MOVE) { -<<<<<<< HEAD - double ma_coef = 10.0; -======= - - if (Normalize_MA) double ma_coef = 10; - else ma_coef = 1; - ->>>>>>> 5675db9edd8b65061d7441f40114a38e07f453b1 - for (i = limit; i >= 0; i--) - ExtMapBuffer3[i] = ma_coef * iMAOnArray(ExtMapBuffer1, Bars, MA_Period, 0, MODE_EMA, i); - for (i = limit; i >= 0; i--) - ExtMapBuffer5[i] = iMAOnArray(ExtMapBuffer3, Bars, MA2_Period, 0, MODE_EMA, i); - - } else { - for (i = limit; i >= 0; i--) { - ExtMapBuffer3[i] = iMAOnArray(ExtMapBuffer6, Bars, MA_Period, 0, MODE_EMA, i); - ExtMapBuffer5[i] = iMAOnArray(ExtMapBuffer1, Bars, MA2_Period, 0, MODE_EMA, i); - } - } - - if (Mode_Function == ABS_MOVE) { - - switch (Sentiment_Expectation) { - case NEUTRAL: - bool going_bull = (ExtMapBuffer2[0] > 0); - break; - - case BULLISH: - going_bull = true; - break; - - case BEARISH: - going_bull = false; - break; - } - - int bulls_len = 0; - int bears_len = 0; - double preval = 0; - - /* get unique movements */ - for (i = 0; i < Retracement_Look_Back; i++) { - if (ExtMapBuffer2[i] == preval) continue; - preval = ExtMapBuffer2[i]; - - if (preval > 0) { - bulls_array[bulls_len] = preval; - bulls_len++; - } else if (preval < 0) { - bears_array[bears_len] = MathAbs(preval); - bears_len++; - } - } - - if (going_bull == true) - double pip_step = calc_pips(bulls_array, bulls_len, bears_array, bears_len); - else - pip_step = calc_pips(bears_array, bears_len, bulls_array, bulls_len); - - ExtMapBuffer4[0] = pip_step; - SetLevelValue(0, pip_step); - SetLevelValue(1, -1.0 * pip_step); - - - } else { - double delta_sign = get_delta_ratio(ExtMapBuffer1, Bars); - double predicted_price = delta_sign; - predicted_price += ExtMapBuffer1[0]; - - if (hTrendLine != -1) { - ObjectSet(TRENDLINE_NAME, OBJPROP_TIME1, iTime(NULL, time_frame, 0) ); - ObjectSet(TRENDLINE_NAME, OBJPROP_TIME2, iTime(NULL, time_frame, 0) + Period() * 60); - ObjectSet(TRENDLINE_NAME, OBJPROP_PRICE1, ExtMapBuffer2[0]); - ObjectSet(TRENDLINE_NAME, OBJPROP_PRICE2, predicted_price); - } - - } - -//---- - return(0); - } -//+------------------------------------------------------------------+ \ No newline at end of file diff --git a/indicators/jmarket_sentiment.mq4.BASE.4229.mq4 b/indicators/jmarket_sentiment.mq4.BASE.4229.mq4 deleted file mode 100644 index 5e7bc17..0000000 --- a/indicators/jmarket_sentiment.mq4.BASE.4229.mq4 +++ /dev/null @@ -1,533 +0,0 @@ -//+------------------------------------------------------------------+ -//| jmarket_sentiment.mq4 | -//| Copyright 2011, Zarko Asenov | -//| http://jaltoh.6x.to | -//+------------------------------------------------------------------+ -#property copyright "Copyright 2011, Zarko Asenov" -#property link "http://jaltoh.6x.to" - -#property indicator_separate_window -#property indicator_buffers 4 -#property indicator_color1 Red -#property indicator_color2 OrangeRed -#property indicator_color3 Gold -#property indicator_color4 SteelBlue -#property indicator_width1 1 -#property indicator_width2 1 -#property indicator_width3 2 -#property indicator_width4 2 -#property indicator_level1 1.0 -#property indicator_level2 -1.0 - -#define DEBUG 1 - - -#define WEIGHT_MOVE 0 -#define ABS_MOVE 1 -#define ABS_PUSH 2 -#define TRENDLINE_NAME "jnextbar_predict" -#define INVALID_VOLUME 50 - -#define NEUTRAL 0 -#define BULLISH 1 -#define BEARISH 2 - - -extern int MA_Period = 32; -extern int Time_Frame_Mins = 0; -extern int Retracement_Look_Back = 32; -extern int Mode_Function = ABS_MOVE; -extern string _Mode_0 = "Bar to bar weighted hi-lo difference aggregate to volume ratio"; // nakaj stiska -extern string _Mode_1 = "Bar to bar any tick max difference"; // grid -extern string _Mode_2 = "Bar to bar tick max difference"; // pletkanje -extern int Sentiment_Expectation = NEUTRAL; -extern string _Expectation_0 = "Neutral sentiment"; -extern string _Expectation_1 = "Bullish sentiment"; -extern string _Expectation_2 = "Bearish sentiment"; - -//--- buffers -double ExtMapBuffer1[]; -double ExtMapBuffer2[]; -double ExtMapBuffer3[]; -double ExtMapBuffer4[]; -double ExtMapBuffer5[]; - - - -int time_frame; -double index_factor = 1.0; -int hTrendLine = -1; -int ind_window_handle; -double prev_best_step; -datetime prev_calc_time; - -void -create_trendline() -{ - ind_window_handle = WindowFind("jmarket_sentiment"); - - if (hTrendLine == -1 && ind_window_handle > -1) { - - hTrendLine = ObjectCreate( - TRENDLINE_NAME, - OBJ_TREND, - ind_window_handle, - iTime(NULL, time_frame, 0), - 0.0, - iTime(NULL, time_frame, 0) + Period() * 60, - 0.0); - - ObjectSet(TRENDLINE_NAME, OBJPROP_COLOR, indicator_color2); - ObjectSet(TRENDLINE_NAME, OBJPROP_STYLE, STYLE_SOLID); - ObjectSet(TRENDLINE_NAME, OBJPROP_WIDTH, indicator_width2); - } -} - -void -remove_trendline() -{ - ObjectDelete(TRENDLINE_NAME); -} - -//+------------------------------------------------------------------+ -//| Custom indicator initialization function | -//+------------------------------------------------------------------+ -int init() - { - IndicatorBuffers(5); - -//---- indicators - SetIndexStyle(0,DRAW_HISTOGRAM); - SetIndexBuffer(0,ExtMapBuffer1); - SetIndexLabel(0,"Movement"); - SetIndexEmptyValue(0, EMPTY_VALUE); - - SetIndexStyle(1,DRAW_LINE); - SetIndexBuffer(1,ExtMapBuffer2); - SetIndexLabel(1,"Movement Sum"); - SetIndexEmptyValue(1, EMPTY_VALUE); - - SetIndexStyle(2,DRAW_LINE); - SetIndexBuffer(2,ExtMapBuffer3); - SetIndexLabel(2,"Smoothed "+MA_Period); - SetIndexEmptyValue(2, EMPTY_VALUE); - - SetIndexStyle(3,DRAW_LINE); - SetIndexBuffer(3,ExtMapBuffer4); - SetIndexLabel(3,"Trend step"); - SetIndexEmptyValue(3, EMPTY_VALUE); - - SetIndexStyle(4,DRAW_LINE); - SetIndexBuffer(4,ExtMapBuffer5); - SetIndexLabel(4,"Absolute movement"); - SetIndexEmptyValue(4, EMPTY_VALUE); - - if (Time_Frame_Mins == 0) - time_frame = Period(); - else - time_frame = Time_Frame_Mins; - - if (time_frame < 1) time_frame = 1; - - index_factor = Period(); - index_factor = index_factor / time_frame; - - prev_calc_time = 0; - prev_best_step = 0; - - if (Mode_Function == ABS_MOVE) remove_trendline(); - else create_trendline(); - -//---- - return(0); - } -//+------------------------------------------------------------------+ -//| Custom indicator deinitialization function | -//+------------------------------------------------------------------+ -int deinit() - { -//---- - remove_trendline(); -//---- - return(0); - } - -double -get_delta_ratio( - double &SignalBuffer[], - int SignalBufferLen) -{ -if (DEBUG == 1) Print("Debug get_delta_ratio"); - - double delta = 0.0; - bool going_up = false; - - for (int i = 0; i < SignalBufferLen; i++) { - if (SignalBuffer[i] == 0. || SignalBuffer[i+1] == 0.) continue; - else if (delta != 0. && going_up != (SignalBuffer[i] > SignalBuffer[i+1]) ) break; - - if (delta == 0.) going_up = (SignalBuffer[i] > SignalBuffer[i+1]); - - delta = (delta * i + (SignalBuffer[i] - SignalBuffer[i+1])) / (i + 1.); - } - - return (delta); -} - -double -get_time_ratio(bool last_bar) -{ -if (DEBUG == 1) Print("Debug get_time_ratio"); - - double time_ratio; - if (last_bar == false) { - time_ratio = 1; - } else { - double frame_delta = TimeCurrent() - iTime(NULL, time_frame, 0); - if (frame_delta > 0) time_ratio = time_frame * 60. / frame_delta; - else time_ratio = 0; - } - - return(time_ratio); -} - -double -get_weighted_move( - double &hi, - double &low, - double &close, - double &prevhi, - double &prevlow, - double &prevclose, - int ix) -{ -if (DEBUG == 1) Print("Debug get_weighted_move"); - - if (hi < prevlow || low > prevhi) return (0); // disconnected bars - - //current weighted high minus previous weighted high - double hidiff = ((2. * hi + close) / 3.) - ((2. * prevhi + prevclose) / 3.); - double lowdiff = ((2. * low + close) / 3.) - ((2. * prevlow + prevclose) / 3.); - double absdiff = hidiff + lowdiff; - - double volume_ratio; - double vol = iVolume(NULL, time_frame, ix); - double prevol = iVolume(NULL, time_frame, ix+1); - - if (vol < INVALID_VOLUME) { - volume_ratio = 0; - } else if (prevol < INVALID_VOLUME) { - volume_ratio = 1; - } else { - volume_ratio = vol / prevol; - } - - double time_ratio = get_time_ratio( (ix == 0) ); - - return (absdiff * time_ratio * volume_ratio); -} - - -double -get_absolute_diff( - double &hi, - double &low, - double &prevhi, - double &prevlow, - int ix) -{ -if (DEBUG == 1) Print("Debug get_absolute_diff"); - - //current weighted high minus previous weighted high - double hidiff = MathAbs(hi - prevhi); - double lowdiff = MathAbs(low - prevlow); - double absdiff = hidiff + lowdiff; - - double time_ratio = get_time_ratio(ix); - - return (absdiff * time_ratio); -} - -/* largest price difference between two bars */ -double -get_absolute_move( - double hi, - double low, - double close, - double prevhi, - double prevlow, - double prevclose) -{ -if (DEBUG == 1) Print("Debug get_absolute_move"); - - double abs_move = 0.0; - - double cur_hlc[3]; - cur_hlc[0] = hi; - cur_hlc[1] = low; - cur_hlc[2] = close; - - double prev_hlc[3]; - prev_hlc[0] = prevhi; - prev_hlc[1] = prevlow; - prev_hlc[2] = prevclose; - - for (int i = 0; i < 3; i++) - for (int j = 0; j < 3; j++) - if ( MathAbs(cur_hlc[i] - prev_hlc[j]) > MathAbs(abs_move) ) - abs_move = cur_hlc[i] - prev_hlc[j]; - - if ( (hi - low) > abs_move ) { - if (close > prevclose) abs_move = hi - low; - else abs_move = low - hi; - } - - return (abs_move); -} - -void -get_bar_move( - int now_bar, - int ref_bar, - double &max_move) -{ -if (DEBUG == 1) Print("Debug get_bar_move"); - - for (int ix = now_bar - 1; ix >= ref_bar; ix--) { - double now_move = get_absolute_move( - iHigh(NULL, time_frame, ix), - iLow(NULL, time_frame, ix), - iClose(NULL, time_frame, ix), - iHigh(NULL, time_frame, now_bar), - iLow(NULL, time_frame, now_bar), - iClose(NULL, time_frame, now_bar) ); - - if ( MathAbs(now_move) > MathAbs(max_move) ) max_move = now_move; - } -} - -/* largest price difference since start_bar trend */ -double -get_max_move(int ref_bar) -{ - if (DEBUG == 1) Print("Debug get_max_move"); - - double max_move = 0; - bool ref_bull = (ExtMapBuffer1[ref_bar] > 0); - - for (int now_bar = ref_bar + 1; - (ref_bull == (ExtMapBuffer1[now_bar] > 0)) && (now_bar < Bars); - now_bar++) { - - get_bar_move(now_bar, ref_bar, max_move); - } - - get_bar_move(now_bar, ref_bar, max_move); - - return (max_move); -} - -#define MAXLEN 1024 - -double bears_array[MAXLEN]; -double bulls_array[MAXLEN]; - - -/* sorted arrays expected ! */ -double -calc_pips( - double &trending[], - int trending_len, - double &retracements[], - int retracements_len - ) -{ - if (DEBUG == 1) Print("Debug calc_pips"); - - double best_ratio = 0; - double best_step = 0; - - if ( (TimeCurrent() - prev_calc_time) < (time_frame * Period() * 5) ) return(prev_best_step); - - double step_base = 0; - for (int ix = 1; - (step_base == 0) && (ix < Bars); - ix++) - { - step_base = ExtMapBuffer5[ix]; - } - if (step_base == 0) return (prev_best_step); - - double interpolation_step = step_base * 0.0312; // 1/16 - - - for (int z = 0; z < 64; z++) { - - double cur_step = step_base + (z * interpolation_step); - - double won_pips = 0; - double lost_pips = 0; - - for (int j = 0; j < retracements_len; j++) { - lost_pips += cur_step * MathFloor(retracements[j] / cur_step) / Point; - if (cur_step >= retracements[j]) lost_pips += 1. * MathMod(retracements[j], cur_step) / Point; - } - - for (j = 0; j < trending_len; j++) { - won_pips += cur_step * MathFloor(trending[j] / cur_step) / Point; - if (cur_step >= trending[j]) lost_pips += 1. * MathMod(trending[j], cur_step) / Point; - } - - if (won_pips == 0) double cur_ratio = 0; - else if (lost_pips == 0) cur_ratio = won_pips; - else cur_ratio = won_pips / lost_pips; - - if (cur_ratio > best_ratio) { - best_step = cur_step; - best_ratio = cur_ratio; - } - - } - - if (best_step == 0) return (prev_best_step); - - prev_best_step = best_step; - prev_calc_time = TimeCurrent(); - - if (DEBUG == 1) Print( - "Recomended minimal pip step " + best_step + " past " + - trending_len + " trending blocks, " + retracements_len + " retracements blocks."); - - return (best_step); -} - -int -gcd(int a, int b) -{ - if (b == 0) return (a); - else return ( gcd(b, a % b) ); -} - -//+------------------------------------------------------------------+ -//| Custom indicator iteration function | -//+------------------------------------------------------------------+ -int start() - { - int counted_bars = IndicatorCounted(); -//---- - int limit = Bars - counted_bars - 1; - if (limit < 0) return (0); - - for (int i = limit; i >= 0; i--) { - - int ix = MathFloor(index_factor * i); - - double hi = iHigh(NULL, time_frame, ix); - double low = iLow(NULL, time_frame, ix); - double close = iClose(NULL, time_frame, ix); - - double prevhi = iHigh(NULL, time_frame, ix+1); - double prevlow = iLow(NULL, time_frame, ix+1); - double prevclose = iClose(NULL, time_frame, ix+1); - - if (prevhi == 0 || prevlow == 0 || prevclose == 0) continue; - if (hi == 0 || low == 0 || close == 0) continue; - - switch (Mode_Function) { - case WEIGHT_MOVE: - ExtMapBuffer1[i] = get_weighted_move(hi, low, close, prevhi, prevlow, prevclose, ix); - ExtMapBuffer2[i] = ExtMapBuffer1[i]; - break; - - case ABS_MOVE: - ExtMapBuffer1[i] = get_absolute_move(hi, low, close, prevhi, prevlow, prevclose); - ExtMapBuffer2[i] = get_max_move(i); - ExtMapBuffer5[i] = MathAbs(ExtMapBuffer1[i]); - - // fixup previous bars - for (int j = i + 1; - ((ExtMapBuffer2[i] > 0) == (ExtMapBuffer2[j] > 0.0)) && (j < Bars); - j++) - ExtMapBuffer2[j] = ExtMapBuffer2[i]; - break; - - case ABS_PUSH: - ExtMapBuffer1[i] = get_absolute_diff(hi, low, prevhi, prevlow, i); - ExtMapBuffer2[i] = ExtMapBuffer1[i]; - break; - } - - } - - if (Mode_Function == WEIGHT_MOVE) { - double ma_coef = 1; - for (i = limit; i >= 0; i--) - ExtMapBuffer3[i] = ma_coef * iMAOnArray(ExtMapBuffer1, Bars, MA_Period, 0, MODE_EMA, i); - } else { - ma_coef = 1; - for (i = limit; i >= 0; i--) - ExtMapBuffer3[i] = ma_coef * iMAOnArray(ExtMapBuffer5, Bars, MA_Period, 0, MODE_EMA, i); - } - - if (Mode_Function == ABS_MOVE) { - - switch (Sentiment_Expectation) { - case NEUTRAL: - bool going_bull = (ExtMapBuffer2[0] > 0); - break; - - case BULLISH: - going_bull = true; - break; - - case BEARISH: - going_bull = false; - break; - } - - int bulls_len = 0; - int bears_len = 0; - double preval = 0; - - /* get unique movements */ - for (i = 0; i < Retracement_Look_Back; i++) { - if (ExtMapBuffer2[i] == preval) continue; - preval = ExtMapBuffer2[i]; - - if (preval > 0) { - bulls_array[bulls_len] = preval; - bulls_len++; - } else if (preval < 0) { - bears_array[bears_len] = MathAbs(preval); - bears_len++; - } - } - - if (going_bull == true) - double pip_step = calc_pips(bulls_array, bulls_len, bears_array, bears_len); - else - pip_step = calc_pips(bears_array, bears_len, bulls_array, bulls_len); - - ExtMapBuffer4[0] = pip_step; - SetLevelValue(0, pip_step); - SetLevelValue(1, -1.0 * pip_step); - - - } else { - double delta_sign = get_delta_ratio(ExtMapBuffer1, Bars); - double predicted_price = delta_sign; - predicted_price += ExtMapBuffer1[0]; - - if (hTrendLine != -1) { - ObjectSet(TRENDLINE_NAME, OBJPROP_TIME1, iTime(NULL, time_frame, 0) ); - ObjectSet(TRENDLINE_NAME, OBJPROP_TIME2, iTime(NULL, time_frame, 0) + Period() * 60); - ObjectSet(TRENDLINE_NAME, OBJPROP_PRICE1, ExtMapBuffer2[0]); - ObjectSet(TRENDLINE_NAME, OBJPROP_PRICE2, predicted_price); - } - - } - -//---- - return(0); - } -//+------------------------------------------------------------------+ \ No newline at end of file diff --git a/indicators/jmarket_sentiment.mq4.LOCAL.4229.mq4 b/indicators/jmarket_sentiment.mq4.LOCAL.4229.mq4 deleted file mode 100644 index 98ab530..0000000 --- a/indicators/jmarket_sentiment.mq4.LOCAL.4229.mq4 +++ /dev/null @@ -1,533 +0,0 @@ -//+------------------------------------------------------------------+ -//| jmarket_sentiment.mq4 | -//| Copyright 2011, Zarko Asenov | -//| http://jaltoh.6x.to | -//+------------------------------------------------------------------+ -#property copyright "Copyright 2011, Zarko Asenov" -#property link "http://jaltoh.6x.to" - -#property indicator_separate_window -#property indicator_buffers 4 -#property indicator_color1 Red -#property indicator_color2 OrangeRed -#property indicator_color3 Gold -#property indicator_color4 SteelBlue -#property indicator_width1 1 -#property indicator_width2 1 -#property indicator_width3 2 -#property indicator_width4 2 -#property indicator_level1 1.0 -#property indicator_level2 -1.0 - -#define DEBUG 1 - - -#define WEIGHT_MOVE 0 -#define ABS_MOVE 1 -#define ABS_PUSH 2 -#define TRENDLINE_NAME "jnextbar_predict" -#define INVALID_VOLUME 50 - -#define NEUTRAL 0 -#define BULLISH 1 -#define BEARISH 2 - - -extern int MA_Period = 32; -extern int Time_Frame_Mins = 0; -extern int Retracement_Look_Back = 32; -extern int Mode_Function = ABS_MOVE; -extern string _Mode_0 = "Bar to bar weighted hi-lo difference aggregate to volume ratio"; // nakaj stiska -extern string _Mode_1 = "Bar to bar any tick max difference"; // grid -extern string _Mode_2 = "Bar to bar tick max difference"; // pletkanje -extern int Sentiment_Expectation = NEUTRAL; -extern string _Expectation_0 = "Neutral sentiment"; -extern string _Expectation_1 = "Bullish sentiment"; -extern string _Expectation_2 = "Bearish sentiment"; - -//--- buffers -double ExtMapBuffer1[]; -double ExtMapBuffer2[]; -double ExtMapBuffer3[]; -double ExtMapBuffer4[]; -double ExtMapBuffer5[]; - - - -int time_frame; -double index_factor = 1.0; -int hTrendLine = -1; -int ind_window_handle; -double prev_best_step; -datetime prev_calc_time; - -void -create_trendline() -{ - ind_window_handle = WindowFind("jmarket_sentiment"); - - if (hTrendLine == -1 && ind_window_handle > -1) { - - hTrendLine = ObjectCreate( - TRENDLINE_NAME, - OBJ_TREND, - ind_window_handle, - iTime(NULL, time_frame, 0), - 0.0, - iTime(NULL, time_frame, 0) + Period() * 60, - 0.0); - - ObjectSet(TRENDLINE_NAME, OBJPROP_COLOR, indicator_color2); - ObjectSet(TRENDLINE_NAME, OBJPROP_STYLE, STYLE_SOLID); - ObjectSet(TRENDLINE_NAME, OBJPROP_WIDTH, indicator_width2); - } -} - -void -remove_trendline() -{ - ObjectDelete(TRENDLINE_NAME); -} - -//+------------------------------------------------------------------+ -//| Custom indicator initialization function | -//+------------------------------------------------------------------+ -int init() - { - IndicatorBuffers(5); - -//---- indicators - SetIndexStyle(0,DRAW_HISTOGRAM); - SetIndexBuffer(0,ExtMapBuffer1); - SetIndexLabel(0,"Movement"); - SetIndexEmptyValue(0, EMPTY_VALUE); - - SetIndexStyle(1,DRAW_LINE); - SetIndexBuffer(1,ExtMapBuffer2); - SetIndexLabel(1,"Movement Sum"); - SetIndexEmptyValue(1, EMPTY_VALUE); - - SetIndexStyle(2,DRAW_LINE); - SetIndexBuffer(2,ExtMapBuffer3); - SetIndexLabel(2,"Smoothed "+MA_Period); - SetIndexEmptyValue(2, EMPTY_VALUE); - - SetIndexStyle(3,DRAW_LINE); - SetIndexBuffer(3,ExtMapBuffer4); - SetIndexLabel(3,"Trend step"); - SetIndexEmptyValue(3, EMPTY_VALUE); - - SetIndexStyle(4,DRAW_LINE); - SetIndexBuffer(4,ExtMapBuffer5); - SetIndexLabel(4,"Absolute movement"); - SetIndexEmptyValue(4, EMPTY_VALUE); - - if (Time_Frame_Mins == 0) - time_frame = Period(); - else - time_frame = Time_Frame_Mins; - - if (time_frame < 1) time_frame = 1; - - index_factor = Period(); - index_factor = index_factor / time_frame; - - prev_calc_time = 0; - prev_best_step = 0; - - if (Mode_Function == ABS_MOVE) remove_trendline(); - else create_trendline(); - -//---- - return(0); - } -//+------------------------------------------------------------------+ -//| Custom indicator deinitialization function | -//+------------------------------------------------------------------+ -int deinit() - { -//---- - remove_trendline(); -//---- - return(0); - } - -double -get_delta_ratio( - double &SignalBuffer[], - int SignalBufferLen) -{ -if (DEBUG == 1) Print("Debug get_delta_ratio"); - - double delta = 0.0; - bool going_up = false; - - for (int i = 0; i < SignalBufferLen; i++) { - if (SignalBuffer[i] == 0. || SignalBuffer[i+1] == 0.) continue; - else if (delta != 0. && going_up != (SignalBuffer[i] > SignalBuffer[i+1]) ) break; - - if (delta == 0.) going_up = (SignalBuffer[i] > SignalBuffer[i+1]); - - delta = (delta * i + (SignalBuffer[i] - SignalBuffer[i+1])) / (i + 1.); - } - - return (delta); -} - -double -get_time_ratio(bool last_bar) -{ -if (DEBUG == 1) Print("Debug get_time_ratio"); - - double time_ratio; - if (last_bar == false) { - time_ratio = 1; - } else { - double frame_delta = TimeCurrent() - iTime(NULL, time_frame, 0); - if (frame_delta > 0) time_ratio = time_frame * 60. / frame_delta; - else time_ratio = 0; - } - - return(time_ratio); -} - -double -get_weighted_move( - double &hi, - double &low, - double &close, - double &prevhi, - double &prevlow, - double &prevclose, - int ix) -{ -if (DEBUG == 1) Print("Debug get_weighted_move"); - - if (hi < prevlow || low > prevhi) return (0); // disconnected bars - - //current weighted high minus previous weighted high - double hidiff = ((2. * hi + close) / 3.) - ((2. * prevhi + prevclose) / 3.); - double lowdiff = ((2. * low + close) / 3.) - ((2. * prevlow + prevclose) / 3.); - double absdiff = hidiff + lowdiff; - - double volume_ratio; - double vol = iVolume(NULL, time_frame, ix); - double prevol = iVolume(NULL, time_frame, ix+1); - - if (vol < INVALID_VOLUME) { - volume_ratio = 0; - } else if (prevol < INVALID_VOLUME) { - volume_ratio = 1; - } else { - volume_ratio = vol / prevol; - } - - double time_ratio = get_time_ratio( (ix == 0) ); - - return (absdiff * time_ratio * volume_ratio); -} - - -double -get_absolute_diff( - double &hi, - double &low, - double &prevhi, - double &prevlow, - int ix) -{ -if (DEBUG == 1) Print("Debug get_absolute_diff"); - - //current weighted high minus previous weighted high - double hidiff = MathAbs(hi - prevhi); - double lowdiff = MathAbs(low - prevlow); - double absdiff = hidiff + lowdiff; - - double time_ratio = get_time_ratio(ix); - - return (absdiff * time_ratio); -} - -/* largest price difference between two bars */ -double -get_absolute_move( - double hi, - double low, - double close, - double prevhi, - double prevlow, - double prevclose) -{ -if (DEBUG == 1) Print("Debug get_absolute_move"); - - double abs_move = 0.0; - - double cur_hlc[3]; - cur_hlc[0] = hi; - cur_hlc[1] = low; - cur_hlc[2] = close; - - double prev_hlc[3]; - prev_hlc[0] = prevhi; - prev_hlc[1] = prevlow; - prev_hlc[2] = prevclose; - - for (int i = 0; i < 3; i++) - for (int j = 0; j < 3; j++) - if ( MathAbs(cur_hlc[i] - prev_hlc[j]) > MathAbs(abs_move) ) - abs_move = cur_hlc[i] - prev_hlc[j]; - - if ( (hi - low) > abs_move ) { - if (close > prevclose) abs_move = hi - low; - else abs_move = low - hi; - } - - return (abs_move); -} - -void -get_bar_move( - int now_bar, - int ref_bar, - double &max_move) -{ -if (DEBUG == 1) Print("Debug get_bar_move"); - - for (int ix = now_bar - 1; ix >= ref_bar; ix--) { - double now_move = get_absolute_move( - iHigh(NULL, time_frame, ix), - iLow(NULL, time_frame, ix), - iClose(NULL, time_frame, ix), - iHigh(NULL, time_frame, now_bar), - iLow(NULL, time_frame, now_bar), - iClose(NULL, time_frame, now_bar) ); - - if ( MathAbs(now_move) > MathAbs(max_move) ) max_move = now_move; - } -} - -/* largest price difference since start_bar trend */ -double -get_max_move(int ref_bar) -{ - if (DEBUG == 1) Print("Debug get_max_move"); - - double max_move = 0; - bool ref_bull = (ExtMapBuffer1[ref_bar] > 0); - - for (int now_bar = ref_bar + 1; - (ref_bull == (ExtMapBuffer1[now_bar] > 0)) && (now_bar < Bars); - now_bar++) { - - get_bar_move(now_bar, ref_bar, max_move); - } - - get_bar_move(now_bar, ref_bar, max_move); - - return (max_move); -} - -#define MAXLEN 1024 - -double bears_array[MAXLEN]; -double bulls_array[MAXLEN]; - - -/* sorted arrays expected ! */ -double -calc_pips( - double &trending[], - int trending_len, - double &retracements[], - int retracements_len - ) -{ - if (DEBUG == 1) Print("Debug calc_pips"); - - double best_ratio = 0; - double best_step = 0; - - if ( (TimeCurrent() - prev_calc_time) < (time_frame * Period() * 5) ) return(prev_best_step); - - double step_base = 0; - for (int ix = 1; - (step_base == 0) && (ix < Bars); - ix++) - { - step_base = ExtMapBuffer5[ix]; - } - if (step_base == 0) return (prev_best_step); - - double interpolation_step = step_base * 0.0312; // 1/16 - - - for (int z = 0; z < 64; z++) { - - double cur_step = step_base + (z * interpolation_step); - - double won_pips = 0; - double lost_pips = 0; - - for (int j = 0; j < retracements_len; j++) { - lost_pips += cur_step * MathFloor(retracements[j] / cur_step) / Point; - if (cur_step >= retracements[j]) lost_pips += 1. * MathMod(retracements[j], cur_step) / Point; - } - - for (j = 0; j < trending_len; j++) { - won_pips += cur_step * MathFloor(trending[j] / cur_step) / Point; - if (cur_step >= trending[j]) lost_pips += 1. * MathMod(trending[j], cur_step) / Point; - } - - if (won_pips == 0) double cur_ratio = 0; - else if (lost_pips == 0) cur_ratio = won_pips; - else cur_ratio = won_pips / lost_pips; - - if (cur_ratio > best_ratio) { - best_step = cur_step; - best_ratio = cur_ratio; - } - - } - - if (best_step == 0) return (prev_best_step); - - prev_best_step = best_step; - prev_calc_time = TimeCurrent(); - - if (DEBUG == 1) Print( - "Recomended minimal pip step " + best_step + " past " + - trending_len + " trending blocks, " + retracements_len + " retracements blocks."); - - return (best_step); -} - -int -gcd(int a, int b) -{ - if (b == 0) return (a); - else return ( gcd(b, a % b) ); -} - -//+------------------------------------------------------------------+ -//| Custom indicator iteration function | -//+------------------------------------------------------------------+ -int start() - { - int counted_bars = IndicatorCounted(); -//---- - int limit = Bars - counted_bars - 1; - if (limit < 0) return (0); - - for (int i = limit; i >= 0; i--) { - - int ix = MathFloor(index_factor * i); - - double hi = iHigh(NULL, time_frame, ix); - double low = iLow(NULL, time_frame, ix); - double close = iClose(NULL, time_frame, ix); - - double prevhi = iHigh(NULL, time_frame, ix+1); - double prevlow = iLow(NULL, time_frame, ix+1); - double prevclose = iClose(NULL, time_frame, ix+1); - - if (prevhi == 0 || prevlow == 0 || prevclose == 0) continue; - if (hi == 0 || low == 0 || close == 0) continue; - - switch (Mode_Function) { - case WEIGHT_MOVE: - ExtMapBuffer1[i] = get_weighted_move(hi, low, close, prevhi, prevlow, prevclose, ix); - ExtMapBuffer2[i] = ExtMapBuffer1[i]; - break; - - case ABS_MOVE: - ExtMapBuffer1[i] = get_absolute_move(hi, low, close, prevhi, prevlow, prevclose); - ExtMapBuffer2[i] = get_max_move(i); - ExtMapBuffer5[i] = MathAbs(ExtMapBuffer1[i]); - - // fixup previous bars - for (int j = i + 1; - ((ExtMapBuffer2[i] > 0) == (ExtMapBuffer2[j] > 0.0)) && (j < Bars); - j++) - ExtMapBuffer2[j] = ExtMapBuffer2[i]; - break; - - case ABS_PUSH: - ExtMapBuffer1[i] = get_absolute_diff(hi, low, prevhi, prevlow, i); - ExtMapBuffer2[i] = ExtMapBuffer1[i]; - break; - } - - } - - if (Mode_Function == WEIGHT_MOVE) { - double ma_coef = 10.0; - for (i = limit; i >= 0; i--) - ExtMapBuffer3[i] = ma_coef * iMAOnArray(ExtMapBuffer1, Bars, MA_Period, 0, MODE_EMA, i); - } else { - ma_coef = 1; - for (i = limit; i >= 0; i--) - ExtMapBuffer3[i] = ma_coef * iMAOnArray(ExtMapBuffer5, Bars, MA_Period, 0, MODE_EMA, i); - } - - if (Mode_Function == ABS_MOVE) { - - switch (Sentiment_Expectation) { - case NEUTRAL: - bool going_bull = (ExtMapBuffer2[0] > 0); - break; - - case BULLISH: - going_bull = true; - break; - - case BEARISH: - going_bull = false; - break; - } - - int bulls_len = 0; - int bears_len = 0; - double preval = 0; - - /* get unique movements */ - for (i = 0; i < Retracement_Look_Back; i++) { - if (ExtMapBuffer2[i] == preval) continue; - preval = ExtMapBuffer2[i]; - - if (preval > 0) { - bulls_array[bulls_len] = preval; - bulls_len++; - } else if (preval < 0) { - bears_array[bears_len] = MathAbs(preval); - bears_len++; - } - } - - if (going_bull == true) - double pip_step = calc_pips(bulls_array, bulls_len, bears_array, bears_len); - else - pip_step = calc_pips(bears_array, bears_len, bulls_array, bulls_len); - - ExtMapBuffer4[0] = pip_step; - SetLevelValue(0, pip_step); - SetLevelValue(1, -1.0 * pip_step); - - - } else { - double delta_sign = get_delta_ratio(ExtMapBuffer1, Bars); - double predicted_price = delta_sign; - predicted_price += ExtMapBuffer1[0]; - - if (hTrendLine != -1) { - ObjectSet(TRENDLINE_NAME, OBJPROP_TIME1, iTime(NULL, time_frame, 0) ); - ObjectSet(TRENDLINE_NAME, OBJPROP_TIME2, iTime(NULL, time_frame, 0) + Period() * 60); - ObjectSet(TRENDLINE_NAME, OBJPROP_PRICE1, ExtMapBuffer2[0]); - ObjectSet(TRENDLINE_NAME, OBJPROP_PRICE2, predicted_price); - } - - } - -//---- - return(0); - } -//+------------------------------------------------------------------+ \ No newline at end of file diff --git a/indicators/jmarket_sentiment.mq4.REMOTE.4229.mq4 b/indicators/jmarket_sentiment.mq4.REMOTE.4229.mq4 deleted file mode 100644 index cb91068..0000000 --- a/indicators/jmarket_sentiment.mq4.REMOTE.4229.mq4 +++ /dev/null @@ -1,550 +0,0 @@ -//+------------------------------------------------------------------+ -//| jmarket_sentiment.mq4 | -//| Copyright 2011, Zarko Asenov | -//| http://jaltoh.6x.to | -//+------------------------------------------------------------------+ -#property copyright "Copyright 2011, Zarko Asenov" -#property link "http://jaltoh.6x.to" - -#property indicator_separate_window -#property indicator_buffers 5 -#property indicator_color1 Red -#property indicator_color2 OrangeRed -#property indicator_color3 Gold -#property indicator_color4 SteelBlue -#property indicator_color5 Lime -#property indicator_width1 1 -#property indicator_width2 1 -#property indicator_width3 2 -#property indicator_width4 2 -#property indicator_width5 1 -#property indicator_level1 1.0 -#property indicator_level2 -1.0 - -#define DEBUG 0 - - -#define WEIGHT_MOVE 0 -#define ABS_MOVE 1 -#define ABS_PUSH 2 -#define TRENDLINE_NAME "jnextbar_predict" -#define INVALID_VOLUME 50 - -#define NEUTRAL 0 -#define BULLISH 1 -#define BEARISH 2 - - -extern int MA_Period = 32; -extern int MA2_Period = 64; -extern int Time_Frame_Mins = 0; -extern int Retracement_Look_Back = 32; -extern int Mode_Function = ABS_MOVE; -extern bool Normalize_MA = true; -extern string _Mode_0 = "Bar to bar weighted hi-lo difference aggregate to volume ratio"; // nakaj stiska -extern string _Mode_1 = "Bar to bar any tick max difference"; // grid -extern string _Mode_2 = "Bar to bar tick max difference"; // pletkanje -extern int Sentiment_Expectation = NEUTRAL; -extern string _Expectation_0 = "Neutral sentiment"; -extern string _Expectation_1 = "Bullish sentiment"; -extern string _Expectation_2 = "Bearish sentiment"; - -//--- buffers -double ExtMapBuffer1[]; -double ExtMapBuffer2[]; -double ExtMapBuffer3[]; -double ExtMapBuffer4[]; -double ExtMapBuffer5[]; -double ExtMapBuffer6[]; - - - -int time_frame; -double index_factor = 1.0; -int hTrendLine = -1; -int ind_window_handle; -double prev_best_step; -datetime prev_calc_time; - -void -create_trendline() -{ - ind_window_handle = WindowFind("jmarket_sentiment"); - - if (hTrendLine == -1 && ind_window_handle > -1) { - - hTrendLine = ObjectCreate( - TRENDLINE_NAME, - OBJ_TREND, - ind_window_handle, - iTime(NULL, time_frame, 0), - 0.0, - iTime(NULL, time_frame, 0) + Period() * 60, - 0.0); - - ObjectSet(TRENDLINE_NAME, OBJPROP_COLOR, indicator_color2); - ObjectSet(TRENDLINE_NAME, OBJPROP_STYLE, STYLE_SOLID); - ObjectSet(TRENDLINE_NAME, OBJPROP_WIDTH, indicator_width2); - } -} - -void -remove_trendline() -{ - ObjectDelete(TRENDLINE_NAME); -} - -//+------------------------------------------------------------------+ -//| Custom indicator initialization function | -//+------------------------------------------------------------------+ -int init() - { - IndicatorBuffers(6); - -//---- indicators - SetIndexStyle(0,DRAW_HISTOGRAM); - SetIndexBuffer(0,ExtMapBuffer1); - SetIndexLabel(0,"Movement"); - SetIndexEmptyValue(0, EMPTY_VALUE); - - SetIndexStyle(1,DRAW_LINE); - SetIndexBuffer(1,ExtMapBuffer2); - SetIndexLabel(1,"Movement Sum"); - SetIndexEmptyValue(1, EMPTY_VALUE); - - SetIndexStyle(2,DRAW_LINE); - SetIndexBuffer(2,ExtMapBuffer3); - SetIndexLabel(2,"Smoothed "+MA_Period); - SetIndexEmptyValue(2, EMPTY_VALUE); - - SetIndexStyle(3,DRAW_LINE); - SetIndexBuffer(3,ExtMapBuffer4); - SetIndexLabel(3,"Trend step"); - SetIndexEmptyValue(3, EMPTY_VALUE); - - SetIndexStyle(4,DRAW_LINE); - SetIndexBuffer(4,ExtMapBuffer5); - SetIndexLabel(4,"Smoothed "+MA2_Period); - SetIndexEmptyValue(4, EMPTY_VALUE); - - SetIndexStyle(5,DRAW_LINE); - SetIndexBuffer(5,ExtMapBuffer6); - SetIndexLabel(5,"Absolute movement"); - SetIndexEmptyValue(5, EMPTY_VALUE); - - if (Time_Frame_Mins == 0) - time_frame = Period(); - else - time_frame = Time_Frame_Mins; - - if (time_frame < 1) time_frame = 1; - - index_factor = Period(); - index_factor = index_factor / time_frame; - - prev_calc_time = 0; - prev_best_step = 0; - - if (Mode_Function != WEIGHT_MOVE) remove_trendline(); - else create_trendline(); - -//---- - return(0); - } -//+------------------------------------------------------------------+ -//| Custom indicator deinitialization function | -//+------------------------------------------------------------------+ -int deinit() - { -//---- - remove_trendline(); -//---- - return(0); - } - -double -get_delta_ratio( - double &SignalBuffer[], - int SignalBufferLen) -{ -if (DEBUG == 1) Print("Debug get_delta_ratio"); - - double delta = 0; - bool going_up = false; - - for (int i = 0; i < SignalBufferLen; i++) { - if (SignalBuffer[i] == 0 || SignalBuffer[i+1] == 0) continue; - else if (delta != 0 && going_up != (SignalBuffer[i] > SignalBuffer[i+1]) ) break; - - if (delta == 0) going_up = (SignalBuffer[i] > SignalBuffer[i+1]); - - delta = ( (delta * i) + (SignalBuffer[i] - SignalBuffer[i+1])) / (i + 1.); - } - - return (delta); -} - -double -get_time_ratio(bool last_bar) -{ -if (DEBUG == 1) Print("Debug get_time_ratio"); - - double time_ratio; - if (last_bar == false) { - time_ratio = 1; - } else { - double frame_delta = TimeCurrent() - iTime(NULL, time_frame, 0); - if (frame_delta > 0) time_ratio = time_frame * 60. / frame_delta; - else time_ratio = 0; - } - - return(time_ratio); -} - -double -get_weighted_move( - double &hi, - double &low, - double &close, - double &prevhi, - double &prevlow, - double &prevclose, - int ix) -{ -if (DEBUG == 1) Print("Debug get_weighted_move"); - - if (hi < prevlow || low > prevhi) return (0); // disconnected bars - - //current weighted high minus previous weighted high - double hidiff = ((2. * hi + close) / 3.) - ((2. * prevhi + prevclose) / 3.); - double lowdiff = ((2. * low + close) / 3.) - ((2. * prevlow + prevclose) / 3.); - double absdiff = hidiff + lowdiff; - - double volume_ratio; - double vol = iVolume(NULL, time_frame, ix); - double prevol = iVolume(NULL, time_frame, ix+1); - - if (vol < INVALID_VOLUME) { - volume_ratio = 0; - } else if (prevol < INVALID_VOLUME) { - volume_ratio = 1; - } else { - volume_ratio = vol / prevol; - } - - double time_ratio = get_time_ratio( (ix == 0) ); - - return (absdiff * time_ratio * volume_ratio); -} - - -double -get_absolute_diff( - double &hi, - double &low, - double &prevhi, - double &prevlow, - int ix) -{ -if (DEBUG == 1) Print("Debug get_absolute_diff"); - - //current weighted high minus previous weighted high - double hidiff = MathAbs(hi - prevhi); - double lowdiff = MathAbs(low - prevlow); - double absdiff = hidiff + lowdiff; - - double time_ratio = get_time_ratio(ix); - - return (absdiff * time_ratio); -} - -/* largest price difference between two bars */ -double -get_absolute_move( - double hi, - double low, - double close, - double prevhi, - double prevlow, - double prevclose) -{ -if (DEBUG == 1) Print("Debug get_absolute_move"); - - double abs_move = 0.0; - - double cur_hlc[3]; - cur_hlc[0] = hi; - cur_hlc[1] = low; - cur_hlc[2] = close; - - double prev_hlc[3]; - prev_hlc[0] = prevhi; - prev_hlc[1] = prevlow; - prev_hlc[2] = prevclose; - - for (int i = 0; i < 3; i++) - for (int j = 0; j < 3; j++) - if ( MathAbs(cur_hlc[i] - prev_hlc[j]) > MathAbs(abs_move) ) - abs_move = cur_hlc[i] - prev_hlc[j]; - - if ( (hi - low) > abs_move ) { - if (close > prevclose) abs_move = hi - low; - else abs_move = low - hi; - } - - return (abs_move); -} - -void -get_bar_move( - int now_bar, - int ref_bar, - double &max_move) -{ -if (DEBUG == 1) Print("Debug get_bar_move"); - - for (int ix = now_bar - 1; ix >= ref_bar; ix--) { - double now_move = get_absolute_move( - iHigh(NULL, time_frame, ix), - iLow(NULL, time_frame, ix), - iClose(NULL, time_frame, ix), - iHigh(NULL, time_frame, now_bar), - iLow(NULL, time_frame, now_bar), - iClose(NULL, time_frame, now_bar) ); - - if ( MathAbs(now_move) > MathAbs(max_move) ) max_move = now_move; - } -} - -/* largest price difference since start_bar trend */ -double -get_max_move(int ref_bar) -{ - if (DEBUG == 1) Print("Debug get_max_move"); - - double max_move = 0; - bool ref_bull = (ExtMapBuffer1[ref_bar] > 0); - - for (int now_bar = ref_bar + 1; - (ref_bull == (ExtMapBuffer1[now_bar] > 0)) && (now_bar < Bars); - now_bar++) { - - get_bar_move(now_bar, ref_bar, max_move); - } - - get_bar_move(now_bar, ref_bar, max_move); - - return (max_move); -} - -#define MAXLEN 1024 - -double bears_array[MAXLEN]; -double bulls_array[MAXLEN]; - - -/* sorted arrays expected ! */ -double -calc_pips( - double &trending[], - int trending_len, - double &retracements[], - int retracements_len - ) -{ - if (DEBUG == 1) Print("Debug calc_pips"); - - double best_ratio = 0; - double best_step = 0; - - if ( (TimeCurrent() - prev_calc_time) < (time_frame * Period() * 5) ) return(prev_best_step); - - double step_base = 0; - for (int ix = 1; - (step_base == 0) && (ix < Bars); - ix++) - { - step_base = ExtMapBuffer6[ix]; - } - if (step_base == 0) return (prev_best_step); - - double interpolation_step = step_base * 0.0312; // 1/16 - - - for (int z = 0; z < 64; z++) { - - double cur_step = step_base + (z * interpolation_step); - - double won_pips = 0; - double lost_pips = 0; - - for (int j = 0; j < retracements_len; j++) { - lost_pips += cur_step * MathFloor(retracements[j] / cur_step) / Point; - if (cur_step >= retracements[j]) lost_pips += 1. * MathMod(retracements[j], cur_step) / Point; - } - - for (j = 0; j < trending_len; j++) { - won_pips += cur_step * MathFloor(trending[j] / cur_step) / Point; - if (cur_step >= trending[j]) lost_pips += 1. * MathMod(trending[j], cur_step) / Point; - } - - if (won_pips == 0) double cur_ratio = 0; - else if (lost_pips == 0) cur_ratio = won_pips; - else cur_ratio = won_pips / lost_pips; - - if (cur_ratio > best_ratio) { - best_step = cur_step; - best_ratio = cur_ratio; - } - - } - - if (best_step == 0) return (prev_best_step); - - prev_best_step = best_step; - prev_calc_time = TimeCurrent(); - - if (DEBUG == 1) Print( - "Recomended minimal pip step " + best_step + " past " + - trending_len + " trending blocks, " + retracements_len + " retracements blocks."); - - return (best_step); -} - -int -gcd(int a, int b) -{ - if (b == 0) return (a); - else return ( gcd(b, a % b) ); -} - -//+------------------------------------------------------------------+ -//| Custom indicator iteration function | -//+------------------------------------------------------------------+ -int start() - { - int counted_bars = IndicatorCounted(); -//---- - int limit = Bars - counted_bars - 1; - if (limit < 0) return (0); - - for (int i = limit; i >= 0; i--) { - - int ix = MathFloor(index_factor * i); - - double hi = iHigh(NULL, time_frame, ix); - double low = iLow(NULL, time_frame, ix); - double close = iClose(NULL, time_frame, ix); - - double prevhi = iHigh(NULL, time_frame, ix+1); - double prevlow = iLow(NULL, time_frame, ix+1); - double prevclose = iClose(NULL, time_frame, ix+1); - - if (prevhi == 0 || prevlow == 0 || prevclose == 0) continue; - if (hi == 0 || low == 0 || close == 0) continue; - - switch (Mode_Function) { - case WEIGHT_MOVE: - ExtMapBuffer1[i] = get_weighted_move(hi, low, close, prevhi, prevlow, prevclose, ix); - ExtMapBuffer2[i] = ExtMapBuffer1[i]; - break; - - case ABS_MOVE: - ExtMapBuffer1[i] = get_absolute_move(hi, low, close, prevhi, prevlow, prevclose); - ExtMapBuffer2[i] = get_max_move(i); - ExtMapBuffer6[i] = MathAbs(ExtMapBuffer1[i]); - - // fixup previous bars - for (int j = i + 1; - ((ExtMapBuffer2[i] > 0) == (ExtMapBuffer2[j] > 0.0)) && (j < Bars); - j++) - ExtMapBuffer2[j] = ExtMapBuffer2[i]; - break; - - case ABS_PUSH: - ExtMapBuffer1[i] = get_absolute_diff(hi, low, prevhi, prevlow, i); - ExtMapBuffer2[i] = ExtMapBuffer1[i]; - break; - } - - } - - if (Mode_Function == WEIGHT_MOVE) { - - if (Normalize_MA) double ma_coef = 10; - else ma_coef = 1; - - for (i = limit; i >= 0; i--) - ExtMapBuffer3[i] = ma_coef * iMAOnArray(ExtMapBuffer1, Bars, MA_Period, 0, MODE_EMA, i); - for (i = limit; i >= 0; i--) - ExtMapBuffer5[i] = iMAOnArray(ExtMapBuffer3, Bars, MA2_Period, 0, MODE_EMA, i); - - } else { - for (i = limit; i >= 0; i--) { - ExtMapBuffer3[i] = iMAOnArray(ExtMapBuffer6, Bars, MA_Period, 0, MODE_EMA, i); - ExtMapBuffer5[i] = iMAOnArray(ExtMapBuffer1, Bars, MA2_Period, 0, MODE_EMA, i); - } - } - - if (Mode_Function == ABS_MOVE) { - - switch (Sentiment_Expectation) { - case NEUTRAL: - bool going_bull = (ExtMapBuffer2[0] > 0); - break; - - case BULLISH: - going_bull = true; - break; - - case BEARISH: - going_bull = false; - break; - } - - int bulls_len = 0; - int bears_len = 0; - double preval = 0; - - /* get unique movements */ - for (i = 0; i < Retracement_Look_Back; i++) { - if (ExtMapBuffer2[i] == preval) continue; - preval = ExtMapBuffer2[i]; - - if (preval > 0) { - bulls_array[bulls_len] = preval; - bulls_len++; - } else if (preval < 0) { - bears_array[bears_len] = MathAbs(preval); - bears_len++; - } - } - - if (going_bull == true) - double pip_step = calc_pips(bulls_array, bulls_len, bears_array, bears_len); - else - pip_step = calc_pips(bears_array, bears_len, bulls_array, bulls_len); - - ExtMapBuffer4[0] = pip_step; - SetLevelValue(0, pip_step); - SetLevelValue(1, -1.0 * pip_step); - - - } else { - double delta_sign = get_delta_ratio(ExtMapBuffer1, Bars); - double predicted_price = delta_sign; - predicted_price += ExtMapBuffer1[0]; - - if (hTrendLine != -1) { - ObjectSet(TRENDLINE_NAME, OBJPROP_TIME1, iTime(NULL, time_frame, 0) ); - ObjectSet(TRENDLINE_NAME, OBJPROP_TIME2, iTime(NULL, time_frame, 0) + Period() * 60); - ObjectSet(TRENDLINE_NAME, OBJPROP_PRICE1, ExtMapBuffer2[0]); - ObjectSet(TRENDLINE_NAME, OBJPROP_PRICE2, predicted_price); - } - - } - -//---- - return(0); - } -//+------------------------------------------------------------------+ \ No newline at end of file -- 2.11.4.GIT