**STOCHASTIC MULTI-TIME FRAME EXPERT ADVISOR** Int Lot_size = Lot //Order ticket size Int Min_equity = 3000 //Capital preservation Int Margin_buffer = 0.4 //Set margin utilization threshold Int K = 14, D = 5, Slowing = 5 //Set Slow Stochastic parameters Int Liquidate = 0 //Liquidate existing position and stop flip //Above parameters able to be modified under “Inputs” Int Oversold = 20, Overbought = 80 Stoch(time_frame, K, D, Slowing) returns Float %K, %D //Signal lines onTick { //For every tick **reCheck //Error handler Float Sell_M5, Sell_M15, Sell_H1, Sell_H4, Sell_D, Buy_M5, Buy_M15, Buy_H1, Buy_H4, Buy_D = 0 Int Sell_Signal, Buy_Signal = 0 //Initialize Int Position = Retrieve Existing Currency Pair Position //-1 (Short), 0 (None), 1 (Long) If Stoch(M5, %K - %D) < 0 and Stoch(M5, %K) > Oversold then Sell_M5 = 1 else 0 //If 5-min Stoch is negative momentum and %K not oversold, return 1 If Stoch(M15, %K - %D) < 0 and Stoch(M15, %K) > Oversold then Sell_M15 = 1 else 0 //If 15-min Stoch is negative momentum and %K not oversold, return 1 If Stoch(H1, %K - %D) < 0 and Stoch(H1, %K) > Oversold then Sell_H1 = 1 else 0 //If 1-hour Stoch is negative momentum and %K not oversold, return 1 If Stoch(H4, %K - %D) < 0 and Stoch(H4, %K) > Oversold then Sell_H1 = 1 else 0 //If 4-hour Stoch is negative momentum and %K not oversold, return 1 If Stoch(D, %K - %D) < 0 and Stoch(D, %K) > Oversold then Sell_D = 1 else 0 //If daily Stoch is negative momentum and %K not oversold, return 1 If Stoch(M5, %K - %D) > 0 and Stoch(M5, %K) < Overbought then Buy_M5 = 1 else 0 //If 5-min Stoch is positive momentum and %K not overbought, return 1 If Stoch(M15, %K - %D) > 0 and Stoch(M15, %K) < Overbought then Buy_M15 = 1 else 0 //If 15-min Stoch is positive momentum and %K not overbought, return 1 If Stoch(H1, %K - %D) > 0 and Stoch(H1, %K) < Overbought then Buy_H1 = 1 else 0 //If 1-hour Stoch is positive momentum and %K not overbought, return 1 If Stoch(H4, %K - %D) > 0 and Stoch(H4, %K) < Overbought then Buy_H4 = 1 else 0 //If 4-hour Stoch is positive momentum and %K not overbought, return 1 If Stoch(Daily, %K - %D) > 0 and Stoch(Daily, %K) < Overbought then Buy_D = 1 else 0 //If daily Stoch is positive momentum and %K not overbought, return 1 If Sell_M5 + Sell_M15 + Sell_H1 + Sell_H4 + Sell_D = 5 then Sell_Signal = 1 else 0 If Buy_M5 + Buy_M15 + Buy_H1 + Buy_H4 + Buy_D = 5 then Buy_Signal = 1 else 0 Error Handling: Sell_Signal and Buy_Signal cannot both be 1, else go back to **recheck If Liquidate = 1 then Liquidate_position(Sell_Signal, Buy_Signal) //Close position else{ If Free_Margin > Equity*Margin_buffer and Equity > Min_equity then { //Sufficient margin, initiate new position If Sell_Signal = 1 and Position = 0 then Sell Lot_size at market //New short position Else if Sell_Signal = 1 and Position = 1 then Sell Lot_size*2 at market //Flip position Else if Buy_Signal = 1 and Position = 0 then Buy Lot_size at market //New long position Else if Buy_Signal = 1 and Position = -1 then Buy Lot_size*2 at market //Flip position Else Do Nothing } Else if Equity > Min_equity { //Insufficient margin buffer, do not increase positions If Sell_Signal = 1 and Position = 1 then Sell Lot_size*2 at market //Flip position Else if Buy_Signal = 1 and Position = -1 then Buy Lot_size*2 at market //Flip position Else Do Nothing } Else Liquidate_position(Sell_Signal, Buy_Signal) //Capital preservation mode }} //End onTick Liquidate_position(int Sell_Signal, int Buy_Signal){ //Do not initiate new position If Sell_Signal = 1 and Position = 1 then Sell Lot_size at market //Close existing Long Else if Buy_Signal = 1 and Position = -1 then Buy Lot_size at market //Close existing Short Else Do Nothing }