Modify stop loss every bar with onBar() method

Created at 06 Feb 2016, 20:47
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
SH

Shawn_Mapilot

Joined 30.04.2015

Modify stop loss every bar with onBar() method
06 Feb 2016, 20:47


I would like to update the stop loss on my position every bar to equal a overlay indicator such as ichimoku kijunsen.

I tried to make it work but it seems to not update every bar, wondering if im missing something.

So my code goes like 

  protected override void OnBar()
        { 

double StopLoss = Math.Abs(ichimoku.KijunSen.Last(1) - longPosition.EntryPrice) / Symbol.PipSize;

if( LongPosition != null)

{

 ModifyPosition(longPosition, StopLoss, null);

}

if(ShortPosition!= null)

{

ModifyPosition(shortPosition,StopLoss,null);

}

}

//////

Any help will be greatly appreciated. Thanks!


@Shawn_Mapilot
Replies

@fxstarforex
07 Feb 2016, 23:02 ( Updated at: 15 Jan 2024, 14:51 )

:)

Hi, look at this cBot [/algos/cbots/show/1077]  there is what You need.

 

 

Shawn_Mapilot said:

I would like to update the stop loss on my position every bar to equal a overlay indicator such as ichimoku kijunsen.

I tried to make it work but it seems to not update every bar, wondering if im missing something.

So my code goes like 

  protected override void OnBar()
        { 

double StopLoss = Math.Abs(ichimoku.KijunSen.Last(1) - longPosition.EntryPrice) / Symbol.PipSize;

if( LongPosition != null)

{

 ModifyPosition(longPosition, StopLoss, null);

}

if(ShortPosition!= null)

{

ModifyPosition(shortPosition,StopLoss,null);

}

}

//////

Any help will be greatly appreciated. Thanks!

 


@@fxstarforex

RodMar
11 Feb 2016, 01:09

RE:

Shawn_Mapilot said:

I would like to update the stop loss on my position every bar to equal a overlay indicator such as ichimoku kijunsen.

I tried to make it work but it seems to not update every bar, wondering if im missing something.

So my code goes like 

  protected override void OnBar()
        { 

double StopLoss = Math.Abs(ichimoku.KijunSen.Last(1) - longPosition.EntryPrice) / Symbol.PipSize;

if( LongPosition != null)

{

 ModifyPosition(longPosition, StopLoss, null);

}

if(ShortPosition!= null)

{

ModifyPosition(shortPosition,StopLoss,null);

}

}

//////

Any help will be greatly appreciated. Thanks!

Try to start "OnBar()" inside "OnStart()". Like this:  protected override void  OnStart() { ...   ....    .... OnBar(); }

Perhaps it works.

J. Martins


@RodMar