I came across the example code attached & it seems it's optimized for 3 trades per day.
I tried to change it it to add in both the Long & short side according to my strategy but it still won't limit trades per each side.
I also tried separating them into Long with Long Logic & Short with Short Logic, & Starting with the short, I'm still not getting a limit entry based upon a trade loss on the short side.
some code is removed, but all else equal, it still runs, it just doesn't limit trades based upon a trade loss
public class x : Strategy { ... ... ... private int lastLongTrades = 0; // This variable holds our value for how profitable the last three trades were. private int priorLongtrades = 0; // This variable holds the number of trades taken prior to each Long session. private int lastShortTrades = 0; // This variable holds our value for how profitable the last three trades were. private int priorShorttrades = 0; // This variable holds the number of trades taken prior to each Short session. private int priorNumberOfTrades = 0; // This variable holds the number of trades taken. It will be checked every OnBarUpdate() to determine when a trade has closed. private MAEnvelopes MAE; double MiddleMAE = MAE.Middle[0]; public int RM RM = 1; protected override void OnStateChange() { if (State == State.SetDefaults) { ... ... ... } else if (State == State.DataLoaded) { ... ... ... } } protected override void OnBarUpdate() { if (Close[0] - MiddleMAE < zero || MiddleMAE - Close[0] < zero) { lastLongTrades = 0; lastShortTrades = 0; priorLongtrades = SystemPerformance.LongTrades.Count; priorShorttrades = SystemPerformance.ShortTrades.Count; } // Makes sure there are total x trades for both & long short side if ((SystemPerformance.LongTrades.Count - priorLongtrades) >= RM || (SystemPerformance.ShortTrades.Count - priorShorttrades) >= RM //priorNumberOfTrades makes sure this code block only executes when a new trade has finished. && SystemPerformance.LongTrades.Count != priorNumberOfTrades || SystemPerformance.ShortTrades.Count != priorNumberOfTrades) { // Reset the counter. - Does the counting lastLongTrades = 0; lastShortTrades = 0; // Set the new number of completed trades. priorNumberOfTrades = SystemPerformance.LongTrades.Count; priorNumberOfTrades = SystemPerformance.ShortTrades.Count; for (int idx = 1; idx <= RM; idx++) { /* The SystemPerformance.AllTrades array stores the most recent trade at the highest index value. If there are a total of 10 trades, this loop will retrieve the 10th trade first (at index position 9), then the 9th trade (at 8), then the 8th trade. */ Trade Longtrade = SystemPerformance.LongTrades[SystemPerformance.LongTrades.Count - idx]; Trade Shorttrade = SystemPerformance.ShortTrades[SystemPerformance.ShortTrades.Count - idx]; /* If the trade's profit is greater than 0, add one to the counter. If the trade's profit is less than 0, subtract one. This logic means break-even trades have no effect on the counter. */ if (Longtrade.ProfitCurrency > 0 || Shorttrade.ProfitCurrency > 0) { lastLongTrades++; lastShortTrades++; } else if (Longtrade.ProfitCurrency < 0 || Shorttrade.ProfitCurrency > 0) { lastLongTrades--; lastShortTrades--; } } // Long Side if (lastLongTrades != -RM) { Entry Logic ... ... ... } if( x happens) { ExitLong("Long"); } //**************************************************************** // ShortSide if (ShortLongTrades != -RM) { Entry Logic ... ... ... } if( x happens) { ExitShort("Short"); } } } }
What do I need to make this work?
Thanks in advance.
Comment