This is my first time posting a question so you'll have to excuse me if I accidentally leave out some relevant information.
I have a strategy that is essentially a simple stochastic cross over strategy. I'd like to preface the code by saying that it might look goofy. That is because I was really hacking around trying to get it to behave as desired. Here is some code:
/// <summary> /// This method is used to configure the strategy and is called once before any strategy method is called. /// </summary> protected override void Initialize() { CalculateOnBarClose = true; ExitOnClose = false; // We do not want to exit on close Add(Stochastics(periodD, periodK, smooth));// We want to see the Stochastics on the chart } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { // If we're flat, just get us in the market if(Position.MarketPosition == MarketPosition.Flat) { EnterShort(); } // If the D value of the stochastic has crossed above our topThrs if( CrossAbove(Stochastics(periodD,periodK,smooth).D,(double) topThrs, 1)) { // Next amount to long by will be 1 j=1; // Trigger OnPositionUpdate Position.Close(); // Next amount to short will be the current amount + 1 i++; } // If the D value of the stochastic has crossed below our botThrs if( CrossBelow(Stochastics(periodD,periodK,smooth).D, (double) botThrs,1)) { // Next amount to short by will be 1 i=1; // Trigger OnPositionUpdate Position.Close(); // Next amount to loong will be the current amount + 1 j++; } } // When our position updates, enter a long or short at the desired position value protected override void OnPositionUpdate(IPosition position) { if(Position.MarketPosition == MarketPosition.Flat) { // If the D value of the stochastic has crossed above our topThrs if( CrossAbove(Stochastics(periodD,periodK,smooth).D,(double) topThrs, 1)) { EnterShort(i); } // If the D value of the stochastic has crossed below our botThrs if( CrossBelow(Stochastics(periodD,periodK,smooth).D, (double) botThrs,1)) { EnterLong(j); } } }
Position.Close()
OnPositionUpdate()
Attached is a photo of how this strategy trades. I would like to NOT have to close my positions to put myself in a new position. I'd like to be able to enter a +1 position at the first cross below, then at another cross below, enter +2, then at the next cross below +3 and finally, at the cross ABOVE, enter -1.
I hope that this text and attached photo is enough but please ask for more information if needed. Thank you in advanced!
Comment