Skip to content

Commit

Permalink
added takeprofit and autorestart
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniocosentino committed Mar 10, 2017
1 parent bc66af0 commit 0252f35
Showing 1 changed file with 57 additions and 5 deletions.
62 changes: 57 additions & 5 deletions cbot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class kTradeBot : Robot
private int currentminute;
private int currentslotnumber;
private bool botactive;
private int hasclosedft;

[Parameter("Contracts (EURO)", DefaultValue = 100000, MinValue = 1, MaxValue = 100000000)]
public int ncontracts { get; set; }
Expand All @@ -35,6 +36,12 @@ public class kTradeBot : Robot
[Parameter("Invert Buy/Sell", DefaultValue = false)]
public bool inverted { get; set; }

[Parameter("Take Profit (pips)", DefaultValue = 0, MinValue = 0, MaxValue = 100000000)]
public int takeprofit { get; set; }

[Parameter("Restart After Profit", DefaultValue = false)]
public bool restartap { get; set; }

// Time Parameters
[Parameter("0:00 - 0:29", DefaultValue = true)]
public bool timeslot_00 { get; set; }
Expand Down Expand Up @@ -200,11 +207,14 @@ protected override void OnStart()
currentminute = 0;
currentslotnumber = 0;
botactive = false;
hasclosedft = 0;

candleslist.Clear();
outlist.Clear();
blackslots.Clear();

Positions.Closed += PositionsOnClosed;

// Adding "black" hours

if (!timeslot_00)
Expand Down Expand Up @@ -479,6 +489,11 @@ protected override void OnBar()
{
botactive = false;

if (hasclosedft == 1)
{
hasclosedft = 2;
}

if (is_position_open)
{
Print("Closing active position");
Expand All @@ -492,7 +507,14 @@ protected override void OnBar()
}
else
{
botactive = true;
if (restartap)
{
botactive = true;
}
else if (restartap == false && (hasclosedft == 2 || hasclosedft == 0))
{
botactive = true;
}
}


Expand Down Expand Up @@ -599,12 +621,26 @@ protected override void OnBar()

if (pointscounter == csequence)
{
var result = ExecuteMarketOrder(TradeType.Buy, Symbol, ncontracts, "kTrade");
if (takeprofit > 0)
{
var result = ExecuteMarketOrder(TradeType.Buy, Symbol, ncontracts, "kTrade", 0, takeprofit);
}
else
{
var result = ExecuteMarketOrder(TradeType.Buy, Symbol, ncontracts, "kTrade");
}
position_type = "buy";
}
else
{
var result = ExecuteMarketOrder(TradeType.Sell, Symbol, ncontracts, "kTrade");
if (takeprofit > 0)
{
var result = ExecuteMarketOrder(TradeType.Sell, Symbol, ncontracts, "kTrade", 0, takeprofit);
}
else
{
var result = ExecuteMarketOrder(TradeType.Sell, Symbol, ncontracts, "kTrade");
}
position_type = "sell";
}
}
Expand Down Expand Up @@ -650,12 +686,28 @@ protected override void OnBar()
// end of on bar event
protected override void OnTick()
{
// Put your core logic here
// no onTick events
}

protected override void OnStop()
{
// Put your deinitialization logic here
Print("kTrade stopped");
}

private void PositionsOnClosed(PositionClosedEventArgs args)
{
var pos = args.Position;
Print("Position closed with €{0} profit", pos.GrossProfit);
is_position_open = false;
position_type = null;

if (!restartap)
{
hasclosedft = 1;
botactive = false;
}

}

}
}

0 comments on commit 0252f35

Please sign in to comment.