/* * =================================================================================================== * POSITION MANAGEMENT * - TEST STRATEGY LOGIC AND OPEN/CLOSE POSITIONS * * =================================================================================================== */ private void ManagePositions() { /* * =============================================================================================== * INITIALISATION OF VARS ON EACH BAR CLOSE OR TICK * =============================================================================================== */ // SET CURRENT SERVER DATE TIME DateTime Vsrvr_time = Server.Time; // SET TRADE VOLUME BASED ON % BALANCE Vsl_pips = (IndAtr.Result.Last(1) * 2) / Symbol.PipSize; Vtrade_vol = tm.GetTradeVolumeFromAccountRisk(Vsl_pips, AccountRisk); // INDEX: 0 - CURRENT LIVE BAR (NOT FINALISED) // 1 - MOST RECENT FINALISED CLOSE BAR // 2 - 2ND MOST RECENT FINALISED CLOSE BAR var LIndSMA_CURR1 = IndSMA.Result.Last(1); var LIndSAR_CURR1 = IndSAR.Result.Last(1); var LIndSMA_PREV1 = IndSMA.Result.Last(2); var LIndSAR_PREV1 = IndSAR.Result.Last(2); var LBARCloseVal_CURR1 = MarketSeries.Close.Last(1); var LBARCloseVal_PREV1 = MarketSeries.Close.Last(2); var LVolume_CURR1 = MarketSeries.TickVolume.Last(1); /* * =============================================================================================== * PRINT VARS TO LOG * =============================================================================================== */ Print("BAR CLOSE - START ==============================================================="); Print("Simple Moving Average (PREV) - {0}", LIndSMA_PREV1); Print("Parabolic SAR (PREV) - {0}", LIndSAR_PREV1); Print("Close Price (PREV) - {0}", LBARCloseVal_PREV1); Print("------------------------------------------------"); Print("Simple Moving Average (CURR) - {0}", LIndSMA_CURR1); Print("Parabolic SAR (CURR) - {0}", LIndSAR_CURR1); Print("Close Price (CURR) - {0}", LBARCloseVal_CURR1); Print("Tick Volume (CURR) - {0}", LVolume_CURR1); Print("BAR CLOSE - END ================================================================="); #region EXIT LOGIC ======================================================================== /* * =========================================================================================== * EXIT LOGIC FOR "LONG" POSITION * - EXIT IS TRIPPED WHEN * - STOP LOSS OR TAKE PROFIT IS HIT * - OR * - SAR VALUE MOVES ABOVE CLOSE BAR PRICE * =========================================================================================== */ if (tm.IsPositionOpenByType(TradeType.Buy, InstanceName)) { if (LBARCloseVal_CURR1 < LIndSAR_CURR1) { //Print("BUY LBARCloseVal_CURR1 : {0}", LBARCloseVal_CURR1); //Print("BUY LIndSAR_CURR1 : {0}", LIndSAR_CURR1); tm.CloseAllPositionsByType(InstanceName, TradeType.Buy); //tm.ClosePosition(TradeType.Buy, InstanceName); } } /* * =========================================================================================== * EXIT LOGIC FOR "SHORT" POSITION * - EXIT IS TRIPPED WHEN * - STOP LOSS OR TAKE PROFIT IS HIT * - OR * - SAR VALUE MOVES BELOW CLOSE BAR PRICE * =========================================================================================== */ if (tm.IsPositionOpenByType(TradeType.Sell, InstanceName)) { if (LBARCloseVal_CURR1 > LIndSAR_CURR1) { //Print("SELL LBARCloseVal_CURR1 : {0}", LBARCloseVal_CURR1); //Print("SELL LIndSAR_CURR1 : {0}", LIndSAR_CURR1); tm.CloseAllPositionsByType(InstanceName, TradeType.Sell); //tm.ClosePosition(TradeType.Sell, InstanceName); } } #endregion #region ENTRY LOGIC ======================================================================= /* * =========================================================================================== * ENTRY LOGIC FOR "LONG" POSITION * WHEN MARKET PRICE > SMA * AND MARKET PRICE > SAR * - SET A STOP LOSS AND A TAKE PROFIT * =========================================================================================== */ if (!tm.IsPositionOpenByType(TradeType.Buy, InstanceName) && LBARCloseVal_PREV1 < LIndSAR_PREV1 && LBARCloseVal_CURR1 > LIndSMA_CURR1 && LBARCloseVal_CURR1 > LIndSAR_CURR1 && LVolume_CURR1 > VOLThreshold) // LIndDMS_ADX_CURR > DMSThreshold && LBARCloseVal_PREV1 < LIndSAR_PREV1 && { int n = 0; // CHECK IF PREVIOUS VALUES MEET CRITERIA (REMOVE MOVES THAT ONLY LAST 1 OR 2 BARS (NOISE)) // i = 2 (STARTING PREV INDEX - EG. PREVIOUS VALUE 2 BARS PRIOR) for (int i = 2; i < SARTrail + 2; i++) { var LIndSAR_PREV = IndSAR.Result.Last(i); var LBARCloseVal_PREV = MarketSeries.Close.Last(i); if (LBARCloseVal_PREV < LIndSAR_PREV) { n = n + 1; } } // IF NBR OF SARS PREVIOUS IS ABOVE CLOSING PRICE (STRING DOWNWARD INDICATION) THEN PLACE TRADE ON REVERSAL) if (n == SARTrail) { tm.OpenPosition("market", true, 0, TradeType.Buy, Vtrade_vol, InstanceName, Vstop_loss, Vtake_profit, null); } } /* * =========================================================================================== * ENTRY LOGIC FOR "SHORT" POSITION * WHEN MARKET PRICE < SMA * AND MARKET PRICE < SAR * - SET A STOP LOSS AND A TAKE PROFIT * =========================================================================================== */ if (!tm.IsPositionOpenByType(TradeType.Sell, InstanceName) && LBARCloseVal_PREV1 > LIndSAR_PREV1 && LBARCloseVal_CURR1 < LIndSMA_CURR1 && LBARCloseVal_CURR1 < LIndSAR_CURR1 && LVolume_CURR1 > VOLThreshold) // LIndDMS_ADX_CURR > DMSThreshold && LBARCloseVal_PREV1 > LIndSAR_PREV1 && { int n = 0; // CHECK IF PREVIOUS VALUES MEET CRITERIA (REMOVE MOVES THAT ONLY LAST 1 OR 2 BARS (NOISE)) // i = 2 (STARTING PREV INDEX - EG. PREVIOUS VALUE 2 BARS PRIOR) for (int i = 2; i < SARTrail + 2; i++) { var LIndSAR_PREV = IndSAR.Result.Last(i); var LBARCloseVal_PREV = MarketSeries.Close.Last(i); if (LBARCloseVal_PREV > LIndSAR_PREV) { n = n + 1; } } // IF NBR OF SARS PREVIOUS IS ABOVE CLOSING PRICE (STRING DOWNWARD INDICATION) THEN PLACE TRADE ON REVERSAL) if (n == SARTrail) { tm.OpenPosition("market", true, 0, TradeType.Sell, Vtrade_vol, InstanceName, Vstop_loss, Vtake_profit, null); } } #endregion #region PROCESS ON LAST TICK ================================================================== if (!CalculateOnBar) { foreach (var position in Positions) { if (IncludeTrailingStop) { tm.SetTrailingStop(position.Label, TrailingStopTrigger, TrailingStopStep); } if (IncludeBreakEven) { tm.BreakEvenAdjustment(position.Label, BreakEvenPips, BreakEvenExtraPips); } } } #endregion }
private void ManagePositions() { // SET DATE TIME DateTime srvrTime = Server.Time; string tf = TimeFrame.ToString(); // SET TRADE VOLUME BASED ON % BALANCE _slPips = (_atr.Result.Last(1) * 2) / Symbol.PipSize; _tradeVol = tm.GetTradeVolumeFromAccountRisk(_slPips, AccountRisk); #region TIME CHECK ON LAST BAR ================================================================ // CHECK IF SERVER TIME IS WITHIN THE INSTANCE TIME FRAME DEFINED WHEN STARTING THE BOT // EG. IF 15MIN TIMEFRAME - CHECK ONBAR CLOSE IF ITS THE LAST BAR BEFORE ANNOUNCEMENT // IF YES - START MONITORING TIME IN TICK INTERVALS // IF NO - DO NOTHING AND CHECK AGAIN ON NEXT BAR COMPLETION // CHECK TIME INTERVAL AGAINST MONITOR TIME =========================================== // IF TIME TO ANNOUNCEMENT IS WITHIN THE TIME OF THE LAST BAR, MOVE TO TICK EVENTS if (CalculateOnBar) { // DO IF TIME INTERVAL IS IN MINUTES if (tf.Contains("Minute")) { int minToAnnce; string nbrString = Regex.Match(tf, "\\d+").Value; try { minToAnnce = Int32.Parse(nbrString); } catch { minToAnnce = Int32.Parse("1"); } int diffMin = (_announceTime.AddMinutes(-minToAnnce) - srvrTime).Minutes; if (diffMin <= minToAnnce) { CalculateOnBar = false; } } // DO IF TIME INTERVAL IS IN HOURS if (tf.Contains("Hour")) { int hrToAnnce; string nbrString = Regex.Match(tf, "\\d+").Value; try { hrToAnnce = Int32.Parse(nbrString); } catch { hrToAnnce = Int32.Parse("1"); } int diffHr = (_announceTime.AddHours(-hrToAnnce) - srvrTime).Hours; if (diffHr <= hrToAnnce) { CalculateOnBar = false; } } } #endregion #region PROCESS ON LAST TICK ================================================================== if (!CalculateOnBar) { foreach (var position in Positions) { if (IncludeTrailingStop) { tm.SetTrailingStop(position.Label, TrailingStopTrigger, TrailingStopStep); } if (IncludeBreakEven) { tm.BreakEvenAdjustment(position.Label, BreakEvenPips, BreakEvenExtraPips); } } // IF CURRENT SERVER TIME BETWEEN T - 3 SEC and T + 0 SEC // THEN SET BUY AND SELL PENDING ORDERS if (srvrTime >= _announceTime.AddSeconds(-4) && srvrTime <= _announceTime) { Phase1(); } // IF CURRENT SERVER TIME >= T + 10 SEC // CANCEL ANY PENDING POSITIONS THAT HAVE NOT BEEN TRIGGERED // -- REMAINING OPEN POSITIONS WILL CLOSE OUT ON STOP LOSS SETTINGS // set to 20 for testing - revert to 10 when ready if (srvrTime >= _announceTime.AddSeconds(20)) { Phase2(); } } #endregion }