I have checked in the database and looked at all the functions and all appears to be fine. The only thing I haven't been able to check is the call to reset. I have called a select in the database command window to get the Lose_Row_Trades from tbl_Strategies and it returns the expected value. I'm not very familiar with MSSQL, but I know enough to do some testing. dbo.sp_Reset_Ipposnif works when run on the database.
If there is a problem and its not just my error, could it be the trigger to call the reset. I haven't witnessed a reset in any strategy while trying to test.
You did well, the reset condition is not included in the default list, you have to add it manually. Regarding the reset of your strategy, for Murphy's law, when you want a loss it never comes so I didn't manage to have three losses in a row. Now I have reduced it to 2 losses in a row. Let's see.
I'm going to try another method as you have the Masaniello strategy check @betsize. I will put the reset statement to @bet_size > 0.15 which should be roughly equivalent to a fourth stake and then hopefully do the reset.
OK, that failed too. But I know what the problem is. The Strategy reset after 3 losers (@bet_size = 0.16) which was > 0.15. But the next stake was still 0.16, so the reset doesn't reset the staking plan progression pointer.
Yes, this is the problem, the D'Alembert was not meant to work with reset (for what I remember it was developed before the reset functionality). So it refunds the capital but doesn't restart the series. You can fix this yourself: open Database Command and execute the code below. The difference with the existing function is in the condition and NumReset=0 that force the function to work only with the last round series. I will apply the fix for everyone in the next release. Cheers!
ALTER FUNCTION [dbo].[fn_AlembertSize] (@id_strategy int, @unit_value numeric(18,6), @current_price numeric(18,6), @bidtype char(1)) RETURNS numeric(18,2)
select @bf_fee_discount=5*convert(numeric(18,6),valuenum) from tbl_syscfg where Codice='bf_fee_discount' select @capital=capital from tbl_Strategies where ID=@id_strategy set @capital_last=@capital set @win_units=0 set @bet_units=1
DECLARE C3 CURSOR LOCAL STATIC FORWARD_ONLY READ_ONLY FOR select netprofit from (select * from tbl_trades union all select * from tbl_trades_old) tbl_trades where id_strategy=@id_strategy and NumReset=0 and deleted=0 and isnull(netprofit,0)<>0 and status='S' order by id OPEN C3 FETCH NEXT FROM C3 INTO @profit WHILE (@@fetch_status <> -1) BEGIN if @profit>0 begin set @win_units=@win_units+@bet_units set @bet_units=@bet_units-1 if @bet_units=0 set @bet_units=1 end else begin set @win_units=@win_units-@bet_units set @bet_units=@bet_units+1 end set @capital_last=@capital_last + case when @profit>0 then (@profit*(1-@bf_fee_discount)) else @profit end
FETCH NEXT FROM C3 INTO @profit END CLOSE C3 DEALLOCATE C3 set @capital_df=@capital+(@win_units*@unit_value)-@capital_last
if @capital_df<0 begin set @capital_df=0 end else begin set @capital_df=(@capital_df/@bet_units) end
if @bidtype='b' begin set @r=((@capital_df+(@bet_units*@unit_value))*(1+@bf_fee_discount))/(@current_price-1) end else begin set @r=((@capital_df+(@bet_units*@unit_value))*(1+@bf_fee_discount)) end if @r<@unit_value set @r=@unit_value return @r
I can confirm that there is an issue. The strategy starts resetting correctly, but after a while, something stops it from working well. I have configured the strategy to reset after two losses in a row. Currently, I'm quite busy with other projects, but I will investigate this issue as soon as I can and let you know.
I found out finally the reason for the wrong reset. Reset conditions are not processed in synchronous with the strategy bet placement condition but in an asynchronous way every 15 seconds. Since your strategy asks for a reset before the capital is finished, it happens sometimes that a new bet is placed before the reset conditions are checked. This prevents the reset to be executed because the reset can be executed only when there are no open bets. This has been done for performance reasons: it is too expensive to check the reset condition every strategy condition check, and normally a strategy is reset when it has finished its capital. Anyway, there is a loop around: no bet must be placed before a certain amount of seconds is elapsed since the last bet settlement. So if we wait, for example, for 30 seconds before placing a new bet, we are sure that the reset condition (every 15 secs) is checked before. Using the Database Command tool, execute the following SQL query, which will add a new variable:
insert into tbl_sysvariables (name,declaration,funct) select 'last_check_bet_seconds', 'declare @last_check_bet_seconds int', 'select @last_check_bet_seconds=DATEDIFF(second, isnull((select top 1 [lastget] from (select [lastget] from tbl_bets_old where ID_Strategy=@id_strategy union all select [lastget] from tbl_bets where ID_Strategy=@id_strategy) dvtbl order by [lastget] desc), dateadd(second,1,getdate())),getdate())';
This new variable is called @last_check_bet_seconds and it returns the number of seconds elapsed from the last status change of the previous bet in the same strategy. If there is no previous bet, for example, because the strategy just started, it returns -1. Ipposnif changes the status of a bet, for the last time, when it has been settled. Finally, you have to add in the strategy's bet Trigger Condition the following assertion:
@last_check_bet_seconds = -1 or @last_check_bet_seconds > 30
I'm testing this for a while without issues. Let me know if it worked also for you. Regards!