Tango Trade Optimizer

by sebnet in category Other at 18/08/2016
Description

This cBot will get the most of your winning trades. It will prevent hitting the target while you are surfing a winning trend, without risking the pips you are already earning.

When the bid/ask price of the currency you are trading is about to hit the target, this cBot will move the take profit setting a few pips further,  and will put the STOP LOSS a few pips away from the current price. 

Download
2326 downloads
How to install
Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
// -------------------------------------------------------------------------------------------------
//
//    TANGO TRADE OPTIMIZER
//
//    When the bid/ask price of the currency you are trading is about to hit the target (take profit), this cBot will move the take profit setting a few pips further, 
//    and will put the STOP LOSS a few pips away from the current price. This way your target will not be hit on a winning trend without risking your winning position. 
//  
//      Additionally, it will send an email every time the position is modified by the bot
//
//
//    Example: the current price is 3(A) pips away from the target. TP will be moved 10(B) pips further and SL trailed 10(C) pips away from the current price. This process will be repeated until the SL is hit.
//    
//     Contact: sebodena@gmail.com
//      Settings:
//      (A) Trigger pips: how many pips away from the target the process is executed
//      (B) Trail SL: how many pips to move the SL away from the current bid/ask price
//      (C) Trail TP:how many pips to move TP from the current TP
// -------------------------------------------------------------------------------------------------
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Text;


namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TangoTradeOptimizer : Robot
    {



        Symbol currentSymbol;
        Position currentPosition;
        double? newStopLoss = 0;
        double? newTakeProfit = 0;
       
      
        [Parameter("Trigger (pips)", DefaultValue = 2.0, MinValue = 0)]
        public double TriggerPips { get; set; }

        [Parameter("Trail SL (pips)", DefaultValue = 10.0, MinValue = 0)]
        public double TrailSLPips { get; set; }

        [Parameter("Trail TP (pips)", DefaultValue = 5.0, MinValue = 0)]
        public double TrailTPPips { get; set; }

        [Parameter("Notify", DefaultValue = true)]
        public bool Notifiy { get; set; }


        [Parameter("Email Address")]
        public string EmailAddress { get; set; }


        protected override void OnStart()
        {
          

        }

        protected override void OnTick()
        {
            foreach (var pos in Positions)
            {
                this.currentSymbol = MarketData.GetSymbol(pos.SymbolCode);
                this.currentPosition = pos;
                if (isNearTarget())
                {
                   
                    CalcNewValues();

                    AdjustPosition();
                   
                    SendMail();
                }
            }
        }
        
        string BuildMessage()
        {
            StringBuilder sb=new StringBuilder();
            sb.AppendFormat("Tango Trade Optimizer modified your position:{0}",System.Environment.NewLine);
            sb.AppendFormat(@"Unr. Net:{0} {3} Stop Loss:{1}{3}  Take Profit:{2}",currentPosition.NetProfit,currentPosition.StopLoss,currentPosition.TakeProfit,System.Environment.NewLine);
            return sb.ToString();
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }

        void CalcNewValues()
        {
            if (currentPosition.TradeType == TradeType.Buy)
            {
                newStopLoss = currentSymbol.Bid - (this.currentSymbol.PipSize * this.TrailSLPips);
                newTakeProfit = currentSymbol.Bid + (this.currentSymbol.PipSize * this.TrailTPPips);
            }
            else
            {
                newStopLoss = currentSymbol.Ask + (this.currentSymbol.PipSize * this.TrailSLPips);
                newTakeProfit = currentSymbol.Ask - (this.currentSymbol.PipSize * this.TrailTPPips);
            }
        }



        void AdjustPosition()
        {
            if (newStopLoss == 0 || newTakeProfit == 0)
                return;
            ModifyPosition(currentPosition, newStopLoss, newTakeProfit);

        }

        void SendMail()
        {
            if (this.Notifiy)
                Notifications.SendEmail(this.EmailAddress, this.EmailAddress, "Tango Trade Optimizer", BuildMessage());

            Print("new values new SL:{0}- New TP{1}", newStopLoss, newTakeProfit);
        }



        bool isNearTarget()
        {

            if (this.currentPosition.TradeType == TradeType.Buy)
            {
                if (currentSymbol.Bid > this.currentPosition.TakeProfit - (this.currentSymbol.PipSize * this.TriggerPips))
                    return true;

            }
            else
            {
                if (currentSymbol.Ask < this.currentPosition.TakeProfit + (this.currentSymbol.PipSize * this.TriggerPips))
                    return true;
            }

            return false;
        }


    }
}
Comments

ChasBrownTH - August 31, 2016 @ 18:58

This is a clever idea, thanks for sharing! ;)

xrichard69 - September 13, 2016 @ 12:12

Why its not working for me? :( i start it but it doesnt set any TP or SL .. nothing.. help me please 

xrichard69 - September 13, 2016 @ 12:29

what is the deinitialization logic?

5