Topics
20 Mar 2015, 13:08
 2641
 2
28 Feb 2015, 10:45
 4414
 10
Replies

edccde
04 Mar 2015, 15:14

RE:

rawand said:

can anyone write a code for me that do the following

opens a random trade type and close it after 5 minutes if profit is >0

any help will be appreciated 

do you find solve you answer ?

i am interesting  too...


@edccde

edccde
04 Mar 2015, 15:04

RE: RE:

quasar888 said:

DateTime time;

void timeclosingtrade()

{time=Server.Time;

int timespanchoice=0;

  foreach (Position item in Positions)
                {

if(item.EntryTime

{

ClosePosition(item);

}

                }

 

}

  protected override void OnStart()
        {

 Timer.Start(1);

}

protected override OnTimer()

{

timeclosingtrade();

}

this code is wrong

i need code for  close opened position after some seconds left (reduce time by timer) not in "X" hour

can you help? 

 

 

 


@edccde

edccde
03 Mar 2015, 19:18

how to put code in expert ?

 can you put your code to this expert  (with 1 change parameter - expire open order in sec)


@edccde

edccde
03 Mar 2015, 00:51

RE: RE:

quasar888 said:

DateTime time;

void timeclosingtrade()

{time=Server.Time;

int timespanchoice=0;

  foreach (Position item in Positions)
                {

if(item.EntryTime

{

ClosePosition(item);

}

                }

 

}

  protected override void OnStart()
        {

 Timer.Start(1);

}

protected override OnTimer()

{

timeclosingtrade();

}

quasar you cool programmer !  but i cant insert right this code to expert, can you put your code to this expert  (with 1 change parameter - expire open order in sec)

you`ll help me very well !!! 

 

 

using System;

using cAlgo.API;
using cAlgo.API.Indicators; 

namespace cAlgo.Robots
{
    [Robot]
    public class NewRobot : Robot
    {
        [Parameter("Vol", DefaultValue = 10000)]
        public int Vol { get; set; }
        
        [Parameter("SL", DefaultValue = 300)]
        public int SL { get; set; }
        
        [Parameter("TP", DefaultValue = 400)]
        public int TP { get; set; }
        
        [Parameter("Tral_Start", DefaultValue = 200)]
        public int Tral_Start { get; set; }
        
        [Parameter("Tral_Stop", DefaultValue = 150)]
        public int Tral_Stop { get; set; }
         
         [Parameter("BB_price")]
        public DataSeries BB_price { get; set; }
        
        [Parameter("BB_period", DefaultValue = 20)]
        public int BB_period { get; set; }
        
        [Parameter("dev", DefaultValue = 2)]
        public double dev { get; set; }
        
        [Parameter("BB_type")]
        public MovingAverageType BB_type { get; set; }   
        
        [Parameter("Period_K", DefaultValue = 5)]
        public int Period_K { get; set; }
        
        [Parameter("Period_D", DefaultValue = 3)]
        public int Period_D { get; set; }
        
        [Parameter("Slowing", DefaultValue = 3)]
        public int Slowing { get; set; }
        
        [Parameter("St_Ma_Type")]
        public MovingAverageType St_Ma_Type { get; set; }
        
        
        private BollingerBands BB;
        private StochasticOscillator St;
        private Position position1;
        private int a;
        
        protected override void OnStart()
        {
        Print("Welcome to the world of infinite financial possibilities!");
        BB=Indicators.BollingerBands(BB_price,BB_period,dev,BB_type);
        St=Indicators.StochasticOscillator(Period_K,Period_D,Slowing, St_Ma_Type);
        }
        
        protected override void OnPositionOpened(Position openedPosition)
        {
            position1 = openedPosition;
            
            if (position1.TradeType == TradeType.Buy)
            {
            Trade.ModifyPosition(openedPosition, position1.EntryPrice-SL*Symbol.PointSize, position1.EntryPrice+TP*Symbol.PointSize);
            Print("StopLoss and TakeProfit were successfully established");
            }
            
            if (position1.TradeType == TradeType.Sell)
            {
            Trade.ModifyPosition(openedPosition, position1.EntryPrice+SL*Symbol.PointSize, position1.EntryPrice-TP*Symbol.PointSize);
            Print("StopLoss and TakeProfit were successfully established");
            }
        }

        protected override void OnTick()
        {
        int bars=MarketSeries.Close.Count-1;
    
        double h=MarketSeries.High[bars-1];
        double l=MarketSeries.Low[bars-1];
       
        double BB1=BB.Bottom[BB.Bottom.Count-2];
        double BB2=BB.Top[BB.Top.Count-2];
        
        double St1=St.PercentK[St.PercentK.Count-2];
        double St2=St.PercentD[St.PercentD.Count-2];
        
        double Bid=Symbol.Bid;
        double Ask=Symbol.Ask;
        double Point=Symbol.PointSize;
         
        if (Trade.IsExecuting) return;
        
          if (l<=BB1 && St1>St2 && a==0)
          {
          Trade.CreateBuyMarketOrder(Symbol, Vol);
          Print("Trade BUY was successfully open");
          a=1;
          }
          
          if (h>=BB2 && St1<St2 && a==0)
          {
          Trade.CreateSellMarketOrder(Symbol, Vol);  
          Print("Trade SELL was successfully open");
          a=1;
          }          
          
           foreach (var position in Account.Positions)
            {
                if(position.SymbolCode == Symbol.Code)
                {
                
                    if(position.TradeType == TradeType.Buy)    
                    {
                    if (Bid-position.EntryPrice>=Tral_Start*Point)
                    if (Bid-Tral_Stop*Point>=position.StopLoss)
                    Trade.ModifyPosition(position, Bid-Tral_Stop*Point, position.TakeProfit);
                    }
                        
                    if(position.TradeType == TradeType.Sell)    
                    {
                    if (position.EntryPrice-Ask>=Tral_Start*Point)
                    if (Ask+Tral_Stop*Point<=position.StopLoss)
                    Trade.ModifyPosition(position, Ask+Tral_Stop*Point, position.TakeProfit);
                    }
                }
             }         
                
        }
        
        protected override void OnPositionClosed(Position pos)
         {
          a=0;
         }
         
          protected override void OnStop()
        {
         Print("The FxPro company wishes you successful day!");
        }

    }
}

 

Thanks!

 I owe you

 


@edccde

edccde
28 Feb 2015, 11:05

RE:

Delta_Gamma said:

Hi Admin,

Thanks for the reply.

I just tested it, it does work, however the programming logic was incorrect, since I wanted the timer to start only when a new position was entered and each new position gets its own instance of that timer so that after half an hour individually the positions close.

I have this code for that, which is called everytime a new position is opened. Would this work?

 public void Reset()
     {
            _timer.Stop();
             _timer.Start();
     }

can you write worked code, for closed opened position by time  ?

possible optimization in tester ?


@edccde